2

JavaScript コードで Web ワーカー スレッドを作成しました。node-gyp と V8 を使用して、スレッドから C++ 関数を呼び出そうとしています。しかし、私はそれを機能させることができません。

hello.cc のコードは次のとおりです。

#include <v8.h>

using namespace v8;
extern std::string myhello();
Handle<Value> Method(const Arguments& args) { 
  HandleScope scope;
  return scope.Close(String::New("hello"));
}

void init(Handle<Object> exports) {
  exports->Set(String::NewSymbol("hello"),
      FunctionTemplate::New(Method)->GetFunction()
    );
}

NODE_MODULE(hello, init)

myhello.js のコードは次のとおりです。

var addon = require('./build/Release/hello');
var thread = require('webworker-threads');

var t = thread.create();
console.log(t.eval("addon.hello()")); 

実行するnode myhello.jsと、次の出力が得られます

{ id: 0,
  eval: [Function: eval],
  load: [Function: load],
  emit: [Function: emit],
  emitSerialized: [Function: emitSerialized],
  destroy: [Function: destroy],
  on: [Function],
  once: [Function],
  removeAllListeners: [Function],
  dispatchEvents: [Function],
  _on: {} }

コンソールに「hello」が表示されることを期待しています。

ヘルプやポインタに感謝します。

4

1 に答える 1