8062937: Need to block constant assumption for index setters and defineOwnProperty, not just delete
authorlagergren
Thu, 13 Nov 2014 16:59:03 +0100
changeset 27531 e8813093f603
parent 27530 518b8ae2dbb9
child 27532 4bb9d8815020
8062937: Need to block constant assumption for index setters and defineOwnProperty, not just delete Reviewed-by: hannesw, jlaskey
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java
nashorn/test/script/basic/JDK-8062937.js
nashorn/test/script/basic/JDK-8062937.js.EXPECTED
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java	Thu Nov 13 15:29:22 2014 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java	Thu Nov 13 16:59:03 2014 +0100
@@ -510,6 +510,13 @@
         }
     }
 
+    private void invalidateGlobalConstant(final String key) {
+        final GlobalConstants globalConstants = getGlobalConstants();
+        if (globalConstants != null) {
+            globalConstants.delete(key);
+        }
+    }
+
     /**
      * ECMA 8.12.9 [[DefineOwnProperty]] (P, Desc, Throw)
      *
@@ -525,6 +532,8 @@
         final Object             current = getOwnPropertyDescriptor(key);
         final String             name    = JSType.toString(key);
 
+        invalidateGlobalConstant(key);
+
         if (current == UNDEFINED) {
             if (isExtensible()) {
                 // add a new own property
@@ -923,10 +932,8 @@
                 if (property instanceof UserAccessorProperty) {
                     ((UserAccessorProperty)property).setAccessors(this, getMap(), null);
                 }
-                final GlobalConstants globalConstants = getGlobalConstants();
-                if (globalConstants != null) {
-                    globalConstants.delete(property.getKey());
-                }
+
+                invalidateGlobalConstant(property.getKey());
                 return true;
             }
         }
@@ -3148,6 +3155,9 @@
      */
     public final void setObject(final FindProperty find, final int callSiteFlags, final String key, final Object value) {
         FindProperty f = find;
+
+        invalidateGlobalConstant(key);
+
         if (f != null && f.isInherited() && !(f.getProperty() instanceof UserAccessorProperty)) {
             final boolean isScope = NashornCallSiteDescriptor.isScopeFlag(callSiteFlags);
             // If the start object of the find is not this object it means the property was found inside a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8062937.js	Thu Nov 13 16:59:03 2014 +0100
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2010, 2014, 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-8062937 - GlobalConstants produces wrong result with defineProperty and index setters
+ *
+ * @test
+ * @run
+ */
+
+var x = 1;
+for (var i = 2; i < 5; i++) {
+    print(x);
+    Object.defineProperty(this, "x", {value: i});
+}
+print(x);
+
+print();
+
+var name = "y";
+var y = 1;
+for (var i = 2; i < 5; i++) {
+    print(y);
+    this[name] = i;
+}
+print(y);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8062937.js.EXPECTED	Thu Nov 13 16:59:03 2014 +0100
@@ -0,0 +1,9 @@
+1
+2
+3
+4
+
+1
+2
+3
+4