Skip to content

Commit

Permalink
tweaks and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Dec 15, 2021
1 parent 74e9b00 commit 7357891
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
6 changes: 2 additions & 4 deletions Jint/Native/ArrayBuffer/ArrayBufferInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,11 @@ private byte[] NumericToRawBytes(TypedArrayElementType type, TypedArrayValue val
}
else if (type == TypedArrayElementType.BigInt64)
{
// Let rawBytes be a List whose elements are the 4 bytes that are the result of converting value to IEEE 754-2019 binary32 format using roundTiesToEven mode. If isLittleEndian is false, the bytes are arranged in big endian order. Otherwise, the bytes are arranged in little endian order. If value is NaN, rawBytes may be set to any implementation chosen IEEE 754-2019 binary32 format Not-a-Number encoding. An implementation must always choose the same encoding for each implementation distinguishable NaN value.
rawBytes = BitConverter.GetBytes((long) value.BigInteger);
rawBytes = BitConverter.GetBytes(TypeConverter.ToBigInt64(value.BigInteger));
}
else if (type == TypedArrayElementType.BigUint64)
{
// Let rawBytes be a List whose elements are the 8 bytes that are the IEEE 754-2019 binary64 format encoding of value. If isLittleEndian is false, the bytes are arranged in big endian order. Otherwise, the bytes are arranged in little endian order. If value is NaN, rawBytes may be set to any implementation chosen IEEE 754-2019 binary64 format Not-a-Number encoding. An implementation must always choose the same encoding for each implementation distinguishable NaN value.
rawBytes = BitConverter.GetBytes((ulong) value.BigInteger);
rawBytes = BitConverter.GetBytes(TypeConverter.ToBigUint64(value.BigInteger));
}
else
{
Expand Down
17 changes: 10 additions & 7 deletions Jint/Native/BigInt/BigIntConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ protected override void Initialize()
/// </summary>
private JsValue AsIntN(JsValue thisObj, JsValue[] arguments)
{
var bits = TypeConverter.ToIndex(_realm, arguments.At(0));
var bits = (int) TypeConverter.ToIndex(_realm, arguments.At(0));
var bigint = arguments.At(1).ToBigInteger(_engine);
var bitsPow = (ulong) System.Math.Pow(2, bits);
var mod = bigint % bitsPow;
if (mod >= bitsPow - 1)

BigInteger.DivRem(bigint, BigInteger.Pow(2, bits), out var mod);
if (mod >= BigInteger.Pow(2, bits - 1))
{
return mod - bitsPow;
return mod - BigInteger.Pow(2, bits);
}

return mod;
Expand All @@ -60,9 +60,12 @@ private JsValue AsIntN(JsValue thisObj, JsValue[] arguments)
/// </summary>
private JsValue AsUintN(JsValue thisObj, JsValue[] arguments)
{
var bits = TypeConverter.ToIndex(_realm, arguments.At(0));
var bits = (int) TypeConverter.ToIndex(_realm, arguments.At(0));
var bigint = arguments.At(1).ToBigInteger(_engine);
return JsBigInt.Create(bigint % (BigInteger) System.Math.Pow(2, bits));

BigInteger.DivRem(bigint, BigInteger.Pow(2, bits), out var result);

return result;
}

public override JsValue Call(JsValue thisObject, JsValue[] arguments)
Expand Down
20 changes: 14 additions & 6 deletions Jint/Runtime/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,19 +660,27 @@ void ThrowInvalidBigIntParserException()
/// <summary>
/// https://tc39.es/ecma262/#sec-tobigint64
/// </summary>
internal static double ToBigInt64(JsValue value)
internal static long ToBigInt64(BigInteger value)
{
ExceptionHelper.ThrowNotImplementedException("BigInt not implemented");
return 0;
BigInteger.DivRem(value, BigInteger.Pow(2, 64), out var int64bit);
if (BigInteger.Abs(int64bit) >= BigInteger.Pow(2, 63))
{
return (long) (int64bit - BigInteger.Pow(2, 64) * int64bit.Sign);
}
return (long) int64bit;
}

/// <summary>
/// https://tc39.es/ecma262/#sec-tobiguint64
/// </summary>
internal static double ToBigUint64(JsValue value)
internal static ulong ToBigUint64(BigInteger value)
{
ExceptionHelper.ThrowNotImplementedException("BigInt not implemented");
return 0;
BigInteger.DivRem(value, BigInteger.Pow(2, 64), out var int64bit);
if (int64bit < 0)
{
return (ulong) (ulong.MaxValue + int64bit + 1);
}
return (ulong) int64bit;
}


Expand Down

0 comments on commit 7357891

Please sign in to comment.