Exemplo n.º 1
0
        public static EcmaValue Reject([This] EcmaValue thisValue, EcmaValue reason)
        {
            Guard.ArgumentIsObject(thisValue);
            PromiseCapability capability = PromiseCapability.CreateFromConstructor(thisValue.ToObject());

            capability.Reject(reason);
            return(capability.Promise);
        }
Exemplo n.º 2
0
        public static EcmaValue Resolve([This] EcmaValue thisValue, EcmaValue value)
        {
            Guard.ArgumentIsObject(thisValue);
            if (thisValue.Equals(value[WellKnownProperty.Constructor], EcmaValueComparison.SameValue))
            {
                return(value);
            }
            PromiseCapability capability = PromiseCapability.CreateFromConstructor(thisValue.ToObject());

            capability.Resolve(value);
            return(capability.Promise);
        }
Exemplo n.º 3
0
        internal static JsValue PerformPromiseThen(
            Engine engine,
            PromiseInstance promise,
            JsValue onFulfilled,
            JsValue onRejected,
            PromiseCapability resultCapability)
        {
            var fulfilReaction = new PromiseReaction(ReactionType.Fulfill, resultCapability, onFulfilled);
            var rejectReaction = new PromiseReaction(ReactionType.Reject, resultCapability, onRejected);

            switch (promise.State)
            {
            case PromiseState.Pending:
                promise.PromiseFulfillReactions.Add(fulfilReaction);
                promise.PromiseRejectReactions.Add(rejectReaction);
                break;

            case PromiseState.Fulfilled:
                engine.AddToEventLoop(NewPromiseReactionJob(fulfilReaction, promise.Value));

                break;

            case PromiseState.Rejected:
                engine.AddToEventLoop(NewPromiseReactionJob(rejectReaction, promise.Value));

                break;

            default:
                ExceptionHelper.ThrowArgumentOutOfRangeException();
                break;
            }

            //https://tc39.es/ecma262/#sec-performpromisethen
            //...
            //13. If resultCapability is undefined, then
            //      a. Return undefined
            //14. Else
            //      a. Return resultCapability.[[Promise]]
            if (resultCapability is null)
            {
                return(JsValue.Undefined);
            }

            return(resultCapability.PromiseInstance);
        }
Exemplo n.º 4
0
    /// <summary>
    /// https://tc39.es/ecma262/#sec-source-text-module-record-execute-module
    /// </summary>
    internal override Completion ExecuteModule(PromiseCapability capability = null)
    {
        var moduleContext = new ExecutionContext(this, _environment, _environment, null, _realm);

        if (!_hasTLA)
        {
            using (new StrictModeScope(true, force: true))
            {
                _engine.EnterExecutionContext(moduleContext);
                var statementList = new JintStatementList(null, _source.Body);
                var result        = statementList.Execute(_engine._activeEvaluationContext ?? new EvaluationContext(_engine)); //Create new evaluation context when called from e.g. module tests
                _engine.LeaveExecutionContext();
                return(result);
            }
        }
        else
        {
            ExceptionHelper.ThrowNotImplementedException("async modules not implemented");
            return(default);
Exemplo n.º 5
0
        public static EcmaValue Race([This] EcmaValue thisValue, EcmaValue iterable)
        {
            Guard.ArgumentIsObject(thisValue);
            PromiseCapability capability = PromiseCapability.CreateFromConstructor(thisValue.ToObject());

            try {
                using (EcmaIteratorEnumerator iterator = iterable.ForOf()) {
                    EcmaValue resolve = thisValue[WellKnownProperty.Resolve];
                    while (MoveNextSafe(iterator))
                    {
                        EcmaValue thenable = resolve.Call(thisValue, iterator.Current);
                        thenable.Invoke(WellKnownProperty.Then, capability.ResolveCallback, capability.RejectCallback);
                    }
                }
            } catch (Exception ex) {
                capability.Reject(EcmaValueUtility.GetValueFromException(ex));
            }
            return(capability.Promise);
        }
Exemplo n.º 6
0
        public static EcmaValue Then([This] EcmaValue thisValue, EcmaValue onfulfill, EcmaValue onreject)
        {
            Promise         promise     = thisValue.GetUnderlyingObject <Promise>();
            RuntimeObject   constructor = RuntimeObject.GetSpeciesConstructor(promise, WellKnownObject.PromiseConstructor);
            PromiseCallback c1          = null;
            PromiseCallback c2          = null;

            if (onfulfill.IsCallable)
            {
                c1 = v => onfulfill.Call(EcmaValue.Undefined, v);
            }
            if (onreject.IsCallable)
            {
                c2 = v => onreject.Call(EcmaValue.Undefined, v);
            }
            PromiseCapability capability = PromiseCapability.CreateFromConstructor(constructor);

            capability.HandlePromise(promise, c1, c2);
            return(capability.Promise);
        }
Exemplo n.º 7
0
        public static EcmaValue All([This] EcmaValue thisValue, EcmaValue iterable)
        {
            Guard.ArgumentIsObject(thisValue);
            PromiseCapability capability = PromiseCapability.CreateFromConstructor(thisValue.ToObject());
            PromiseAggregator aggregator = new AllFulfilledAggregator(capability);

            try {
                using (EcmaIteratorEnumerator iterator = iterable.ForOf()) {
                    EcmaValue resolve = thisValue[WellKnownProperty.Resolve];
                    while (MoveNextSafe(iterator))
                    {
                        EcmaValue thenable = resolve.Call(thisValue, iterator.Current);
                        thenable.Invoke(WellKnownProperty.Then, (PromiseResolver)aggregator.CreateHandler().ResolveHandler, capability.RejectCallback);
                    }
                }
                aggregator.ResolveIfConditionMet();
            } catch (Exception ex) {
                capability.Reject(EcmaValueUtility.GetValueFromException(ex));
            }
            return(capability.Promise);
        }
Exemplo n.º 8
0
 internal abstract Completion ExecuteModule(PromiseCapability capability = null);
Exemplo n.º 9
0
 public ResumeRecord(GeneratorResumeState state, EcmaValue value, PromiseCapability capability)
 {
     this.State      = state;
     this.Value      = value;
     this.Capability = capability;
 }
Exemplo n.º 10
0
 public AllSettledAggregator(PromiseCapability capability)
     : base(capability)
 {
 }
Exemplo n.º 11
0
 public AllFulfilledAggregator(PromiseCapability capability)
     : base(capability)
 {
 }