我遇到這個錯誤時,寫一個nodejs c/c + +插件,它發生時,我嘗試存儲一個異步回調持久。沒有匹配函數調用Persistent <Function> ::新(隔離*&,本地<Function>&)
但我在v8.h中發現了聲明:V8_INLINE持久性(Isolate * isolate,Local < S> that)。通話似乎沒有任何問題。
附上我的代碼。先謝謝你!它讓我困惑了幾天。
struct reqData
{
int result;
int a;
int b;
char name[128];
Persistent<Function> callback;
};
static Handle<Value> test(const FunctionCallbackInfo<Value>& args)
{
Isolate *isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
if (args.Length() < 3 || !args[0]->IsNumber() || !args[1]->IsNumber())
{
return (*isolate).ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Bad argument")));
}
ssize_t int1 (args[0]->Int32Value());
ssize_t int2 (args[1]->Int32Value());
char nameBuffer[128] = {0};
args[2]->ToString()->WriteOneByte(nameBuffer);
if (args[3]->IsFunction())
{
Local<Function> callback = Local<Function>::Cast(args[3]);
reqData* request = new reqData;
request->callback = Persistent<Function>::New(isolate,callback);
request->a = int1;
request->b = int2;
strcpy(request->name, nameBuffer);
uv_work_t* req = new uv_work_t();
req->data = request;
uv_queue_work(uv_default_loop(), req, workerFunc, afterWorkFunc);
}
else
{
return (*isolate).ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Callback missing")));
}
return Undefined(isolate);
}
extern "C"
{
// equal to js
//
// exports.test = function Test(){};
//
static void init(Local<Object> exports)
{
//target->Set(String::NewSymbol("asyncAddon"), FunctionTemplate::New(test)->GetFunction());
NODE_SET_METHOD(exports, "asyncAddon", test);
}
}
NODE_MODULE(asyncAddon, init)
您所針對的節點的版本是? – mscdex
@mscdex Node.js v6.2.0。 –