Update browser runner to honor `onlyStrict` flag

Unlike the console runner, the browser runner does not modify the
strictness of tests prior to running them. Regardless of a given test's
metadata, it runs every test exactly once, and it never enables strict
mode. This means that tests intended to function in strict mode must
declare the "use strict"; directive prologue in addition to the
`onlyStrict` flag.

For any test that specifies the `onlyStrict` metadata flag, transform
the source code by injecting a "use strict" directive prologue prior to
running the test.
This commit is contained in:
Mike Pennisi 2015-06-08 17:05:22 -04:00
parent 12cc01484e
commit cb617493d7
2 changed files with 26 additions and 6 deletions

View File

@ -113,7 +113,7 @@ This tag is used to identify the author of a test case.
This tag is for boolean properties associated with the test.
- **`onlyStrict`** - only run the test in strict mode (*not supported by the browser runner*)
- **`onlyStrict`** - only run the test in strict mode
- **`noStrict`** - only run the test in "sloppy" mode
- **`module`** - interpret the source text as [module
code](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-modules)

View File

@ -235,11 +235,7 @@ function BrowserRunner() {
//Run the code
idoc.writeln("<script type='text/javascript'>");
if (! instance.supportsWindowOnerror) {
idoc.writeln("try {eval(\"" + this.convertForEval(code) + "\");} catch(e) {window.onerror(e.toString(), null, null);}");
} else {
idoc.writeln(code);
}
idoc.writeln(this.compileSource(test, code));
idoc.writeln("</script>");
idoc.writeln("<script type='text/javascript'>");
@ -269,6 +265,30 @@ function BrowserRunner() {
};
}
/**
* Transform the test source code according to the test metadata and the
* capabilities of the current environment.
*
* @param {object} test - a test object as retrieved by TestLoader
* @param {string} code - unmodified test source code
*
* @returns {string} the transformed source code
*/
BrowserRunner.prototype.compileSource = function(test, code) {
var flags = test.flags;
if (flags && flags.indexOf("onlyStrict") > -1) {
code = "'use strict';\n" + code;
}
if (!this.supportsWindowOnerror) {
code = "try {eval(\"" + this.convertForEval(code) +
"\");} catch(e) {window.onerror(e.toString(), null, null);}";
}
return code;
};
/* Loads tests from the sections specified in testcases.json.
* Public Methods:
* * getNextTest() - Start loading the next test.