Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Created April 19, 2012 18:09
Show Gist options
  • Save jorendorff/2422704 to your computer and use it in GitHub Desktop.
Save jorendorff/2422704 to your computer and use it in GitHub Desktop.
EvaluateNonCompileAndGo, finally
diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp
--- a/js/src/shell/js.cpp
+++ b/js/src/shell/js.cpp
@@ -843,17 +843,32 @@ Evaluate(JSContext *cx, unsigned argc, j
return false;
if ((JS_GetClass(thisobj)->flags & JSCLASS_IS_GLOBAL) != JSCLASS_IS_GLOBAL) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_UNEXPECTED_TYPE,
"this-value passed to evaluate()", "not a global object");
return false;
}
- return JS_EvaluateUCScript(cx, thisobj, codeChars, codeLength, "@evaluate", 0, vp);
+ // Separate compile and execute steps for the benefit of EvaluteNonCompileAndGo, below.
+ // JS_EvaulateUCScript is shorter, but it always enables the compile-and-go option.
+ JSScript *script = JS_CompileUCScript(cx, thisobj, codeChars, codeLength, "@evaluate", 1);
+ if (!script)
+ return false;
+ return JS_ExecuteScript(cx, thisobj, script, vp);
}
+static JSBool
+EvaluateNonCompileAndGo(JSContext *cx, unsigned argc, jsval *vp)
+{
+ uint32_t saved = JS_GetOptions(cx);
+ JS_SetOptions(cx, saved & ~JSOPTION_COMPILE_N_GO);
+ bool ok = Evaluate(cx, argc, vp);
+ JS_SetOptions(cx, saved);
+ return ok;
+}
+
static JSString *
FileAsString(JSContext *cx, const char *pathname)
{
FILE *file;
JSString *str = NULL;
size_t len, cc;
@@ -3526,16 +3541,20 @@ static JSFunctionSpecWithHelp shell_func
JS_FN_HELP("load", Load, 1, 0,
"load(['foo.js' ...])",
" Load files named by string arguments."),
JS_FN_HELP("evaluate", Evaluate, 1, 0,
"evaluate(code)",
" Evaluate code as though it were the contents of a file."),
+ JS_FN_HELP("evaluateNonCompileAndGo", EvaluateNonCompileAndGo, 1, 0,
+"evaluateNonCompileAndGo(code)",
+" Evaluate code like evaluate() but with compile-and-go turned off."),
+
JS_FN_HELP("evalWithLocation", EvaluateWithLocation, 3, 0,
"evalWithLocation(code, filename, lineno)",
" Eval code as if loaded from the given filename and line number."),
JS_FN_HELP("run", Run, 1, 0,
"run('foo.js')",
" Run the file named by the first argument, returning the number of\n"
" of milliseconds spent compiling and executing it."),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment