aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/jdk/nashorn/api/scripting/ScriptObjectMirror.java2
-rw-r--r--src/jdk/nashorn/internal/codegen/CodeGenerator.java4
-rw-r--r--src/jdk/nashorn/internal/objects/Global.java4
-rw-r--r--src/jdk/nashorn/internal/objects/NativeObject.java39
-rw-r--r--src/jdk/nashorn/internal/runtime/GlobalFunctions.java1
-rw-r--r--src/jdk/nashorn/internal/runtime/ScriptObject.java48
-rw-r--r--test/script/basic/JDK-8044520.js145
7 files changed, 222 insertions, 21 deletions
diff --git a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
index 07f4e8a6..b8035d2e 100644
--- a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
+++ b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
@@ -496,7 +496,7 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
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;
}
});
diff --git a/src/jdk/nashorn/internal/codegen/CodeGenerator.java b/src/jdk/nashorn/internal/codegen/CodeGenerator.java
index 882846c6..52bdc7cb 100644
--- a/src/jdk/nashorn/internal/codegen/CodeGenerator.java
+++ b/src/jdk/nashorn/internal/codegen/CodeGenerator.java
@@ -1500,7 +1500,9 @@ final class CodeGenerator extends NodeOperatorVisitor<CodeGeneratorLexicalContex
method.dup();
if (protoNode != null) {
load(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 {
globalObjectPrototype();
method.invoke(ScriptObject.SET_PROTO);
diff --git a/src/jdk/nashorn/internal/objects/Global.java b/src/jdk/nashorn/internal/objects/Global.java
index 00019382..0614c4b4 100644
--- a/src/jdk/nashorn/internal/objects/Global.java
+++ b/src/jdk/nashorn/internal/objects/Global.java
@@ -1908,8 +1908,8 @@ public final class Global extends ScriptObject implements Scope {
// 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);
diff --git a/src/jdk/nashorn/internal/objects/NativeObject.java b/src/jdk/nashorn/internal/objects/NativeObject.java
index 9cde4faf..4fd8aeb5 100644
--- a/src/jdk/nashorn/internal/objects/NativeObject.java
+++ b/src/jdk/nashorn/internal/objects/NativeObject.java
@@ -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 @@ import jdk.nashorn.internal.runtime.linker.NashornBeansLinker;
*/
@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,35 @@ public final class NativeObject {
});
}
+ @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 Object obj = Global.toObject(self);
+ Global.checkObject(obj);
+ final ScriptObject sobj = (ScriptObject)obj;
+ 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 +193,7 @@ public final class NativeObject {
@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 +810,8 @@ public final class NativeObject {
return new LinkRequestImpl(CallSiteDescriptorFactory.create(MethodHandles.publicLookup(), operation,
methodType), 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));
+ }
}
diff --git a/src/jdk/nashorn/internal/runtime/GlobalFunctions.java b/src/jdk/nashorn/internal/runtime/GlobalFunctions.java
index c750d80e..10747a1b 100644
--- a/src/jdk/nashorn/internal/runtime/GlobalFunctions.java
+++ b/src/jdk/nashorn/internal/runtime/GlobalFunctions.java
@@ -459,5 +459,4 @@ loop:
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));
}
-
}
diff --git a/src/jdk/nashorn/internal/runtime/ScriptObject.java b/src/jdk/nashorn/internal/runtime/ScriptObject.java
index d4556d89..a9e1e90c 100644
--- a/src/jdk/nashorn/internal/runtime/ScriptObject.java
+++ b/src/jdk/nashorn/internal/runtime/ScriptObject.java
@@ -132,8 +132,6 @@ public abstract class ScriptObject implements PropertyAccess {
/** Method handle to retrive prototype of this object */
public static final MethodHandle GETPROTO = findOwnMH("getProto", ScriptObject.class);
- /** Method handle to set prototype of this object */
- public static final MethodHandle SETPROTOCHECK = findOwnMH("setProtoCheck", void.class, Object.class);
static final MethodHandle MEGAMORPHIC_GET = findOwnMH("megamorphicGet", Object.class, String.class, boolean.class, boolean.class);
static final MethodHandle GLOBALFILTER = findOwnMH("globalFilter", Object.class, Object.class);
@@ -160,7 +158,7 @@ public abstract class ScriptObject implements PropertyAccess {
public static final Call SET_PROTO = virtualCallNoLookup(ScriptObject.class, "setInitialProto", 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 */
public static final Call SET_USER_ACCESSORS = virtualCall(MethodHandles.lookup(), ScriptObject.class, "setUserAccessors", void.class, String.class, ScriptFunction.class, ScriptFunction.class);
@@ -1127,14 +1125,21 @@ public abstract class ScriptObject implements PropertyAccess {
/**
* 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) {
@@ -1145,14 +1150,27 @@ public abstract class ScriptObject implements PropertyAccess {
}
setProto((ScriptObject)newProto);
} else {
- final Global global = Context.getGlobal();
- final Object newProtoObject = JSType.toScriptObject(global, newProto);
+ throw typeError("cant.set.proto.to.non.object", ScriptRuntime.safeToString(this), ScriptRuntime.safeToString(newProto));
+ }
+ }
- if (newProtoObject instanceof ScriptObject) {
- setProto((ScriptObject)newProtoObject);
- } else {
- throw typeError(global, "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());
}
}
diff --git a/test/script/basic/JDK-8044520.js b/test/script/basic/JDK-8044520.js
new file mode 100644
index 00000000..edcaa116
--- /dev/null
+++ b/test/script/basic/JDK-8044520.js
@@ -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);