我希望我的类(class)拥有一个 v8::Context
和一个 v8::External
作为成员。因此,我认为我必须使用持久句柄。
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 在构造函数体内。
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
我考虑过使用指向持久句柄的指针,但这似乎违反直觉,因为句柄的概念已经暗示了某种引用。此外,我认为句柄会被破坏,以便 V8 的内部垃圾收集器可以清理对象。
如何将 V8 对象持久存储为类成员?
更新:即使我使用指向持久句柄的指针,对于返回持久句柄的方法,我也会遇到相同的编译器错误。
最佳答案
默认情况下,持久句柄使用不可复制的特性。显式传递可复制特征作为模板参数使它们像以前的版本一样工作。
Persistent<Value, CopyablePersistentTraits<Value>> persistent(isolate, value);
关于c++ - 如何在 V8 中存储持久句柄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22646546/