Skip to content

Commit

Permalink
eth/tracers/js: add memory.length method (ethereum#24887)
Browse files Browse the repository at this point in the history
  • Loading branch information
ed255 authored and cp-wjhan committed Dec 2, 2022
1 parent fa3315a commit 5e81224
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions eth/tracers/js/goja.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions eth/tracers/js/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
3 changes: 3 additions & 0 deletions eth/tracers/js/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]`,
Expand Down

0 comments on commit 5e81224

Please sign in to comment.