8130853: Non-extensible global is not handled property

Reviewed-by: jlaskey, hannesw
This commit is contained in:
Athijegannathan Sundararajan 2015-07-13 20:09:14 +05:30
parent e834520e7e
commit 94c9e33a38
3 changed files with 106 additions and 6 deletions

View File

@ -2118,17 +2118,18 @@ public final class Global extends Scope {
}
}
final boolean extensible = isExtensible();
for (final jdk.nashorn.internal.runtime.Property property : properties) {
if (property.isLexicalBinding()) {
assert lexScope != null;
lexicalMap = lexScope.addBoundProperty(lexicalMap, source, property);
lexicalMap = lexScope.addBoundProperty(lexicalMap, source, property, true);
if (ownMap.findProperty(property.getKey()) != null) {
// If property exists in the global object invalidate any global constant call sites.
invalidateGlobalConstant(property.getKey());
}
} else {
ownMap = addBoundProperty(ownMap, source, property);
ownMap = addBoundProperty(ownMap, source, property, extensible);
}
}
@ -2730,9 +2731,9 @@ public final class Global extends Scope {
}
@Override
protected PropertyMap addBoundProperty(final PropertyMap propMap, final ScriptObject source, final jdk.nashorn.internal.runtime.Property property) {
protected PropertyMap addBoundProperty(final PropertyMap propMap, final ScriptObject source, final jdk.nashorn.internal.runtime.Property property, final boolean extensible) {
// We override this method just to make it callable by Global
return super.addBoundProperty(propMap, source, property);
return super.addBoundProperty(propMap, source, property, extensible);
}
private static GuardedInvocation filterInvocation(final GuardedInvocation invocation) {

View File

@ -287,9 +287,10 @@ public abstract class ScriptObject implements PropertyAccess, Cloneable {
*/
public void addBoundProperties(final ScriptObject source, final Property[] properties) {
PropertyMap newMap = this.getMap();
final boolean extensible = newMap.isExtensible();
for (final Property property : properties) {
newMap = addBoundProperty(newMap, source, property);
newMap = addBoundProperty(newMap, source, property, extensible);
}
this.setMap(newMap);
@ -302,13 +303,18 @@ public abstract class ScriptObject implements PropertyAccess, Cloneable {
* @param propMap the property map
* @param source the source object
* @param property the property to be added
* @param extensible whether the current object is extensible or not
* @return the new property map
*/
protected PropertyMap addBoundProperty(final PropertyMap propMap, final ScriptObject source, final Property property) {
protected PropertyMap addBoundProperty(final PropertyMap propMap, final ScriptObject source, final Property property, final boolean extensible) {
PropertyMap newMap = propMap;
final String key = property.getKey();
final Property oldProp = newMap.findProperty(key);
if (oldProp == null) {
if (! extensible) {
throw typeError("object.non.extensible", key, ScriptRuntime.safeToString(this));
}
if (property instanceof UserAccessorProperty) {
// Note: we copy accessor functions to this object which is semantically different from binding.
final UserAccessorProperty prop = this.newUserAccessors(key, property.getFlags(), property.getGetterFunction(source), property.getSetterFunction(source));
@ -337,11 +343,15 @@ public abstract class ScriptObject implements PropertyAccess, Cloneable {
*/
public void addBoundProperties(final Object source, final AccessorProperty[] properties) {
PropertyMap newMap = this.getMap();
final boolean extensible = newMap.isExtensible();
for (final AccessorProperty property : properties) {
final String key = property.getKey();
if (newMap.findProperty(key) == null) {
if (! extensible) {
throw typeError("object.non.extensible", key, ScriptRuntime.safeToString(this));
}
newMap = newMap.addPropertyBind(property, source);
}
}

View File

@ -0,0 +1,89 @@
/*
* 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-8130853: Non-extensible global is not handled property
*
* @test
* @run
*/
// don't allow extensions to global
Object.preventExtensions(this);
try {
eval("var x = 34;");
throw new Error("should have thrown TypeError");
} catch (e) {
if (! (e instanceof TypeError)) {
throw e;
}
}
try {
eval("function func() {}");
throw new Error("should have thrown TypeError");
} catch (e) {
if (! (e instanceof TypeError)) {
throw e;
}
}
function checkLoad(code) {
try {
load({ name: "test", script: code });
throw new Error("should have thrown TypeError for load: " + code);
} catch (e) {
if (! (e instanceof TypeError)) {
throw e;
}
}
}
checkLoad("var y = 55");
checkLoad("function f() {}");
// check script engine eval
var ScriptEngineManager = Java.type("javax.script.ScriptEngineManager");
var e = new ScriptEngineManager().getEngineByName("nashorn");
var global = e.eval("this");
e.eval("Object.preventExtensions(this);");
try {
e.eval("var myVar = 33;");
throw new Error("should have thrown TypeError");
} catch (e) {
if (! (e.cause.ecmaError instanceof global.TypeError)) {
throw e;
}
}
// Object.bindProperties on arbitrary non-extensible object
var obj = {};
Object.preventExtensions(obj);
try {
Object.bindProperties(obj, { foo: 434 });
throw new Error("should have thrown TypeError");
} catch (e) {
if (! (e instanceof TypeError)) {
throw e;
}
}