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?