Tue, 06 May 2014 17:54:15 +0530
8042364: Make __proto__ ES6 draft compliant
Reviewed-by: jlaskey, lagergren, attila
1.1 --- a/src/jdk/nashorn/internal/codegen/CodeGenerator.java Tue May 06 12:38:12 2014 +0200 1.2 +++ b/src/jdk/nashorn/internal/codegen/CodeGenerator.java Tue May 06 17:54:15 2014 +0530 1.3 @@ -1451,7 +1451,10 @@ 1.4 1.5 if (value == null) { 1.6 hasGettersSetters = true; 1.7 - } else if (key.equals(ScriptObject.PROTO_PROPERTY_NAME)) { 1.8 + } else if (propertyNode.getKey() instanceof IdentNode && 1.9 + key.equals(ScriptObject.PROTO_PROPERTY_NAME)) { 1.10 + // ES6 draft compliant __proto__ inside object literal 1.11 + // Identifier key and name is __proto__ 1.12 protoNode = value; 1.13 continue; 1.14 }
2.1 --- a/src/jdk/nashorn/internal/objects/Global.java Tue May 06 12:38:12 2014 +0200 2.2 +++ b/src/jdk/nashorn/internal/objects/Global.java Tue May 06 17:54:15 2014 +0530 2.3 @@ -1906,6 +1906,13 @@ 2.4 // Object.getPrototypeOf(Function.prototype) === Object.prototype 2.5 anon.setInitialProto(ObjectPrototype); 2.6 2.7 + // ES6 draft compliant __proto__ property of Object.prototype 2.8 + // accessors on Object.prototype for "__proto__" 2.9 + final ScriptFunction getProto = ScriptFunctionImpl.makeFunction("getProto", ScriptObject.GETPROTO); 2.10 + final ScriptFunction setProto = ScriptFunctionImpl.makeFunction("setProto", ScriptObject.SETPROTOCHECK); 2.11 + ObjectPrototype.addOwnProperty("__proto__", Attribute.NOT_ENUMERABLE, getProto, setProto); 2.12 + 2.13 + 2.14 // Function valued properties of Function.prototype were not properly 2.15 // initialized. Because, these were created before global.function and 2.16 // global.object were not initialized.
3.1 --- a/src/jdk/nashorn/internal/runtime/ScriptObject.java Tue May 06 12:38:12 2014 +0200 3.2 +++ b/src/jdk/nashorn/internal/runtime/ScriptObject.java Tue May 06 17:54:15 2014 +0530 3.3 @@ -91,7 +91,7 @@ 3.4 */ 3.5 3.6 public abstract class ScriptObject implements PropertyAccess { 3.7 - /** __proto__ special property name */ 3.8 + /** __proto__ special property name inside object literals. ES6 draft. */ 3.9 public static final String PROTO_PROPERTY_NAME = "__proto__"; 3.10 3.11 /** Search fall back routine name for "no such method" */ 3.12 @@ -130,8 +130,10 @@ 3.13 /** Indexed array data. */ 3.14 private ArrayData arrayData; 3.15 3.16 - static final MethodHandle GETPROTO = findOwnMH("getProto", ScriptObject.class); 3.17 - static final MethodHandle SETPROTOCHECK = findOwnMH("setProtoCheck", void.class, Object.class); 3.18 + /** Method handle to retrive prototype of this object */ 3.19 + public static final MethodHandle GETPROTO = findOwnMH("getProto", ScriptObject.class); 3.20 + /** Method handle to set prototype of this object */ 3.21 + public static final MethodHandle SETPROTOCHECK = findOwnMH("setProtoCheck", void.class, Object.class); 3.22 static final MethodHandle MEGAMORPHIC_GET = findOwnMH("megamorphicGet", Object.class, String.class, boolean.class, boolean.class); 3.23 static final MethodHandle GLOBALFILTER = findOwnMH("globalFilter", Object.class, Object.class); 3.24 3.25 @@ -1732,10 +1734,6 @@ 3.26 MethodHandle methodHandle; 3.27 3.28 if (find == null) { 3.29 - if (PROTO_PROPERTY_NAME.equals(name)) { 3.30 - return new GuardedInvocation(GETPROTO, NashornGuards.getScriptObjectGuard()); 3.31 - } 3.32 - 3.33 if ("getProp".equals(operator)) { 3.34 return noSuchProperty(desc, request); 3.35 } else if ("getMethod".equals(operator)) { 3.36 @@ -1890,9 +1888,7 @@ 3.37 return createEmptySetMethod(desc, "property.not.writable", true); 3.38 } 3.39 } else { 3.40 - if (PROTO_PROPERTY_NAME.equals(name)) { 3.41 - return new GuardedInvocation(SETPROTOCHECK, NashornGuards.getScriptObjectGuard()); 3.42 - } else if (! isExtensible()) { 3.43 + if (! isExtensible()) { 3.44 return createEmptySetMethod(desc, "object.non.extensible", false); 3.45 } 3.46 }
4.1 --- a/test/script/basic/JDK-8024120.js Tue May 06 12:38:12 2014 +0200 4.2 +++ b/test/script/basic/JDK-8024120.js Tue May 06 17:54:15 2014 +0530 4.3 @@ -32,10 +32,6 @@ 4.4 4.5 obj.__proto__ = null; 4.6 4.7 -if (obj.__proto__ !== null || typeof(obj.__proto__) != 'object') { 4.8 - fail("obj.__proto__ is expected to be null"); 4.9 -} 4.10 - 4.11 var p = Object.getPrototypeOf(obj); 4.12 if (p !== null || typeof(p) != 'object') { 4.13 fail("Object.getPrototypeOf(obj) is expected to be null");
5.1 --- a/test/script/basic/JDK-8024174.js Tue May 06 12:38:12 2014 +0200 5.2 +++ b/test/script/basic/JDK-8024174.js Tue May 06 17:54:15 2014 +0530 5.3 @@ -46,6 +46,6 @@ 5.4 __proto__: null 5.5 }; 5.6 5.7 -if (obj2.__proto__ !== null || Object.getPrototypeOf(obj2) !== null) { 5.8 +if (Object.getPrototypeOf(obj2) !== null) { 5.9 fail("obj2.__proto__ was not set to null inside literal"); 5.10 }
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/script/basic/JDK-8042364.js Tue May 06 17:54:15 2014 +0530 6.3 @@ -0,0 +1,65 @@ 6.4 +/* 6.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. 6.11 + * 6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.15 + * version 2 for more details (a copy is included in the LICENSE file that 6.16 + * accompanied this code). 6.17 + * 6.18 + * You should have received a copy of the GNU General Public License version 6.19 + * 2 along with this work; if not, write to the Free Software Foundation, 6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.21 + * 6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.23 + * or visit www.oracle.com if you need additional information or have any 6.24 + * questions. 6.25 + */ 6.26 + 6.27 +/** 6.28 + * JDK-8042364: Make __proto__ ES6 draft compliant 6.29 + * 6.30 + * @test 6.31 + * @run 6.32 + */ 6.33 + 6.34 +// check for Object.prototype.__proto__ accessor property 6.35 +print("Object.prototype has __proto__?", 6.36 + Object.prototype.hasOwnProperty("__proto__")) 6.37 + 6.38 +var desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__") 6.39 +print("descriptor"); 6.40 +print(JSON.stringify(desc)) 6.41 +print("getter", desc.get) 6.42 +print("setter", desc.set) 6.43 + 6.44 +// no computed "__proto__" name, only identifier! 6.45 +var p = {} 6.46 +var obj = { 6.47 + "__proto__" : p 6.48 +} 6.49 + 6.50 +if (Object.getPrototypeOf(obj) === p) { 6.51 + fail("obj has wrong __proto__, allows computed __proto__!") 6.52 +} 6.53 + 6.54 +if (obj.__proto__ !== p) { 6.55 + fail("__proto__ not created as normal property!") 6.56 +} 6.57 + 6.58 +if (Object.getPrototypeOf(obj) !== Object.prototype) { 6.59 + fail("obj has wrong __proto__") 6.60 +} 6.61 + 6.62 +var obj2 = { 6.63 + __proto__: p 6.64 +} 6.65 + 6.66 +if (Object.getPrototypeOf(obj2) !== p) { 6.67 + fail("can't set __proto__ in object literal") 6.68 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/script/basic/JDK-8042364.js.EXPECTED Tue May 06 17:54:15 2014 +0530 7.3 @@ -0,0 +1,5 @@ 7.4 +Object.prototype has __proto__? true 7.5 +descriptor 7.6 +{"configurable":true,"enumerable":false} 7.7 +getter function getProto() { [native code] } 7.8 +setter function setProto() { [native code] }