Subj : Re: (Spidermonkey) Converting a property name to a tinyId To : James Turner From : Brendan Eich Date : Wed Mar 12 2003 10:59 am James Turner wrote: >I have a pressing need to map a string which *might* be the name of >property I've defined on an object to it's numerical ID. Essentially I >need a function which returns either a failure or the value I passed >in via the JSPropertySpec to JS_InitClass. > >Essential I want: > >int8 tinyid; >JSBool hasTinyId = JS_GetPropertyTinyId(ctx, obj, "foo", &tinyid); > >to return true, and set tinyid to be (say) 42, assuming the >JSPropertySpec looked like > >{ "foo", 42, ... flags .... }, > >I've looked through the jsobj.c code but am getting lost rather >easily, so could someone tell me if this is even possible? I could >maintain a seperate lookup table by hand by copying the JSPropertySpec >array I pass to JS_InitClass, but I figure that table has *got* to >exist somewhere internally. > > It does, but maybe you can get it from where you need it. Where in your code do you have this pressing need? If within a property getter or setter, then of course JSVAL_IS_INT(id) is true, and JSVAL_TO_INT(id) gives the tinyid. Otherwise, if you have only a short list of properties, linear search should be ok. We could add a JS_GetPropertyTinyId as you suggest, but maybe it's not worth the code bloat (minor, but still) in the common engine, if only a few embeddings need it and can compute what it returns otherwise. So sell me! /be >Many thanks, >James > > .