从C++代码抛出错误调用 JavaScript 回调函数 - nbind
calling JavaScript callback function from C++ code throwing errors - nbind
我正在尝试注册一个JavaScript回调函数,以便稍后从C++类调用。我正在使用 nbind 来制作一个节点.js插件。这是我编写的一些更复杂的代码的示例代码,这些代码需要完成相同的操作:
C++ 代码 (testing.cc(:
#include "nbind/api.h"
#include <string>
#include <iostream>
class Test
{
nbind::cbFunction *callback;
public:
Test() {}
//this dummy var is only here so that doCallback() will be recognized as a function
void doCallback(int dummyVar)
{
std::cout << "Calling Javascript callback" << std::endl;
//call javascript code
if (callback != nullptr)
{
(*callback)("Hi there!n");
}
return;
}
void enrollCallback(nbind::cbFunction &cb)
{
callback = &cb;
return;
}
};
#include "nbind/nbind.h"
NBIND_CLASS(Test) {
construct<>();
method(doCallback);
method(enrollCallback);
method(unenrollCallback);
}
JavaScript代码(test.js(:
var nbind = require('nbind');
function printMessage(message) {
console.log(message);
}
var lib = nbind.init().lib;
var test = lib.Test();
test.enrollCallback(printMessage);
try{
test.doCallback(11);
} catch(err) {
console.log(err);
}
当我在命令行中运行上述代码时,我根本没有获得任何输出。当我像这样运行代码时:node inspect test.js
并输入继续命令,调用回调函数时出现以下错误:
Error: read ECONNRESET
at _errnoException (util.js:992:11)
at TCP.onread (net.js:618:25)
当我单步执行程序时,控制台出现以下错误:
TypeError: test.doCallback is not a function
at Object.<anonymous> (C:UsersmrcoleDesktoptestytest.js:22:10)
at Module._compile (module.js:649:14)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
我哪里出错了,为什么节点的行为不一致?
为了回答我自己的问题,不要使用 nbind 以这种方式调用 Node JavaScript 回调函数。这是一个异步调用(如果我错了,请纠正我(,截至 2018 年 6 月,nbind 不支持异步回调。
一个不错的替代方法是实现 NAN 提供的 AsyncWorker 类之一。
下面是一些示例代码:
class MyAsyncWorker : public Nan::AsyncProgressWorkerBase<T>
{
public:
MyAsyncWorker(Nan::Callback *callback_, const char *resource_name = "My Async Worker")
: AsyncProgressWorkerBase(callback_, resource_name)
{
}
~MyAsyncWorker()
{
}
//Do work, or process notifications here
void Execute(const ExecutionProgress &progress)
{
//do work. when you have something to report,
//call ExecutionProgress::Send( T * data, size_t count) to eventually call
//the HandleProgressCallback function.
}
//Call JavaScript callback from this function
void HandleProgressCallback(T *data, size_t count)
{
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
//initialize your data to passback to callback here
};
callback->Call(count,argv,async_resource);
}
//This is called when you are done executing
virtual void HandleOKCallback()
{
Nan::HandleScope scope;
v8::Local<v8::Value> argv[] = {
//Prepare final values to be returned
};
callback->Call(1,argv,async_resource);
}
};
相关文章:
- 架构决策:返回std::future还是提供回调
- 正在为Xtensa simcall函数编写回调函数
- 如何在C++中使用非静态成员函数作为回调函数
- 我可以使用谷歌 v8 在回调函数中获取 JavaScript 函数C++源文本吗?
- 从C++代码抛出错误调用 JavaScript 回调函数 - nbind
- node.js Nan:在函数C++调用 JavaScript 回调
- v8 回调不适用于堆上的 JavaScript 对象
- 如何使用iOS上的JavaScriptCore创建从JavaScript调用的C 回调
- 在V8中从C++回调调用Javascript函数
- JavaScript和C 回调功能具有参数
- Chromium Embedded如何从c++执行javascript回调
- C++信号回调(类似javascript)
- 从多线程C++插件回调NodeJS Javascript函数
- NodeJS Addon从NAN AsyncWorker::Execute内部调用Javascript回调
- 如何将包装的C++对象传递给 Javascript 回调
- 我如何做一个回调从FireBreath到JavaScript
- JavaScript V8 回调中的访问'Arguments'
- 在c++11中,类似于node.js/javascript回调的线程
- v8,libuv,nodejs,win32api-如何调用EnumWindows并具有回调调用javascript函数
- XPCOM C++多线程和javascript回调