Subj : Re: Spidermonkey: How to call a 'static method' from C To : netscape.public.mozilla.jseng From : Peter Paulus Date : Wed May 26 2004 01:27 pm Sterling Bates wrote: > Peter Paulus wrote: > >> ok = JS_CallFunctionName(context, globalObj, "XYZHandler.onOpen", 0, >> NULL, &result); > > > The obj parameter is the object containing the function, so you would > pass your XYZHandler JSObject in place of globalObj here, then simply > call "onOpen". > > Sterling Thank you for the prompt response. I could not find a operation in the API that resolves a name like 'XYZHandler.onOpen' to the XYZHandler object. So I've come up with the code below: void* Context::callFunction(const char* name, unsigned int argc, long argv[]) { jsval result = 0L; JSScript* script = scripts.top(); JSBool ok = JS_ExecuteScript(context, globalObj, script, &result); if (not ok) { ... } result=0L; JSObject* targetObject = 0L; char targetName[256]; ok = resolveScope(name, globalObj, &targetObject, (char*) targetName); if (not ok) { ... } ok = JS_CallFunctionName(context, targetObject, targetName, argc, argv, &result); if (not ok) { ... } return((void*) result); } JSBool Context::resolveScope(const char* qualifiedName, JSObject* startObject, JSObject** targetObject, char* targetName) { JSBool ok = true; jsval result = JSVAL_VOID; JSObject* currentObject = startObject; *targetObject = JSVAL_NULL; *targetName = '\0'; char namePart[256] = { '\0' }; unsigned short nameIndex = 0; unsigned short partIndex = 0; char c; while(c = qualifiedName[nameIndex], c and ok) { switch(c) { case '.': // all but last namepart found { // zero-terminate namepart namePart[partIndex] = '\0'; // get property ok = JS_GetProperty(context, currentObject, (char*) namePart, &result); if (JSVAL_IS_OBJECT(result)) { currentObject = JSVAL_TO_OBJECT(result); } else { ok = false; } // reset the namepart partIndex = 0; namePart[0] = '\0'; break; } default: // build the namepart { namePart[partIndex++] = c; } } nameIndex++; } if (ok) { // zero-terminate namepart namePart[partIndex] = '\0'; ok = JS_GetProperty(context, currentObject, namePart, &result); if (ok) { *targetObject = currentObject; strcpy(targetName, namePart); } } return(ok); } This now works fine for me. With kind regards, Peter Paulus .