fra*_*ans 8 javascript c++ node.js n-api
也许我只是没有搜索正确的词,但是我被困住了。
我需要从C ++调用JavaScript函数,这与使用普通C API可以完成的操作非常相似。
注:我不希望传递一个回调到C ++代码,但我已经知道要调用的函数的名称!
因此,例如,我在JavaScript中具有以下功能:
function log_message_callback(context, message) {
console.log(`${context}: ${message}`);
}
my_napi_module.initialize(); // <-- starts thread that would call log_message_callback
Run Code Online (Sandbox Code Playgroud)
从C ++我想调用它(顺便说一句,来自与主线程不同的线程):
function log_message_callback(context, message) {
console.log(`${context}: ${message}`);
}
my_napi_module.initialize(); // <-- starts thread that would call log_message_callback
Run Code Online (Sandbox Code Playgroud)
我可以这样做吗?我该怎么做?我应该一直在寻找什么?
通常只能从本机插件的主线程调用JavaScript函数。您可能会从这里获得更多信息
所述napi_call_function()可用于调用从本地层JavaScript函数。该文档还包含一个代码片段以供使用。 https://nodejs.org/dist/latest-v11.x/docs/api/n-api.html#n_api_napi_call_function
这不是一个答案- 只是一些研究成果..
看起来我必须编写的代码必须看起来像这样 - 目前我不知道如何设置一切才能工作..
大部分代码取自此事件分支。
这些值必须可用 - 似乎我必须在模块初始化中初始化它们。
static v8::Persistent<v8::Context> context_;
static v8::Isolate *isolate_;
Run Code Online (Sandbox Code Playgroud)
该函数既log_message_callback()从 JavaScript 世界转换为 a
v8::Function又调用它。更复杂的方法将这些步骤分开:
extern "C" {
void log_message_callback(char const* message) {
v8::Locker locker(isolate_);
v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handle_scope(isolate_);
auto context = context_.Get(isolate_);
v8::Context::Scope context_scope(context);
v8::Persistent<v8::Function> log_message_callback_fn;
/// this is only needed once - just for demonstration
{
auto global = context->Global();
v8::Local<v8::Value> log_message_callback_def;
if (!global->Get(
context, v8Str(isolate_, "log_message_callback")).ToLocal(&log_message_callback_def)) {
return;
}
if (!log_message_callback_def->IsFunction()) {
return;
}
if (log_message_callback_def->IsFunction()) {
auto on_update_fun = log_message_callback_def.As<v8::Function>();
log_message_callback_fn.Reset(isolate_, on_update_fun);
}
}
v8::Local<v8::Value> args[2];
log_message_callback_fn.Get(isolate_)->Call(context->Global(), 2, args);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
271 次 |
| 最近记录: |