8087211: Indirect evals should be strict with -strict option

Reviewed-by: lagergren, hannesw
This commit is contained in:
Athijegannathan Sundararajan 2015-06-12 16:55:20 +05:30
parent 733c4e989f
commit 907f7f2c7c
9 changed files with 130 additions and 13 deletions

View File

@ -172,7 +172,7 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
return Context.getContext();
}
}, GET_CONTEXT_ACC_CTXT);
return wrapLikeMe(context.eval(global, s, sobj, null, false));
return wrapLikeMe(context.eval(global, s, sobj, null));
}
});
}

View File

@ -1451,7 +1451,7 @@ public final class Global extends ScriptObject implements Scope {
* @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 @@ public final class Global extends ScriptObject implements Scope {
* @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
*

View File

@ -279,8 +279,8 @@ public final class NativeFunction {
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) {

View File

@ -668,12 +668,11 @@ public final class Context {
* @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 @@ public final class Context {
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 @@ public final class Context {
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);

View File

@ -156,7 +156,7 @@ final class DebuggerSupport {
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;
}

View File

@ -439,7 +439,7 @@ public class Shell {
}
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));
}

View File

@ -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) {
}

View File

@ -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();

View File

@ -97,12 +97,14 @@ public class JSONCompatibleTest {
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);