8058179: Global constants get in the way of self-modifying properties
authorhannesw
Thu, 11 Sep 2014 17:23:02 +0200
changeset 26511 65722244e6b2
parent 26510 a6f3dd80da4c
child 26512 f3d4b01ac41e
8058179: Global constants get in the way of self-modifying properties Reviewed-by: attila, jlaskey, sundar, lagergren
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/GlobalConstants.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java
nashorn/test/script/basic/JDK-8058179.js
nashorn/test/script/basic/JDK-8058179.js.EXPECTED
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/GlobalConstants.java	Thu Sep 11 17:12:38 2014 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/GlobalConstants.java	Thu Sep 11 17:23:02 2014 +0200
@@ -28,6 +28,8 @@
 import static jdk.nashorn.internal.codegen.CompilerConstants.staticCall;
 import static jdk.nashorn.internal.codegen.CompilerConstants.virtualCall;
 import static jdk.nashorn.internal.lookup.Lookup.MH;
+import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
+import static jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor.getProgramPoint;
 import static jdk.nashorn.internal.runtime.logging.DebugLogger.quote;
 
 import java.lang.invoke.MethodHandle;
@@ -370,22 +372,19 @@
      * @param find      property lookup
      * @param receiver  receiver
      * @param desc      callsite descriptor
-     * @param request   link request
-     * @param operator  operator
      *
      * @return resulting getter, or null if failed to create constant
      */
-    synchronized GuardedInvocation findGetMethod(final FindProperty find, final ScriptObject receiver, final CallSiteDescriptor desc, final LinkRequest request, final String operator) {
-        if (GLOBAL_ONLY && !find.getOwner().isGlobal()) {
+    synchronized GuardedInvocation findGetMethod(final FindProperty find, final ScriptObject receiver, final CallSiteDescriptor desc) {
+        // Also return null if property may have side effects
+        if ((GLOBAL_ONLY && !find.getOwner().isGlobal()) || find.getProperty() instanceof UserAccessorProperty) {
             return null;
         }
 
-        final int programPoint         = NashornCallSiteDescriptor.isOptimistic(desc) ?
-            NashornCallSiteDescriptor.getProgramPoint(desc) :
-            UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
-        final boolean     isOptimistic = programPoint != UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
-        final Class<?>    retType      = desc.getMethodType().returnType();
-        final String      name         = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
+        final boolean  isOptimistic = NashornCallSiteDescriptor.isOptimistic(desc);
+        final int      programPoint = isOptimistic ? getProgramPoint(desc) : INVALID_PROGRAM_POINT;
+        final Class<?> retType      = desc.getMethodType().returnType();
+        final String   name         = desc.getNameToken(CallSiteDescriptor.NAME_OPERAND);
 
         final Access acc = getOrCreateSwitchPoint(name);
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java	Thu Sep 11 17:12:38 2014 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptObject.java	Thu Sep 11 17:23:02 2014 +0200
@@ -1971,7 +1971,7 @@
             }
         }
 
-        final GuardedInvocation cinv = Global.getConstants().findGetMethod(find, this, desc, request, operator);
+        final GuardedInvocation cinv = Global.getConstants().findGetMethod(find, this, desc);
         if (cinv != null) {
             return cinv;
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8058179.js	Thu Sep 11 17:23:02 2014 +0200
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 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-8058179: Global constants get in the way of self-modifying properties
+ *
+ * @test
+ * @run
+ */
+
+var global = this;
+
+Object.defineProperty(global, "value", {
+    get: function() {
+        print("getting value");
+        global["value"] = "value 2";
+        return "value 1";
+    },
+    set: function(value) {
+        print("setting value: " + value);
+        delete global["value"];
+        global["value"] = value;
+    },
+    configurable: true
+});
+
+print(value);
+print(value);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8058179.js.EXPECTED	Thu Sep 11 17:23:02 2014 +0200
@@ -0,0 +1,4 @@
+getting value
+setting value: value 2
+value 1
+value 2