JDK-8044520.js revision 871:f855686309df
1/*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * JDK-8044520: Nashorn cannot execute node.js's express module
26 *
27 * @test
28 * @run
29 */
30
31function checkNullProto() {
32    var obj = {};
33    obj.__proto__ = null;
34    var proto = Object.getPrototypeOf(obj);
35    if (typeof proto != 'object' || proto !== null) {
36        fail("__proto__ can't be set to null!");
37    }
38}
39
40checkNullProto();
41
42function checkSetProto(proto) {
43    var obj = {};
44    obj.__proto__ = proto;
45    if (Object.getPrototypeOf(obj) !== Object.prototype) {
46        fail("obj.__proto__ set not ignored for " + proto);
47    }
48}
49
50checkSetProto(undefined);
51checkSetProto(42);
52checkSetProto(false);
53checkSetProto("hello");
54
55function checkLiteralSetProto(proto) {
56    var obj = { __proto__: proto };
57    if (obj.__proto__ !== Object.prototype) {
58        fail("object literal _proto__ set not ignored for " + proto);
59    }
60}
61
62checkLiteralSetProto(undefined);
63checkLiteralSetProto(34);
64checkLiteralSetProto(true);
65checkLiteralSetProto("world");
66
67function checkNullProtoFromLiteral() {
68    var obj = { __proto__: null };
69    var proto = Object.getPrototypeOf(obj);
70    if (typeof proto != 'object' || proto !== null) {
71        fail("__proto__ can't be set to null!");
72    }
73}
74
75checkNullProtoFromLiteral();
76
77function checkSetPrototypeOf(proto) {
78    try {
79        Object.setPrototypeOf({}, proto);
80        fail("should have thrown error for " + proto);
81    } catch (e) {
82        if (! (e instanceof TypeError)) {
83            fail("should have thrown TypeError, got " + e);
84        }
85    }
86}
87
88checkSetPrototypeOf(undefined);
89checkSetPrototypeOf(43);
90checkSetPrototypeOf(false);
91checkSetPrototypeOf("nashorn");
92
93function checkNullSetPrototypeOf() {
94    var obj = { };
95    Object.setPrototypeOf(obj, null);
96    var proto = Object.getPrototypeOf(obj);
97    if (typeof proto != 'object' || proto !== null) {
98        fail("__proto__ can't be set to null!");
99    }
100}
101
102checkNullSetPrototypeOf();
103
104var desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");
105
106function checkProtoGetterOnPrimitive(value) {
107    // call __proto__ getter on primitive - check ToObject
108    // is called on 'this' value as per draft spec
109    if (desc.get.call(value) !== Object(value).__proto__) {
110        fail("can't call __proto__ getter on " + value);
111    }
112}
113
114checkProtoGetterOnPrimitive(32);
115checkProtoGetterOnPrimitive(false);
116checkProtoGetterOnPrimitive("great!");
117
118function checkProtoSetterOnNonObjectThis(self) {
119    try {
120        desc.set.call(self);
121        fail("should have thrown TypeError");
122    } catch (e) {
123        if (! (e instanceof TypeError)) {
124            fail("should throw TypeError on non-object self, got " +e);
125        }
126    }
127}
128
129checkProtoSetterOnNonObjectThis(undefined);
130checkProtoSetterOnNonObjectThis(null);
131
132function checkProtoSetterReturnValue(obj, p) {
133    if (typeof desc.set.call(obj, p) != "undefined") {
134        fail("__proto__ setter does not return undefined: " + obj + " " + p);
135    }
136}
137
138// try non-object 'this'. setter is expected to return undefined.
139checkProtoSetterReturnValue(23);
140checkProtoSetterReturnValue(false);
141checkProtoSetterReturnValue("foo");
142
143// set proper __proto__. Still return value is undefined.
144checkProtoSetterReturnValue({}, {});
145checkProtoSetterReturnValue({}, null);
146