Subj : run-time installation into the Javascript context using DLLs To : netscape.public.mozilla.jseng From : Jason Date : Fri Mar 18 2005 02:55 pm anything seem wrong with this approach? I have a javascript shell running under Windows that I'd like to compile once & be done with it, and add functionality incrementally by putting it into a DLL. It seems to work quite well, actually (the "loaddll" function is compiled into my shell; I made a small DLL called "jspkg" which has a registration function MyRegFunc and a single test function jtest01 which multiplies argv[0]*argv[1] and returns the product, MyRegFunc calls JS_DefineFunction(cx, obj, "jtest01", jtest01, 0, JSPROP_READONLY) and both are exported from the DLL): js> loaddll("jspkg","MyRegFunc") Loading library jspkg... Found library; locating MyRegFunc()... calling MyRegFunc()... int jtest01(int x,y) returns x*y DLL jspkg registered successfully! js> jtest01(8,33) 264 static JSBool js_loaddll(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSString *dllname_s, *regfuncname_s; char *dllname, *regfuncname = "JSRegisterDLL"; HINSTANCE hdll; JSREGFUNC pregfunc = NULL; JSBool retval; // syntax: loaddll(dllname [,regfuncname]); // dllname = name of DLL // regfuncname = name of registration function // // Calls regfuncname(cx,obj) within library "dllname" if (argc < 1 || argc > 2) return JS_FALSE; dllname_s = JS_ValueToString(cx, argv[0]); if (!dllname_s) return JS_FALSE; dllname = JS_GetStringBytes(dllname_s); if (argc == 2) { regfuncname_s = JS_ValueToString(cx, argv[1]); if (!regfuncname_s) return JS_FALSE; regfuncname = JS_GetStringBytes(regfuncname_s); } printf("Loading library %s...\n",dllname); hdll = LoadLibrary(dllname); if (!hdll) { printf("Couldn't load library\n"); return JS_FALSE; } printf("Found library; locating %s()...\n",regfuncname); pregfunc = (JSREGFUNC)GetProcAddress(hdll, regfuncname); if (!pregfunc) { printf("Couldn't locate registration function\n"); return JS_FALSE; } printf("calling %s()...\n",regfuncname); retval = pregfunc(cx,obj); if (retval) printf("DLL %s registered successfully!\n",dllname); else printf("error registering DLL %s\n",dllname); return retval; } .