Skip to content

Commit

Permalink
complete hex handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jul 12, 2024
1 parent 59f8148 commit d82b81c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Jint/Native/ArrayBuffer/ArrayBufferConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal ArrayBufferConstructor(
_prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
}

private ArrayBufferPrototype PrototypeObject { get; }
internal ArrayBufferPrototype PrototypeObject { get; }

protected override void Initialize()
{
Expand Down
1 change: 1 addition & 0 deletions Jint/Native/JsArrayBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal JsArrayBuffer(
ExceptionHelper.ThrowRangeError(engine.Realm, "arrayBufferMaxByteLength cannot be larger than int32.MaxValue");
}

_prototype = engine.Intrinsics.ArrayBuffer.PrototypeObject;
_arrayBufferData = data;
_arrayBufferMaxByteLength = (int?) arrayBufferMaxByteLength;
}
Expand Down
17 changes: 7 additions & 10 deletions Jint/Native/TypedArray/TypedArrayConstructor.Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ protected override void Initialize()

public JsTypedArray Construct(byte[] values)
{
var array = (JsTypedArray) base.Construct([values.Length], this);
FillTypedArrayInstance(array, values);
return array;
return base.Construct(new JsArrayBuffer(_engine, values));
}

private JsTypedArray FromBase64(JsValue thisObject, JsValue[] arguments)
Expand All @@ -73,8 +71,7 @@ private JsTypedArray FromBase64(JsValue thisObject, JsValue[] arguments)
throw result.Error;
}

var resultLength = result.Bytes.Length;
var ta = AllocateTypedArray(resultLength);
var ta = _realm.Intrinsics.Uint8Array.Construct(new JsArrayBuffer(_engine, result.Bytes));
ta._viewedArrayBuffer._arrayBufferData = result.Bytes;
return ta;
}
Expand Down Expand Up @@ -135,25 +132,25 @@ private JsTypedArray FromHex(JsValue thisObject, JsValue[] arguments)
throw result.Error;
}

var resultLength = result.Bytes.Length;
var ta = AllocateTypedArray(resultLength);
var ta = _realm.Intrinsics.Uint8Array.Construct(new JsArrayBuffer(_engine, result.Bytes));
ta._viewedArrayBuffer._arrayBufferData = result.Bytes;
return ta;
}

internal static FromEncodingResult FromHex(Engine engine, string s, uint maxLength = int.MaxValue)
{
var length = s.Length;
var bytes = new byte[length / 2];
var bytes = new byte[System.Math.Min(maxLength, length / 2)];
var read = 0;

if (length % 2 != 0)
{
return new FromEncodingResult(bytes, ExceptionHelper.CreateSyntaxError(engine.Realm, "Invalid hex string"), read);
}

int byteIndex = 0;
const string Allowed = "0123456789abcdefABCDEF";
while (read < length && bytes.Length < maxLength)
while (read < length && byteIndex < maxLength)
{
var hexits = s.AsSpan(read, 2);
if (!Allowed.Contains(hexits[0]) || !Allowed.Contains(hexits[1]))
Expand All @@ -166,7 +163,7 @@ internal static FromEncodingResult FromHex(Engine engine, string s, uint maxLeng
#else
var b = byte.Parse(hexits.ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
#endif
bytes[read / 2] = b;
bytes[byteIndex++] = b;
read += 2;
}
return new FromEncodingResult(bytes, Error: null, read);
Expand Down
1 change: 0 additions & 1 deletion Jint/Native/TypedArray/TypedArrayConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
ExceptionHelper.ThrowTypeError(_realm);
}


var numberOfArgs = arguments.Length;
if (numberOfArgs == 0)
{
Expand Down

0 comments on commit d82b81c

Please sign in to comment.