如何在V8中存储持久句柄? [英] How to store persistent handles in V8?

查看:1517
本文介绍了如何在V8中存储持久句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的班级持有 v8 :: Context v8 :: External 因此,我认为我必须使用持久处理。

  ScriptHelper类{
public:
ScriptHelper v8 :: Persistent< v8 :: Context> Context):context(Context){
// ...
}
// ...
private:
v8 :: Persistent< v8 :: Context>上下文;
v8 :: Persistent< v8 :: External>外部;
};

但是,持久句柄在V8中是不可复制的,所以代码不能编译。在两个memberes被初始化的行中发生错误。对于上下文,这是在构造函数的初始化器列表中,因为外部这是在构造函数体内。


1>错误C2440 :'=':不能从'v8 :: Primitive *'转换为'v8 :: Object * volatile'

1>指向的类型是不相关的;转换需要reinterpret_cast,C风格的转换或函数式转换

1> include\v8\v8.h(603):参见函数模板实例化void v8 :: NonCopyablePersistentTraits :: Uncompable (void)'正在编译


我想到使用指针持久句柄,但这似乎是直观的,因为句柄的概念已经暗示了一些种类的参考。此外,我认为句柄会被破坏,这样V8的内部垃圾收集器可以清理对象。



如何存储V8对象作为类成员永久? p>

更新:即使我使用指针持久句柄,我得到相同的编译器错误返回持久句柄的方法。

解决方案

默认情况下,持久句柄使用非可复制的trait。明确传递可复制的trait作为模板参数,使它们像以前的版本一样工作。

  Persistent< Value,CopyablePersistentTraits< Value>持久性(隔离,价值); 


I want my class to hold a v8::Context and a v8::External as members. Therefore, I thought I had to use persistent handles.

class ScriptHelper {
public:
    ScriptHelper(v8::Persistent<v8::Context> Context) : context(Context) {
        // ...
    }
    // ...
private:
    v8::Persistent<v8::Context> context;
    v8::Persistent<v8::External> external;
};

However, persistent handles are non copyable in V8, so the code does not compile. The error occurs in the lines where the two memberes get initialized. For the context, this is in the initializer list of the constructor, for the external this is inside the constructor body.

1> error C2440: '=' : cannot convert from 'v8::Primitive *' to 'v8::Object *volatile '
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> include\v8\v8.h(603) : see reference to function template instantiation 'void v8::NonCopyablePersistentTraits::Uncompilable(void)' being compiled

I thought about using pointers to persistent handles but that seems counter intuitive since the concept of handles already implies some kind of reference. Moreover, I think the handles would get destructed then so that V8's internal garbage collector could clean up the objects.

How can I store V8 objects as class members persistently?

Update: Even if I use pointer to persistent handles, I have get same compiler errors for methods that return persistent handles.

解决方案

By default, persistent handles use a non copyable trait. Explicitly passing the copyable trait as template argument makes them work like in prior versions.

Persistent<Value, CopyablePersistentTraits<Value>> persistent(isolate, value);

这篇关于如何在V8中存储持久句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆