Skip to content

Commit

Permalink
Upgrade test262 suite and fix issues
Browse files Browse the repository at this point in the history
* create shims for async generators
* add initial version of Math.sumPrecise which won't pass all the tests (insane as usual)
* harmonize function expression building
  • Loading branch information
lahma committed Jul 9, 2024
1 parent 670d4ee commit 9a67250
Show file tree
Hide file tree
Showing 13 changed files with 592 additions and 189 deletions.
5 changes: 4 additions & 1 deletion Jint.Tests.Test262/Test262Harness.settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"SuiteGitSha": "c3a326ace810e7c80a4e1b8df8c8b704ed223c28",
"SuiteGitSha": "b8cb40b66a61afd57550a84f4170e16ebfbd1e46",
//"SuiteDirectory": "//mnt/c/work/test262",
"TargetPath": "./Generated",
"Namespace": "Jint.Tests.Test262",
Expand All @@ -11,11 +11,14 @@
"decorators",
"import-assertions",
"iterator-helpers",
"Math.sumPrecise",
"regexp-lookbehind",
"regexp-modifiers",
"regexp-unicode-property-escapes",
"regexp-v-flag",
"source-phase-imports",
"tail-call-optimization",
"uint8array-base64",
"Temporal",
"u180e"
],
Expand Down
21 changes: 0 additions & 21 deletions Jint/IsExternalInit.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Jint/Jint.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>

<PolySharpExcludeGeneratedTypes>System.Runtime.CompilerServices.IsExternalInit;System.Runtime.CompilerServices.RequiresLocationAttribute</PolySharpExcludeGeneratedTypes>
<PolySharpExcludeGeneratedTypes>System.Runtime.CompilerServices.RequiresLocationAttribute</PolySharpExcludeGeneratedTypes>
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>

</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Array/ArrayOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public override void EnsureCapacity(ulong capacity)

public override bool TryGetValue(ulong index, out JsValue value)
{
if (index < _target.GetLength())
if (_target.IsValidIntegerIndex(index))
{
value = _target[(int) index];
return true;
Expand Down
47 changes: 47 additions & 0 deletions Jint/Native/AsyncGenerator/AsyncGeneratorFunctionConstructor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Jint.Native.AsyncFunction;
using Jint.Native.Function;
using Jint.Native.Iterator;
using Jint.Native.Object;
using Jint.Runtime;
using Jint.Runtime.Descriptors;

namespace Jint.Native.Generator;

/// <summary>
/// https://tc39.es/ecma262/#sec-asyncgeneratorfunction-constructor
/// </summary>
internal sealed class AsyncGeneratorFunctionConstructor : Constructor
{
private static readonly JsString _functionName = new("AsyncGeneratorFunction");

internal AsyncGeneratorFunctionConstructor(
Engine engine,
Realm realm,
AsyncFunctionPrototype prototype,
IteratorPrototype iteratorPrototype)
: base(engine, realm, _functionName)
{
PrototypeObject = new AsyncGeneratorFunctionPrototype(engine, this, prototype, iteratorPrototype);
_prototype = PrototypeObject;
_prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
_length = new PropertyDescriptor(JsNumber.PositiveOne, PropertyFlag.Configurable);
}

public AsyncGeneratorFunctionPrototype PrototypeObject { get; }

protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
{
return Construct(arguments, thisObject);
}

public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
{
var function = _realm.Intrinsics.Function.CreateDynamicFunction(
this,
newTarget,
FunctionKind.AsyncGenerator,
arguments);

return function;
}
}
44 changes: 44 additions & 0 deletions Jint/Native/AsyncGenerator/AsyncGeneratorFunctionPrototype.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Jint.Collections;
using Jint.Native.AsyncFunction;
using Jint.Native.Iterator;
using Jint.Native.Symbol;
using Jint.Runtime;
using Jint.Runtime.Descriptors;

namespace Jint.Native.Generator;

/// <summary>
/// https://tc39.es/ecma262/#sec-properties-of-asyncgeneratorfunction-prototype
/// </summary>
internal sealed class AsyncGeneratorFunctionPrototype : Prototype
{
private readonly AsyncGeneratorFunctionConstructor? _constructor;

internal AsyncGeneratorFunctionPrototype(
Engine engine,
AsyncGeneratorFunctionConstructor constructor,
AsyncFunctionPrototype prototype,
IteratorPrototype iteratorPrototype) : base(engine, engine.Realm)
{
_constructor = constructor;
_prototype = prototype;
PrototypeObject = new AsyncGeneratorPrototype(engine, this, iteratorPrototype);
}

public AsyncGeneratorPrototype PrototypeObject { get; }

protected override void Initialize()
{
var properties = new PropertyDictionary(2, checkExistingKeys: false)
{
[KnownKeys.Constructor] = new PropertyDescriptor(_constructor, PropertyFlag.Configurable),
[KnownKeys.Prototype] = new PropertyDescriptor(PrototypeObject, PropertyFlag.Configurable)
};
SetProperties(properties);
var symbols = new SymbolDictionary(1)
{
[GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("AsyncGeneratorFunction", PropertyFlag.Configurable)
};
SetSymbols(symbols);
}
}
89 changes: 89 additions & 0 deletions Jint/Native/AsyncGenerator/AsyncGeneratorPrototype.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using Jint.Collections;
using Jint.Native.Iterator;
using Jint.Native.Object;
using Jint.Native.Symbol;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Interop;

namespace Jint.Native.Generator;

/// <summary>
/// https://tc39.es/ecma262/#sec-asyncgenerator-objects
/// </summary>
internal sealed class AsyncGeneratorPrototype : ObjectInstance
{
private readonly AsyncGeneratorFunctionPrototype _constructor;

internal AsyncGeneratorPrototype(
Engine engine,
AsyncGeneratorFunctionPrototype constructor,
IteratorPrototype iteratorPrototype) : base(engine)
{
_constructor = constructor;
_prototype = iteratorPrototype;
}

protected override void Initialize()
{
const PropertyFlag PropertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
const PropertyFlag LengthFlags = PropertyFlag.Configurable;
var properties = new PropertyDictionary(4, false)
{
["constructor"] = new(_constructor, PropertyFlag.Configurable),
["next"] = new(new ClrFunction(Engine, "next", Next, 1, LengthFlags), PropertyFlags),
["return"] = new(new ClrFunction(Engine, "return", Return, 1, LengthFlags), PropertyFlags),
["throw"] = new(new ClrFunction(Engine, "throw", Throw, 1, LengthFlags), PropertyFlags)
};
SetProperties(properties);

var symbols = new SymbolDictionary(1)
{
[GlobalSymbolRegistry.ToStringTag] = new("Generator", PropertyFlag.Configurable)
};
SetSymbols(symbols);
}

/// <summary>
/// https://tc39.es/ecma262/#sec-generator.prototype.next
/// </summary>
private ObjectInstance Next(JsValue thisObject, JsValue[] arguments)
{
var g = AssertGeneratorInstance(thisObject);
var value = arguments.At(0, null!);
return g.GeneratorResume(value, null);
}

/// <summary>
/// https://tc39.es/ecma262/#sec-generator.prototype.return
/// </summary>
private JsValue Return(JsValue thisObject, JsValue[] arguments)
{
var g = AssertGeneratorInstance(thisObject);
var value = arguments.At(0);
var C = new Completion(CompletionType.Return, value, null!);
return g.GeneratorResumeAbrupt(C, null);
}

/// <summary>
/// https://tc39.es/ecma262/#sec-generator.prototype.throw
/// </summary>
private JsValue Throw(JsValue thisObject, JsValue[] arguments)
{
var g = AssertGeneratorInstance(thisObject);
var exception = arguments.At(0);
var C = new Completion(CompletionType.Throw, exception, null!);
return g.GeneratorResumeAbrupt(C, null);
}

private GeneratorInstance AssertGeneratorInstance(JsValue thisObj)
{
var generatorInstance = thisObj as GeneratorInstance;
if (generatorInstance is null)
{
ExceptionHelper.ThrowTypeError(_engine.Realm, "object must be a Generator instance");
}

return generatorInstance;
}
}
12 changes: 6 additions & 6 deletions Jint/Native/Function/FunctionInstance.Dynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ internal Function CreateDynamicFunction(
fallbackProto = static intrinsics => intrinsics.GeneratorFunction.PrototypeObject;
break;
case FunctionKind.AsyncGenerator:
fallbackProto = static intrinsics => intrinsics.AsyncGeneratorFunction.PrototypeObject;
break;
default:
ExceptionHelper.ThrowArgumentOutOfRangeException(nameof(kind), kind.ToString());
break;
Expand Down Expand Up @@ -91,7 +93,7 @@ internal Function CreateDynamicFunction(
functionExpression = "async function f(){}";
break;
case FunctionKind.AsyncGenerator:
ExceptionHelper.ThrowNotImplementedException("Async generators not implemented");
functionExpression = "async function* f(){}";
break;
default:
ExceptionHelper.ThrowArgumentOutOfRangeException(nameof(kind), kind.ToString());
Expand All @@ -112,7 +114,7 @@ internal Function CreateDynamicFunction(
functionExpression = "function* f(";
break;
case FunctionKind.AsyncGenerator:
ExceptionHelper.ThrowNotImplementedException("Async generators not implemented");
functionExpression = "async function* f(";
break;
default:
ExceptionHelper.ThrowArgumentOutOfRangeException(nameof(kind), kind.ToString());
Expand Down Expand Up @@ -171,10 +173,8 @@ internal Function CreateDynamicFunction(
}
else if (kind == FunctionKind.AsyncGenerator)
{
// TODO
// Let prototype be ! OrdinaryObjectCreate(%AsyncGeneratorFunction.prototype.prototype%).
// Perform DefinePropertyOrThrow(F, "prototype", PropertyDescriptor { [[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
ExceptionHelper.ThrowNotImplementedException("async generators not implemented");
var prototype = OrdinaryObjectCreate(_engine, _realm.Intrinsics.AsyncGeneratorFunction.PrototypeObject.PrototypeObject);
F.DefinePropertyOrThrow(CommonProperties.Prototype, new PropertyDescriptor(prototype, PropertyFlag.Writable));
}
else if (kind == FunctionKind.Normal)
{
Expand Down
Loading

0 comments on commit 9a67250

Please sign in to comment.