Bug 736792 - Add JSOPTION_STRICT_MODE for embedders to force ES5 strict mode for all code without requiring "use strict" annotations. r=njn

This commit is contained in:
Andrew Paprocki 2012-05-05 15:26:16 -04:00
parent 681e85d16f
commit 77fe9cf5f6
9 changed files with 40 additions and 2 deletions

View File

@ -426,6 +426,8 @@ struct TreeContext { /* tree context for semantic checks */
};
bool init(JSContext *cx, InitBehavior ib = USED_AS_TREE_CONTEXT) {
if (cx->hasRunOption(JSOPTION_STRICT_MODE))
flags |= TCF_STRICT_MODE_CODE;
if (ib == USED_AS_CODE_GENERATOR)
return true;
return decls.init() && lexdeps.ensureMap(cx);

View File

@ -144,6 +144,8 @@ Parser::init(const jschar *base, size_t length, const char *filename, unsigned l
cx->tempLifoAlloc().release(tempPoolMark);
return false;
}
if (context->hasRunOption(JSOPTION_STRICT_MODE))
tokenStream.setStrictMode();
return true;
}

View File

@ -2768,11 +2768,15 @@ JS_StringToVersion(const char *string);
#define JSOPTION_PCCOUNT JS_BIT(17) /* Collect per-op execution counts */
#define JSOPTION_TYPE_INFERENCE JS_BIT(18) /* Perform type inference. */
#define JSOPTION_STRICT_MODE JS_BIT(19) /* Provides a way to force
strict mode for all code
without requiring
"use strict" annotations. */
/* Options which reflect compile-time properties of scripts. */
#define JSCOMPILEOPTION_MASK (JSOPTION_XML)
#define JSRUNOPTION_MASK (JS_BITMASK(19) & ~JSCOMPILEOPTION_MASK)
#define JSRUNOPTION_MASK (JS_BITMASK(20) & ~JSCOMPILEOPTION_MASK)
#define JSALLOPTION_MASK (JSCOMPILEOPTION_MASK | JSRUNOPTION_MASK)
extern JS_PUBLIC_API(uint32_t)

View File

@ -618,6 +618,7 @@ static const struct JSOption {
{"typeinfer", JSOPTION_TYPE_INFERENCE},
{"werror", JSOPTION_WERROR},
{"xml", JSOPTION_XML},
{"strict_mode", JSOPTION_STRICT_MODE},
};
static uint32_t

View File

@ -224,7 +224,8 @@ function optionsInit() {
xml: true,
relimit: true,
methodjit: true,
methodjit_always: true
methodjit_always: true,
strict_mode: true
};
// record initial values to support resetting

View File

@ -0,0 +1,23 @@
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
*/
if (typeof options == "function") {
var opts = options();
if (!/\bstrict_mode\b/.test(opts))
options("strict_mode");
}
var ok = false;
try {
eval('foo = true;');
} catch (e) {
if (/^ReferenceError:/.test(e.toString()))
ok = true;
}
if (ok)
reportCompare(0, 0, "ok");
else
reportCompare(true, false, "this should have thrown a ReferenceError");

View File

@ -360,6 +360,9 @@ interface nsIXPCComponents_Utils : nsISupports
[implicit_jscontext]
attribute boolean methodjit_always;
[implicit_jscontext]
attribute boolean strict_mode;
[implicit_jscontext]
void setGCZeal(in long zeal);
};

View File

@ -717,6 +717,7 @@ static const struct JSOption {
{"strict", JSOPTION_STRICT},
{"werror", JSOPTION_WERROR},
{"xml", JSOPTION_XML},
{"strict_mode", JSOPTION_STRICT_MODE},
};
static uint32_t

View File

@ -4092,6 +4092,7 @@ GENERATE_JSOPTION_GETTER_SETTER(Xml, JSOPTION_XML)
GENERATE_JSOPTION_GETTER_SETTER(Relimit, JSOPTION_RELIMIT)
GENERATE_JSOPTION_GETTER_SETTER(Methodjit, JSOPTION_METHODJIT)
GENERATE_JSOPTION_GETTER_SETTER(Methodjit_always, JSOPTION_METHODJIT_ALWAYS)
GENERATE_JSOPTION_GETTER_SETTER(Strict_mode, JSOPTION_STRICT_MODE)
#undef GENERATE_JSOPTION_GETTER_SETTER