My apologies for the poor cut and paste. I've pulled the code out of the
project and built a small sample which I'm pasting below along with a
backtrace. I imagine I'm missing something in the conception. What I'm
trying to do is expose an instantiated object and it's methods to the
scripting engine.
////////////////// gdb output /////////////////////////////
(gdb) run
Starting program: /home/greg/devel/tests/v8/HelloV8/hello_v8
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
[New Thread 0xb7fdab40 (LWP 17748)]
loaded
handle
template ok
Program received signal SIGSEGV, Segmentation fault.
0x0806c849 in v8::internal::FixedArrayBase::length (this=0x0) at
../src/objects-inl.h:2464
2464 SMI_ACCESSORS(FixedArrayBase, length, kLengthOffset)
(gdb) bt
#0 0x0806c849 in v8::internal::FixedArrayBase::length (this=0x0) at
../src/objects-inl.h:2464
#1 0x0806bdf7 in v8::internal::FixedArray::get (this=0x0, index=3) at
../src/objects-inl.h:1785
#2 0x08068e3b in v8::internal::Context::global_object (this=0x0) at
../src/contexts.h:333
#3 0x08192452 in v8::internal::Isolate::native_context (this=0x8690038) at
../src/isolate.cc:1479
#4 0x080d8ce8 in v8::internal::Execution::InstantiateFunction (data=...,
exc=0xbffff05e) at ../src/execution.cc:713
#5 0x080d8edc in v8::internal::Execution::InstantiateObject (data=...,
exc=0xbffff05e) at ../src/execution.cc:740
#6 0x08060056 in v8::ObjectTemplate::NewInstance (this=0x86bdffc) at
../src/api.cc:4768
#7 0x0804aa9f in wrap_object (object=0x86b6500) at main.cpp:90
#8 0x0804ae8b in handle_object (obj_request=0x86b6500) at main.cpp:137
#9 0x0804b06c in main (argc=1, argv=0xbffff254) at main.cpp:165
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <cstdlib>
#include <v8.h>
class MyRequest {
public:
std::string methodToString () {
return "Method to my madness";
}
};
v8::Persistent<v8::Context> g_object_context;
v8::Persistent<v8::ObjectTemplate> g_object_template;
v8::Handle<v8::Value> get_method (v8::Local<v8::String> name, const
v8::AccessorInfo& info);
const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
void ReportException(v8::TryCatch* try_catch) {
v8::HandleScope handle_scope;
v8::String::Utf8Value exception(try_catch->Exception());
const char* exception_string = ToCString(exception);
v8::Handle<v8::Message> message = try_catch->Message();
if (message.IsEmpty()) {
printf("%s\n", exception_string);
} else {
v8::String::Utf8Value filename(message->GetScriptResourceName());
const char* filename_string = ToCString(filename);
int linenum = message->GetLineNumber();
printf("%s:%i: %s\n", filename_string, linenum, exception_string);
v8::String::Utf8Value sourceline(message->GetSourceLine());
const char* sourceline_string = ToCString(sourceline);
printf("%s\n", sourceline_string);
int start = message->GetStartColumn();
for (int i = 0; i < start; i++) {
printf(" ");
}
int end = message->GetEndColumn();
for (int i = start; i < end; i++) {
printf("^");
}
printf("\n");
}
}
v8::Handle<v8::ObjectTemplate> make_object_template () {
v8::HandleScope handle_scope;
v8::Handle<v8::ObjectTemplate> result = v8::ObjectTemplate::New();
result->SetInternalFieldCount(1);
// Add accessors for each of the fields of the object.
result->SetAccessor(v8::String::NewSymbol("method"), get_method);
return handle_scope.Close(result);
}
v8::Handle<v8::Object> wrap_object (MyRequest* object) {
v8::HandleScope handle_scope;
if (g_object_template.IsEmpty ()) {
v8::Handle<v8::ObjectTemplate> raw_template = make_object_template ();
g_object_template = v8::Persistent<v8::ObjectTemplate>::New (
g_object_context->GetIsolate (), raw_template
);
}
v8::Handle<v8::ObjectTemplate> templ = g_object_template;
if (templ.IsEmpty ()) {
std::cout << "template empty" << std::endl;
} else {
std::cout << "template ok" << std::endl;
}
v8::TryCatch try_catch;
v8::Handle<v8::Object> result = templ->NewInstance();
if (try_catch.HasCaught () || result.IsEmpty ()) {
ReportException (&try_catch);
::exit (EXIT_FAILURE);
}
std::cout << "Getting ptr" << std::endl;
v8::Handle<v8::External> req_ptr = v8::External::New(object);
result->SetInternalField(0, req_ptr);
return handle_scope.Close(result);
}
MyRequest* unwrap_object (v8::Handle<v8::Object> obj) {
v8::Handle<v8::External> field = v8::Handle<v8::External>::Cast
(obj->GetInternalField (0));
void* ptr = field->Value ();
return static_cast<MyRequest*> (ptr);
}
v8::Handle<v8::Value> get_method (v8::Local<v8::String> name, const
v8::AccessorInfo& info) {
MyRequest* object = unwrap_object (info.Holder ());
std::string method = object->methodToString ();
return v8::String::New(method.c_str(), static_cast<int> (method.length()));
}
v8::Persistent<v8::Context> create_context () {
// Create a template for the global object.
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
return v8::Context::New(NULL, global);
}
void onload () {
v8::HandleScope handle_scope;
g_object_context = create_context();
std::cout << "loaded" << std::endl;
}
bool handle_object (MyRequest* obj_request) {
std::cout << "handle" << std::endl;
v8::HandleScope handle_scope;
v8::Handle<v8::Object> req_obj = wrap_object (obj_request);
g_object_context->Global()->Set(v8::String::New("my_object"), req_obj);
v8::Handle<v8::String> source = v8::String::New("my_object.method");
v8::Handle<v8::Script> script = v8::Script::Compile(source);
v8::Handle<v8::Value> result = script->Run();
v8::String::AsciiValue ascii(result);
std::cout << *ascii << std::endl;
return true;
}
void ondestroy () {
g_object_context.Dispose();
std::cout << "object destroyed" << std::endl;
}
int main(int argc, char* argv[]) {
v8::HandleScope handle_scope;
onload ();
MyRequest* mr = new MyRequest;
handle_object (mr);
ondestroy ();
return 0;
}
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.