Re: JS 45 problem
Kent Williams <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.jseng |
|---|---|
| Message-ID | <[email protected]> |
So my change ends up being this: - obj = JS_NewObject(cx, &spider_xml); + JS::RootedObject newTarget(cx,&args.newTarget().toObject()); + JS::RootedObject protoObj(cx); + if(!JS_GetPrototype(cx,newTarget,&protoObj)) + return false; + obj = JS_NewObjectWithGivenProto(cx, &spider_xml, protoObj); Is there a more succinct way to accomplish this? I remember this being talked about on this mailing list a couple weeks ago, in a slightly different context. On 05/24/2016 09:47 AM, Boris Zbarsky wrote: > On 5/24/16 10:32 AM, Kent Williams wrote: >> obj = JS_NewObject(cx, &TESTO); > > Kent, > > This line is the problem. JS_NewObject used to guess at the prototype > to use based on the name of the given class and what that name > resolved to in global scope. > > This was all sorts of broken and fragile, and was removed (see > <https://bugzilla.mozilla.org/show_bug.cgi?id=1125567>). If you use > JS_NewObject, you will get an object whose prototype is Object.prototype. > > What you want here is JS_NewObjectWithGivenProto and pass in the thing > you want as your prototype. > > I think you have a few options for how to do that: > > 1) Save the value returned from JS_InitClass and use that. Should be > the right thing as long as no one is subclassing you. > > 2) In the constructor, get the "prototype" property of args.callee() > and use that. Again, should be the right thing as long as no one is > subclassing you. > > 3) In the constructor, get the "prototype" property of > args.newTarget(). Should do the right thing (as in, use the proto of > the constructor that was actually invoked) even if you're subclassed. > > -Boris > _______________________________________________ > dev-tech-js-engine mailing list > [email protected] > https://lists.mozilla.org/listinfo/dev-tech-js-engine