diff --git a/mozilla/js/rhino/docs/shell.html b/mozilla/js/rhino/docs/shell.html index 47af938ac13..c0cb968d4d3 100644 --- a/mozilla/js/rhino/docs/shell.html +++ b/mozilla/js/rhino/docs/shell.html @@ -40,6 +40,11 @@ or 140. See JavaScript Language Versions for more information on language versions. +-strict +
+Enable strict mode. ++ -continuations
Enable experiments support for continuations and set the optimization level to -1 to force interpretation mode. diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/Context.java b/mozilla/js/rhino/src/org/mozilla/javascript/Context.java index acc9bb04bea..87cd979173b 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/Context.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/Context.java @@ -233,9 +233,9 @@ public class Context public static final int FEATURE_DYNAMIC_SCOPE = 7; /** - * Control if highly experimental support for the continuation in the + * Control if experimental support for the continuations in the * interpreter is enabled. - * If Rhino embedding enables this highly experimental features, then + * If Rhino embedding enables this experimental features, then * Continuation object will be available to scripts. * When used with pure interpretation mode (optimizatio level is -1) * Continuationit allows to capture and restore continuations. @@ -248,6 +248,21 @@ public class Context */ public static final int FEATURE_INTERPRETER_CONTINUATIONS = 8; + /** + * Control if strict mode is enabled. + * With strict mode enabled Rhino reports runtime errors in the following + * cases: + *+ *
+ *- Assignment to non-existing names which typically indicates missed + * var declaration. + *
- Passing non-string arguments to the eval function. + *
+ * By default {@link #hasFeature(int)} returns false. + * @since 1.6 Release 1 + */ + public static final int FEATURE_STRICT_MODE = 9; + public static final String languageVersionProperty = "language version"; public static final String errorReporterProperty = "error reporter"; @@ -2106,6 +2121,7 @@ public class Context * @see #FEATURE_E4X * @see #FEATURE_DYNAMIC_SCOPE * @see #FEATURE_INTERPRETER_CONTINUATIONS + * @see #FEATURE_STRICT_MODE */ public boolean hasFeature(int featureIndex) { diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/ContextFactory.java b/mozilla/js/rhino/src/org/mozilla/javascript/ContextFactory.java index 8906cf43b2c..c1c4b0ddc3f 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/ContextFactory.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/ContextFactory.java @@ -271,6 +271,9 @@ public class ContextFactory case Context.FEATURE_INTERPRETER_CONTINUATIONS: return false; + + case Context.FEATURE_STRICT_MODE: + return false; } // It is a bug to call the method with unknown featureIndex throw new IllegalArgumentException(String.valueOf(featureIndex)); diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java b/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java index cddab819168..b93efe24fec 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java +++ b/mozilla/js/rhino/src/org/mozilla/javascript/ScriptRuntime.java @@ -1777,20 +1777,16 @@ public class ScriptRuntime { } else { // "newname = 7;", where 'newname' has not yet // been defined, creates a new property in the - // global object. Find the global object by - // walking up the scope chain. + // top scope unless strict mode is specified. + if (cx.hasFeature(Context.FEATURE_STRICT_MODE)) { + throw Context.reportRuntimeError1("msg.assn.create.strict", id); + } + // Find the top scope by walking up the scope chain. bound = ScriptableObject.getTopLevelScope(scope); if (cx.useDynamicScope) { bound = locateDynamicScope(cx, bound); } bound.put(id, bound, value); - /* - This code is causing immense performance problems in - scripts that assign to the variables as a way of creating them. - XXX need strict mode - String message = getMessage1("msg.assn.create", id); - Context.reportWarning(message); - */ } return value; } @@ -2213,6 +2209,9 @@ public class ScriptRuntime { return Undefined.instance; Object x = args[0]; if (!(x instanceof String)) { + if (cx.hasFeature(Context.FEATURE_STRICT_MODE)) { + throw Context.reportRuntimeError0("msg.eval.nonstring.strict"); + } String message = Context.getMessage0("msg.eval.nonstring"); Context.reportWarning(message); return x; diff --git a/mozilla/js/rhino/src/org/mozilla/javascript/resources/Messages.properties b/mozilla/js/rhino/src/org/mozilla/javascript/resources/Messages.properties index ff1dd8cc660..0f69072796b 100644 --- a/mozilla/js/rhino/src/org/mozilla/javascript/resources/Messages.properties +++ b/mozilla/js/rhino/src/org/mozilla/javascript/resources/Messages.properties @@ -130,6 +130,10 @@ msg.eval.nonstring =\ Calling eval() with anything other than a primitive string value will \ simply return the value. Is this what you intended? +msg.eval.nonstring.strict =\ + Calling eval() with anything other than a primitive string value is not \ + allowed in the strict mode. + # NativeCall msg.only.from.new =\ "{0}" may only be invoked from a "new" expression. @@ -401,9 +405,9 @@ mag.too.deep.parser.recursion =\ Too deep recursion while parsing # ScriptRuntime -msg.assn.create =\ - Assignment to undefined "{0}" will create a new variable. \ - Add a variable statement at the top level scope to remove this warning. +msg.assn.create.strict =\ + Attempt to assign non-existing name "{0}" in the strict mode. \ + It could indicate a missing variable statement. msg.prop.not.found =\ Property {0} not found. @@ -452,7 +456,7 @@ msg.no.ref.to.get =\ msg.no.ref.to.set =\ {0} is not a reference to set reference value tpo {1}. - + msg.no.ref.from.function =\ Function {0} can not be used as the left-hand side of assignment \ or as an operand of ++ or -- operator. diff --git a/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/Main.java b/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/Main.java index a885f32d76b..a55592f4eb9 100644 --- a/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/Main.java +++ b/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/Main.java @@ -192,6 +192,10 @@ public class Main } continue; } + if (arg.equals("-strict")) { + shellContextFactory.setStrictMode(true); + continue; + } if (arg.equals("-continuations")) { shellContextFactory.setEnableContinuations(true); cx.setOptimizationLevel(-1); diff --git a/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/ShellContextFactory.java b/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/ShellContextFactory.java index ef3cfb42c51..d5d2d25b54e 100644 --- a/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/ShellContextFactory.java +++ b/mozilla/js/rhino/toolsrc/org/mozilla/javascript/tools/shell/ShellContextFactory.java @@ -40,10 +40,14 @@ import org.mozilla.javascript.*; public class ShellContextFactory extends ContextFactory { private boolean enableContinuations; + private boolean strictMode; protected boolean hasFeature(Context cx, int featureIndex) { - if (featureIndex == Context.FEATURE_INTERPRETER_CONTINUATIONS) { + switch (featureIndex) { + case Context.FEATURE_STRICT_MODE: + return strictMode; + case Context.FEATURE_INTERPRETER_CONTINUATIONS: return enableContinuations; } return super.hasFeature(cx, featureIndex); @@ -55,4 +59,10 @@ public class ShellContextFactory extends ContextFactory this.enableContinuations = flag; } + public void setStrictMode(boolean flag) + { + checkNotSealed(); + this.strictMode = flag; + } + }