On Thu, May 1, 2014 at 2:04 AM, Brandon Harvey <[email protected]> wrote: > Hi, I must be missing something obvious. Can anyone tell me why this > lightly modified Hello World program crashes? (e.g. "terminated by signal > SIGSEGV (Address boundary error)") > > #include <v8.h> > using namespace v8; > > int main (int argc, char* argv[]) > { Isolate* i = Isolate::GetCurrent(); > HandleScope _ (i); > > // These 3 lines are taken basically from v8.h docs on FunctionTemplate > v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); > t -> Set ("func_property", v8::Number::New(1)); > t -> GetFunction (); // crash in here > > v8::Handle <v8::Context> context = v8::Context::New (i); > Context::Scope context_scope (context); > Handle <String> source = String::NewFromUtf8 (i, "hello world"); > Handle <Script> script = Script::Compile (source); > Handle <Value> result = script -> Run(); > String::Utf8Value utf8 (result); > printf ("%s\n", *utf8); > return 0; > } > > If I put the offending 3 lines after the context initialization, it works. > However, that's a problem -- all my interesting binding code (just like all > the binding code in the examples) needs to happen BEFORE the context is set > up, right? I want to decorate a global object and pass that as the 3rd > argument to Context::New(). What I actually want to do are things like > this, found in the node addon sample code, but is is the case that I just > can't do things like this until after the context is already in place? > > > void MyObject::Init(Handle<Object> exports) { > Isolate* isolate = Isolate::GetCurrent(); > > // Prepare constructor template > Local<FunctionTemplate> tpl = FunctionTemplate::New(New); > tpl->SetClassName(String::NewFromUtf8(isolate, "MyObject")); > tpl->InstanceTemplate()->SetInternalFieldCount(1); > > // Prototype > NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne); > > constructor.Reset(isolate, tpl->GetFunction()); > exports->Set(String::NewFromUtf8(isolate, "MyObject"), > tpl->GetFunction()); > } > > > thanks, > Brandon
The rule of thumb (somewhat simplified perhaps, someone correct me if I'm wrong or incomplete) is that primitives can be created outside of a context but objects only inside a context. If you look at v8.h and the type inherits from v8::Object (as v8::Function does), then it requires a context. -- -- 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/d/optout.
