Subj : Re: (Spidermonkey) Converting a property name to a tinyId To : James Turner From : Brendan Eich Date : Sat Mar 15 2003 10:41 am > > >Well ... I have no illusions about the general aplicability of this >function to other embedders, feel free to simply post a code snippet >that gets the job done. Anyway, the reason I need this is I'm >'dual-purpose'ing my property setters (and maybe getters too). I.e >they are hooked up as 'direct' JSPropertyOps in the property >defintions for my object. But I also want to invoke the same code >logic in response to some other events (persistence and network >transmission, it's a long story). > > Couldn't you use JS_GetProperty and JS_SetProperty to do the getting and setting from native code? True, those have to hash the property name, but so would any JS_GetPropertyTinyId that takes a const char *name parameter. Anyway, hashing a string is cheaper by orders than executing a precompiled script, which is cheaper by far in turn than compiling (evaluating). Here's a more general API proposal, not meant to be used if it's not needed, that returns the jsval id that you would get passed into your getters and setters: JSBool JS_GetPropertyUserId(JSContext *cx, JSObject *obj, const char *name, jsval *idp) { JSAtom *atom; JSObject *pobj; JSScopeProperty *sprop; if (!OBJ_IS_NATIVE(obj)) { JS_ReportError(cx, "can't get tinyid from non-native property %s", name); return JS_FALSE; } atom = js_Atomize(cx, name, strlen(name), 0); if (!atom) return JS_FALSE; if (!js_LookupProperty(cx, obj, (jsid)atom, &pobj, (JSProperty **)&sprop)) return JS_FALSE; *idp = SPROP_USERID(sprop); return JS_TRUE; } But if you call this, then call your getter (say) directly, you're doing about as much work as JS_GetProperty does, for no significant gain. For the setter case, you have to be sure the object has already had ad-hoc properties set on it, or else that you are setting only properties defined with the JSPROP_SHARED attribute. Otherwise, you'll fail to give the particular object on which a property is being set its own map (scope). So I think you should be good with JS_GetProperty and JS_SetProperty. Let me know if I'm missing something. /be .