From what I understand, SetInternalFieldCount(1) tells the template
there will be one C++ object accessible from javascript, and
SetInternalField(0, External::New(p)) actually puts that C++ object
into the Local<Object> making it accessible to javascript, correct?
So, say for example, if I wanted to make 25 Point instances accessible
to javascript, I'd do something like:
Handle<ObjectTemplate> point_templ = ObjectTemplate::New();
point_templ->SetInternalFieldCount(25);
Point* p[] = ...; // 25 instances of Point
Local<Object> obj = point_templ->NewInstance();
for (int i = 0; i < 25; i++)
obj->SetInternalField(i, External::New(p[i]));
Is that correct?
Does this need to be done on classes that return
other classes as return types? So if I setup a class template for
class A, if it returns a class B on a query, do I just need a template
for class B, or do I need to actually "expose" all the possible B's to
javascript first before making any calls to A that can return a B?
Handle<ObjectTemplate> point_templ = ObjectTemplate::New();
point_templ->SetInternalFieldCount(25);
Point* p[] = ...; // 25 instances of Point
Local<Object> obj = point_templ->NewInstance();
for (int i = 0; i < 25; i++)
obj->SetInternalField(i, External::New(p[i]));
Is that correct?That is technically legal but almost certainly _not_ what you want. JS cannot access those Point objects. It can only access obj, which itself contains a single Point[25].
On Tue, Jul 19, 2011 at 1:18 AM, strattonbrazil <stratto...@gmail.com> wrote:From what I understand, SetInternalFieldCount(1) tells the template
there will be one C++ object accessible from javascript, andAlmost - it tells v8 that you are reserving enough space for 1 (void *).