从C ++代码调用JavaScript回调函数引发错误-nbind [英] calling JavaScript callback function from C++ code throwing errors - nbind

查看:101
本文介绍了从C ++代码调用JavaScript回调函数引发错误-nbind的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试注册一个JavaScript回调函数,以便以后从C ++类调用。我正在使用 nbind 制作一个node.js插件。我编写的一些示例代码包含一些需要完成同一操作的更复杂的代码:

I'm trying to register a JavaScript callback function to be called from a C++ class later. I'm using nbind to make a node.js addon. This some sample code I've written of some more complex code that needs to accomplish the same thing:

c ++代码(testing.cc):

the c++ code (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):

the JavaScript code (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 并输入继续命令时,调用回调函数时出现以下错误:

When I run the above code in a command line, I don't get any output at all. When I run the code like this: node inspect test.js and enter the continue command, I get the following error when the callback function is called:

Error: read ECONNRESET
    at _errnoException (util.js:992:11)
    at TCP.onread (net.js:618:25)

当我逐步执行程序时,控制台出现以下错误:

When I step through my program, I get the following error to the console:

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

我在哪里错了,为什么节点行为不一致?

Where am I going wrong, and why is node acting inconsistantly?

推荐答案

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

To answer my own question, don't use nbind for calling a Node JavaScript callback function in this manner. This is an asynchronous call (correct me if I'm wrong) and nbind does not, as of June 2018, support asynchronous callbacks.

一个不错的选择是实现一个提供的 AsyncWorker类。

A good alternative is to implement one of the 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);
    }
};

这篇关于从C ++代码调用JavaScript回调函数引发错误-nbind的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆