8087211: Indirect evals should be strict with -strict option
authorsundar
Fri, 12 Jun 2015 16:55:20 +0530
changeset 31192 1e019aeea9b5
parent 31119 d69c968463f0
child 31193 f019295fdeb3
8087211: Indirect evals should be strict with -strict option Reviewed-by: lagergren, hannesw
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/scripting/ScriptObjectMirror.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFunction.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/DebuggerSupport.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/tools/Shell.java
nashorn/test/script/basic/JDK-8087211.js
nashorn/test/script/basic/JDK-8087211_2.js
nashorn/test/src/jdk/nashorn/api/scripting/JSONCompatibleTest.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Wed Jul 05 20:38:06 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Fri Jun 12 16:55:20 2015 +0530
@@ -172,7 +172,7 @@
                                 return Context.getContext();
                             }
                         }, GET_CONTEXT_ACC_CTXT);
-                return wrapLikeMe(context.eval(global, s, sobj, null, false));
+                return wrapLikeMe(context.eval(global, s, sobj, null));
             }
         });
     }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java	Wed Jul 05 20:38:06 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java	Fri Jun 12 16:55:20 2015 +0530
@@ -1451,7 +1451,7 @@
      * @return the result of eval
      */
     public static Object eval(final Object self, final Object str) {
-        return directEval(self, str, UNDEFINED, UNDEFINED, false);
+        return directEval(self, str, Global.instanceFrom(self), UNDEFINED, false);
     }
 
     /**
@@ -1461,7 +1461,7 @@
      * @param str      Evaluated code
      * @param callThis "this" to be passed to the evaluated code
      * @param location location of the eval call
-     * @param strict   is eval called a strict mode code?
+     * @param strict   is eval called from a strict mode code?
      *
      * @return the return value of the eval
      *
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFunction.java	Wed Jul 05 20:38:06 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/NativeFunction.java	Fri Jun 12 16:55:20 2015 +0530
@@ -279,8 +279,8 @@
         sb.append("})");
 
         final Global global = Global.instance();
-
-        return (ScriptFunction)Global.directEval(global, sb.toString(), global, "<function>", global.isStrictContext());
+        final Context context = global.getContext();
+        return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
     }
 
     private static void checkFunctionParameters(final String params) {
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java	Wed Jul 05 20:38:06 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Context.java	Fri Jun 12 16:55:20 2015 +0530
@@ -668,12 +668,11 @@
      * @param string       Evaluated code as a String
      * @param callThis     "this" to be passed to the evaluated code
      * @param location     location of the eval call
-     * @param strict       is this {@code eval} call from a strict mode code?
      * @return the return value of the {@code eval}
      */
     public Object eval(final ScriptObject initialScope, final String string,
-            final Object callThis, final Object location, final boolean strict) {
-        return eval(initialScope, string, callThis, location, strict, false);
+            final Object callThis, final Object location) {
+        return eval(initialScope, string, callThis, location, false, false);
     }
 
     /**
@@ -692,14 +691,16 @@
             final Object callThis, final Object location, final boolean strict, final boolean evalCall) {
         final String  file       = location == UNDEFINED || location == null ? "<eval>" : location.toString();
         final Source  source     = sourceFor(file, string, evalCall);
-        final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
+        // is this direct 'eval' builtin call?
+        final boolean directEval = evalCall && (location != UNDEFINED);
         final Global  global = Context.getGlobal();
         ScriptObject scope = initialScope;
 
         // ECMA section 10.1.1 point 2 says eval code is strict if it begins
         // with "use strict" directive or eval direct call itself is made
         // from from strict mode code. We are passed with caller's strict mode.
-        boolean strictFlag = directEval && strict;
+        // Nashorn extension: any 'eval' is unconditionally strict when -strict is specified.
+        boolean strictFlag = strict || this._strict;
 
         Class<?> clazz = null;
         try {
@@ -740,7 +741,8 @@
         if (directEval) {
             evalThis = (callThis != UNDEFINED && callThis != null) || strictFlag ? callThis : global;
         } else {
-            evalThis = global;
+            // either indirect evalCall or non-eval (Function, engine.eval, ScriptObjectMirror.eval..)
+            evalThis = callThis;
         }
 
         return ScriptRuntime.apply(func, evalThis);
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/DebuggerSupport.java	Wed Jul 05 20:38:06 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/DebuggerSupport.java	Fri Jun 12 16:55:20 2015 +0530
@@ -156,7 +156,7 @@
         final Context context = global.getContext();
 
         try {
-            return context.eval(initialScope, string, callThis, ScriptRuntime.UNDEFINED, false);
+            return context.eval(initialScope, string, callThis, ScriptRuntime.UNDEFINED);
         } catch (final Throwable ex) {
             return returnException ? ex : null;
         }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/tools/Shell.java	Wed Jul 05 20:38:06 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/tools/Shell.java	Fri Jun 12 16:55:20 2015 +0530
@@ -439,7 +439,7 @@
                 }
 
                 try {
-                    final Object res = context.eval(global, source, global, "<shell>", env._strict);
+                    final Object res = context.eval(global, source, global, "<shell>");
                     if (res != ScriptRuntime.UNDEFINED) {
                         err.println(JSType.toString(res));
                     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8087211.js	Fri Jun 12 16:55:20 2015 +0530
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8087211: Indirect evals should be strict with -strict option
+ *
+ * @test
+ * @run
+ * @option -strict
+ */
+
+var global = this;
+
+try {
+    // indirect eval call.
+    global.eval("x = 34;");
+    throw new Error("should have thrown ReferenceError");
+} catch (e if e instanceof ReferenceError) {
+}
+
+
+function teststrict() {
+    "use strict";
+    // strict caller, indirect eval.
+    global.eval('public = 1;');
+}
+
+try {
+    teststrict();
+    throw new Error("should have thrown SyntaxError");
+} catch (e if e instanceof SyntaxError) {
+}
+
+function testnonstrict() {
+    // non strict caller, indirect eval.
+    global.eval('public = 1;');
+}
+
+try {
+    testnonstrict();
+    throw new Error("should have thrown SyntaxError");
+} catch (e if e instanceof SyntaxError) {
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8087211_2.js	Fri Jun 12 16:55:20 2015 +0530
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8087211: Indirect evals should be strict with -strict option
+ * Make sure without -strict option, indirect evals are not strict.
+ *
+ * @test
+ * @run
+ */
+
+var global = this;
+
+// indirect eval call.
+global.eval("x = 34;");
+
+function teststrict() {
+    "use strict";
+    // strict caller, indirect eval.
+    global.eval('public = 1;');
+}
+
+teststrict();
+
+function testnonstrict() {
+    // non strict caller, indirect eval.
+    global.eval('public = 1;');
+}
+
+testnonstrict();
--- a/nashorn/test/src/jdk/nashorn/api/scripting/JSONCompatibleTest.java	Wed Jul 05 20:38:06 2017 +0200
+++ b/nashorn/test/src/jdk/nashorn/api/scripting/JSONCompatibleTest.java	Fri Jun 12 16:55:20 2015 +0530
@@ -97,12 +97,14 @@
         assertEquals(x2.get("1"), 5);
     }
 
+    @SuppressWarnings("unchecked")
     private static List<Object> asList(final Object obj) {
         assertJSObject(obj);
         Assert.assertTrue(obj instanceof List);
         return (List)obj;
     }
 
+    @SuppressWarnings("unchecked")
     private static Map<String, Object> asMap(final Object obj) {
         assertJSObject(obj);
         Assert.assertTrue(obj instanceof Map);