我试图将从JavaScript传递给Node.js插件的对象存储在void *
中。我似乎无法得到这个编译;用node-gyp构建产生error: no matching function for call to 'Cast'
。在void *中检索和存储V8对象Node.js插件
我想要做的长版本是编写一个运行Csound的Node.js插件。 Csound从鸟瞰角度出发,使用C函数,该函数将(通常)第一个参数作为不透明Csound结构的指针。该结构包含void *
到“hostData
”,由Csound程序托管的任意数据。 Csound所做的一些事情,比如发布消息,在这种情况下用回调函数指针进行修改。我需要一个地方为Csound的每个实例存储回调,因此我试图让某人将hostData
设置为来自JavaScript的对象,但我也希望将Csound实例的回调设置为对象上的hidden properties。
我觉得代码需要看起来像
#include "csound.h"
#include <node.h>
static void CsoundMessageCallback(CSOUND *Csound, int attributes,
const char *format, va_list valist)
{
// Call the JavaScript function we stored in the hostData of Csound.
}
static void _wrap_csoundSetMessageCallback(
const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::HandleScope scope(v8::Isolate::GetCurrent());
CSOUND *Csound;
// Pretend we get the Csound instance from args[0] here. This is actually done
// by SWIG <http://www.swig.org>.
// This does not compile. csoundGetHostData() returns a void *, but I’m assuming
// hostData was set to an object from JavaScript.
v8::Persistent<v8::Object> hostData =
v8::Persistent<v8::Object>::Cast(csoundGetHostData(Csound));
hostData.SetHiddenValue(
v8::String::New("CsoundMessageCallback"),
v8::Persistent<v8::Function>::Cast(args[1])
);
csoundSetMessageCallback(Csound, CsoundMessageCallback);
}
我猜我需要在V8的内部领域的密切关注,但我真的不知道。
只需发信号给主线程(例如使用'uv_async_send()')并从那里执行JavaScript回调可能更安全。 – mscdex
我同意,但我是否还需要存储JavaScript中的回调?我的另一个想法是将Csound结构包装在C++类中,并将回调函数存储为成员变量,但如果可以使用Csound的'void *''hostData',我想我也可以。 – Nate