首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ V8对象上下文

C++ V8对象上下文
EN

Stack Overflow用户
提问于 2012-11-21 07:36:34
回答 2查看 2.7K关注 0票数 1

我试图在C++中执行回调( C++是作为node.js程序的一部分运行的)。回调是对第三方库的调用,当有数据要传递时,它将调用回调。

我似乎遇到的问题是变量类型:

代码语言:javascript
复制
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将接受的对象,有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-21 08:03:27

窥探一下,GetCurrent似乎是在V8头中定义的,以返回Local<Context>

GitHub上的v8.h,GetCurrent()在上下文对象定义中的位置

Local<T>是从基类Handle<T>派生的“轻量级堆栈分配句柄”的模板。

v8.h关于GitHub的定义

v8.h在GitHub上,句柄的定义

因此,似乎您有一个上下文指针,它的生存期由一个名为HandleScope的东西管理。如果您取出上下文指针并将其保存到以后的回调中,则在进行调用时,它可能仍然存在,也可能不存在。

如果您知道所有回调将在句柄作用域释放之前发生,则可以尝试使用dereference操作符重载并传递指针:

()

但你可能没有这个保证。

票数 4
EN

Stack Overflow用户

发布于 2012-11-21 08:02:38

作为n.m.。我猜应该传递上下文对象的地址。然后,你可以把它放回你的回调中。

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13488402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档