eth/tracers/js: add memory.length method (#24887)

This commit is contained in:
Eduard S 2022-05-19 14:35:30 +02:00 committed by GitHub
parent a35a5cad22
commit 310f751639
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 0 deletions

@ -517,10 +517,15 @@ func (mo *memoryObj) GetUint(addr int64) goja.Value {
return res
}
func (mo *memoryObj) Length() int {
return mo.w.memory.Len()
}
func (m *memoryObj) setupObject() *goja.Object {
o := m.vm.NewObject()
o.Set("slice", m.vm.ToValue(m.Slice))
o.Set("getUint", m.vm.ToValue(m.GetUint))
o.Set("length", m.vm.ToValue(m.Length))
return o
}

@ -148,6 +148,10 @@ func (mw *memoryWrapper) getUint(addr int64) *big.Int {
func (mw *memoryWrapper) pushObject(vm *duktape.Context) {
obj := vm.PushObject()
// Generate the `length` method which returns the memory length
vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushInt(mw.memory.Len()); return 1 })
vm.PutPropString(obj, "length")
// Generate the `slice` method which takes two ints and returns a buffer
vm.PushGoFunction(func(ctx *duktape.Context) int {
blob := mw.slice(int64(ctx.GetInt(-2)), int64(ctx.GetInt(-1)))

@ -125,6 +125,9 @@ func testTracer(t *testing.T, newTracer tracerCtor) {
}, { // tests that depth is reported correctly
code: "{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}",
want: `[0,1,2]`,
}, { // tests memory length
code: "{lengths: [], step: function(log) { this.lengths.push(log.memory.length()); }, fault: function() {}, result: function() { return this.lengths; }}",
want: `[0,0,0]`,
}, { // tests to-string of opcodes
code: "{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}",
want: `["PUSH1","PUSH1","STOP"]`,