Skip to content

Commit

Permalink
switch array buffer approach a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Dec 14, 2021
1 parent 5b85d0e commit 572348e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
42 changes: 23 additions & 19 deletions Jint/Native/ArrayBuffer/ArrayBufferInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ internal TypedArrayValue RawBytesToNumeric(TypedArrayElementType type, int byteI
var rawBytes = _arrayBufferData;

// floats require a little more at the moment
var needsReverse = !isLittleEndian && elementSize > 1 && (type == TypedArrayElementType.Float32 || type == TypedArrayElementType.Float64);
var needsReverse = !isLittleEndian && elementSize > 1 && type is TypedArrayElementType.Float32 or TypedArrayElementType.Float64;
if (needsReverse)
{
System.Array.Copy(rawBytes, byteIndex, _workBuffer, 0, elementSize);
Expand Down Expand Up @@ -146,17 +146,19 @@ internal TypedArrayValue RawBytesToNumeric(TypedArrayElementType type, int byteI
return value;
}

if (type.IsBigIntElementType())
if (type == TypedArrayElementType.BigUint64)
{
// return the BigInt value that corresponds to intValue
#if NETSTANDARD2_1
return new System.Numerics.BigInteger(rawBytes.AsSpan().Slice(byteIndex, 8));
#else
ExceptionHelper.ThrowNotImplementedException();
#endif
var value = BitConverter.ToUInt64(rawBytes, byteIndex);
return value;
}

if (type == TypedArrayElementType.BigInt64)
{
var value = BitConverter.ToInt64(rawBytes, byteIndex);
return value;
}

long? intValue = type switch
TypedArrayValue? arrayValue = type switch
{
TypedArrayElementType.Int8 => ((sbyte) rawBytes[byteIndex]),
TypedArrayElementType.Uint8 => (rawBytes[byteIndex]),
Expand All @@ -180,12 +182,12 @@ internal TypedArrayValue RawBytesToNumeric(TypedArrayElementType type, int byteI
_ => null
};

if (intValue is null)
if (arrayValue is null)
{
ExceptionHelper.ThrowArgumentOutOfRangeException(nameof(type), type.ToString());
}

return (double) intValue;
return arrayValue.Value;
}

/// <summary>
Expand Down Expand Up @@ -263,9 +265,9 @@ private byte[] NumericToRawBytes(TypedArrayElementType type, TypedArrayValue val
break;
case TypedArrayElementType.Int32:
#if !NETSTANDARD2_1
rawBytes = BitConverter.GetBytes((int) intValue);
rawBytes = BitConverter.GetBytes((uint) intValue);
#else
BitConverter.TryWriteBytes(rawBytes, (int) intValue);
BitConverter.TryWriteBytes(rawBytes, (uint) intValue);
#endif
break;
case TypedArrayElementType.Uint32:
Expand All @@ -276,15 +278,17 @@ private byte[] NumericToRawBytes(TypedArrayElementType type, TypedArrayValue val
#endif
break;
case TypedArrayElementType.BigInt64:
#if !NETSTANDARD2_1
rawBytes = BitConverter.GetBytes((long) value.BigInteger);
#else
BitConverter.TryWriteBytes(rawBytes, (long) value.BigInteger);
#endif
break;
case TypedArrayElementType.BigUint64:
rawBytes = _workBuffer;
System.Array.Clear(rawBytes, 0, rawBytes.Length);
#if !NETSTANDARD2_1
// array returned is variable length
var bigIntBytes = value.BigInteger.ToByteArray();
System.Array.Copy(bigIntBytes, 0, rawBytes, 0, bigIntBytes.Length);
rawBytes = BitConverter.GetBytes((ulong) value.BigInteger);
#else
value.BigInteger.TryWriteBytes(rawBytes.AsSpan(), out _);
BitConverter.TryWriteBytes(rawBytes, (ulong) value.BigInteger);
#endif
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/BigInt/BigIntInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Jint.Native.BigInt;

public class BigIntInstance : ObjectInstance, IPrimitiveInstance
public sealed class BigIntInstance : ObjectInstance, IPrimitiveInstance
{
public BigIntInstance(Engine engine)
: base(engine, ObjectClass.Object)
Expand Down
2 changes: 1 addition & 1 deletion Jint/Native/Json/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private JsValue SerializeJSONProperty(JsValue key, JsValue holder)

if (!ReferenceEquals(_replacerFunction, Undefined.Instance))
{
var replacerFunctionCallable = (ICallable)_replacerFunction.AsObject();
var replacerFunctionCallable = (ICallable) _replacerFunction.AsObject();
value = replacerFunctionCallable.Call(holder, Arguments.From(key, value));
}

Expand Down
35 changes: 35 additions & 0 deletions Jint/Native/TypedArray/TypedArrayValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,46 @@ namespace Jint.Native.TypedArray;
return new TypedArrayValue(Types.Number, value, default);
}

public static implicit operator TypedArrayValue(byte value)
{
return new TypedArrayValue(Types.Number, value, default);
}

public static implicit operator TypedArrayValue(int value)
{
return new TypedArrayValue(Types.Number, value, default);
}

public static implicit operator TypedArrayValue(ushort value)
{
return new TypedArrayValue(Types.Number, value, default);
}

public static implicit operator TypedArrayValue(short value)
{
return new TypedArrayValue(Types.Number, value, default);
}

public static implicit operator TypedArrayValue(uint value)
{
return new TypedArrayValue(Types.Number, value, default);
}

public static implicit operator TypedArrayValue(BigInteger value)
{
return new TypedArrayValue(Types.BigInt, default, value);
}

public static implicit operator TypedArrayValue(ulong value)
{
return new TypedArrayValue(Types.BigInt, default, value);
}

public static implicit operator TypedArrayValue(long value)
{
return new TypedArrayValue(Types.BigInt, default, value);
}

public JsValue ToJsValue()
{
return Type == Types.Number
Expand Down

0 comments on commit 572348e

Please sign in to comment.