On Thu, Sep 4, 2014 at 9:59 PM, Peter Stoyanov <[email protected]> wrote: > Hello, > > can i pass a context as argument to a function, execute a script there and > then use the same context again when the function returns. Or how can I copy > the state of the context and return it, so I can use it outside the > function. Or pass the context by reference? I use here local context. In my > case this function is node.js. > > I am trying to embed node.js into my application. If i run the following > code with executeScript (commented) it's ok, the C++ functions are exposed > to JavaScript, the script is run and then the C++ functions are run. But I > want to use the APIs of node.js in my functions. So instead of executeScript > i use node.js. I built node.js as shared library and pass to node the > Isolate from my app. I also pass the context as parameter: > > Isolate* isolate = Isolate::GetCurrent(); > HandleScope handleScope(isolate); > Handle<String> source = readFile(isolate, param); > > Handle<ObjectTemplate> global = ObjectTemplate::New(isolate); > //Log > global->Set(String::NewFromUtf8(isolate, "log"), > FunctionTemplate::New(isolate, LogCallback)); > > Handle<Context> context = Context::New(isolate, NULL, global); > context_.Reset(isolate, context); > > //Context::Scope contextScope(context); > //if (!executeScript(source)) return; > > char *argv_n[] = {(char*)"node", (char*)"sourceOperator.js", NULL}; > > int argc_n = sizeof(argv_n) / sizeof(char*) - 1; > > Local<Context> new_context; > new_context = node::Start2(argc_n, argv_n, context, global); > > Context::Scope contextScope2(new_context); > stateInitFunctionJS.Reset(isolate, > registerFunction((char*)"stateInit")); > > ///////////////////////////////// > modified node Start: > Local<Context> Start2(int argc, char** argv, Handle<Context> &context) > Then i pass the context to CreateEnvironment > ... > In Start() of node.cc i commented some lines so the V8 will not be disposed > > ... > //code = EmitExit(env); > //RunAtExit(env); > } > > //env->Dispose(); > //env = NULL; > printf("Before return \n"); > return handle_scope.Escape(env->context()); > > //CHECK_NE(node_isolate, NULL); > //node_isolate->Dispose(); > //node_isolate = NULL; > //V8::Dispose(); > > //delete[] exec_argv; > //exec_argv = NULL; > > //return 1;// code; > .... > And then i have in the console: > > Before return > Function stateInit not found! > > If i use console.log in the JavaScript file it is executed correctly, but my > functions don't execute. > And do I have to enter a context before node::Start()? > > Thanks, > Peter Stoyanov
Your question is somewhat off-topic for v8-dev but I'll try to answer it. It's not entirely clear to me when your code runs but you should probably be using node::Init() and node::CreateEnvironment(), not node::Start(). Those functions are available in master but not in the latest v0.11 release (I think.) -- -- 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.
