最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

c + +添加物为嵌套的NodeJS功能不会被调用

IT培训 admin 5浏览 0评论

c + +添加物为嵌套的NodeJS功能不会被调用

我写一个C ++本地的NodeJS附加的使用实现极小井字棋AI的V8引擎。

我有哪里嵌套函数不工作的问题。

这里是我的代码:

namespace Game {
    Move bestMove(...) {
        // implementation
    }
}
namespace addon {
    using namespace v8;
    using std::vector;
    ...

    // this function returns the best move back to nodejs
    void bestMove(const FunctionCallbackInfo<Value>& args) {
        Isolate* isolate = args.GetIsolate();
        ...

        auto returnVal = Game::bestMove(params); // Game::bestMove() returns the best move for the computer

        args.GetReturnValue().Set((returnVal.row * 3) + returnVal.col); // returns the move back to nodejs
}

通常情况下,如果游戏板是这样的(是计算机O):

    x _ _
    _ _ _
    _ _ _

因为它已经采取0函数不应返回x。但是它似乎总是返回0

我调查了一下后,我意识到,功能Game::bestMove()不会被调用。

添加是的,我知道这是问题,因为在我的功能std::cout << "Computing";添加Move bestMove(),它从来没有得到打印到控制台。

但是,如果我在函数std::cout << "Computing";添加addon::bestMove(),它的工作原理。

还有没有抛出编译时错误。

谢谢你的帮助。

回答如下:

如果你愿意搬到通过C ++绑定,节点 - 附加API(NPM通过可用),使用N-API这个回答有帮助。您正在使用C ++所以它可能使编码更加简单和容易的工作最彻底的方法。净,我不能告诉你什么是你的代码错误从什么贴,所以如果这是搅局者则没有必要继续读下去。

随着节点 - 附加API插件你会看起来像:

#include <napi.h>

// your move function
Napi::Value bestMove(const Napi::CallbackInfo& info) {
  Napi::Env env = info.Env();

  int move = Game::bestMove(params);

  // just return the number and the C++ inline wrappers handle
  // the details
  return Napi::Number::New(env, move);
}

// the module init function used in the NODE_API_MODULES macro below
Napi::Object Init(Napi::Env env, Napi::Object exports) {
  Napi::HandleScope scope(env);

  // expose the bestMove function on the exports object.
  exports.Set("bestMove", Napi::Function::New(env, bestMove));

  return exports;
}

NODE_API_MODULES(my_game, Init)

在JavaScript中你只需要绑定文件,通常在build/Release/my_game.node(或使用绑定包,以便你可以只需要(“my_game”))。所以

const game = require('my_game')
...

move = game.bestMove()

我不知道足够的细节来充实这个例子更好。

我与南工作的节点 - 附加API包前,发现它令人沮丧。我没有尝试直接使用V8,因为它关系我的申请节点的特定版本。

如果您想更详细检查https://github/nodejs/node-addon-api。这真的相当出色。

道歉,如果上述任何代码的有错误;我只是做它作为我去。

c + +添加物为嵌套的NodeJS功能不会被调用

我写一个C ++本地的NodeJS附加的使用实现极小井字棋AI的V8引擎。

我有哪里嵌套函数不工作的问题。

这里是我的代码:

namespace Game {
    Move bestMove(...) {
        // implementation
    }
}
namespace addon {
    using namespace v8;
    using std::vector;
    ...

    // this function returns the best move back to nodejs
    void bestMove(const FunctionCallbackInfo<Value>& args) {
        Isolate* isolate = args.GetIsolate();
        ...

        auto returnVal = Game::bestMove(params); // Game::bestMove() returns the best move for the computer

        args.GetReturnValue().Set((returnVal.row * 3) + returnVal.col); // returns the move back to nodejs
}

通常情况下,如果游戏板是这样的(是计算机O):

    x _ _
    _ _ _
    _ _ _

因为它已经采取0函数不应返回x。但是它似乎总是返回0

我调查了一下后,我意识到,功能Game::bestMove()不会被调用。

添加是的,我知道这是问题,因为在我的功能std::cout << "Computing";添加Move bestMove(),它从来没有得到打印到控制台。

但是,如果我在函数std::cout << "Computing";添加addon::bestMove(),它的工作原理。

还有没有抛出编译时错误。

谢谢你的帮助。

回答如下:

如果你愿意搬到通过C ++绑定,节点 - 附加API(NPM通过可用),使用N-API这个回答有帮助。您正在使用C ++所以它可能使编码更加简单和容易的工作最彻底的方法。净,我不能告诉你什么是你的代码错误从什么贴,所以如果这是搅局者则没有必要继续读下去。

随着节点 - 附加API插件你会看起来像:

#include <napi.h>

// your move function
Napi::Value bestMove(const Napi::CallbackInfo& info) {
  Napi::Env env = info.Env();

  int move = Game::bestMove(params);

  // just return the number and the C++ inline wrappers handle
  // the details
  return Napi::Number::New(env, move);
}

// the module init function used in the NODE_API_MODULES macro below
Napi::Object Init(Napi::Env env, Napi::Object exports) {
  Napi::HandleScope scope(env);

  // expose the bestMove function on the exports object.
  exports.Set("bestMove", Napi::Function::New(env, bestMove));

  return exports;
}

NODE_API_MODULES(my_game, Init)

在JavaScript中你只需要绑定文件,通常在build/Release/my_game.node(或使用绑定包,以便你可以只需要(“my_game”))。所以

const game = require('my_game')
...

move = game.bestMove()

我不知道足够的细节来充实这个例子更好。

我与南工作的节点 - 附加API包前,发现它令人沮丧。我没有尝试直接使用V8,因为它关系我的申请节点的特定版本。

如果您想更详细检查https://github/nodejs/node-addon-api。这真的相当出色。

道歉,如果上述任何代码的有错误;我只是做它作为我去。

发布评论

评论列表 (0)

  1. 暂无评论