Skip to content

Commit

Permalink
Unify js types, use typed arrays everywhere + long for slice paramete…
Browse files Browse the repository at this point in the history
…rs (#6319)
  • Loading branch information
LukaszRozmej authored Dec 11, 2023
1 parent 874cd73 commit 3f2f149
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ namespace Nethermind.Evm.Tracing.GethStyle.JavaScript;

public class Context
{
private IList? _blockHashConverted;
private IList? _txHashConverted;
private IList? _inputConverted;
private IList? _fromConverted;
private IList? _toConverted;
private IList? _outputConverted;
private ITypedArray<byte>? _blockHashConverted;
private ITypedArray<byte>? _txHashConverted;
private ITypedArray<byte>? _inputConverted;
private ITypedArray<byte>? _fromConverted;
private ITypedArray<byte>? _toConverted;
private ITypedArray<byte>? _outputConverted;
private dynamic? _valueConverted;
private dynamic? _gasPriceConverted;
private Address? _from;
Expand Down Expand Up @@ -111,17 +111,17 @@ public UInt256 GasPrice
}

public string type { get; set; } = null!;
public IList? from => _fromConverted ??= From?.Bytes.ToUnTypedScriptArray();
public IList? to => _toConverted ??= To?.Bytes.ToUnTypedScriptArray();
public IList? input => _inputConverted ??= Input.ToArray().ToUnTypedScriptArray();
public ITypedArray<byte>? from => _fromConverted ??= From?.Bytes.ToTypedScriptArray();
public ITypedArray<byte>? to => _toConverted ??= To?.Bytes.ToTypedScriptArray();
public ITypedArray<byte>? input => _inputConverted ??= Input.ToArray().ToTypedScriptArray();
public long gas { get; set; }
public long gasUsed { get; set; }
public IJavaScriptObject gasPrice => _gasPriceConverted ??= GasPrice.ToBigInteger();
public IJavaScriptObject value => _valueConverted ??= Value.ToBigInteger();
public long block { get; set; }
public IList? output => _outputConverted ??= Output?.ToUnTypedScriptArray();
public IList? blockHash => _blockHashConverted ??= BlockHash?.BytesToArray().ToUnTypedScriptArray();
public ITypedArray<byte>? output => _outputConverted ??= Output?.ToTypedScriptArray();
public ITypedArray<byte>? blockHash => _blockHashConverted ??= BlockHash?.BytesToArray().ToTypedScriptArray();
public int? txIndex { get; set; }
public IList? txHash => _txHashConverted ??= TxHash?.BytesToArray().ToUnTypedScriptArray();
public ITypedArray<byte>? txHash => _txHashConverted ??= TxHash?.BytesToArray().ToTypedScriptArray();
public dynamic? error { get; set; } = Undefined.Value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class Engine : IDisposable

private readonly IReleaseSpec _spec;

private readonly dynamic _createUntypedArray;
private dynamic _bigInteger;
private dynamic _createUint8Array;

Expand Down Expand Up @@ -89,7 +88,7 @@ public Engine(IReleaseSpec spec)
Func<object?, string> toHex = ToHex;
Func<object, ITypedArray<byte>> toAddress = ToAddress;
Func<object, bool> isPrecompiled = IsPrecompiled;
Func<object, int, int, ITypedArray<byte>> slice = Slice;
Func<object, long, long, ITypedArray<byte>> slice = Slice;
Func<object, ulong, ITypedArray<byte>> toContract = ToContract;
Func<object, string, object, ITypedArray<byte>> toContract2 = ToContract2;

Expand All @@ -100,7 +99,6 @@ public Engine(IReleaseSpec spec)
V8Engine.AddHostObject(nameof(slice), slice);
V8Engine.AddHostObject(nameof(toContract), toContract);
V8Engine.AddHostObject(nameof(toContract2), toContract2);
_createUntypedArray = V8Engine.Script.Array.from;

if (!IsDebugging)
{
Expand Down Expand Up @@ -134,7 +132,7 @@ public Engine(IReleaseSpec spec)
/// <summary>
/// Returns a slice of input
/// </summary>
private ITypedArray<byte> Slice(object input, int start, int end)
private ITypedArray<byte> Slice(object input, long start, long end)
{
if (input == null)
{
Expand All @@ -146,7 +144,7 @@ private ITypedArray<byte> Slice(object input, int start, int end)
throw new ArgumentOutOfRangeException(nameof(start), $"tracer accessed out of bound memory: offset {start}, end {end}");
}

return input.ToBytes().Slice(start, end - start).ToTypedScriptArray();
return input.ToBytes().Slice((int)start, (int)(end - start)).ToTypedScriptArray();
}

/// <summary>
Expand All @@ -171,11 +169,6 @@ public void Dispose()
/// </summary>
public ITypedArray<byte> CreateUint8Array(byte[] buffer) => _createUint8Array(buffer);

/// <summary>
/// Creates a JavaScript V8Engine untyped array with number values
/// </summary>
public IList CreateUntypedArray(byte[] buffer) => _createUntypedArray(buffer);

/// <summary>
/// Creates a JavaScript BigInteger object
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public static class JavaScriptConverter

public static ITypedArray<byte> ToTypedScriptArray(this ReadOnlyMemory<byte> memory) => memory.ToArray().ToTypedScriptArray();

public static IList ToUnTypedScriptArray(this byte[] array) => CurrentEngine.CreateUntypedArray(array);

public static IJavaScriptObject ToBigInteger(this BigInteger bigInteger) => CurrentEngine.CreateBigInteger(bigInteger);
public static IJavaScriptObject ToBigInteger(this UInt256 bigInteger) => CurrentEngine.CreateBigInteger((BigInteger)bigInteger);

Expand Down

0 comments on commit 3f2f149

Please sign in to comment.