也许我只是没有搜索正确的术语,但我被困住了..
我需要从 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
我想从 C++ 调用它(顺便说一句,来自与主线程不同的线程):
#include <napi.h>
void log_message_callback(char const* message) {
// magic Napi code which would call log_message_callback in JavaScript
}
void some_thread_fn() {
log_message_callback("hello world");
}
我可以这样做吗?我该怎么做?我应该一直在寻找什么?!