diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -4796,16 +4796,26 @@ JS_EvaluateScriptForPrincipals(JSContext return JS_FALSE; JSBool ok = JS_EvaluateUCScriptForPrincipals(cx, obj, principals, chars, length, filename, lineno, rval); cx->free(chars); return ok; } JS_PUBLIC_API(JSBool) +JS_EvaluateScriptForPrincipalsVersion(JSContext *cx, JSObject *obj, JSPrincipals *principals, + const char *bytes, uintN nbytes, + const char *filename, uintN lineno, jsval *rval, JSVersion version) +{ + AutoVersionAPI avi(cx, version); + return JS_EvaluateScriptForPrincipals(cx, obj, principals, bytes, nbytes, filename, lineno, + rval); +} + +JS_PUBLIC_API(JSBool) JS_EvaluateScript(JSContext *cx, JSObject *obj, const char *bytes, uintN nbytes, const char *filename, uintN lineno, jsval *rval) { return JS_EvaluateScriptForPrincipals(cx, obj, NULL, bytes, nbytes, filename, lineno, rval); } JS_PUBLIC_API(JSBool) JS_CallFunction(JSContext *cx, JSObject *obj, JSFunction *fun, uintN argc, jsval *argv, diff --git a/js/src/jsapi.h b/js/src/jsapi.h --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -2535,16 +2535,23 @@ JS_EvaluateScript(JSContext *cx, JSObjec extern JS_PUBLIC_API(JSBool) JS_EvaluateScriptForPrincipals(JSContext *cx, JSObject *obj, JSPrincipals *principals, const char *bytes, uintN length, const char *filename, uintN lineno, jsval *rval); extern JS_PUBLIC_API(JSBool) +JS_EvaluateScriptForPrincipalsVersion(JSContext *cx, JSObject *obj, + JSPrincipals *principals, + const char *bytes, uintN length, + const char *filename, uintN lineno, + jsval *rval, JSVersion version); + +extern JS_PUBLIC_API(JSBool) JS_EvaluateUCScript(JSContext *cx, JSObject *obj, const jschar *chars, uintN length, const char *filename, uintN lineno, jsval *rval); extern JS_PUBLIC_API(JSBool) JS_EvaluateUCScriptForPrincipalsVersion(JSContext *cx, JSObject *obj, JSPrincipals *principals, diff --git a/js/src/xpconnect/loader/mozJSSubScriptLoader.cpp b/js/src/xpconnect/loader/mozJSSubScriptLoader.cpp --- a/js/src/xpconnect/loader/mozJSSubScriptLoader.cpp +++ b/js/src/xpconnect/loader/mozJSSubScriptLoader.cpp @@ -55,16 +55,18 @@ #include "nsAutoPtr.h" #include "nsNetUtil.h" #include "nsIProtocolHandler.h" #include "nsIFileURL.h" #include "jsapi.h" #include "jsdbgapi.h" #include "jsobj.h" +#include "jsscript.h" +#include "jscntxt.h" #include "mozilla/FunctionTimer.h" /* load() error msgs, XXX localize? */ #define LOAD_ERROR_NOSERVICE "Error creating IO Service." #define LOAD_ERROR_NOURI "Error creating URI (invalid URL scheme?)" #define LOAD_ERROR_NOSCHEME "Failed to get URI scheme. This is bad." #define LOAD_ERROR_URI_NOT_LOCAL "Trying to load a non-local URI." @@ -343,18 +345,20 @@ mozJSSubScriptLoader::LoadSubScript (con errmsg = JS_NewStringCopyZ (cx, LOAD_ERROR_NOPRINCIPALS); goto return_exception; } /* set our own error reporter so we can report any bad things as catchable * exceptions, including the source/line number */ er = JS_SetErrorReporter (cx, mozJSLoaderErrorReporter); - ok = JS_EvaluateScriptForPrincipals (cx, target_obj, jsPrincipals, - buf, len, uriStr.get(), 1, rval); + ok = JS_EvaluateScriptForPrincipalsVersion(cx, target_obj, jsPrincipals, + buf, len, uriStr.get(), 1, rval, + JSVersion(script->getVersion() & + ~js::VersionFlags::ANONFUNFIX)); /* repent for our evil deeds */ JS_SetErrorReporter (cx, er); cc->SetReturnValueWasSet (ok); JSPRINCIPALS_DROP(cx, jsPrincipals); return NS_OK; diff --git a/js/src/xpconnect/tests/unit/bug596580_versioned.js b/js/src/xpconnect/tests/unit/bug596580_versioned.js new file mode 100644 --- /dev/null +++ b/js/src/xpconnect/tests/unit/bug596580_versioned.js @@ -0,0 +1,4 @@ +/* Some constructs that require a high default version number. */ +let x = 12; +function simpleGen() { yield 12; } +var e4xy = Huzzah! diff --git a/js/src/xpconnect/tests/unit/test_bug596580.js b/js/src/xpconnect/tests/unit/test_bug596580.js new file mode 100644 --- /dev/null +++ b/js/src/xpconnect/tests/unit/test_bug596580.js @@ -0,0 +1,19 @@ +const Cc = Components.classes; +const Ci = Components.interfaces; + +function run_test() { + var file = do_get_file("bug596580_versioned.js"); + var ios = Cc["@mozilla.org/network/io-service;1"] + .getService(Ci.nsIIOService); + var uri = ios.newFileURI(file); + var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"] + .getService(Ci.mozIJSSubScriptLoader); + scriptLoader.loadSubScript(uri.spec); + version(150) + try { + scriptLoader.loadSubScript(uri.spec); + throw new Exception("Subscript should fail to load."); + } catch (e if e instanceof SyntaxError) { + // Okay. + } +}