aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsundar <none@none>2014-06-03 17:04:23 +0530
committersundar <none@none>2014-06-03 17:04:23 +0530
commit66e95be4970fa8efb1dec62d03f9af775f2a8b9d (patch)
tree1adc5cb091314d1e846aef66ad3e0df031d61e55 /src
parent9632c562f98fcde891baffe603dd8f85a10e2e13 (diff)
downloadnashorn-66e95be4970fa8efb1dec62d03f9af775f2a8b9d.tar.gz
8044520: Nashorn cannot execute node.js's express module
Reviewed-by: hannesw, lagergren
Diffstat (limited to 'src')
-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
6 files changed, 77 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());
}
}