int ExecScriptFile(JSContext * context, JSObject * global, const char * file, jsval * jsResult) { register FILE * filep; filep = fopen(file, "rb"); if (!filep) { JS_ReportError(context, "%s: %s", file, "No such file or directory"); return 0; } JSObject * jsTemp = JS_CompileFileHandle(context, global, file, filep); if (jsTemp) { return JS_ExecuteScript(context, global, jsTemp, jsResult); } else { JS_ReportPendingException(context); return 0; } }
bool parseECMAScript(const char *name) {// begin parseECMAScript jsval rval; uintN lineno = 0; g_bJSSuccess = 0; printf("Spidermonkey compiling \"%s\"...",name); FILE *file = fopen(name, "r"); JSScript *pJSScript = JS_CompileFileHandle(g_pCx, g_pObject, name, file);; #warning potential leak here /* In normal operation the file is closed by JS_CompileFileHandle (jsapi.c:3772) JS_CompileFileHandleForPrincipals (jsapi.c:3795) CompileTokenStream (jsapi.c:3609) js_CloseTokenStream (jsscan.c:322) */ /* DOUBLE CLOSE fclose(file); */ printf("Done.\n"); if(pJSScript != NULL) {// begin execute external file char curDir[PATH_MAX + 1]; char scriptPath[strlen(name) + 1]; strcpy(scriptPath, name); ADM_PathStripName(scriptPath); getcwd(curDir, PATH_MAX); chdir(scriptPath); printf("Spidermonkey executing \"%s\"...",name); JSBool ok = JS_ExecuteScript(g_pCx, g_pObject, pJSScript, &rval); JS_DestroyScript(g_pCx,pJSScript); printf("Done.\n"); chdir(curDir); }// end execute external file // Run garbage collector now, it is safe JS_GC(g_pCx); A_Resync(); return g_bJSSuccess; }// end parseECMAScript
/* void CompileFile (in nsILocalFile aFile, in PRBool strict); */ NS_IMETHODIMP nsXPCToolsCompiler::CompileFile(nsILocalFile *aFile, PRBool strict) { // use the xpccallcontext stuff to get the current JSContext // get the xpconnect service nsresult rv; nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv)); if(NS_FAILED(rv)) return NS_ERROR_FAILURE; // get the xpconnect native call context nsCOMPtr<nsIXPCNativeCallContext> callContext; xpc->GetCurrentNativeCallContext(getter_AddRefs(callContext)); if(!callContext) return NS_ERROR_FAILURE; // verify that we are being called from JS (i.e. the current call is // to this object - though we don't verify that it is to this exact method) nsCOMPtr<nsISupports> callee; callContext->GetCallee(getter_AddRefs(callee)); if(!callee || callee.get() != (nsISupports*)this) return NS_ERROR_FAILURE; // Get JSContext of current call JSContext* cx; rv = callContext->GetJSContext(&cx); if(NS_FAILED(rv) || !cx) return NS_ERROR_FAILURE; FILE* handle; if(NS_FAILED(aFile->OpenANSIFileDesc("r", &handle))) return NS_ERROR_FAILURE; JSObject* glob = JS_NewObject(cx, &global_class, NULL, NULL); if (!glob) return NS_ERROR_FAILURE; if (!JS_InitStandardClasses(cx, glob)) return NS_ERROR_FAILURE; nsCAutoString path; if(NS_FAILED(aFile->GetNativePath(path))) return NS_ERROR_FAILURE; uint32 oldoptions = JS_GetOptions(cx); JS_SetOptions(cx, JSOPTION_WERROR | (strict ? JSOPTION_STRICT : 0)); JSErrorReporter older = JS_SetErrorReporter(cx, ErrorReporter); JSExceptionState *es =JS_SaveExceptionState(cx); if(!JS_CompileFileHandle(cx, glob, path.get(), handle)) { jsval v; JSErrorReport* report; if(JS_GetPendingException(cx, &v) && nsnull != (report = JS_ErrorFromException(cx, v))) { JSString* str; const char* msg = "Error"; str = JS_ValueToString(cx, v); if(str) msg = JS_GetStringBytes(str); printf("%s [%s,%d]\n\n", msg, report->filename, (int)report->lineno); } else { printf("no script and no error report!\n"); } } JS_RestoreExceptionState(cx, es); JS_SetErrorReporter(cx, older); JS_SetOptions(cx, oldoptions); return NS_OK; }
int ExecShellScript(const char * file, int argc, char** argv, JSContext * context, JSObject * env, jsval * vp) { register FILE * filep; register int c; filep = fopen(file, "rb"); if (!filep) { JS_ReportError(context, "%s: %s", file, "No such file or directory"); return 0; } if (getc(filep) == '#' && getc(filep) == '!') { c = 1; while (c != EOF && c != '\n') c = getc(filep); ungetc(c, filep); } else { fclose(filep); JS_ReportError(context, "%s: %s", file, "is not a shell script"); return 0; } JSObject* argsObj = JS_NewArrayObject(context, 0, NULL); JS_DefineProperty(context, env, "parameter", OBJECT_TO_JSVAL(argsObj), NULL, NULL, 0); JSString* str = JS_NewStringCopyZ(context, file); JS_DefineElement(context, argsObj, 0, STRING_TO_JSVAL(str), NULL, NULL, JSPROP_ENUMERATE); int i; for (i = 0; i < argc; i++) { str = JS_NewStringCopyZ(context, argv[i]); JS_DefineElement(context, argsObj, i + 1, STRING_TO_JSVAL(str), NULL, NULL, JSPROP_ENUMERATE); } setenv(SMASH_RESOURCE_PATH_ENV_ID, GET_STRING_DEF(SMASH_RESOURCE_PATH), 0); JS_InitCTypesClass(context, env); JS_DefineFunctions(context, env, shell_functions); JS_DefineProperties(context, env, shell_properties); jsval fun; JS_GetProperty(context, env, "system", &fun); JS_DefineFunction(context, JSVAL_TO_OBJECT(fun), "read", ShellSystemRead, 1, JSPROP_ENUMERATE); JS_DefineFunction(context, JSVAL_TO_OBJECT(fun), "write", ShellSystemWrite, 2, JSPROP_ENUMERATE); jsval fun2; JS_GetProperty(context, env, "echo", &fun2); JS_DefineFunction(context, JSVAL_TO_OBJECT(fun2), "error", ShellEchoError, 1, JSPROP_ENUMERATE); jsval fun3; JS_GetProperty(context, env, "setFileContent", &fun3); JS_DefineFunction(context, JSVAL_TO_OBJECT(fun3), "append", ShellSetFileContentAppend, 2, JSPROP_ENUMERATE); #ifdef SMASH_MAIN_LOADER //ExecScriptFile(context, env, GET_STRING_DEF(SMASH_MAIN_LOADER), NULL); #endif #ifdef SMASH_SHELL_LOADER ExecScriptFile(context, env, GET_STRING_DEF(SMASH_SHELL_LOADER), NULL); #endif JSObject * jsTemp = JS_CompileFileHandle(context, env, file, filep); fclose(filep); if (jsTemp) { return JS_ExecuteScript(context, env, jsTemp, vp); } else { JS_ReportPendingException(context); return 1; } }