只要回调参数是字符串,我就可以正常使用以下功能:
void Server::AppUse(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
Local<Function> nextCb = Local<Function>::Cast(args[0]);
Local<Function> argv[2] = {
String::NewFromUtf8(isolate, "hello world"),
String::NewFromUtf8(isolate, "hello again"),
};
nextCb->Call(isolate->GetCurrentContext()->Global(), 2, argv);
}
在节点中实现:
var app = require('./build/Release/middleware');
app.use(function (next, again) {
console.log(next);
console.log(again);;
});
这将输出以下实现节点:
$ node ./example.js
hello world
hello again
但是,现在我想添加一个回调。例如:
void Server::AppUse(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
Local<Function> nextCb = Local<Function>::Cast(args[0]);
Local<Function> argv[1] = {
nextCb
};
nextCb->Call(isolate->GetCurrentContext()->Global(), 1, argv);
}
这会导致 C++ 编译错误:
./src/server.cc:73:63: error: no matching function for call to ‘v8::Function::Call(v8::Local<v8::Object>, int, v8::Local<v8::Function> [1])’
../src/server.cc:73:63: note: candidate is:
In file included from ~/.node-gyp/0.12.4/src/node.h:61:0,
from ../src/server.cc:1:
~/.node-gyp/0.12.4/deps/v8/include/v8.h:2557:16: note: v8::Local<v8::Value> v8::Function::Call(v8::Handle<v8::Value>, int, v8::Handle<v8::Value>*)
这基本上意味着 V8::Call 只需要一个值数组。但是如果我想返回函数呢?当前插件文档中没有示例。