8024120: Setting __proto__ to null removes the __proto__ property
authorsundar
Wed, 04 Sep 2013 14:29:07 +0530
changeset 19880 ba17a955c28b
parent 19639 aa4207b72f72
child 19881 d92851923f86
8024120: Setting __proto__ to null removes the __proto__ property Reviewed-by: lagergren, attila
nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java
nashorn/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js
nashorn/test/script/basic/JDK-8023368.js
nashorn/test/script/basic/JDK-8023368.js.EXPECTED
nashorn/test/script/basic/JDK-8024120.js
nashorn/test/script/basic/circular_proto.js
nashorn/test/script/basic/nonextensible_proto_assign.js
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java	Tue Aug 27 19:26:48 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java	Wed Sep 04 14:29:07 2013 +0530
@@ -87,6 +87,8 @@
  */
 
 public abstract class ScriptObject extends PropertyListenerManager implements PropertyAccess {
+    /** __proto__ special property name */
+    static final String PROTO_PROPERTY_NAME   = "__proto__";
 
     /** Search fall back routine name for "no such method" */
     static final String NO_SUCH_METHOD_NAME   = "__noSuchMethod__";
@@ -130,6 +132,9 @@
     /** Indexed array data. */
     private ArrayData arrayData;
 
+    static final MethodHandle GETPROTO           = findOwnMH("getProto", ScriptObject.class);
+    static final MethodHandle SETPROTOCHECK      = findOwnMH("setProtoCheck", void.class, Object.class);
+
     static final MethodHandle SETFIELD           = findOwnMH("setField",         void.class, CallSiteDescriptor.class, PropertyMap.class, PropertyMap.class, MethodHandle.class, Object.class, Object.class);
     static final MethodHandle SETSPILL           = findOwnMH("setSpill",         void.class, CallSiteDescriptor.class, PropertyMap.class, PropertyMap.class, int.class, Object.class, Object.class);
     static final MethodHandle SETSPILLWITHNEW    = findOwnMH("setSpillWithNew",  void.class, CallSiteDescriptor.class, PropertyMap.class, PropertyMap.class, int.class, Object.class, Object.class);
@@ -1745,6 +1750,10 @@
         MethodHandle methodHandle;
 
         if (find == null) {
+            if (PROTO_PROPERTY_NAME.equals(name)) {
+                return new GuardedInvocation(GETPROTO, NashornGuards.getScriptObjectGuard());
+            }
+
             if ("getProp".equals(operator)) {
                 return noSuchProperty(desc, request);
             } else if ("getMethod".equals(operator)) {
@@ -1851,6 +1860,7 @@
          * toString = function() { print("global toString"); } // don't affect Object.prototype.toString
          */
         FindProperty find = findProperty(name, true, scope, this);
+
         // If it's not a scope search, then we don't want any inherited properties except those with user defined accessors.
         if (!scope && find != null && find.isInherited() && !(find.getProperty() instanceof UserAccessorProperty)) {
             // We should still check if inherited data property is not writable
@@ -1866,9 +1876,12 @@
                 // Existing, non-writable property
                 return createEmptySetMethod(desc, "property.not.writable", true);
             }
-        } else if (!isExtensible()) {
-            // Non-existing property on a non-extensible object
-            return createEmptySetMethod(desc, "object.non.extensible", false);
+        } else {
+            if (PROTO_PROPERTY_NAME.equals(name)) {
+                return new GuardedInvocation(SETPROTOCHECK, NashornGuards.getScriptObjectGuard());
+            } else if (! isExtensible()) {
+                return createEmptySetMethod(desc, "object.non.extensible", false);
+            }
         }
 
         return new SetMethodCreator(this, find, desc).createGuardedInvocation();
--- a/nashorn/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js	Tue Aug 27 19:26:48 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/resources/mozilla_compat.js	Wed Sep 04 14:29:07 2013 +0530
@@ -137,17 +137,6 @@
     }
 });
 
-// Object.prototype.__proto__ (read-only)
-Object.defineProperty(Object.prototype, "__proto__", {
-    configurable: true, enumerable: false,
-    get: function() {
-        return Object.getPrototypeOf(this);
-    },
-    set: function(x) {
-        Object.setPrototypeOf(this, x);
-    }
-});
-
 // Object.prototype.toSource
 Object.defineProperty(Object.prototype, "toSource", {
     configurable: true, enumerable: false, writable: true,
--- a/nashorn/test/script/basic/JDK-8023368.js	Tue Aug 27 19:26:48 2013 +0530
+++ b/nashorn/test/script/basic/JDK-8023368.js	Wed Sep 04 14:29:07 2013 +0530
@@ -28,8 +28,6 @@
  * @run
  */
 
-load("nashorn:mozilla_compat.js");
-
 // function to force same callsites
 function check(obj) {
     print(obj.func());
--- a/nashorn/test/script/basic/JDK-8023368.js.EXPECTED	Tue Aug 27 19:26:48 2013 +0530
+++ b/nashorn/test/script/basic/JDK-8023368.js.EXPECTED	Wed Sep 04 14:29:07 2013 +0530
@@ -4,15 +4,15 @@
 Func.prototype.func
 hello
 [object Object]
-obj.__proto__.func @ 57
+obj.__proto__.func @ 55
 344
 [object Object]
-obj.__proto__.func @ 57
+obj.__proto__.func @ 55
 344
 [object Object]
-obj.__proto__.func @ 57
+obj.__proto__.func @ 55
 344
 new object.toString
-obj.__proto__.func @ 57
+obj.__proto__.func @ 55
 344
 new object.toString
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8024120.js	Wed Sep 04 14:29:07 2013 +0530
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2010, 2013, 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-8024120: Setting __proto__ to null removes the __proto__ property
+ *
+ * @test
+ * @run
+ */
+
+var obj = {};
+
+obj.__proto__ = null;
+
+if (obj.__proto__ !== null || typeof(obj.__proto__) != 'object') {
+    fail("obj.__proto__ is expected to be null");
+}
+
+var p = Object.getPrototypeOf(obj);
+if (p !== null || typeof(p) != 'object') {
+    fail("Object.getPrototypeOf(obj) is expected to be null");
+}
--- a/nashorn/test/script/basic/circular_proto.js	Tue Aug 27 19:26:48 2013 +0530
+++ b/nashorn/test/script/basic/circular_proto.js	Wed Sep 04 14:29:07 2013 +0530
@@ -29,7 +29,6 @@
  */
 
 // check that we cannot create __proto__ cycle
-load("nashorn:mozilla_compat.js");
 
 var obj = {};
 var obj2 = Object.create(obj);
--- a/nashorn/test/script/basic/nonextensible_proto_assign.js	Tue Aug 27 19:26:48 2013 +0530
+++ b/nashorn/test/script/basic/nonextensible_proto_assign.js	Wed Sep 04 14:29:07 2013 +0530
@@ -28,8 +28,6 @@
  * @run
  */
 
-load("nashorn:mozilla_compat.js")
-
 // check that we cannot assign to __proto__ of a non-extensible object
 try {
     var obj = {}