假设我有两个用 C++ 开发的类,但使用 Nan NodeJS 本机模块公开。例如,“Sprite”和“Texture”类。
在 JavaScript 中,我希望能够让 Sprite 存储一个 Texture,就好像它只是另一个属性一样:
// Local instance of texture
var t = new Texture();
// Global instances of Sprites that won't be GC'd
global.spr = new Sprite();
global.spr2 = new Sprite();
// Same texture can be assigned to multiple sprites
spr.texture = t;
spr2.texture = t;
console.log(spr.texture);
在 C++ 方面,Sprite 的“纹理”属性将是存储或检索纹理的 Getter/Setter。但问题是,在内部存储 Texture 实例的正确方法是什么(来自提供的 v8::Local)?就像,我需要做些什么才能将它注册为 Sprite “拥有”并使其不会像上面的示例中那样被 GC 处理?