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" <[email protected]> Mon, 5 Jun 2017 09:41:48 +0000
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <[email protected]>
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