Subj : Re: Calling functions without the lookup To : Brian Barnes From : Brendan Eich Date : Tue Jan 14 2003 10:53 pm Brian Barnes wrote: > I've asked this before, and gotten some answers, tried them out, then > I looked through the code and found some equivalents. > > My problem is that I can certain functions over and over again, and > want to avoid the name to object lookup. > > Is this equivalent? > > JS_CallFunctionName(cx,obj,"BLECH",argc,&argv,&rval); > > To this: > > jsval myfunc; > > JS_GetProperty(cx,obj,"BLECH",&myfunc); > JS_CallFunctionValue(cx,obj,"BLECH",argc,&argv,&rval); You must mean JS_CallFunctionValue(cx, obj, (JSFunction *) JS_GetPrivate(cx, JSVAL_TO_OBJECT(myfunc), argc, &argv, &rval); a jsval is not a JSFunction*, or even a tagged JSFunction*. > Is there any problem with this across garbage collections? Possibly -- is obj well-connected to the live thing graph? Assuming so, is the property in obj named "BLECH" permanent and readonly? If so, you don't need to worry. If not, then you might hold a dangling pointer if someone reassigns to obj.BLECH or deletes it, so you should either make the property permanent and readonly, or root a pointer to the function's object. > I'd be caching the myfunc and using it continually. > > It would be nice to have an additional API that got you a JSFunction > from a name, like: > > JS_GetFunctionObject(cx,obj,"BLECH",&fun); > > Then I could call JS_CallFunction and the code would make a bit more > readable, but if this is OK, then this way is good for me. There is a JS_GetFunctionObject API, but it's just an infallible getter that returns the JSObject (pointer) associated with a JSFunction. You want the inverse, (JSFunction *) JS_GetPrivate(cx, myfuncobj), where myfuncobj is the JSObject* you found tagged as a jsval, stored in the "BLECH" property of obj. /be .