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 
  31 function 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 
  40 checkNullProto();
  41 
  42 function 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 
  50 checkSetProto(undefined);
  51 checkSetProto(42);
  52 checkSetProto(false);
  53 checkSetProto("hello");
  54 
  55 function 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 
  62 checkLiteralSetProto(undefined);
  63 checkLiteralSetProto(34);
  64 checkLiteralSetProto(true);
  65 checkLiteralSetProto("world");
  66 
  67 function 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 
  75 checkNullProtoFromLiteral();
  76 
  77 function 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 
  88 checkSetPrototypeOf(undefined);
  89 checkSetPrototypeOf(43);
  90 checkSetPrototypeOf(false);
  91 checkSetPrototypeOf("nashorn");
  92 
  93 function 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 
 102 checkNullSetPrototypeOf();
 103 
 104 var desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");
 105 
 106 function 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 
 114 checkProtoGetterOnPrimitive(32);
 115 checkProtoGetterOnPrimitive(false);
 116 checkProtoGetterOnPrimitive("great!");
 117 
 118 function 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 
 129 checkProtoSetterOnNonObjectThis(undefined);
 130 checkProtoSetterOnNonObjectThis(null);
 131 
 132 function 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.
 139 checkProtoSetterReturnValue(23);
 140 checkProtoSetterReturnValue(false);
 141 checkProtoSetterReturnValue("foo");
 142 
 143 // set proper __proto__. Still return value is undefined.
 144 checkProtoSetterReturnValue({}, {});
 145 checkProtoSetterReturnValue({}, null);