繁体   English   中英

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

[英]calling javascript function from c++ node.js extension

我有一些javascript文件,我想在调用c ++函数的过程中调用此文件中定义的一些函数。 请注意,我想按名称调用它,而不仅仅是作为参数传递的一些随机函数。

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()));
}

所有这些示例似乎都将函数作为参数(回调)接受,如下所示:

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

但我需要按名称查找该函数。

我没有准备测试该示例的快速示例,但是正在查看一些代码,这些代码暗示您应该可以执行以下操作:

  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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM