Upgrading from Spidermonkey 1.8.5 to 45. How to get property name in getters/setters now they are JSNatives and tiny IDs have been removed?

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

Upgrading from Spidermonkey 1.8.5 to 45. How to get property name in getters/setters now they are JSNatives and tiny IDs have been removed?

Miles Thornton
Hi,

I'm trying to upgrade my embedding from 1.8.5 to 45 and one of the things that I'm struggling with is how property getters/setters now work in 45.

In 1.8.5 I used tiny IDs and JSPropertySpec to define properties and getters/setters for a class.
e.g. In 1.8.5 I had something like

enum part_tinyid {
    PART_HEADING,
    PART_PID,
    PART_SECID,
    PART_MID,
    ...
};

JSPropertySpec dj_Part_dynamic_props[] =
{
    {"heading", PART_HEADING,  flags,  dj_get_part_prop, dj_set_part_prop },
    {"pid",     PART_PID,      flags,  dj_get_part_prop, dj_set_part_prop },
    {"secid",   PART_SECID,    flags,  dj_get_part_prop, dj_set_part_prop },
    {"mid",     PART_MID,      flags,  dj_get_part_prop, dj_set_part_prop },
...
    { 0 }
};  


and then my getter was something like

JSBool dj_get_part_prop(JSContext *cx, JSObject *obj, jsid propid, jsval *vp)
{
    jsval id = JS_IdToValue(cx, propid, &id);
    int idint = JSVAL_TO_INT(id);

    switch (idint)
    {
        case PART_HEADING:
            ...
            break;

        case PART_PID:
            ...
            break;
    }

}

i.e. I had a single getter and a single setter for all of the properties and I could find which property I was getting/setting from propid

Now in Spidermonkey 45 Tiny IDs have been removed and dj_get_part_prop/dj_set_part_prop are JSNatives so I have something like:

JSPropertySpec dj_Part_dynamic_props[] =
{
    JS_PSGS("heading", dj_get_part_prop, dj_set_part_prop, flags),
    JS_PSGS("pid",     dj_get_part_prop, dj_set_part_prop, flags),
    JS_PSGS("secid",   dj_get_part_prop, dj_set_part_prop, flags),
    JS_PSGS("mid",     dj_get_part_prop, dj_set_part_prop, flags),
...
    JS_PS_END
};


JSBool dj_get_part_prop(JSContext *cx, unsigned int argc, JS::Value *vp)
{
    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
    JS::RootedObject obj(cx, JS_THIS_OBJECT(cx, vp));
}

I can get the object by using JS_THIS_OBJECT and I can see that in the getter argc is 0, in the setter argc is 1 and args[0] contains the value of the property to set.
However, I can't see any way to find out *which* property I am getting/setting.

Am I being stupid here? How can I find out which property I am getting/setting? I know that tiny IDs have gone so I was expecting the property to be passed as a string or something.
Or is there no way of getting the property name and I have to use a different property getter/setter for each property?

If someone can help me I would be very grateful.

Thanks

Miles
_______________________________________________
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: Upgrading from Spidermonkey 1.8.5 to 45. How to get property name in getters/setters now they are JSNatives and tiny IDs have been removed?

Nicolas B. Pierron
On 06/05/2017 09:31 AM, Miles wrote:

> Now in Spidermonkey 45 Tiny IDs have been removed and dj_get_part_prop/dj_set_part_prop are JSNatives so I have something like:
>
> JSPropertySpec dj_Part_dynamic_props[] =
> {
>      JS_PSGS("heading", dj_get_part_prop, dj_set_part_prop, flags),
>      JS_PSGS("pid",     dj_get_part_prop, dj_set_part_prop, flags),
>      JS_PSGS("secid",   dj_get_part_prop, dj_set_part_prop, flags),
>      JS_PSGS("mid",     dj_get_part_prop, dj_set_part_prop, flags),
> ....
>      JS_PS_END
> };
>
>
> JSBool dj_get_part_prop(JSContext *cx, unsigned int argc, JS::Value *vp)
> {
>      JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
>      JS::RootedObject obj(cx, JS_THIS_OBJECT(cx, vp));
> }
>
> I can get the object by using JS_THIS_OBJECT and I can see that in the getter argc is 0, in the setter argc is 1 and args[0] contains the value of the property to set.
> However, I can't see any way to find out *which* property I am getting/setting.

You should specify a different function for each getter/setter, and thus you
can either move the code out of the switch-case that you had previously or
use an enum to identify the callers, to reuse almost the same code has you
had previously.

JSPropertySpec dj_Part_dynamic_props[] =
{
      JS_PSGS("heading", dj_get_part_prop_heading, dj_set_part_prop_heading,
flags),
      JS_PSGS("pid",     dj_get_part_prop_pid,     dj_set_part_prop_pid, flags),
      JS_PSGS("secid",   dj_get_part_prop_secid,   dj_set_part_prop_secid,
flags),
      ...
      JS_PS_END
};

enum PartPropertyNames {
   PartHeading,
   PartPid,
   PartSecid,
   ...
}

JSBool dj_get_part_prop_heading(JSContext *cx, unsigned int argc, JS::Value *vp)
{
      JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
      return dj_get_part_pro(cx, PartHeading, args)
}

Does that answer your question?


--
Nicolas B. Pierron
_______________________________________________
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: Upgrading from Spidermonkey 1.8.5 to 45. How to get property name in getters/setters now they are JSNatives and tiny IDs have been removed?

Thomas Richter
In reply to this post by Miles Thornton
Hi Miles,

so far I know, you have define different property getter/setter for each
property. That's the way I did it.

Thomas


Am 05.06.2017 um 11:31 schrieb Miles:

> Hi,
>
> I'm trying to upgrade my embedding from 1.8.5 to 45 and one of the things that I'm struggling with is how property getters/setters now work in 45.
>
> In 1.8.5 I used tiny IDs and JSPropertySpec to define properties and getters/setters for a class.
> e.g. In 1.8.5 I had something like
>
> enum part_tinyid {
>     PART_HEADING,
>     PART_PID,
>     PART_SECID,
>     PART_MID,
>     ...
> };
>
> JSPropertySpec dj_Part_dynamic_props[] =
> {
>     {"heading", PART_HEADING,  flags,  dj_get_part_prop, dj_set_part_prop },
>     {"pid",     PART_PID,      flags,  dj_get_part_prop, dj_set_part_prop },
>     {"secid",   PART_SECID,    flags,  dj_get_part_prop, dj_set_part_prop },
>     {"mid",     PART_MID,      flags,  dj_get_part_prop, dj_set_part_prop },
> ...
>     { 0 }
> };  
>
>
> and then my getter was something like
>
> JSBool dj_get_part_prop(JSContext *cx, JSObject *obj, jsid propid, jsval *vp)
> {
>     jsval id = JS_IdToValue(cx, propid, &id);
>     int idint = JSVAL_TO_INT(id);
>
>     switch (idint)
>     {
>         case PART_HEADING:
>             ...
>             break;
>
>         case PART_PID:
>             ...
>             break;
>     }
>
> }
>
> i.e. I had a single getter and a single setter for all of the properties and I could find which property I was getting/setting from propid
>
> Now in Spidermonkey 45 Tiny IDs have been removed and dj_get_part_prop/dj_set_part_prop are JSNatives so I have something like:
>
> JSPropertySpec dj_Part_dynamic_props[] =
> {
>     JS_PSGS("heading", dj_get_part_prop, dj_set_part_prop, flags),
>     JS_PSGS("pid",     dj_get_part_prop, dj_set_part_prop, flags),
>     JS_PSGS("secid",   dj_get_part_prop, dj_set_part_prop, flags),
>     JS_PSGS("mid",     dj_get_part_prop, dj_set_part_prop, flags),
> ...
>     JS_PS_END
> };
>
>
> JSBool dj_get_part_prop(JSContext *cx, unsigned int argc, JS::Value *vp)
> {
>     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
>     JS::RootedObject obj(cx, JS_THIS_OBJECT(cx, vp));
> }
>
> I can get the object by using JS_THIS_OBJECT and I can see that in the getter argc is 0, in the setter argc is 1 and args[0] contains the value of the property to set.
> However, I can't see any way to find out *which* property I am getting/setting.
>
> Am I being stupid here? How can I find out which property I am getting/setting? I know that tiny IDs have gone so I was expecting the property to be passed as a string or something.
> Or is there no way of getting the property name and I have to use a different property getter/setter for each property?
>
> If someone can help me I would be very grateful.
>
> Thanks
>
> Miles
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>

--
Dipl.-Ing. Thomas Richter

otris software AG
Königswall 21
D-44137 Dortmund

Fon +49 (0)231  958069-0
Fax +49 (0)231  958069-44
www.otris.de

GPG/PGP: 0x37739AF8

HRB 15040, Amtsgericht Dortmund
Vorstand: Dr. Frank Hofmann, Dr. Christoph Niemann
Aufsichtsratsvorsitzender: Norbert Niemann
_______________________________________________
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: Upgrading from Spidermonkey 1.8.5 to 45. How to get property name in getters/setters now they are JSNatives and tiny IDs have been removed?

Miles Thornton
In reply to this post by Miles Thornton
I'll stop looking for a way to get the property name now then! :-)
In that case  I think I will try to do something clever with macros to automatically write stub functions for the getters/setters so I can still have a single function and reuse as much as I can.
Many thanks for the replies.
_______________________________________________
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: Upgrading from Spidermonkey 1.8.5 to 45. How to get property name in getters/setters now they are JSNatives and tiny IDs have been removed?

paraboul
I wrote a "compatibility class" some time ago in order to handle tinyid
when they vanished.

Macro :
https://github.com/nidium/Nidium/blob/6ba6cffc94dd6441e7fb7558b07849
d9bc93d641/src/Binding/JSExposer.h#L135-L142

Class :
https://github.com/nidium/Nidium/blob/6ba6cffc94dd6441e7fb7558b07849
d9bc93d641/src/Binding/JSExposer.h#L819-L884

However, we eventually get rid of this and created "ClassMapper" :

https://github.com/nidium/Nidium/blob/master/src/Binding/ClassMapper.h

Usage :
https://github.com/nidium/Nidium/wiki/ClassMapper

2017-06-05 11:53 GMT+02:00 Miles <[hidden email]>:

> I'll stop looking for a way to get the property name now then! :-)
> In that case  I think I will try to do something clever with macros to
> automatically write stub functions for the getters/setters so I can still
> have a single function and reuse as much as I can.
> Many thanks for the replies.
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>



--
A.
_______________________________________________
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: Upgrading from Spidermonkey 1.8.5 to 45. How to get property name in getters/setters now they are JSNatives and tiny IDs have been removed?

Boris Zbarsky
In reply to this post by Miles Thornton
On 6/5/17 5:31 AM, Miles wrote:
> How can I find out which property I am getting/setting?

You _could_ examine the callee function name in your JSNative, but just
using different functions for different properties is probably better.

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