我试图在C++中执行回调( C++是作为node.js程序的一部分运行的)。回调是对第三方库的调用,当有数据要传递时,它将调用回调。
我似乎遇到的问题是变量类型:
static void sensorEventCallback(const char *protocol, const char *model,
int id, int dataType, const char *value, int timestamp,
int callbackId, void *context)
{
//process the data here
}
Handle<Value> sensorEvents( const Arguments& args ) {
HandleScope scope;
...
callbackId = tdRegisterSensorEvent(
reinterpret_cast<TDSensorEvent>(&telldus_v8::sensorEventCallback),
Context::GetCurrent()->Global());
}我所犯的错误:
错误:不能将参数‘2’的‘v8::Local’转换为‘void*’以“int tdRegisterSensorEvent()”( const char,const char*,int,int,const char*,int,int*),void*‘
它似乎在挣扎于论点2,这就是背景。对于如何将V8对象转换为tdRegisterSensorEvent将接受的对象,有什么想法吗?
发布于 2012-11-21 08:03:27
窥探一下,GetCurrent似乎是在V8头中定义的,以返回Local<Context>
GitHub上的v8.h,GetCurrent()在上下文对象定义中的位置
此Local<T>是从基类Handle<T>派生的“轻量级堆栈分配句柄”的模板。
因此,似乎您有一个上下文指针,它的生存期由一个名为HandleScope的东西管理。如果您取出上下文指针并将其保存到以后的回调中,则在进行调用时,它可能仍然存在,也可能不存在。
如果您知道所有回调将在句柄作用域释放之前发生,则可以尝试使用dereference操作符重载并传递指针:
但你可能没有这个保证。
发布于 2012-11-21 08:02:38
作为n.m.。我猜应该传递上下文对象的地址。然后,你可以把它放回你的回调中。
void telldus_v8::sensorEventCallback(const char *protocol, const char *model,
int id, int dataType, const char *value, int timestamp,
int callbackId, void *context)
{
v8::Local<v8::Object>* ctx_ptr = static_cast<v8::Local<v8::Object>*>(context);
//process the data here
}
v8::Local<v8::Object> ctx = Context::GetCurrent()->Global();
callbackId = tdRegisterSensorEvent(
reinterpret_cast<TDSensorEvent>(&telldus_v8::sensorEventCallback),
&ctx);https://stackoverflow.com/questions/13488402
复制相似问题