8087211: Indirect evals should be strict with -strict option

Fri, 12 Jun 2015 16:55:20 +0530

author
sundar
date
Fri, 12 Jun 2015 16:55:20 +0530
changeset 1406
d314052d7f5e
parent 1405
98b090e45df3
child 1407
46a3d8588ad2

8087211: Indirect evals should be strict with -strict option
Reviewed-by: lagergren, hannesw

src/jdk/nashorn/api/scripting/ScriptObjectMirror.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/objects/Global.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/objects/NativeFunction.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/Context.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/DebuggerSupport.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/tools/Shell.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8087211.js file | annotate | diff | comparison | revisions
test/script/basic/JDK-8087211_2.js file | annotate | diff | comparison | revisions
test/src/jdk/nashorn/api/scripting/JSONCompatibleTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Thu Jun 11 13:33:34 2015 +0530
     1.2 +++ b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Fri Jun 12 16:55:20 2015 +0530
     1.3 @@ -172,7 +172,7 @@
     1.4                                  return Context.getContext();
     1.5                              }
     1.6                          }, GET_CONTEXT_ACC_CTXT);
     1.7 -                return wrapLikeMe(context.eval(global, s, sobj, null, false));
     1.8 +                return wrapLikeMe(context.eval(global, s, sobj, null));
     1.9              }
    1.10          });
    1.11      }
     2.1 --- a/src/jdk/nashorn/internal/objects/Global.java	Thu Jun 11 13:33:34 2015 +0530
     2.2 +++ b/src/jdk/nashorn/internal/objects/Global.java	Fri Jun 12 16:55:20 2015 +0530
     2.3 @@ -1451,7 +1451,7 @@
     2.4       * @return the result of eval
     2.5       */
     2.6      public static Object eval(final Object self, final Object str) {
     2.7 -        return directEval(self, str, UNDEFINED, UNDEFINED, false);
     2.8 +        return directEval(self, str, Global.instanceFrom(self), UNDEFINED, false);
     2.9      }
    2.10  
    2.11      /**
    2.12 @@ -1461,7 +1461,7 @@
    2.13       * @param str      Evaluated code
    2.14       * @param callThis "this" to be passed to the evaluated code
    2.15       * @param location location of the eval call
    2.16 -     * @param strict   is eval called a strict mode code?
    2.17 +     * @param strict   is eval called from a strict mode code?
    2.18       *
    2.19       * @return the return value of the eval
    2.20       *
     3.1 --- a/src/jdk/nashorn/internal/objects/NativeFunction.java	Thu Jun 11 13:33:34 2015 +0530
     3.2 +++ b/src/jdk/nashorn/internal/objects/NativeFunction.java	Fri Jun 12 16:55:20 2015 +0530
     3.3 @@ -279,8 +279,8 @@
     3.4          sb.append("})");
     3.5  
     3.6          final Global global = Global.instance();
     3.7 -
     3.8 -        return (ScriptFunction)Global.directEval(global, sb.toString(), global, "<function>", global.isStrictContext());
     3.9 +        final Context context = global.getContext();
    3.10 +        return (ScriptFunction)context.eval(global, sb.toString(), global, "<function>");
    3.11      }
    3.12  
    3.13      private static void checkFunctionParameters(final String params) {
     4.1 --- a/src/jdk/nashorn/internal/runtime/Context.java	Thu Jun 11 13:33:34 2015 +0530
     4.2 +++ b/src/jdk/nashorn/internal/runtime/Context.java	Fri Jun 12 16:55:20 2015 +0530
     4.3 @@ -668,12 +668,11 @@
     4.4       * @param string       Evaluated code as a String
     4.5       * @param callThis     "this" to be passed to the evaluated code
     4.6       * @param location     location of the eval call
     4.7 -     * @param strict       is this {@code eval} call from a strict mode code?
     4.8       * @return the return value of the {@code eval}
     4.9       */
    4.10      public Object eval(final ScriptObject initialScope, final String string,
    4.11 -            final Object callThis, final Object location, final boolean strict) {
    4.12 -        return eval(initialScope, string, callThis, location, strict, false);
    4.13 +            final Object callThis, final Object location) {
    4.14 +        return eval(initialScope, string, callThis, location, false, false);
    4.15      }
    4.16  
    4.17      /**
    4.18 @@ -692,14 +691,16 @@
    4.19              final Object callThis, final Object location, final boolean strict, final boolean evalCall) {
    4.20          final String  file       = location == UNDEFINED || location == null ? "<eval>" : location.toString();
    4.21          final Source  source     = sourceFor(file, string, evalCall);
    4.22 -        final boolean directEval = location != UNDEFINED; // is this direct 'eval' call or indirectly invoked eval?
    4.23 +        // is this direct 'eval' builtin call?
    4.24 +        final boolean directEval = evalCall && (location != UNDEFINED);
    4.25          final Global  global = Context.getGlobal();
    4.26          ScriptObject scope = initialScope;
    4.27  
    4.28          // ECMA section 10.1.1 point 2 says eval code is strict if it begins
    4.29          // with "use strict" directive or eval direct call itself is made
    4.30          // from from strict mode code. We are passed with caller's strict mode.
    4.31 -        boolean strictFlag = directEval && strict;
    4.32 +        // Nashorn extension: any 'eval' is unconditionally strict when -strict is specified.
    4.33 +        boolean strictFlag = strict || this._strict;
    4.34  
    4.35          Class<?> clazz = null;
    4.36          try {
    4.37 @@ -740,7 +741,8 @@
    4.38          if (directEval) {
    4.39              evalThis = (callThis != UNDEFINED && callThis != null) || strictFlag ? callThis : global;
    4.40          } else {
    4.41 -            evalThis = global;
    4.42 +            // either indirect evalCall or non-eval (Function, engine.eval, ScriptObjectMirror.eval..)
    4.43 +            evalThis = callThis;
    4.44          }
    4.45  
    4.46          return ScriptRuntime.apply(func, evalThis);
     5.1 --- a/src/jdk/nashorn/internal/runtime/DebuggerSupport.java	Thu Jun 11 13:33:34 2015 +0530
     5.2 +++ b/src/jdk/nashorn/internal/runtime/DebuggerSupport.java	Fri Jun 12 16:55:20 2015 +0530
     5.3 @@ -156,7 +156,7 @@
     5.4          final Context context = global.getContext();
     5.5  
     5.6          try {
     5.7 -            return context.eval(initialScope, string, callThis, ScriptRuntime.UNDEFINED, false);
     5.8 +            return context.eval(initialScope, string, callThis, ScriptRuntime.UNDEFINED);
     5.9          } catch (final Throwable ex) {
    5.10              return returnException ? ex : null;
    5.11          }
     6.1 --- a/src/jdk/nashorn/tools/Shell.java	Thu Jun 11 13:33:34 2015 +0530
     6.2 +++ b/src/jdk/nashorn/tools/Shell.java	Fri Jun 12 16:55:20 2015 +0530
     6.3 @@ -439,7 +439,7 @@
     6.4                  }
     6.5  
     6.6                  try {
     6.7 -                    final Object res = context.eval(global, source, global, "<shell>", env._strict);
     6.8 +                    final Object res = context.eval(global, source, global, "<shell>");
     6.9                      if (res != ScriptRuntime.UNDEFINED) {
    6.10                          err.println(JSType.toString(res));
    6.11                      }
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/script/basic/JDK-8087211.js	Fri Jun 12 16:55:20 2015 +0530
     7.3 @@ -0,0 +1,63 @@
     7.4 +/*
     7.5 + * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
     7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     7.7 + * 
     7.8 + * This code is free software; you can redistribute it and/or modify it
     7.9 + * under the terms of the GNU General Public License version 2 only, as
    7.10 + * published by the Free Software Foundation.
    7.11 + * 
    7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    7.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    7.15 + * version 2 for more details (a copy is included in the LICENSE file that
    7.16 + * accompanied this code).
    7.17 + * 
    7.18 + * You should have received a copy of the GNU General Public License version
    7.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    7.21 + * 
    7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    7.23 + * or visit www.oracle.com if you need additional information or have any
    7.24 + * questions.
    7.25 + */
    7.26 +
    7.27 +/**
    7.28 + * JDK-8087211: Indirect evals should be strict with -strict option
    7.29 + *
    7.30 + * @test
    7.31 + * @run
    7.32 + * @option -strict
    7.33 + */
    7.34 +
    7.35 +var global = this;
    7.36 +
    7.37 +try {
    7.38 +    // indirect eval call.
    7.39 +    global.eval("x = 34;");
    7.40 +    throw new Error("should have thrown ReferenceError");
    7.41 +} catch (e if e instanceof ReferenceError) {
    7.42 +}
    7.43 +
    7.44 +
    7.45 +function teststrict() {
    7.46 +    "use strict";
    7.47 +    // strict caller, indirect eval.
    7.48 +    global.eval('public = 1;');
    7.49 +}
    7.50 +
    7.51 +try {
    7.52 +    teststrict();
    7.53 +    throw new Error("should have thrown SyntaxError");
    7.54 +} catch (e if e instanceof SyntaxError) {
    7.55 +}
    7.56 +
    7.57 +function testnonstrict() {
    7.58 +    // non strict caller, indirect eval.
    7.59 +    global.eval('public = 1;');
    7.60 +}
    7.61 +
    7.62 +try {
    7.63 +    testnonstrict();
    7.64 +    throw new Error("should have thrown SyntaxError");
    7.65 +} catch (e if e instanceof SyntaxError) {
    7.66 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/test/script/basic/JDK-8087211_2.js	Fri Jun 12 16:55:20 2015 +0530
     8.3 @@ -0,0 +1,50 @@
     8.4 +/*
     8.5 + * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
     8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     8.7 + * 
     8.8 + * This code is free software; you can redistribute it and/or modify it
     8.9 + * under the terms of the GNU General Public License version 2 only, as
    8.10 + * published by the Free Software Foundation.
    8.11 + * 
    8.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    8.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    8.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    8.15 + * version 2 for more details (a copy is included in the LICENSE file that
    8.16 + * accompanied this code).
    8.17 + * 
    8.18 + * You should have received a copy of the GNU General Public License version
    8.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    8.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    8.21 + * 
    8.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    8.23 + * or visit www.oracle.com if you need additional information or have any
    8.24 + * questions.
    8.25 + */
    8.26 +
    8.27 +/**
    8.28 + * JDK-8087211: Indirect evals should be strict with -strict option
    8.29 + * Make sure without -strict option, indirect evals are not strict.
    8.30 + *
    8.31 + * @test
    8.32 + * @run
    8.33 + */
    8.34 +
    8.35 +var global = this;
    8.36 +
    8.37 +// indirect eval call.
    8.38 +global.eval("x = 34;");
    8.39 +
    8.40 +function teststrict() {
    8.41 +    "use strict";
    8.42 +    // strict caller, indirect eval.
    8.43 +    global.eval('public = 1;');
    8.44 +}
    8.45 +
    8.46 +teststrict();
    8.47 +
    8.48 +function testnonstrict() {
    8.49 +    // non strict caller, indirect eval.
    8.50 +    global.eval('public = 1;');
    8.51 +}
    8.52 +
    8.53 +testnonstrict();
     9.1 --- a/test/src/jdk/nashorn/api/scripting/JSONCompatibleTest.java	Thu Jun 11 13:33:34 2015 +0530
     9.2 +++ b/test/src/jdk/nashorn/api/scripting/JSONCompatibleTest.java	Fri Jun 12 16:55:20 2015 +0530
     9.3 @@ -97,12 +97,14 @@
     9.4          assertEquals(x2.get("1"), 5);
     9.5      }
     9.6  
     9.7 +    @SuppressWarnings("unchecked")
     9.8      private static List<Object> asList(final Object obj) {
     9.9          assertJSObject(obj);
    9.10          Assert.assertTrue(obj instanceof List);
    9.11          return (List)obj;
    9.12      }
    9.13  
    9.14 +    @SuppressWarnings("unchecked")
    9.15      private static Map<String, Object> asMap(final Object obj) {
    9.16          assertJSObject(obj);
    9.17          Assert.assertTrue(obj instanceof Map);

mercurial