|
| 1 | +using Jint.Native.Iterator; |
| 2 | +using Jint.Native.Object; |
| 3 | +using Jint.Runtime; |
| 4 | +using Jint.Runtime.Environments; |
| 5 | +using Jint.Runtime.Interpreter; |
| 6 | + |
| 7 | +namespace Jint.Native.Generator; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// https://tc39.es/ecma262/#sec-properties-of-generator-instances |
| 11 | +/// </summary> |
| 12 | +internal sealed class GeneratorInstance : ObjectInstance |
| 13 | +{ |
| 14 | + internal GeneratorState _generatorState; |
| 15 | + private ExecutionContext _generatorContext; |
| 16 | + private readonly JsValue? _generatorBrand = null; |
| 17 | + private JintStatementList _generatorBody = null!; |
| 18 | + |
| 19 | + public GeneratorInstance(Engine engine) : base(engine) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// https://tc39.es/ecma262/#sec-generatorstart |
| 25 | + /// </summary> |
| 26 | + public JsValue GeneratorStart(JintStatementList generatorBody) |
| 27 | + { |
| 28 | + var genContext = _engine.UpdateGenerator(this); |
| 29 | + _generatorBody = generatorBody; |
| 30 | + |
| 31 | + _generatorContext = genContext; |
| 32 | + _generatorState = GeneratorState.SuspendedStart; |
| 33 | + |
| 34 | + return Undefined; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// https://tc39.es/ecma262/#sec-generatorresume |
| 39 | + /// </summary> |
| 40 | + public JsValue GeneratorResume(JsValue value, JsValue? generatorBrand) |
| 41 | + { |
| 42 | + var state = GeneratorValidate(generatorBrand); |
| 43 | + if (state == GeneratorState.Completed) |
| 44 | + { |
| 45 | + return new IteratorResult(_engine, value, JsBoolean.True); |
| 46 | + } |
| 47 | + |
| 48 | + var genContext = _generatorContext; |
| 49 | + var methodContext = _engine.ExecutionContext; |
| 50 | + |
| 51 | + // 6. Suspend methodContext. |
| 52 | + |
| 53 | + var context = _engine._activeEvaluationContext; |
| 54 | + return ResumeExecution(genContext, context!); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// https://tc39.es/ecma262/#sec-generatorresumeabrupt |
| 59 | + /// </summary> |
| 60 | + public JsValue GeneratorResumeAbrupt(in Completion abruptCompletion, JsValue? generatorBrand) |
| 61 | + { |
| 62 | + var state = GeneratorValidate(generatorBrand); |
| 63 | + if (state == GeneratorState.SuspendedStart) |
| 64 | + { |
| 65 | + _generatorState = GeneratorState.Completed; |
| 66 | + state = GeneratorState.Completed; |
| 67 | + } |
| 68 | + |
| 69 | + if (state == GeneratorState.Completed) |
| 70 | + { |
| 71 | + if (abruptCompletion.Type == CompletionType.Return) |
| 72 | + { |
| 73 | + return new IteratorResult(_engine, abruptCompletion.Value, JsBoolean.True); |
| 74 | + } |
| 75 | + |
| 76 | + return abruptCompletion.Value; |
| 77 | + } |
| 78 | + |
| 79 | + var genContext = _generatorContext; |
| 80 | + var methodContext = _engine.ExecutionContext; |
| 81 | + |
| 82 | + // Suspend methodContext. |
| 83 | + |
| 84 | + return ResumeExecution(genContext, new EvaluationContext(_engine, abruptCompletion)); |
| 85 | + } |
| 86 | + |
| 87 | + private JsValue ResumeExecution(ExecutionContext genContext, EvaluationContext context) |
| 88 | + { |
| 89 | + _generatorState = GeneratorState.Executing; |
| 90 | + _engine.EnterExecutionContext(genContext); |
| 91 | + |
| 92 | + var result = _generatorBody.Execute(context); |
| 93 | + _engine.LeaveExecutionContext(); |
| 94 | + |
| 95 | + ObjectInstance? resultValue = null; |
| 96 | + if (result.Type == CompletionType.Normal) |
| 97 | + { |
| 98 | + _generatorState = GeneratorState.Completed; |
| 99 | + var value = context.ResumedCompletion.GetValueOrDefault(); |
| 100 | + resultValue = IteratorInstance.ValueIteratorPosition.Done(_engine, value); |
| 101 | + } |
| 102 | + else if (result.Type == CompletionType.Return) |
| 103 | + { |
| 104 | + resultValue = new IteratorInstance.ValueIteratorPosition(_engine, result.Value, false); |
| 105 | + if (_generatorBody.Completed) |
| 106 | + { |
| 107 | + _generatorState = GeneratorState.Completed; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (result.Type == CompletionType.Throw) |
| 112 | + { |
| 113 | + _generatorState = GeneratorState.Completed; |
| 114 | + ExceptionHelper.ThrowJavaScriptException(_engine, result.Value, result); |
| 115 | + } |
| 116 | + |
| 117 | + return resultValue!; |
| 118 | + } |
| 119 | + |
| 120 | + private GeneratorState GeneratorValidate(JsValue? generatorBrand) |
| 121 | + { |
| 122 | + if (!ReferenceEquals(generatorBrand, _generatorBrand)) |
| 123 | + { |
| 124 | + ExceptionHelper.ThrowTypeError(_engine.Realm, "Generator brand differs from attached brand"); |
| 125 | + } |
| 126 | + |
| 127 | + if (_generatorState == GeneratorState.Executing) |
| 128 | + { |
| 129 | + ExceptionHelper.ThrowTypeError(_engine.Realm, "Generator state was unexpectedly executing"); |
| 130 | + } |
| 131 | + |
| 132 | + return _generatorState; |
| 133 | + } |
| 134 | +} |
0 commit comments