خرید بک لینک

Vote count: 0

I would like to call a nodejs callback from within my asynchronous addon function. I have seen the synchronous example (here) and I am using a wonderful asynchronous example (here) as a starting base.

However, when I try to execute a callback that was given to the c++ AsyncWorker child class, I get a Segmentation fault.

Here is my code:

#include <nan.h>
#include <functional>
#include <iostream>
#include <exception>
using namespace Nan;
using namespace v8;
using namespace std;

class ScriptWorker : public AsyncWorker {
  public:
    ScriptWorker(Callback *callback, const std::map<std::string, Callback*>)
    : AsyncWorker(callback), script(script), cbs(cbs) {}

    ~ScriptWorker() {}

    void Execute () {
      Local<Value> argv[] = {
        New<v8::Number>(id)
      };
      // Segmentation fault here
      cbs["getUser"]->Call(1, argv);
    }

  private:
    std::string script;
    std::map<std::string, Callback*> cbs;
};

NAN_METHOD(Method) {
  Local<Object> array = info[0]->ToObject();
  Callback *callback = new Callback(info[1].As<Function>());

  // Build up callbacks passed in from javascript.
  // Will be a dynamic loop, but for now, hard code the one getUser example.
  std::map<std::string, Callback*> cbs;
  cbs.insert(std::pair<std::string, Callback*>("getUser",
    new Callback(
      array->Get(
        v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "getUser")
      ).As<Function>()
    )
  ));
  AsyncQueueWorker(new ScriptWorker(callback, cbs));
}

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 28 فروردين 1395 ساعت: 8:37

صفحه بندی