Get the context of a script

155 views
Skip to first unread message

Peter Stoyanov

unread,
Sep 14, 2014, 9:10:19 PM9/14/14
to nod...@googlegroups.com
Hello,

I am embedding node.js(v0.11.13) into my C++ application. I use node::Init() and node::CreateEnvironment(). I pass a .js file to CreateEnvironment and it is executed.

How can I get the context of the executed script, because I want to get the function name from JavaScript and cast it to a Function. Then I want it to store the function in persistent Function to be able to call it later from C++.
Or if this is not possible or too complicated is there any other way to do it?

Thanks,
Peter

Ben Noordhuis

unread,
Sep 15, 2014, 5:34:33 PM9/15/14
to nod...@googlegroups.com
It's not clear to me at what point in your application's life cycle
you want to get hold of a reference to the context. If it's from a
v8::FunctionTemplate callback, you can retrieve the context with
v8::Isolate::GetCallingContext().

Peter Stoyanov

unread,
Sep 16, 2014, 2:43:54 PM9/16/14
to nod...@googlegroups.com
Hi Ben,

I want to get the context after the javascript file is executed from node::CreateEnvironment(). I am trying like this:

    Isolate* isolate = Isolate::GetCurrent();    
    HandleScope handleScope(isolate);           

    Handle<String> source = readFile(isolate, param);
   
    if (source.IsEmpty())
    {
        LOG4CXX_ERROR(logger, "Error reading: " << param);
        return;
    }

    Handle<ObjectTemplate> global = ObjectTemplate::New(isolate);
  
    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);
 
    char *argv_n[] = {(char*)"node", param, NULL};
    int argc_n = sizeof(argv_n) / sizeof(char*) - 1;
 
    int exec_argc = 0;
    const char** exec_argv = NULL;
    node::Init(&argc_n, const_cast<const char**>(argv_n), &exec_argc, &exec_argv);
    env = node::CreateEnvironment(isolate, context, argc_n, argv_n, exec_argc, exec_argv);  
  
    Local<Context> context2 = env->context();

    if (context2.IsEmpty()) printf("Empty context. \n");
    context_.Reset(isolate, context2);

    Context::Scope context_scope(context2);
    Handle<String> processFunctionName = String::NewFromUtf8(isolate, "stateInit");   
    Handle<Value> processFunctionValue = context2->Global()->Get(processFunctionName);


    if (!processFunctionValue->IsFunction())
    {
        LOG4CXX_ERROR(logger, "Function " << "stateInit" << " not found!");
        exit(1);
    }

    Local<Function> processFunction = Handle<Function>::Cast(processFunctionValue);
    stateInitFunctionJS.Reset(isolate, processFunction);

And then the out is Empty context. and segfault. "param" is my javascript file and I don't know in which context of node.js is executed.
Or may be my mistake is at:
    Handle<Value> processFunctionValue = context2->Global()->Get(processFunctionName);
How can I cat the name of the function from node.js?

Peter Stoyanov

unread,
Sep 16, 2014, 3:10:35 PM9/16/14
to nod...@googlegroups.com
--corrected
And then the ouput is "Function stateInit not found. "param" is my javascript file and I don't know in which context of node.js is executed.

Ben Noordhuis

unread,
Sep 16, 2014, 11:54:10 PM9/16/14
to nod...@googlegroups.com
How and where is that function stateInit() defined?

Peter Stoyanov

unread,
Sep 17, 2014, 7:09:01 AM9/17/14
to nod...@googlegroups.com
It is in the javascript file, that i pass to node::CreateEnvironmet():

function stateInit()
{
    return 0;
}

and it is called later from C++.

I am trying to figure out, where exactly in node.js the javascript file that is passed is compiled and run. I am reading the code but at some point I am losing the track.





Ben Noordhuis

unread,
Sep 17, 2014, 3:38:07 PM9/17/14
to nod...@googlegroups.com
Your script is executed as a module. If you want to expose it to C++,
you need to make it globally visible:

global.stateInit = function() { return 0 };

Peter Stoyanov

unread,
Sep 18, 2014, 6:04:10 AM9/18/14
to nod...@googlegroups.com
It works now, this is the solution. I don't even have to use env->context().

Thank you, Ben


Reply all
Reply to author
Forward
0 new messages