Segmentation fault while trying to wrap an object

34 views
Skip to first unread message

Andre Oliveira

unread,
Apr 15, 2015, 2:02:09 PM4/15/15
to v8-u...@googlegroups.com
I am trying to embed a C++ program to v8, and after trying to follow the official embedder's guide and some other old (ancient, I'd say) guides all over the web, I came to the following program:
void testing()
{   // Initialize V8.
  V8
::InitializeICU();
 
Platform* platform = platform::CreateDefaultPlatform();
  V8
::InitializePlatform(platform);
  V8
::Initialize();
 
 
// Create a new Isolate and make it the current one.
 
Isolate* isolate = Isolate::New();
 
{
   
Isolate::Scope isolate_scope(isolate);

   
// Create a stack-allocated handle scope.
   
HandleScope handle_scope(isolate);
    scripthelper
* sh = new scripthelper();

   
// Create a new context with a global template
   
Handle<ObjectTemplate> global = ObjectTemplate::New(isolate);
   
   
Local<ObjectTemplate> design_templ = ObjectTemplate::New(isolate);
    design_templ
->SetInternalFieldCount(1);
   
Rsyn::Design* d = new Rsyn::Design();

   
//this line causes a segmentation fault:
   
Local<Object> obj = design_templ->NewInstance();
   
    obj
->SetInternalField(0, External::New(isolate, d));    
   
global->Set(String::NewFromUtf8(isolate, "Design"), FunctionTemplate::New(isolate, designConstructor));  
   
global->Set(String::NewFromUtf8(isolate, "print"), FunctionTemplate::New(isolate, jsprint));    
   
Local<Context> context = Context::New(isolate, NULL, global);
       
   
// Enter the context for compiling and running the hello world script.
   
Context::Scope context_scope(context);    
   
   
// Create a string containing the JavaScript source code.
   
Local<String> source = String::NewFromUtf8(isolate, sh->getSourceFromFile("/home/andreoliv/uplace2/project/src/script/test.js").c_str());
   
   
// Compile the source code.
   
Local<Script> script = Script::Compile(source);
   
   
//run script
    script
->Run();

   
// Run the desired function
   
Handle<Value> args[2];
    sh
->addStringToArguments("HAHA ", args, 0);
    sh
->addNumberToArguments(5.0, args, 1);    
   
Handle<Value> result = sh->callJSFunction(context->Global(), "JSrepeat", args, 2);          
   
// Convert the result to an UTF8 string and print it.
   
String::Utf8Value utf8(result);
    printf
("result: %s\n", *utf8);
   
    sh
->callJSFunction(context->Global(), "useCppFunc", args, 2);
}



   
Local<Object> obj = design_templ->NewInstance();

This line causes a segmentation fault that I could not fix.
Can someone explain what's going on?
While I'm on topic, is there any good and working guides on embedding? All guides I come across are either shallow or won't work anymore because of updates.

Reply all
Reply to author
Forward
0 new messages