Skip to content

Commit

Permalink
some star yielding
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Nov 19, 2021
1 parent 9b47695 commit aeea811
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Jint/Native/JsValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal JsValue(InternalTypes type)

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal IIterator GetIterator(Realm realm, GeneratorKind hint = GeneratorKind.Sync, ICallable method = null)
internal IteratorInstance GetIterator(Realm realm, GeneratorKind hint = GeneratorKind.Sync, ICallable method = null)
{
if (!TryGetIterator(realm, out var iterator, hint, method))
{
Expand All @@ -49,7 +49,7 @@ internal IIterator GetIterator(Realm realm, GeneratorKind hint = GeneratorKind.S

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool TryGetIterator(Realm realm, out IIterator iterator, GeneratorKind hint = GeneratorKind.Sync, ICallable method = null)
internal bool TryGetIterator(Realm realm, out IteratorInstance iterator, GeneratorKind hint = GeneratorKind.Sync, ICallable method = null)
{
var obj = TypeConverter.ToObject(realm, this);

Expand Down Expand Up @@ -84,7 +84,7 @@ internal bool TryGetIterator(Realm realm, out IIterator iterator, GeneratorKind
ExceptionHelper.ThrowTypeError(realm, "Result of the Symbol.iterator method is not an object");
}

if (iteratorResult is IIterator i)
if (iteratorResult is IteratorInstance i)
{
iterator = i;
}
Expand Down
55 changes: 35 additions & 20 deletions Jint/Runtime/Interpreter/Expressions/JintYieldExpression.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Esprima.Ast;
using Jint.Native;
using Jint.Native.Generator;
using Jint.Native.Iterator;
using Jint.Native.Object;

namespace Jint.Runtime.Interpreter.Expressions
Expand Down Expand Up @@ -33,21 +34,20 @@ protected override ExpressionResult EvaluateInternal(EvaluationContext context)

if (expression.Delegate)
{
ExceptionHelper.ThrowNotImplementedException();
// TODO return YieldDelegate(value);
value = YieldDelegate(value);
}

return new ExpressionResult(ExpressionCompletionType.Return, Yield(value), _expression.Location);
}

/*
/// <summary>
/// https://tc39.es/ecma262/#sec-generator-function-definitions-runtime-semantics-evaluation
/// </summary>
private object YieldDelegate(JsValue value)
private JsValue YieldDelegate(JsValue value)
{
var generatorKind = _engine.ExecutionContext.GetGeneratorKind();
var iterator = value.GetIterator(_engine.Realm, generatorKind);
var iteratorRecord = iterator;
var received = new Completion(CompletionType.Normal, JsValue.Undefined, default);
while (true)
{
Expand Down Expand Up @@ -83,9 +83,9 @@ private object YieldDelegate(JsValue value)
else if (received.Type == CompletionType.Throw)
{
var throwMethod = iterator.GetMethod("throw");
if (!throwMethod.IsUndefined())
if (throwMethod is not null)
{
var innerResult = throwMethod.Call(iterator, « received.[[Value]] »));
var innerResult = throwMethod.Call(iterator, new[]{ received.Value });
if (generatorKind == GeneratorKind.Async)
{
innerResult = Await(innerResult);
Expand All @@ -97,8 +97,8 @@ private object YieldDelegate(JsValue value)
ExceptionHelper.ThrowTypeError(_engine.Realm);
}

done = IteratorComplete(innerResult);
if (done is true)
var done = IteratorComplete(innerResult);
if (done)
{
IteratorValue(innerResult);
}
Expand All @@ -119,11 +119,11 @@ private object YieldDelegate(JsValue value)
var closeCompletion = new Completion(CompletionType.Normal, null, "", _expression.Location);
if (generatorKind == GeneratorKind.Async)
{
AsyncIteratorClose(iteratorRecord, closeCompletion);
AsyncIteratorClose(iteratorRecord, CompletionType.Normal);
}
else
{
IteratorClose(iteratorRecord, closeCompletion);
iteratorRecord.Close(CompletionType.Normal);
}

ExceptionHelper.ThrowTypeError(_engine.Realm, "Iterator does not have close method");
Expand All @@ -134,12 +134,13 @@ private object YieldDelegate(JsValue value)
var returnMethod = iterator.GetMethod("return");
if (returnMethod is null)
{
var temp = received.Value;
if (generatorKind == GeneratorKind.Async)
{
received.Value = Await(received.Value);
temp = Await(received.Value);
}

return received;
return temp;
}

var innerReturnResult = returnMethod.Call(iterator, received.Value);
Expand All @@ -156,8 +157,8 @@ private object YieldDelegate(JsValue value)
var done = IteratorComplete(innerReturnResult);
if (done)
{
var value = IteratorValue(innerReturnResult);
return new Completion(CompletionType.Return, value, "", _expression.Location);
var val = IteratorValue(innerReturnResult);
return val;
}

if (generatorKind == GeneratorKind.Async)
Expand All @@ -167,30 +168,44 @@ private object YieldDelegate(JsValue value)
else
{
received = GeneratorYield(innerReturnResult);
}
}
}
}
*/
private void AsyncIteratorClose(object iteratorRecord, Completion closeCompletion)

private Completion GeneratorYield(JsValue innerResult)
{
throw new System.NotImplementedException();
}

private static bool IteratorComplete(JsValue iterResult)
{
return TypeConverter.ToBoolean(iterResult.Get(CommonProperties.Done));
}

private static JsValue IteratorValue(JsValue iterResult)
{
return iterResult.Get(CommonProperties.Value);
}

private void AsyncIteratorClose(object iteratorRecord, CompletionType closeCompletion)
{
ExceptionHelper.ThrowNotImplementedException("async");
}

/// <summary>
/// https://tc39.es/ecma262/#sec-asyncgeneratoryield
/// </summary>
private object AsyncGeneratorYield(object iteratorValue)
private Completion AsyncGeneratorYield(object iteratorValue)
{
ExceptionHelper.ThrowNotImplementedException("async");
return null;
return default;
}

/// <summary>
/// https://tc39.es/ecma262/#await
/// </summary>
private ObjectInstance Await(ObjectInstance innerResult)
private ObjectInstance Await(JsValue innerResult)
{
ExceptionHelper.ThrowNotImplementedException("await");
return null;
Expand Down

0 comments on commit aeea811

Please sign in to comment.