从c ++ node.js扩展中调用javascript函数

我有一些JavaScript文件,我想在调用我的c ++函数期间调用此文件中定义的一些函数。 请注意,我想调用它的名称,而不是一些随机函数作为parameter passing。

test.js:

function someFunc() { // do something... } var my_module = require("my_native_module"); my_module.nativeFunc(); 

TEST.CPP:

 using namespace v8; Handle< Value > nativeFunc(const Arguments & args) { HandleScope scope; // I want to somehow get someFunc value from current context by name Local< Value > function_value = Context::GetCurrent()->Global()->Get_By_Name(String::New("someFunc")); // this doesn't work // probably check that function_value is really a function... Local< Function > function_callable = Local< Function >::Cast(function_value); function_callable->Call(Context::GetCurrent()->Global(), 0, nullptr); return(scope.Close(Undefined())); } 

所有的例子似乎都接受这样的函数作为参数(callback):

 Local<Function> cb = Local<Function>::Cast(args[0]); 

但是我需要通过名字来find这个函数。

我没有一个快速的例子来准备testing这个,但是看到一些暗示你应该能够这样做的代码:

  Handle<String> process_name = String::NewFromUtf8(GetIsolate(), "Foo"); Handle<Value> process_val = context->Global()->Get(process_name); if (!process_val->IsFunction()) return false; Handle<Function> process_fun = Handle<Function>::Cast(process_val); process_fun->Call(Context::GetCurrent()->Global(), 0, nullptr);