Picking up this thread again:
I have no problem exposing the class to the global context uisng the
pattern shown above. My problem is that the returned value of the class
constructor is not an instanceof that class if I construct object in it to
return:
static void
foo(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::Isolate* isolate = args.GetIsolate();
v8::HandleScope handle_scope(isolate);
v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New(isolate);
result->Set(v8::String::NewFromUtf8(isolate, "print"),
v8::FunctionTemplate::New(isolate, Print));
result->SetInternalFieldCount(1);
v8::Local<v8::Object> builder = result->NewInstance();
builder->SetAlignedPointerInInternalField(0,0);
return args.GetReturnValue().Set(builder);
}
v8::Local<v8::FunctionTemplate> result = v8::FunctionTemplate::New(isolate,
foo);
result->SetClassName(v8::String::NewFromUtf8(isolate,"Foo"));
global->Set(v8::String::NewFromUtf8(isolate, "Foo"),result);
> var x = new Foo();
> x instanceof Foo;
false
Is there any API that allows me to construct a wrapped object in foo() that
is an instanceof Foo?
Thanks!
On Wednesday, July 9, 2014 12:52:39 PM UTC-7, Ben Noordhuis wrote:
>
> On Wed, Jul 9, 2014 at 9:01 PM, Jane Chen <[email protected] <javascript:>>
> wrote:
> > Ben,
> >
> > How am I supposed to expose the class to the global object?
> >
> > I have this:
> >
> > // create and bind XMLBuilder function
> > v8::Local<v8::FunctionTemplate> bldrTemp =
> > v8::FunctionTemplate::New(isolate,createBuilder);
> > v8::Local<v8::String> bldrLabel =
> > v8::String::NewFromUtf8(isolate, "XMLBuilder");
> > bldrTemp->SetClassName(bldrLabel);
> > global->Set(bldrLabel,bldrTemp);
> >
> >> var b = new XMLBuilder();
> >> b instanceof XMLBuilder;
> > false
> >> Object.prototype.toString.call(b);
> > [object XMLBuilder]
>
> In pseudo-code:
>
> Local<Context> context = /* ... */;
> Local<String> name = /* ... */;
> Local<FunctionTemplate> t = FunctionTemplate::New(...);
> t->SetClassName(name);
> context->Global()->Set(name, t->GetFunction());
>
> Note that you don't need to expose the constructor on the actual
> global object, as long as it's in scope when you call `instanceof`.
> HTH.
>
--
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.