Skip to content

Commit

Permalink
Update test262 suite and fix typed array issues (#1902)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Jun 27, 2024
1 parent bbc65dc commit dafb525
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Jint.Tests.Test262/Test262Harness.settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"SuiteGitSha": "9dec509c93fa9b19d46330d90a43f871bb1aaf84",
"SuiteGitSha": "c3a326ace810e7c80a4e1b8df8c8b704ed223c28",
//"SuiteDirectory": "//mnt/c/work/test262",
"TargetPath": "./Generated",
"Namespace": "Jint.Tests.Test262",
Expand Down
9 changes: 8 additions & 1 deletion Jint/Native/Array/ArrayIteratorPrototype.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Jint.Collections;
using Jint.Native.ArrayBuffer;
using Jint.Native.Iterator;
using Jint.Native.Object;
using Jint.Native.Symbol;
using Jint.Native.TypedArray;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Interop;
Expand Down Expand Up @@ -119,7 +121,12 @@ public override bool TryIteratorStep(out ObjectInstance nextItem)
if (_typedArray is not null)
{
_typedArray._viewedArrayBuffer.AssertNotDetached();
len = _typedArray.GetLength();
var taRecord = IntrinsicTypedArrayPrototype.MakeTypedArrayWithBufferWitnessRecord(_typedArray, ArrayBufferOrder.SeqCst);
if (!_closed && taRecord.IsTypedArrayOutOfBounds)
{
ExceptionHelper.ThrowTypeError(_typedArray.Engine.Realm, "TypedArray is out of bounds");
}
len = taRecord.TypedArrayLength;
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions Jint/Native/JsTypedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public JsValue this[uint index]

internal override uint GetLength() => IntrinsicTypedArrayPrototype.MakeTypedArrayWithBufferWitnessRecord(this, ArrayBufferOrder.Unordered).TypedArrayLength;

internal override bool IsArrayLike => true;

internal override bool IsIntegerIndexedArray => true;

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/TypedArray/IntrinsicTypedArrayConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ internal static JsTypedArray TypedArrayCreate(Realm realm, IConstructor construc
{
if (taRecord.IsTypedArrayOutOfBounds)
{
ExceptionHelper.ThrowTypeError(realm);
ExceptionHelper.ThrowTypeError(realm, "TypedArray is out of bounds");
}
if (newTypedArray.GetLength() < number._value)
{
Expand Down
47 changes: 36 additions & 11 deletions Jint/Native/TypedArray/IntrinsicTypedArrayPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ public uint TypedArrayLength
var byteOffset = o._byteOffset;
var elementSize = o._arrayElementType.GetElementSize();
var byteLength = (double) CachedBufferByteLength;
return (uint) System.Math.Floor((byteLength - byteOffset) / elementSize);
var floor = System.Math.Floor((byteLength - byteOffset) / elementSize);
return floor < 0 ? 0 : (uint) floor;
}
}

Expand Down Expand Up @@ -338,8 +339,16 @@ private JsValue CopyWithin(JsValue thisObject, JsValue[] arguments)
var buffer = o._viewedArrayBuffer;
buffer.AssertNotDetached();

taRecord = MakeTypedArrayWithBufferWitnessRecord(o, ArrayBufferOrder.SeqCst);
if (taRecord.IsTypedArrayOutOfBounds)
{
ExceptionHelper.ThrowTypeError(_realm, "TypedArray is out of bounds");
}

len = taRecord.TypedArrayLength;
var elementSize = o._arrayElementType.GetElementSize();
var byteOffset = o._byteOffset;
var bufferByteLimit = len * elementSize + byteOffset;
var toByteIndex = to * elementSize + byteOffset;
var fromByteIndex = from * elementSize + byteOffset;
var countBytes = count * elementSize;
Expand All @@ -358,11 +367,18 @@ private JsValue CopyWithin(JsValue thisObject, JsValue[] arguments)

while (countBytes > 0)
{
var value = buffer.GetValueFromBuffer((int) fromByteIndex, TypedArrayElementType.Uint8, true, ArrayBufferOrder.Unordered);
buffer.SetValueInBuffer((int) toByteIndex, TypedArrayElementType.Uint8, value, true, ArrayBufferOrder.Unordered);
fromByteIndex += direction;
toByteIndex += direction;
countBytes--;
if (fromByteIndex < bufferByteLimit && toByteIndex < bufferByteLimit)
{
var value = buffer.GetValueFromBuffer((int) fromByteIndex, TypedArrayElementType.Uint8, isTypedArray: true, ArrayBufferOrder.Unordered);
buffer.SetValueInBuffer((int) toByteIndex, TypedArrayElementType.Uint8, value, isTypedArray: true, ArrayBufferOrder.Unordered);
fromByteIndex += direction;
toByteIndex += direction;
countBytes--;
}
else
{
countBytes = 0;
}
}
}

Expand Down Expand Up @@ -451,24 +467,33 @@ private JsValue Fill(JsValue thisObject, JsValue[] arguments)
k = (int) System.Math.Min(relativeStart, len);
}

uint final;
uint endIndex;
var relativeEnd = end.IsUndefined() ? len : TypeConverter.ToIntegerOrInfinity(end);
if (double.IsNegativeInfinity(relativeEnd))
{
final = 0;
endIndex = 0;
}
else if (relativeEnd < 0)
{
final = (uint) System.Math.Max(len + relativeEnd, 0);
endIndex = (uint) System.Math.Max(len + relativeEnd, 0);
}
else
{
final = (uint) System.Math.Min(relativeEnd, len);
endIndex = (uint) System.Math.Min(relativeEnd, len);
}

taRecord = MakeTypedArrayWithBufferWitnessRecord(o, ArrayBufferOrder.SeqCst);
if (taRecord.IsTypedArrayOutOfBounds)
{
ExceptionHelper.ThrowTypeError(_realm, "TypedArray is out of bounds");
}

len = taRecord.TypedArrayLength;
endIndex = System.Math.Min(endIndex, len);

o._viewedArrayBuffer.AssertNotDetached();

for (var i = k; i < final; ++i)
for (var i = k; i < endIndex; ++i)
{
o[i] = value;
}
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/TypedArray/TypeArrayHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static IntrinsicTypedArrayPrototype.TypedArrayWithBufferWitnessRecord V
var taRecord = IntrinsicTypedArrayPrototype.MakeTypedArrayWithBufferWitnessRecord(typedArray, order);
if (taRecord.IsTypedArrayOutOfBounds)
{
ExceptionHelper.ThrowTypeError(realm);
ExceptionHelper.ThrowTypeError(realm, "TypedArray is out of bounds");
}

return taRecord;
Expand Down

0 comments on commit dafb525

Please sign in to comment.