Support catch and other test fixes.
diff --git a/src/org/mozilla/javascript/IteratorLikeIterable.java b/src/org/mozilla/javascript/IteratorLikeIterable.java index acc9900..97d082a 100644 --- a/src/org/mozilla/javascript/IteratorLikeIterable.java +++ b/src/org/mozilla/javascript/IteratorLikeIterable.java
@@ -70,8 +70,15 @@ private Object nextVal; private boolean isDone; + /** + * To support using this in a standard situation, this calls the appropriate + * "next" function to manipulate this. + */ @Override public boolean hasNext() { + if (isDone) { + return false; + } Object val = next.call(cx, scope, iterator, ScriptRuntime.emptyArgs); // This will throw if "val" is not an object. // "getObjectPropNoWarn" won't, so do this as follows. @@ -96,5 +103,19 @@ } return nextVal; } + + /** + * Find out if "hasNext" returned done without invoking the function again. + */ + public boolean isDone() { + return isDone; + } + + /** + * Sometimes we need to manually set "done" for exception handling. + */ + public void setDone(boolean done) { + this.isDone = done; + } } }
diff --git a/src/org/mozilla/javascript/NativePromise.java b/src/org/mozilla/javascript/NativePromise.java index 243e148..5b2ee58 100644 --- a/src/org/mozilla/javascript/NativePromise.java +++ b/src/org/mozilla/javascript/NativePromise.java
@@ -5,12 +5,14 @@ package org.mozilla.javascript; import java.util.ArrayList; +import org.mozilla.javascript.TopLevel.NativeErrors; public class NativePromise extends ScriptableObject { - enum State {PENDING, FULFILLED, REJECTED}; - enum ReactionType {FULFILL, REJECT}; + enum State {PENDING, FULFILLED, REJECTED} + + enum ReactionType {FULFILL, REJECT} private boolean isHandled = false; private State state = State.PENDING; @@ -22,19 +24,25 @@ public static void init(Context cx, Scriptable scope, boolean sealed) { LambdaConstructor constructor = new LambdaConstructor(scope, "Promise", 1, + LambdaConstructor.CONSTRUCTOR_NEW, NativePromise::constructor ); constructor.defineConstructorMethod(scope, "resolve", 1, NativePromise::resolve); constructor.defineConstructorMethod(scope, "reject", 1, NativePromise::reject); + constructor.defineConstructorMethod(scope, "all", 1, NativePromise::all); + constructor.defineConstructorMethod(scope, "race", 1, NativePromise::race); - constructor.definePrototypeMethod(scope, "then", 1, + constructor.definePrototypeMethod(scope, "then", 2, (Context lcx, Scriptable lscope, Scriptable thisObj, Object[] args) -> { - NativePromise self = LambdaConstructor.convertThisObject(thisObj, NativePromise.class); + NativePromise self = LambdaConstructor.convertThisObject(thisObj, NativePromise.class); return self.then(lcx, lscope, args); }); + constructor.definePrototypeMethod(scope, "catch", 1, NativePromise::doCatch); - ScriptableObject.defineProperty(scope, "Promise", constructor, PERMANENT); + constructor.definePrototypeProperty(SymbolKey.TO_STRING_TAG, "Promise", DONTENUM | READONLY); + + ScriptableObject.defineProperty(scope, "Promise", constructor, DONTENUM); constructor.optimizeStorage(); if (sealed) { @@ -58,10 +66,9 @@ try { executor.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, new Object[]{resolving.resolve, resolving.reject}); - } catch (JavaScriptException je) { - resolving.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, new Object[]{je.getValue()}); } catch (RhinoException re) { - resolving.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, new Object[]{re}); + resolving.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, + new Object[]{getErrorObject(cx, scope, re)}); } return promise; @@ -72,54 +79,164 @@ return "Promise"; } + // Promise resolve private static Object resolve(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { + if (!ScriptRuntime.isObject(thisObj)) { + throw ScriptRuntime.typeError0("msg.this.not.object"); + } Object arg = (args.length > 0 ? args[0] : Undefined.instance); if (arg instanceof NativePromise) { - // If it's a NativePromise, then its constructor is our constructor - return arg; + Object argConstructor = ScriptRuntime.getObjectProp(arg, "constructor", cx, scope); + if (argConstructor == thisObj) { + return arg; + } } Capability cap = new Capability(cx, scope, thisObj); - cap.resolve.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, new Object[] { arg }); + cap.resolve.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, new Object[]{arg}); return cap.promise; } + // Promise.reject private static Object reject(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { + if (!ScriptRuntime.isObject(thisObj)) { + throw ScriptRuntime.typeError0("msg.this.not.object"); + } Object arg = (args.length > 0 ? args[0] : Undefined.instance); Capability cap = new Capability(cx, scope, thisObj); - cap.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, new Object[] { arg }); + cap.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, new Object[]{arg}); return cap.promise; } + // Promise.all + private static Object all(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { + Capability cap = new Capability(cx, scope, thisObj); + Object arg = (args.length > 0 ? args[0] : Undefined.instance); + + IteratorLikeIterable iterable; + try { + Object maybeIterable = ScriptRuntime.callIterator(arg, cx, scope); + iterable = new IteratorLikeIterable(cx, scope, maybeIterable); + } catch (RhinoException re) { + cap.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, + new Object[]{getErrorObject(cx, scope, re)}); + return cap.promise; + } + + IteratorLikeIterable.Itr iterator = iterable.iterator(); + try { + PromiseAllResolver resolver = new PromiseAllResolver(iterator, thisObj, cap); + try { + return resolver.resolve(cx, scope); + } finally { + if (!iterator.isDone()) { + iterable.close(); + } + } + } catch (RhinoException re) { + cap.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, + new Object[]{getErrorObject(cx, scope, re)}); + return cap.promise; + } + } + + // Promise.race + private static Object race(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { + Capability cap = new Capability(cx, scope, thisObj); + Object arg = (args.length > 0 ? args[0] : Undefined.instance); + + IteratorLikeIterable iterable; + try { + Object maybeIterable = ScriptRuntime.callIterator(arg, cx, scope); + iterable = new IteratorLikeIterable(cx, scope, maybeIterable); + } catch (RhinoException re) { + cap.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, + new Object[]{getErrorObject(cx, scope, re)}); + return cap.promise; + } + + IteratorLikeIterable.Itr iterator = iterable.iterator(); + try { + try { + return performRace(cx, scope, iterator, thisObj, cap); + } finally { + if (!iterator.isDone()) { + iterable.close(); + } + } + } catch (RhinoException re) { + cap.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, + new Object[]{getErrorObject(cx, scope, re)}); + return cap.promise; + } + } + + private static Object performRace(Context cx, Scriptable scope, + IteratorLikeIterable.Itr iterator, Scriptable thisObj, Capability cap) { + // Manually iterate for exception handling purposes + while (true) { + boolean hasNext; + Object nextVal = Undefined.instance; + boolean nextOk = false; + try { + hasNext = iterator.hasNext(); + if (hasNext) { + nextVal = iterator.next(); + } + nextOk = true; + } finally { + if (!nextOk) { + iterator.setDone(true); + } + } + + if (!hasNext) { + return cap.promise; + } + + // Call "resolve" to get the next promise in the chain + Callable resolve = ScriptRuntime.getPropFunctionAndThis(thisObj, "resolve", + cx, scope); + Object nextPromise = resolve + .call(cx, scope, ScriptRuntime.lastStoredScriptable(cx), + new Object[]{nextVal}); + + // And then call "then" on it. + // Logic in the resolution function ensures we don't deliver duplicate results + Callable thenFunc = ScriptRuntime.getPropFunctionAndThis(nextPromise, "then", + cx, scope); + thenFunc.call(cx, scope, ScriptRuntime.lastStoredScriptable(cx), + new Object[]{cap.resolve, cap.reject}); + } + } + + // Promise.prototype.then private Object then(Context cx, Scriptable scope, Object[] args) { Object ourNativeConstructor = ScriptableObject.getProperty(this, "constructor"); Capability capability = new Capability(cx, scope, ourNativeConstructor); Callable onFulfilled = null; if (args.length >= 1 && args[0] instanceof Callable) { - onFulfilled = (Callable)args[0]; + onFulfilled = (Callable) args[0]; } Callable onRejected = null; if (args.length >= 2 && args[1] instanceof Callable) { - onRejected = (Callable)args[1]; + onRejected = (Callable) args[1]; } + Reaction fulfillReaction = new Reaction(capability, ReactionType.FULFILL, onFulfilled); + Reaction rejectReaction = new Reaction(capability, ReactionType.REJECT, onRejected); + if (state == State.PENDING) { - if (onFulfilled != null) { - fulfillReactions.add(new Reaction(capability, ReactionType.FULFILL, onFulfilled)); - rejectReactions.add(new Reaction(capability, ReactionType.REJECT, onRejected)); - } + fulfillReactions.add(fulfillReaction); + rejectReactions.add(rejectReaction); } else if (state == State.FULFILLED) { - final Callable localFulfilled = onFulfilled; cx.enqueueMicrotask(() -> { - callReaction(cx, scope, new Reaction(capability, ReactionType.FULFILL, localFulfilled), - result); - }); + fulfillReaction.invoke(cx, scope, result); + }); } else { - assert(state == State.REJECTED); - final Callable localRejected = onRejected; + assert (state == State.REJECTED); cx.enqueueMicrotask(() -> { - callReaction(cx, scope, new Reaction(capability, ReactionType.REJECT, localRejected), - result); + rejectReaction.invoke(cx, scope, result); }); } @@ -127,6 +244,17 @@ return capability.promise; } + // Promise.prototype.catch + private static Object doCatch(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { + Object arg = (args.length > 0 ? args[0] : Undefined.instance); + // No guarantee that the caller didn't change the prototype of "then"! + Callable thenFunc = ScriptRuntime.getPropFunctionAndThis(thisObj, "then", + cx, scope); + return thenFunc.call(cx, scope, ScriptRuntime.lastStoredScriptable(cx), + new Object[]{Undefined.instance, arg}); + } + + // Abstract operation to fulfill a promise private Object fulfillPromise(Context cx, Scriptable scope, Object value) { assert (state == State.PENDING); result = value; @@ -136,14 +264,15 @@ rejectReactions = new ArrayList<>(); } state = State.FULFILLED; - for (Reaction r: reactions) { + for (Reaction r : reactions) { cx.enqueueMicrotask(() -> { - callReaction(cx, scope, r, value); + r.invoke(cx, scope, value); }); } return Undefined.instance; } + // Abstract operation to reject a promise. private Object rejectPromise(Context cx, Scriptable scope, Object reason) { assert (state == State.PENDING); result = reason; @@ -153,72 +282,64 @@ fulfillReactions = new ArrayList<>(); } state = State.REJECTED; - for (Reaction r: reactions) { + for (Reaction r : reactions) { cx.enqueueMicrotask(() -> { - callReaction(cx, scope, r, reason); + r.invoke(cx, scope, reason); }); } return Undefined.instance; } - // Promise Reaction Job - // This gets called when fulfilling and rejecting promises - private void callReaction(Context cx, Scriptable scope, Reaction reaction, Object arg) { - Object result; - if (reaction.handler == null) { - result = arg; - } else { - result = reaction.handler.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, - new Object[] { arg }); - } - if (reaction.capability != null) { - if (result instanceof JavaScriptException) { - reaction.capability.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, - new Object[]{((JavaScriptException) result).getValue()}); - } else if (result instanceof RhinoException) { - reaction.capability.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, - new Object[] { result }); - } else { - reaction.capability.resolve.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, - new Object[] { result }); - } - } - } - // Promise Resolve Thenable Job. // This gets called by the "resolving func" as a microtask. private Object callThenable(Context cx, Scriptable scope, Object resolution, Callable thenFunc) { ResolvingFunctions resolving = new ResolvingFunctions(cx, scope, this); - Scriptable thisObj = (resolution instanceof Scriptable ? (Scriptable)resolution : - Undefined.SCRIPTABLE_UNDEFINED); - Object result; + Scriptable thisObj = (resolution instanceof Scriptable ? (Scriptable) resolution : + Undefined.SCRIPTABLE_UNDEFINED); try { - result = thenFunc.call(cx, scope, thisObj, new Object[]{resolving.resolve, resolving.reject}); - } catch (JavaScriptException e) { - result = resolving.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, - new Object[]{e.getValue()}); + thenFunc.call(cx, scope, thisObj, new Object[]{resolving.resolve, resolving.reject}); } catch (RhinoException re) { - result = resolving.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, - new Object[]{re}); + resolving.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, + new Object[]{getErrorObject(cx, scope, re)}); } - // This is shown as a "Completion" in the spec - return result; + return Undefined.instance; + } + + private static Object getErrorObject(Context cx, Scriptable scope, RhinoException re) { + if (re instanceof JavaScriptException) { + return ((JavaScriptException) re).getValue(); + } + + TopLevel.NativeErrors constructor = NativeErrors.Error; + if (re instanceof EcmaError) { + EcmaError ee = (EcmaError) re; + switch (ee.getName()) { + case "TypeError": + constructor = NativeErrors.TypeError; + break; + default: + break; + } + } + return ScriptRuntime.newNativeError(cx, scope, constructor, new Object[]{re.getMessage()}); } // Output of "CreateResolvingFunctions." Carries with it an "alreadyResolved" state, - // so we make it a separate object + // so we make it a separate object. This actually fires resolution functions on + // the passed callbacks. private static class ResolvingFunctions { + private boolean alreadyResolved = false; Callable resolve; Callable reject; ResolvingFunctions(Context topCx, Scriptable topScope, NativePromise promise) { - resolve = new LambdaFunction(topScope, "stepsResolve", 1, + resolve = new LambdaFunction(topScope, 1, (Context cx, Scriptable scope, Scriptable thisObj, Object[] args) -> - resolve(cx, scope, promise, (args.length > 0 ? args[0] : Undefined.instance)) + resolve(cx, scope, promise, (args.length > 0 ? args[0] : Undefined.instance)) ); - reject = new LambdaFunction(topScope, "stepsReject", 1, + reject = new LambdaFunction(topScope, 1, (Context cx, Scriptable scope, Scriptable thisObj, Object[] args) -> reject(cx, scope, promise, (args.length > 0 ? args[0] : Undefined.instance)) ); @@ -241,7 +362,8 @@ alreadyResolved = true; if (resolution == promise) { - Object err = ScriptRuntime.typeError0("msg.promise.self.resolution"); + Object err = ScriptRuntime.newNativeError(cx, scope, + NativeErrors.TypeError, new Object[]{"No promise self-resolution"}); return promise.rejectPromise(cx, scope, err); } @@ -256,14 +378,15 @@ } cx.enqueueMicrotask(() -> { - promise.callThenable(cx, scope, resolution, (Callable)thenObj); + promise.callThenable(cx, scope, resolution, (Callable) thenObj); }); return Undefined.instance; } } - // "Promise Reaction" record + // "Promise Reaction" record. This is an input to the microtask. private static class Reaction { + Capability capability = null; ReactionType reaction = ReactionType.REJECT; Callable handler = null; @@ -273,39 +396,69 @@ this.reaction = type; this.handler = handler; } + + void invoke(Context cx, Scriptable scope, Object arg) { + try { + Object result = null; + if (handler == null) { + switch (reaction) { + case FULFILL: + result = arg; + break; + case REJECT: + capability.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, + new Object[]{arg}); + return; + } + } else { + result = handler.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, + new Object[]{arg}); + } + + capability.resolve.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, + new Object[]{result}); + + } catch (RhinoException re) { + capability.reject.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, + new Object[]{getErrorObject(cx, scope, re)}); + } + } } - // "Promise Capability Record". + // "Promise Capability Record" + // This abstracts a promise from the specific native implementation by keeping track + // of the "resolve" and "reject" functions. private static class Capability { - Object promise = Undefined.instance; + + Object promise; private Object rawResolve = Undefined.instance; - Callable resolve = null; + Callable resolve; private Object rawReject = Undefined.instance; - Callable reject = null; + Callable reject; // Given an object that represents a constructor function, execute it as if it // meets the "Promise" constructor pattern, which takes a function that will // be called with "resolve" and "reject" functions. - Capability(Context cx, Scriptable scope, Object pc) { + Capability(Context topCx, Scriptable topScope, Object pc) { if (!(pc instanceof Constructable)) { throw ScriptRuntime.typeError0("msg.constructor.expected"); } - Constructable promiseConstructor = (Constructable)pc; - LambdaFunction executorFunc = new LambdaFunction(scope, "", 2, - (Context lcx, Scriptable lscope, Scriptable thisObj, Object[] args) -> - executor(args)); + Constructable promiseConstructor = (Constructable) pc; + LambdaFunction executorFunc = new LambdaFunction(topScope, 2, + (Context cx, Scriptable scope, Scriptable thisObj, Object[] args) -> + executor(args)); - promise = promiseConstructor.construct(cx, scope, new Object[] { executorFunc }); + promise = promiseConstructor.construct(topCx, topScope, new Object[]{executorFunc}); if (!(rawResolve instanceof Callable)) { throw ScriptRuntime.typeError0("msg.function.expected"); } - resolve = (Callable)rawResolve; + resolve = (Callable) rawResolve; if (!(rawReject instanceof Callable)) { throw ScriptRuntime.typeError0("msg.function.expected"); } - reject = (Callable)rawReject; + reject = (Callable) rawReject; } private Object executor(Object[] args) { @@ -321,4 +474,105 @@ return Undefined.instance; } } + + // This object keeps track of the state necessary to execute Promise.all + private static class PromiseAllResolver { + + final ArrayList<Object> values = new ArrayList<>(); + int remainingElements = 1; + + IteratorLikeIterable.Itr iterator; + Scriptable thisObj; + Capability capability; + + PromiseAllResolver( + IteratorLikeIterable.Itr iter, Scriptable thisObj, Capability cap) { + this.iterator = iter; + this.thisObj = thisObj; + this.capability = cap; + } + + Object resolve(Context topCx, Scriptable topScope) { + int index = 0; + + // Iterate manually because we need to catch exceptions in a special way. + while (true) { + boolean hasNext; + Object nextVal = Undefined.instance; + boolean nextOk = false; + try { + hasNext = iterator.hasNext(); + if (hasNext) { + nextVal = iterator.next(); + } + nextOk = true; + } finally { + if (!nextOk) { + iterator.setDone(true); + } + } + + if (!hasNext) { + if (--remainingElements == 0) { + finalResolution(topCx, topScope); + } + return capability.promise; + } + + values.add(Undefined.instance); + + // Call "resolve" to get the next promise in the chain + Callable resolve = ScriptRuntime.getPropFunctionAndThis(thisObj, "resolve", + topCx, topScope); + Object nextPromise = resolve + .call(topCx, topScope, ScriptRuntime.lastStoredScriptable(topCx), + new Object[]{nextVal}); + + // Create a resolution func that will stash its result in the right place + PromiseElementResolver eltResolver = new PromiseElementResolver(index); + LambdaFunction resolveFunc = new LambdaFunction(topScope, 1, + (Context cx, Scriptable scope, Scriptable thisObj, Object[] args) -> + eltResolver.resolve(cx, scope, (args.length > 0 ? args[0] : Undefined.instance), + this) + ); + remainingElements++; + + // Call "then" on the promise with the resolution func + Callable thenFunc = ScriptRuntime.getPropFunctionAndThis(nextPromise, "then", + topCx, topScope); + thenFunc.call(topCx, topScope, ScriptRuntime.lastStoredScriptable(topCx), + new Object[]{resolveFunc, capability.reject}); + index++; + } + } + + void finalResolution(Context cx, Scriptable scope) { + Scriptable newArray = cx.newArray(scope, values.toArray()); + capability.resolve.call(cx, scope, Undefined.SCRIPTABLE_UNDEFINED, new Object[]{newArray}); + } + } + + // This object keeps track of the state necessary to resolve one element in Promise.all + private static class PromiseElementResolver { + + private boolean alreadyCalled = false; + private final int index; + + PromiseElementResolver(int ix) { + this.index = ix; + } + + Object resolve(Context cx, Scriptable scope, Object result, + PromiseAllResolver resolver) { + if (alreadyCalled) { + return Undefined.instance; + } + alreadyCalled = true; + resolver.values.set(index, result); + if (--resolver.remainingElements == 0) { + resolver.finalResolution(cx, scope); + } + return Undefined.instance; + } + } }
diff --git a/src/org/mozilla/javascript/ScriptRuntime.java b/src/org/mozilla/javascript/ScriptRuntime.java index 0ee72f1..bcacf8d 100644 --- a/src/org/mozilla/javascript/ScriptRuntime.java +++ b/src/org/mozilla/javascript/ScriptRuntime.java
@@ -1045,7 +1045,7 @@ public static Scriptable toObject(Scriptable scope, Object val) { - if (val instanceof Scriptable) { + if ((val instanceof Scriptable) && !Undefined.isUndefined(val)) { return (Scriptable)val; } return toObject(Context.getContext(), scope, val); @@ -1060,9 +1060,12 @@ @Deprecated public static Scriptable toObjectOrNull(Context cx, Object obj) { + if (Undefined.isUndefined(obj)) { + return null; + } if (obj instanceof Scriptable) { return (Scriptable)obj; - } else if (obj != null && obj != Undefined.instance) { + } else if (obj != null) { return toObject(cx, getTopCallScope(cx), obj); } return null; @@ -1074,9 +1077,12 @@ public static Scriptable toObjectOrNull(Context cx, Object obj, Scriptable scope) { + if (Undefined.isUndefined(obj)) { + return null; + } if (obj instanceof Scriptable) { return (Scriptable)obj; - } else if (obj != null && obj != Undefined.instance) { + } else if (obj != null) { return toObject(cx, scope, obj); } return null; @@ -1089,7 +1095,7 @@ public static Scriptable toObject(Scriptable scope, Object val, Class<?> staticClass) { - if (val instanceof Scriptable) { + if ((val instanceof Scriptable) && !Undefined.isUndefined(val)) { return (Scriptable)val; } return toObject(Context.getContext(), scope, val); @@ -2916,7 +2922,7 @@ return "object".equals(type) || "function".equals(type); } if (value instanceof Scriptable) { - return (!(value instanceof Callable)); + return (!(value instanceof Callable) && !(value instanceof Symbol)); } return false; }
diff --git a/src/org/mozilla/javascript/ScriptableObject.java b/src/org/mozilla/javascript/ScriptableObject.java index 954ecae..be2311b 100644 --- a/src/org/mozilla/javascript/ScriptableObject.java +++ b/src/org/mozilla/javascript/ScriptableObject.java
@@ -1687,6 +1687,20 @@ so.defineProperty(propertyName, value, attributes); } + public static void defineProperty(Scriptable destination, + Symbol propertyKey, Object value, + int attributes) + { + if (destination instanceof ScriptableObject) { + ScriptableObject so = (ScriptableObject)destination; + so.defineProperty(propertyKey, value, attributes); + } else if (destination instanceof SymbolScriptable) { + ((SymbolScriptable) destination).put(propertyKey, destination, value); + } else { + throw ScriptRuntime.typeError0("msg.bad.prop"); + } + } + /** * Utility method to add properties to arbitrary Scriptable object. * If destination is instance of ScriptableObject, calls
diff --git a/src/org/mozilla/javascript/resources/Messages.properties b/src/org/mozilla/javascript/resources/Messages.properties index 0d76cb0..fe06e64 100644 --- a/src/org/mozilla/javascript/resources/Messages.properties +++ b/src/org/mozilla/javascript/resources/Messages.properties
@@ -905,11 +905,12 @@ msg.constructor.expected =\ Expecting the first argument to be a constructor -msg.promise.self.resolution =\ - Promise is being self-resolved - -msg.this.not.instance \= - "this" is not an instance of the class +msg.this.not.instance \= \"this\" is not an instance of the class msg.promise.capability.state \= Invalid promise capability state + +msg.constructor.no.function \= + The constructor for {0} may not be invoked as a function + +msg.this.not.object \= "this" is not an object \ No newline at end of file
diff --git a/testsrc/jstests/es6/promises.js b/testsrc/jstests/es6/promises.js index eec5d19..13c7b4f 100644 --- a/testsrc/jstests/es6/promises.js +++ b/testsrc/jstests/es6/promises.js
@@ -103,7 +103,7 @@ function assertAsync(b, s) { if (b) { - print(s, "succeeded") + //print(s, "succeeded") } else { AbortJS(s + " FAILED!") // Simply throwing here will have no effect. } @@ -116,7 +116,7 @@ var iterations = 0; function runAssertion() { if (f()) { - print(name, "succeeded"); + //print(name, "succeeded"); --asyncAssertsExpected; } else if (iterations++ < 10) { EnqueueMicrotask(runAssertion); @@ -139,11 +139,9 @@ }); } -/* TODO Rhino requires new (function() { assertThrows(function() { Promise(function() {}) }, TypeError) })(); -*/ (function() { assertTrue(new Promise(function() {}) instanceof Promise) @@ -551,7 +549,8 @@ deferred.resolve(p) p.then( assertUnreachable, - function(r) { assertAsync(r instanceof TypeError, "cyclic/deferred/then") } + function(r) { + assertAsync(r instanceof TypeError, "cyclic/deferred/then") } ) assertAsyncRan() })(); @@ -562,7 +561,7 @@ deferred.resolve(p) p.then( assertUnreachable, - function(r) { assertAsync(r instanceof TypeError, "cyclic/deferred/then") } + function(r) { assertAsync(r instanceof TypeError, "cyclic/deferred/then 2") } ) assertAsyncRan() })(); @@ -589,18 +588,19 @@ testPromiseAllNonIterable({}); testPromiseAllNonIterable(42); })(); -/**/ -/* TODO Rhino syntax + (function() { - Promise.all({[symbolIterator](){ return null; }}).then( + // Rhino slightly different syntax works + let o = {}; + o[Symbol.iterator] = function() { return null; }; + Promise.all(o).then( assertUnreachable, function(r) { assertAsync(r instanceof TypeError, 'all/non iterable'); }); assertAsyncRan(); })(); -*/ -/* + (function() { var deferred = defer(Promise); var p = deferred.promise; @@ -678,7 +678,7 @@ deferred2.reject(2) assertAsyncRan() })(); - +/* TODO Rhino need to keep working on strict mode (function() { 'use strict'; var getCalls = 0; @@ -722,7 +722,7 @@ assertAsyncRan(); assertAsyncRan(); })(); - +*/ (function() { Promise.race([]).then( @@ -873,6 +873,7 @@ assertAsyncRan() })(); +/* TODO Rhino need to keep working on strict mode (function() { 'use strict'; var getCalls = 0; @@ -910,7 +911,9 @@ assertEquals(nextCalls, 3 + 1); // + 1 for {done: true} assertAsyncRan(); })(); +*/ +/* TODO Rhino need to keep working on subclassing. (function() { var log function MyPromise(resolver) { @@ -974,6 +977,7 @@ "subclass/all/self") })(); */ + /* TODO Rhino no classes yet (function() { 'use strict'; @@ -998,7 +1002,7 @@ "subclass/resolve/descendant with transplanted own constructor"); }()); */ -/* + (function() { var thenCalled = false; @@ -1033,7 +1037,7 @@ p.then(); assertEquals(1, callCount); })(); -*/ + assertAsyncDone() 'success';
diff --git a/testsrc/org/mozilla/javascript/tests/Test262SuiteTest.java b/testsrc/org/mozilla/javascript/tests/Test262SuiteTest.java index 54230e7..b62ca80 100644 --- a/testsrc/org/mozilla/javascript/tests/Test262SuiteTest.java +++ b/testsrc/org/mozilla/javascript/tests/Test262SuiteTest.java
@@ -186,7 +186,7 @@ boolean failedEarly = true; try { - Script caseScript = cx.compileString(str, testFilePath, 0, null); + Script caseScript = cx.compileString(str, testFilePath, 1, null); failedEarly = false; // not after this line caseScript.exec(cx, scope);
diff --git a/testsrc/test262.properties b/testsrc/test262.properties index 055a0ea..7a53812 100644 --- a/testsrc/test262.properties +++ b/testsrc/test262.properties
@@ -555,7 +555,39 @@ ! name.js ! S15.1.2.2_A9.2.js -# built-ins/Promise +built-ins/Promise + ! length.js + ! name.js + ! Symbol.species/length.js + ! Symbol.species/name.js + ! Symbol.species/return-value.js + ! Symbol.species/symbol-species-name.js + ! Symbol.species/symbol-species.js + ! Symbol.species/prop-desc.js + ! reject/length.js + ! reject/name.js + ! resolve/length.js + ! resolve/name.js + ! executor-function-name.js + ! all/length.js + ! all/name.js + ! prototype/prop-desc.js + ! prototype/then/length.js + ! prototype/then/name.js + ! prototype/then/prop-desc.js + ! prototype/then/ctor-throws.js + ! prototype/catch/length.js + ! prototype/catch/name.js + ! prototype/catch/prop-desc.js + ! resolve-function-name.js + ! resolve-function-length.js + ! reject-function-name.js + ! reject-function-length.js + ! race/length.js + ! race/name.js +# Pretty sure that our version of test262 is wrong on "this" in these: + ! reject/capability-invocation.js + ! resolve/resolve-from-promise-capability.js # built-ins/Proxy