1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/script/basic/JDK-8044520.js Tue Jun 03 17:04:23 2014 +0530 1.3 @@ -0,0 +1,145 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * JDK-8044520: Nashorn cannot execute node.js's express module 1.29 + * 1.30 + * @test 1.31 + * @run 1.32 + */ 1.33 + 1.34 +function checkNullProto() { 1.35 + var obj = {}; 1.36 + obj.__proto__ = null; 1.37 + var proto = Object.getPrototypeOf(obj); 1.38 + if (typeof proto != 'object' || proto !== null) { 1.39 + fail("__proto__ can't be set to null!"); 1.40 + } 1.41 +} 1.42 + 1.43 +checkNullProto(); 1.44 + 1.45 +function checkSetProto(proto) { 1.46 + var obj = {}; 1.47 + obj.__proto__ = proto; 1.48 + if (Object.getPrototypeOf(obj) !== Object.prototype) { 1.49 + fail("obj.__proto__ set not ignored for " + proto); 1.50 + } 1.51 +} 1.52 + 1.53 +checkSetProto(undefined); 1.54 +checkSetProto(42); 1.55 +checkSetProto(false); 1.56 +checkSetProto("hello"); 1.57 + 1.58 +function checkLiteralSetProto(proto) { 1.59 + var obj = { __proto__: proto }; 1.60 + if (obj.__proto__ !== Object.prototype) { 1.61 + fail("object literal _proto__ set not ignored for " + proto); 1.62 + } 1.63 +} 1.64 + 1.65 +checkLiteralSetProto(undefined); 1.66 +checkLiteralSetProto(34); 1.67 +checkLiteralSetProto(true); 1.68 +checkLiteralSetProto("world"); 1.69 + 1.70 +function checkNullProtoFromLiteral() { 1.71 + var obj = { __proto__: null }; 1.72 + var proto = Object.getPrototypeOf(obj); 1.73 + if (typeof proto != 'object' || proto !== null) { 1.74 + fail("__proto__ can't be set to null!"); 1.75 + } 1.76 +} 1.77 + 1.78 +checkNullProtoFromLiteral(); 1.79 + 1.80 +function checkSetPrototypeOf(proto) { 1.81 + try { 1.82 + Object.setPrototypeOf({}, proto); 1.83 + fail("should have thrown error for " + proto); 1.84 + } catch (e) { 1.85 + if (! (e instanceof TypeError)) { 1.86 + fail("should have thrown TypeError, got " + e); 1.87 + } 1.88 + } 1.89 +} 1.90 + 1.91 +checkSetPrototypeOf(undefined); 1.92 +checkSetPrototypeOf(43); 1.93 +checkSetPrototypeOf(false); 1.94 +checkSetPrototypeOf("nashorn"); 1.95 + 1.96 +function checkNullSetPrototypeOf() { 1.97 + var obj = { }; 1.98 + Object.setPrototypeOf(obj, null); 1.99 + var proto = Object.getPrototypeOf(obj); 1.100 + if (typeof proto != 'object' || proto !== null) { 1.101 + fail("__proto__ can't be set to null!"); 1.102 + } 1.103 +} 1.104 + 1.105 +checkNullSetPrototypeOf(); 1.106 + 1.107 +var desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__"); 1.108 + 1.109 +function checkProtoGetterOnPrimitive(value) { 1.110 + // call __proto__ getter on primitive - check ToObject 1.111 + // is called on 'this' value as per draft spec 1.112 + if (desc.get.call(value) !== Object(value).__proto__) { 1.113 + fail("can't call __proto__ getter on " + value); 1.114 + } 1.115 +} 1.116 + 1.117 +checkProtoGetterOnPrimitive(32); 1.118 +checkProtoGetterOnPrimitive(false); 1.119 +checkProtoGetterOnPrimitive("great!"); 1.120 + 1.121 +function checkProtoSetterOnNonObjectThis(self) { 1.122 + try { 1.123 + desc.set.call(self); 1.124 + fail("should have thrown TypeError"); 1.125 + } catch (e) { 1.126 + if (! (e instanceof TypeError)) { 1.127 + fail("should throw TypeError on non-object self, got " +e); 1.128 + } 1.129 + } 1.130 +} 1.131 + 1.132 +checkProtoSetterOnNonObjectThis(undefined); 1.133 +checkProtoSetterOnNonObjectThis(null); 1.134 + 1.135 +function checkProtoSetterReturnValue(obj, p) { 1.136 + if (typeof desc.set.call(obj, p) != "undefined") { 1.137 + fail("__proto__ setter does not return undefined: " + obj + " " + p); 1.138 + } 1.139 +} 1.140 + 1.141 +// try non-object 'this'. setter is expected to return undefined. 1.142 +checkProtoSetterReturnValue(23); 1.143 +checkProtoSetterReturnValue(false); 1.144 +checkProtoSetterReturnValue("foo"); 1.145 + 1.146 +// set proper __proto__. Still return value is undefined. 1.147 +checkProtoSetterReturnValue({}, {}); 1.148 +checkProtoSetterReturnValue({}, null);