Пример #1
0
 private static bool MoveNextSafe(EcmaIteratorEnumerator iterator)
 {
     try {
         return(iterator.MoveNext());
     } catch {
         // do not explicitly close iterator when abrupt completion from getting next value
         // mark iterator done when exception throw from iterator.MoveNext()
         iterator.Done();
         throw;
     }
 }
Пример #2
0
        public static StatefulIterator GetAsyncIterator(this RuntimeObject obj)
        {
            Guard.ArgumentNotNull(obj, "obj");
            RuntimeObject method = obj.GetMethod(Symbol.AsyncIterator);

            if (method == null)
            {
                EcmaIteratorEnumerator syncIterator = GetIterator(obj);
                return(new AsyncFromSyncIterator(syncIterator));
            }
            EcmaValue iterator = method.Call(obj);

            Guard.ArgumentIsObject(iterator);
            return(iterator.GetUnderlyingObject <AsyncGenerator>());
        }
Пример #3
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);
        }
Пример #4
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);
        }