内部字段计数是什么,SetInternalFieldCount使用的是什么?

内部字段计数是什么,SetInternalFieldCount使用的是什么?

我看不懂什么 SetInternalFieldCount()函数实际执行。v8文档中的函数被描述为设置" 对象的内部字段的数量从该模板生成消息" 这些都很意义和unilluminating 。

v8 embedder指南" 他们给本示例

point_templ->SetInternalFieldCount(1); 

并说" 这内部字段计数设置为1,其索引为0,这意味着该对象有一个内部字段指向C++对象。"

但到底是一个内部字段,设置此值真正辨别出什么计划?

时间:15年06月02日原作者:Loourr共2个回答

- 事务中不能使用高级函数. SetInternalFieldCount指示V8分配内部存储插槽以满足每个实例使用模板创建。此allowes你存储任何类型的信息在那些实例。

很有用,例如,存储连接V8对象与C++类实例。

void* p;  // any pointer
Local<Object> obj = point_templ->NewInstance();
obj->SetInternalField(0, External::New(p));      // 0 means 1-st internal field

还是用于对齐的指针:

obj->SetAlignedPointerInInternalField(0, p);     // 0 means 1-st internal field

此后在另一个程序的一部分,就能够把指针如下:

v8::Handle<v8::Object> handle;    // some object handle
if (handle->InternalFieldCount() > 0)
{
    void* p = handle->GetAlignedPointerFromInternalField(0); // from 1-st field
    // ... do something with p, e.g. cast it to wrapped C++ class instance
}
发布时间: 15年06月02日原作者:shayief

从v8 .h:

/**
 * Sets the number of internal fields for objects generated from
 * this template.
 */
 void SetInternalFieldCount(int value);

如果给一个类设置了5 个数据成员,那么你应该SetInternalFieldCount为5 。

class MyObject: public node::ObjectWrap {
public:
    int i;
    int j;
    int n;
    int z;
    int w;
}
发布时间: 15年06月02日原作者:luistm

21 down vote accepted

Function SetInternalFieldCount instructs V8 to allocate internal storage slots for every instance created using template. This allowes you store any kind of information inside those instances.

It is useful, for example, to store connection between V8 object and C++ class instance.

void* p;  // any pointer
Local<Object> obj = point_templ->NewInstance();
obj->SetInternalField(0, External::New(p));      // 0 means 1-st internal field

or for aligned pointer:

obj->SetAlignedPointerInInternalField(0, p);     // 0 means 1-st internal field

After this in another part of a program you can get your pointer like this:

v8::Handle<v8::Object> handle;    // some object handle
if (handle->InternalFieldCount() > 0)
{
    void* p = handle->GetAlignedPointerFromInternalField(0); // from 1-st field
    // ... do something with p, e.g. cast it to wrapped C++ class instance
}
share improve this answer
 
 
Exactly. This allows some c++ data to tag along with the object so it's available to the object template when it needs to do things like look up or set a value. –  xaxxon Dec 28 '15 at 3:53
 
In this case, when the "obj" is GCed in JavaScript, will the object pointed by "p" be deleted as well? (I should have checked the V8 source code first.) –  Shane Lu May 24 at 5:16

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值