Subj : Re: Function name...? To : Robert Bates From : Brendan Eich Date : Tue Jul 08 2003 09:53 pm Robert Bates wrote: >Great! Exactly what I was looking for... I knew there had to be some way to >get back to the function object from within a native C call. I had poked >around in the SpiderMonkey code and seen references to argv[-2] but didn't >know if that was after an internal argument array shift or not. > That's part of the public API (argv[-1] is the |obj| parameter to natives, corresponding to |this| in scripted functions). One improvement to jband's fine reply: use JS_GetFunctionId, not JS_GetFunctionName, for Unicode fidelity and disambiguation of anonymous functions from functions that happen to be named "anonymous". See the comments in jsapi.h /be > >Thanks! > >"John Bandhauer" wrote in message >news:beevc4$nmq2@ripley.netscape.com... > > >>Quick and dirty... >> >>The function object (as a jsval) is in argv[-2]. Use >>JS_ValueToFunction on it to get the JSFunction*. Then >>JS_GetFunctionName to get its name. >> >>If you don't want to work in terms of strings then you would tag >>the function object when you create it by setting a private >>property on the object using JS_SetReservedSlot (you can't mess >>with the 'main' private slot on a function object, but there are >>2 available 'reserved' slots). See an example at: >> >> >> >http://lxr.mozilla.org/seamonkey/source/js/src/xpconnect/src/xpcwrappednativeinfo.cpp#144 > > >>And then use JS_GetReservedSlot on the function object at call time: >> >> >> >http://lxr.mozilla.org/seamonkey/source/js/src/xpconnect/src/xpcwrappednativeinfo.cpp#44 > > >>You can get the function object directly from argv[-2]... >> >> JSObject* funobj = JSVAL_TO_OBJECT(argv[-2]); >> >> >> >> >http://lxr.mozilla.org/seamonkey/source/js/src/xpconnect/src/xpcwrappednativejsops.cpp#1257 > > >>John. >> >>Robert Bates wrote: >> >> >> >>>OK, in pursuit of my Quick-n-Dirty C++ wrapper for JSObject, I am trying >>> >>> >to > > >>>figure out if there's a way (short of using the debug API) to get the >>> >>> >actual > > >>>method name invoked from the JSNative function call. For example: >>> >>>JSBool c_method(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, >>> >>> >jsval > > >>>*rval) >>>{ >>> JSString *jsMethod = ... >>> ... >>>} >>> >>>Is there a way, or am I trying to do something not intended...? >>> >>>Brendan's made me aware that there are lost cycles if I don't do the >>>straight-away mapping, but the objects I'm exposing will only have these >>>methods called a couple times at most, and not iteratively, so >>> >>> >Quick-n-Dirty > > >>>is the name of the game right now... >>> >>>Thanks! >>> >>> >>> > > > > .