如何在 V8 中存储持久句柄?

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

问题描述

我希望我的班级拥有一个 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 中存储持久句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)