Subj : Re: relating to JS_DefineFunction To : SteveHolt From : Brendan Eich Date : Fri Apr 22 2005 03:04 pm SteveHolt wrote: > I don't know if this is what Frank wants to do, but I currently have > two related issues. > > I am currently planning to implement an object with a lot of methods > which do similar things, and it would be good to be able to have them > all invoke the same C function and apply minor variations there - > except I don't know which call the user requested when it gets hit. Ah, that's easy (and not what Frank wants, AFAICT). It's done by LiveConnect and XPConnect all the time. The object that was invoked when you're in the JSNative (C native function) it wraps is available as argv[-2]. That's the callee, always, in a native (argv[-1] is OBJECT_TO_JSVAL(obj), i.e., the |this| parameter). > Similarly I want to implement arbitrary function calls on an object. > That is, if you invoke a function on it, it will work out if it can > support it at the time. That's different. You can't get into a native function without having some kind of call hook. You could implement JSClass.call (an optional hook usually stubbed with null), but remember: you implement on the object being called, not on the object that contains a property whose value is the object being called. You could, in recent SpiderMonkey releases (perhaps only in the latest?) use __noSuchMethod__ as well. > I have already figured that I can pick up on the Get which precedes the > call and return a generic function, but again, when that function > actually gets called, what was it they asked for again? Getting argv[-2], or JSVAL_TO_OBJECT(argv[-2]), won't get you the name by which the function was invoked. You'll still need something about that function to help you dispatch. You could make a separate function object wrapping the same, single native function, and use the function object's identity to dispatch. You could set one of the two reserved slots for the Function class -- see JS_SetReservedSlot -- and then get the slot's value to help decide how to dispatch. Again, this assumes a distinct function object for each method you want to call, all sharing the same native C function. It's not really a good idea to dispatch by name, because unless you take special steps, a function can be invoked by any name, on any object as its |this| parameter. /be .