Re: JS 45 problem

[email protected] Thu, 3 Nov 2016 14:18:07 -0700 (PDT)
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <[email protected]>
Hey,


Out of curiosity, what's the advantage of using |JS_NewObjectWithGivenProto| over |JS_NewObjectForConstructor| within a constructor call?

On Tuesday, May 24, 2016 at 6:29:24 PM UTC+2, Boris Zbarsky wrote:
> On 5/24/16 12:17 PM, Kent Williams wrote:
> > +        JS::RootedObject newTarget(cx,&args.newTarget().toObject());
> > +        JS::RootedObject protoObj(cx);
> > +        if(!JS_GetPrototype(cx,newTarget,&protoObj))
> 
> No, that's wrong.  What you want is more like:
> 
>    JS::RootedValue protoVal(cx);
>    if (!JS_GetProperty(cx, newTarget, "prototype", &protoVal))
>      return false;
>    JS::RootedObject protoObj(cx);
>    if (protoVal.isObject())
>      protoObj = &protoVal.toObject();
>    else
>      protoObj = JS_GetObjectPrototype(cx, newTarget);
>    obj = JS_NewObjectWithGivenProto(cx, &spider_xml, protoObj);
> 
> (yes, the signature of JS_GetObjectPrototype is dumb; we should fix that).
> 
> Basically, implement 
> http://www.ecma-international.org/ecma-262/6.0/#sec-getprototypefromconstructor 
> with the second arg being %ObjectPrototype%.
> 
> I don't think there's a more succinct API for this, sadly.  We should 
> totally consider adding one; something like:
> 
>    JS::GetPrototypeFromConstructor(JSContext *cx,
>                                    JSObject* ctor,
>                                    JSProtoKey key);
> 
> or something.  I filed 
> https://bugzilla.mozilla.org/show_bug.cgi?id=1275312 on that.
> 
> -Boris