问题描述
我希望我的班级拥有一个 v8::Context
和一个 v8::External
作为成员.因此,我认为我必须使用持久句柄.
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;
};
但是,持久句柄在 V8 中是不可复制的,因此代码无法编译.错误发生在两个成员被初始化的行中.对于上下文,this 在构造函数的初始化列表中,对于外部的 this 在构造函数体中.
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> 错误 C2440:=":无法从v8::Primitive *"转换为v8::Object *volatile"
1> 指向的类型不相关;转换需要 reinterpret_cast、C 样式转换或函数样式转换
1> includev8v8.h(603) : 请参阅正在编译的函数模板实例化 'void v8::NonCopyablePersistentTraits::Uncompilable(void)' 的参考
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> includev8v8.h(603) : see reference to function template instantiation 'void v8::NonCopyablePersistentTraits::Uncompilable(void)' being compiled
我考虑过使用指向持久句柄的指针,但这似乎违反直觉,因为句柄的概念已经暗示了某种引用.此外,我认为句柄会被破坏,以便 V8 的内部垃圾收集器可以清理对象.
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.
如何将 V8 对象永久存储为类成员?
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 中存储持久句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!