8044520: Nashorn cannot execute node.js's express module
authorsundar
Tue, 03 Jun 2014 14:13:03 +0530
changeset 24772 0fc1013a1785
parent 24771 60a75955c830
child 24773 34c21f8464c5
8044520: Nashorn cannot execute node.js's express module Reviewed-by: hannesw, lagergren
nashorn/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java
nashorn/src/jdk/nashorn/internal/objects/Global.java
nashorn/src/jdk/nashorn/internal/objects/NativeObject.java
nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java
nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java
nashorn/test/script/basic/JDK-8044520.js
--- a/nashorn/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Tue Jun 03 13:57:52 2014 +0530
+++ b/nashorn/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java	Tue Jun 03 14:13:03 2014 +0530
@@ -502,7 +502,7 @@
     public void setProto(final Object proto) {
         inGlobal(new Callable<Void>() {
             @Override public Void call() {
-                sobj.setProtoCheck(unwrap(proto, global));
+                sobj.setPrototypeOf(unwrap(proto, global));
                 return null;
             }
         });
--- a/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Tue Jun 03 13:57:52 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Tue Jun 03 14:13:03 2014 +0530
@@ -2346,7 +2346,9 @@
         method.dup();
         if (protoNode != null) {
             loadExpressionAsObject(protoNode);
-            method.invoke(ScriptObject.SET_PROTO_CHECK);
+            // take care of { __proto__: 34 } or some such!
+            method.convert(Type.OBJECT);
+            method.invoke(ScriptObject.SET_PROTO_FROM_LITERAL);
         } else {
             method.invoke(ScriptObject.SET_GLOBAL_OBJECT_PROTO);
         }
--- a/nashorn/src/jdk/nashorn/internal/objects/Global.java	Tue Jun 03 13:57:52 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/objects/Global.java	Tue Jun 03 14:13:03 2014 +0530
@@ -1975,11 +1975,10 @@
 
         // ES6 draft compliant __proto__ property of Object.prototype
         // accessors on Object.prototype for "__proto__"
-        final ScriptFunction getProto = ScriptFunctionImpl.makeFunction("getProto", ScriptObject.GETPROTO);
-        final ScriptFunction setProto = ScriptFunctionImpl.makeFunction("setProto", ScriptObject.SETPROTOCHECK);
+        final ScriptFunction getProto = ScriptFunctionImpl.makeFunction("getProto", NativeObject.GET__PROTO__);
+        final ScriptFunction setProto = ScriptFunctionImpl.makeFunction("setProto", NativeObject.SET__PROTO__);
         ObjectPrototype.addOwnProperty("__proto__", Attribute.NOT_ENUMERABLE, getProto, setProto);
 
-
         // Function valued properties of Function.prototype were not properly
         // initialized. Because, these were created before global.function and
         // global.object were not initialized.
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeObject.java	Tue Jun 03 13:57:52 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeObject.java	Tue Jun 03 14:13:03 2014 +0530
@@ -25,6 +25,7 @@
 
 package jdk.nashorn.internal.objects;
 
+import static jdk.nashorn.internal.lookup.Lookup.MH;
 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
 
@@ -74,6 +75,9 @@
  */
 @ScriptClass("Object")
 public final class NativeObject {
+    public static final MethodHandle GET__PROTO__ = findOwnMH("get__proto__", ScriptObject.class, Object.class);
+    public static final MethodHandle SET__PROTO__ = findOwnMH("set__proto__", Object.class, Object.class, Object.class);
+
     private static final Object TO_STRING = new Object();
 
     private static InvokeByName getTO_STRING() {
@@ -86,6 +90,33 @@
                 });
     }
 
+    @SuppressWarnings("unused")
+    private static ScriptObject get__proto__(final Object self) {
+        // See ES6 draft spec: B.2.2.1.1 get Object.prototype.__proto__
+        // Step 1 Let O be the result of calling ToObject passing the this.
+        final ScriptObject sobj = Global.checkObject(Global.toObject(self));
+        return sobj.getProto();
+    }
+
+    @SuppressWarnings("unused")
+    private static Object set__proto__(final Object self, final Object proto) {
+        // See ES6 draft spec: B.2.2.1.2 set Object.prototype.__proto__
+        // Step 1
+        Global.checkObjectCoercible(self);
+        // Step 4
+        if (! (self instanceof ScriptObject)) {
+            return UNDEFINED;
+        }
+
+        final ScriptObject sobj = (ScriptObject)self;
+        // __proto__ assignment ignores non-nulls and non-objects
+        // step 3: If Type(proto) is neither Object nor Null, then return undefined.
+        if (proto == null || proto instanceof ScriptObject) {
+            sobj.setPrototypeOf(proto);
+        }
+        return UNDEFINED;
+    }
+
     private static final MethodType MIRROR_GETTER_TYPE = MethodType.methodType(Object.class, ScriptObjectMirror.class);
     private static final MethodType MIRROR_SETTER_TYPE = MethodType.methodType(Object.class, ScriptObjectMirror.class, Object.class);
 
@@ -160,7 +191,7 @@
     @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
     public static Object setPrototypeOf(final Object self, final Object obj, final Object proto) {
         if (obj instanceof ScriptObject) {
-            ((ScriptObject)obj).setProtoCheck(proto);
+            ((ScriptObject)obj).setPrototypeOf(proto);
             return obj;
         } else if (obj instanceof ScriptObjectMirror) {
             ((ScriptObjectMirror)obj).setProto(proto);
@@ -777,4 +808,8 @@
         return new LinkRequestImpl(CallSiteDescriptorFactory.create(MethodHandles.publicLookup(), operation,
                 methodType), null, 0, false, source);
     }
+
+    private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
+        return MH.findStatic(MethodHandles.lookup(), NativeObject.class, name, MH.type(rtype, types));
+    }
 }
--- a/nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java	Tue Jun 03 13:57:52 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java	Tue Jun 03 14:13:03 2014 +0530
@@ -503,5 +503,4 @@
     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
         return MH.findStatic(MethodHandles.lookup(), GlobalFunctions.class, name, MH.type(rtype, types));
     }
-
 }
--- a/nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java	Tue Jun 03 13:57:52 2014 +0530
+++ b/nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java	Tue Jun 03 14:13:03 2014 +0530
@@ -155,8 +155,6 @@
 
     /** Method handle to retrieve prototype of this object */
     public static final MethodHandle GETPROTO      = findOwnMH_V("getProto", ScriptObject.class);
-    /** Method handle to set prototype of this object */
-    public static final MethodHandle SETPROTOCHECK = findOwnMH_V("setProtoCheck", void.class, Object.class);
 
     static final MethodHandle MEGAMORPHIC_GET    = findOwnMH_V("megamorphicGet", Object.class, String.class, boolean.class, boolean.class);
     static final MethodHandle GLOBALFILTER       = findOwnMH_S("globalFilter", Object.class, Object.class);
@@ -191,7 +189,7 @@
     public static final Call SET_GLOBAL_OBJECT_PROTO = staticCallNoLookup(ScriptObject.class, "setGlobalObjectProto", void.class, ScriptObject.class);
 
     /** Method handle for setting the proto of a ScriptObject after checking argument */
-    public static final Call SET_PROTO_CHECK    = virtualCallNoLookup(ScriptObject.class, "setProtoCheck", void.class, Object.class);
+    public static final Call SET_PROTO_FROM_LITERAL    = virtualCallNoLookup(ScriptObject.class, "setProtoFromLiteral", void.class, Object.class);
 
     /** Method handle for setting the user accessors of a ScriptObject */
     //TODO fastpath this
@@ -1268,14 +1266,22 @@
 
     /**
      * Set the __proto__ of an object with checks.
+     * This is the built-in operation [[SetPrototypeOf]]
+     * See ES6 draft spec: 9.1.2 [[SetPrototypeOf]] (V)
+     *
      * @param newProto Prototype to set.
      */
-    public final void setProtoCheck(final Object newProto) {
-        if (!isExtensible()) {
-            throw typeError("__proto__.set.non.extensible", ScriptRuntime.safeToString(this));
-        }
-
+    public final void setPrototypeOf(final Object newProto) {
         if (newProto == null || newProto instanceof ScriptObject) {
+            if (! isExtensible()) {
+                // okay to set same proto again - even if non-extensible
+
+                if (newProto == getProto()) {
+                    return;
+                }
+                throw typeError("__proto__.set.non.extensible", ScriptRuntime.safeToString(this));
+            }
+
             // check for circularity
             ScriptObject p = (ScriptObject)newProto;
             while (p != null) {
@@ -1286,14 +1292,27 @@
             }
             setProto((ScriptObject)newProto);
         } else {
-            final Global global = Context.getGlobal();
-            final Object  newProtoObject = JSType.toScriptObject(global, newProto);
-
-            if (newProtoObject instanceof ScriptObject) {
-                setProto((ScriptObject)newProtoObject);
-            } else {
-                throw typeError(global, "cant.set.proto.to.non.object", ScriptRuntime.safeToString(this), ScriptRuntime.safeToString(newProto));
-            }
+            throw typeError("cant.set.proto.to.non.object", ScriptRuntime.safeToString(this), ScriptRuntime.safeToString(newProto));
+        }
+    }
+
+    /**
+     * Set the __proto__ of an object from an object literal.
+     * See ES6 draft spec: B.3.1 __proto__ Property Names in
+     * Object Initializers. Step 6 handling of "__proto__".
+     *
+     * @param newProto Prototype to set.
+     */
+    public final void setProtoFromLiteral(final Object newProto) {
+        if (newProto == null || newProto instanceof ScriptObject) {
+            setPrototypeOf(newProto);
+        } else {
+            // Some non-object, non-null. Then, we need to set
+            // Object.prototype as the new __proto__
+            //
+            // var obj = { __proto__ : 34 };
+            // print(obj.__proto__ === Object.prototype); // => true
+            setPrototypeOf(Global.objectPrototype());
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8044520.js	Tue Jun 03 14:13:03 2014 +0530
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2014, 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-8044520: Nashorn cannot execute node.js's express module
+ *
+ * @test
+ * @run
+ */
+
+function checkNullProto() {
+    var obj = {};
+    obj.__proto__ = null;
+    var proto = Object.getPrototypeOf(obj);
+    if (typeof proto != 'object' || proto !== null) {
+        fail("__proto__ can't be set to null!");
+    }
+}
+
+checkNullProto();
+
+function checkSetProto(proto) {
+    var obj = {};
+    obj.__proto__ = proto;
+    if (Object.getPrototypeOf(obj) !== Object.prototype) {
+        fail("obj.__proto__ set not ignored for " + proto);
+    }
+}
+
+checkSetProto(undefined);
+checkSetProto(42);
+checkSetProto(false);
+checkSetProto("hello");
+
+function checkLiteralSetProto(proto) {
+    var obj = { __proto__: proto };
+    if (obj.__proto__ !== Object.prototype) {
+        fail("object literal _proto__ set not ignored for " + proto);
+    }
+}
+
+checkLiteralSetProto(undefined);
+checkLiteralSetProto(34);
+checkLiteralSetProto(true);
+checkLiteralSetProto("world");
+
+function checkNullProtoFromLiteral() {
+    var obj = { __proto__: null };
+    var proto = Object.getPrototypeOf(obj);
+    if (typeof proto != 'object' || proto !== null) {
+        fail("__proto__ can't be set to null!");
+    }
+}
+
+checkNullProtoFromLiteral();
+
+function checkSetPrototypeOf(proto) {
+    try {
+        Object.setPrototypeOf({}, proto);
+        fail("should have thrown error for " + proto);
+    } catch (e) {
+        if (! (e instanceof TypeError)) {
+            fail("should have thrown TypeError, got " + e);
+        }
+    }
+}
+
+checkSetPrototypeOf(undefined);
+checkSetPrototypeOf(43);
+checkSetPrototypeOf(false);
+checkSetPrototypeOf("nashorn");
+
+function checkNullSetPrototypeOf() {
+    var obj = { };
+    Object.setPrototypeOf(obj, null);
+    var proto = Object.getPrototypeOf(obj);
+    if (typeof proto != 'object' || proto !== null) {
+        fail("__proto__ can't be set to null!");
+    }
+}
+
+checkNullSetPrototypeOf();
+
+var desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");
+
+function checkProtoGetterOnPrimitive(value) {
+    // call __proto__ getter on primitive - check ToObject
+    // is called on 'this' value as per draft spec
+    if (desc.get.call(value) !== Object(value).__proto__) {
+        fail("can't call __proto__ getter on " + value);
+    }
+}
+
+checkProtoGetterOnPrimitive(32);
+checkProtoGetterOnPrimitive(false);
+checkProtoGetterOnPrimitive("great!");
+
+function checkProtoSetterOnNonObjectThis(self) {
+    try {
+        desc.set.call(self);
+        fail("should have thrown TypeError");
+    } catch (e) {
+        if (! (e instanceof TypeError)) {
+            fail("should throw TypeError on non-object self, got " +e);
+        }
+    }
+}
+
+checkProtoSetterOnNonObjectThis(undefined);
+checkProtoSetterOnNonObjectThis(null);
+
+function checkProtoSetterReturnValue(obj, p) {
+    if (typeof desc.set.call(obj, p) != "undefined") {
+        fail("__proto__ setter does not return undefined: " + obj + " " + p);
+    }
+}
+
+// try non-object 'this'. setter is expected to return undefined.
+checkProtoSetterReturnValue(23);
+checkProtoSetterReturnValue(false);
+checkProtoSetterReturnValue("foo");
+
+// set proper __proto__. Still return value is undefined.
+checkProtoSetterReturnValue({}, {});
+checkProtoSetterReturnValue({}, null);