从C ++代码调用JavaScript回调函数抛出错误 - nbind

时间:2018-06-14 17:19:47

标签: javascript c++ node.js callback

我试图注册一个稍后从C ++类调用的JavaScript回调函数。我使用nbind创建了node.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并输入continue命令时,在调用回调函数时出现以下错误:

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:\Users\mrcole\Desktop\testy\test.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

我哪里出错,为什么节点表现不一致?

1 个答案:

答案 0 :(得分:0)

要回答我自己的问题,请不要使用nbind以这种方式调用Node JavaScript回调函数。这是一个异步调用(如果我错了,请纠正我),并且自2018年6月起,nbind不支持异步回调。

一个好的选择是实现AsyncWorker classes provided by NAN之一。

以下是一些示例代码:

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