Ejemplo n.º 1
0
        public bool MoveNext()
        {
            GeneratorState currentState = this.Iterator.State;

            switch (currentState)
            {
            case GeneratorState.Closed:
            case GeneratorState.AwaitingReturn:
                this.Current = EcmaValue.Undefined;
                return(false);
            }
            GeneratorResumeState resumeState = this.Context.ResumeState;

            try {
                this.Current = GetNextValue(resumeState, this.Context.ResumeValue);
            } catch (GeneratorClosedException ex) {
                this.Current = ex.ReturnValue;
                if (resumeState != GeneratorResumeState.Return)
                {
                    return(false);
                }
                throw;
            }
            return(resumeState != GeneratorResumeState.Resume || this.Iterator.State != GeneratorState.Closed);
        }
Ejemplo n.º 2
0
        public override EcmaValue Resume(GeneratorResumeState state, EcmaValue value)
        {
            PromiseCapability capability = PromiseCapability.Create();
            ResumeRecord      record     = new ResumeRecord(state, value, capability);

            queue.Enqueue(record);
            if (this.State != GeneratorState.Running)
            {
                ResumeNext();
            }
            return(capability.Promise);
        }
Ejemplo n.º 3
0
        EcmaValue IGeneratorEnumerable.Resume(GeneratorResumeState state, EcmaValue value)
        {
            switch (state)
            {
            case GeneratorResumeState.Return:
                throw new GeneratorClosedException(value);

            case GeneratorResumeState.Throw:
                throw EcmaException.FromValue(value);
            }
            if (MoveNext())
            {
                return(EcmaValueUtility.CreateIterResultObject(this.Current, false));
            }
            return(EcmaValueUtility.CreateIterResultObject(EcmaValue.Undefined, true));
        }
Ejemplo n.º 4
0
        public override EcmaValue Resume(GeneratorResumeState state, EcmaValue value)
        {
            if (this.State == GeneratorState.Running)
            {
                throw new EcmaTypeErrorException(InternalString.Error.GeneratorRunning);
            }
            nextState = state;
            if (this.State != GeneratorState.SuspendedYield)
            {
                switch (state)
                {
                case GeneratorResumeState.Throw:
                    Close();
                    throw EcmaException.FromValue(value);

                case GeneratorResumeState.Return:
                    Close();
                    return(EcmaValueUtility.CreateIterResultObject(value, true));
                }
            }
            else
            {
                resumeValue = value;
            }
            if (this.State != GeneratorState.Closed)
            {
                try {
                    this.State = GeneratorState.Running;
                    if (TryYieldNext(out EcmaValue result))
                    {
                        return(EcmaValueUtility.CreateIterResultObject(result, false));
                    }
                } catch {
                    if (nextState != GeneratorResumeState.Return)
                    {
                        Close();
                        throw;
                    }
                } finally {
                    if (this.State != GeneratorState.Closed)
                    {
                        this.State = GeneratorState.SuspendedYield;
                    }
                }
                Close();
            }
            return(EcmaValueUtility.CreateIterResultObject(nextState == GeneratorResumeState.Return ? resumeValue : default, true));
Ejemplo n.º 5
0
        public override EcmaValue Resume(GeneratorResumeState state, EcmaValue value)
        {
            if (this.State != GeneratorState.Closed)
            {
                this.State = GeneratorState.Running;
            }
            try {
                EcmaValue result = default;
                bool      done   = true;
                switch (state)
                {
                case GeneratorResumeState.Resume:
                    done   = !iterator.MoveNext(value);
                    result = iterator.Current;
                    break;

                case GeneratorResumeState.Return:
                    if (!TryGetResult(WellKnownProperty.Return, value, out done, out result))
                    {
                        this.State = GeneratorState.Closed;
                        return(Promise.Resolve(EcmaValueUtility.CreateIterResultObject(value, true)));
                    }
                    break;

                case GeneratorResumeState.Throw:
                    if (!TryGetResult(WellKnownProperty.Throw, value, out done, out result))
                    {
                        this.State = GeneratorState.Closed;
                        return(Promise.Reject(value));
                    }
                    break;
                }
                if (done)
                {
                    this.State = GeneratorState.Closed;
                }
                return(Promise.Resolve(result, new ResultHandler(done).CreateIterResultObject, null));
            } catch (Exception ex) {
                return(Promise.Reject(ex));
            }
        }
Ejemplo n.º 6
0
 public abstract EcmaValue Resume(GeneratorResumeState state, EcmaValue value);
Ejemplo n.º 7
0
 protected virtual EcmaValue GetNextValue(GeneratorResumeState state, EcmaValue value)
 {
     return(UnwrapResult(this.Iterator.Resume(state, value)));
 }
Ejemplo n.º 8
0
 public ResumeRecord(GeneratorResumeState state, EcmaValue value, PromiseCapability capability)
 {
     this.State      = state;
     this.Value      = value;
     this.Capability = capability;
 }
Ejemplo n.º 9
0
 protected override EcmaValue GetNextValue(GeneratorResumeState state, EcmaValue value)
 {
     return(Promise.Resolve(this.Iterator.Resume(state, value), UnwrapResult, null));
 }