Create a new native object with a constructor

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Create a new native object with a constructor

Braden McDaniel
What's the modern way to create a new native object that has a
constructor?

The documentation for JS_NewObject says:

    If clasp has a constructor (JSClass.construct), use
    JS_ConstructObject instead to ensure that the constructor is called.

But JS_ConstructObject is obsolete (and nonexistent in recent
releases).  A note on the doc page for JS_ConstructObject says:

    The preferred alternative is to save a copy of the constructor
    function for the class, then to call it using JS_New.

But that doesn't look like the right answer for a natively implemented
object (since JS_NewObject still does look like part of the answer for
natively implemented objects).

--
Braden McDaniel <[hidden email]>
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create a new native object with a constructor

Boris Zbarsky
On 6/19/16 9:05 PM, Braden McDaniel wrote:
> What's the modern way to create a new native object that has a
> constructor?

Has a constructor in what sense?  Do you mean that there is a JSNative
constructor that you want to get called that creates the object?  Or
that you create the object but then call the JSNative on it?  Something
else?

>     The preferred alternative is to save a copy of the constructor
>     function for the class, then to call it using JS_New.
>
> But that doesn't look like the right answer for a natively implemented
> object (since JS_NewObject still does look like part of the answer for
> natively implemented objects).

This comes back to my question above.  If the constructor JSNative
creates the object, then just calling it via JS_New will do the right
thing; it'll do the JS_NewObject itself.

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create a new native object with a constructor

Braden McDaniel
On Sun, 2016-06-19 at 21:19 -0400, Boris Zbarsky wrote:
> On 6/19/16 9:05 PM, Braden McDaniel wrote:
> > What's the modern way to create a new native object that has a
> > constructor?
>
> Has a constructor in what sense?  Do you mean that there is a
> JSNative 
> constructor that you want to get called that creates the object?

This.  That is, the JSClass::constructor member for the JSClass that
defines the native object implementation is not null.

> >     The preferred alternative is to save a copy of the constructor
> >     function for the class, then to call it using JS_New.
> >
> > But that doesn't look like the right answer for a natively
> > implemented
> > object (since JS_NewObject still does look like part of the answer
> > for
> > natively implemented objects).
>
> This comes back to my question above.  If the constructor JSNative 
> creates the object, then just calling it via JS_New will do the
> right 
> thing; it'll do the JS_NewObject itself.

Ah, got it.  Thanks.

On a related note, the JS_InitClass documentation includes this:

    If you provide a constructor for the class, then you should also
    pass an object to parent_proto. JS_InitClass uses parent_proto to
    build a prototype object for the class. The prototype object
    inherits properties and methods from the parent_proto object you
    provide.

I gather, then, that passing null for parent_proto is insufficient even
if the parent is Object? Is there some convenient way to get the proto
for Object in order to pass it here?

--
Braden McDaniel <[hidden email]>
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create a new native object with a constructor

Josh Matthews-4
In reply to this post by Boris Zbarsky
On 2016-06-20 7:06 AM, Braden McDaniel wrote:

> On a related note, the JS_InitClass documentation includes this:
>
>     If you provide a constructor for the class, then you should also
>     pass an object to parent_proto. JS_InitClass uses parent_proto to
>     build a prototype object for the class. The prototype object
>     inherits properties and methods from the parent_proto object you
>     provide.
>
> I gather, then, that passing null for parent_proto is insufficient even
> if the parent is Object? Is there some convenient way to get the proto
> for Object in order to pass it here?

You're looking for JS_GetObjectPrototype.

Cheers,
Josh

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create a new native object with a constructor

Braden McDaniel
On Mon, 2016-06-20 at 08:43 -0400, Josh Matthews wrote:

> On 2016-06-20 7:06 AM, Braden McDaniel wrote:
> > On a related note, the JS_InitClass documentation includes this:
> >
> >     If you provide a constructor for the class, then you should
> > also
> >     pass an object to parent_proto. JS_InitClass uses parent_proto
> > to
> >     build a prototype object for the class. The prototype object
> >     inherits properties and methods from the parent_proto object
> > you
> >     provide.
> >
> > I gather, then, that passing null for parent_proto is insufficient
> > even
> > if the parent is Object? Is there some convenient way to get the
> > proto
> > for Object in order to pass it here?
>
> You're looking for JS_GetObjectPrototype.

Is it?  That requires that I have an Object.  The global object is
handy enough; but is that what I want?

I think I want what, in JavaScript, this would give me:

    new Object().__proto__

So, I guess I'm asking if there's a way for me to get to that via the
SpiderMonkey API without actually instantiating a JavaScript Object.

--
Braden McDaniel <[hidden email]>
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create a new native object with a constructor

Jason Orendorff-2
On Mon, Jun 20, 2016 at 6:09 PM, Braden McDaniel <[hidden email]>
wrote:

> > You're looking for JS_GetObjectPrototype.
>
> Is it?  That requires that I have an Object.  The global object is
> handy enough; but is that what I want?
>

Yes.

-j
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create a new native object with a constructor

Boris Zbarsky
In reply to this post by Josh Matthews-4
On 6/20/16 1:09 PM, Braden McDaniel wrote:
> Is it?  That requires that I have an Object.  The global object is
> handy enough; but is that what I want?

It is.

Note also https://bugzilla.mozilla.org/show_bug.cgi?id=1275315 which
will harmonize the signatures of the various JS_Get*Prototype functions,
or rather create new functions that do the same thing and just take a cx
and have maybe less-confusing names.  But in the meantime, passing the
global to JS_GetObjectPrototype is what you want.

-Boris

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Create a new native object with a constructor

Braden McDaniel
On Mon, 2016-06-20 at 15:11 -0400, Boris Zbarsky wrote:

> On 6/20/16 1:09 PM, Braden McDaniel wrote:
> > Is it?  That requires that I have an Object.  The global object is
> > handy enough; but is that what I want?
>
> It is.
>
> Note also https://bugzilla.mozilla.org/show_bug.cgi?id=1275315 which 
> will harmonize the signatures of the various JS_Get*Prototype
> functions, or rather create new functions that do the same thing and
> just take a cx and have maybe less-confusing names.  But in the
> meantime, passing the global to JS_GetObjectPrototype is what you
> want.

Okay; got it.  (And as it happens, it just made more sense for me to
keep the prototypes resulting from JS_InitClass somewhere accessible.)

--

Braden McDaniel <[hidden email]>
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Loading...