changeset 1354:87123e713c92

8130853: Non-extensible global is not handled property Reviewed-by: jlaskey, hannesw
author sundar
date Mon, 13 Jul 2015 20:09:14 +0530
parents a5e202d6eb99
children 9b45ff9ffa8f
files src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java test/script/basic/JDK-8130853.js
diffstat 3 files changed, 106 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java	Fri Jul 10 12:55:10 2015 +0530
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/objects/Global.java	Mon Jul 13 20:09:14 2015 +0530
@@ -2118,17 +2118,18 @@
             }
         }
 
+        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 @@
         }
 
         @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) {
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java	Fri Jul 10 12:55:10 2015 +0530
+++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java	Mon Jul 13 20:09:14 2015 +0530
@@ -287,9 +287,10 @@
      */
     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 @@
      * @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 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);
             }
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/script/basic/JDK-8130853.js	Mon Jul 13 20:09:14 2015 +0530
@@ -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;
+    }
+}