Skip to content

Commit b27c8d2

Browse files
authored
eth/tracers/js: add memory.length method ethereum#24887 (#1284)
1 parent 5226fa2 commit b27c8d2

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

eth/tracers/js/goja.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,15 @@ func (mo *memoryObj) GetUint(addr int64) goja.Value {
516516
return res
517517
}
518518

519+
func (mo *memoryObj) Length() int {
520+
return mo.w.memory.Len()
521+
}
522+
519523
func (m *memoryObj) setupObject() *goja.Object {
520524
o := m.vm.NewObject()
521525
o.Set("slice", m.vm.ToValue(m.Slice))
522526
o.Set("getUint", m.vm.ToValue(m.GetUint))
527+
o.Set("length", m.vm.ToValue(m.Length))
523528
return o
524529
}
525530

eth/tracers/js/tracer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ func (mw *memoryWrapper) getUint(addr int64) *big.Int {
148148
func (mw *memoryWrapper) pushObject(vm *duktape.Context) {
149149
obj := vm.PushObject()
150150

151+
// Generate the `length` method which returns the memory length
152+
vm.PushGoFunction(func(ctx *duktape.Context) int { ctx.PushInt(mw.memory.Len()); return 1 })
153+
vm.PutPropString(obj, "length")
154+
151155
// Generate the `slice` method which takes two ints and returns a buffer
152156
vm.PushGoFunction(func(ctx *duktape.Context) int {
153157
blob := mw.slice(int64(ctx.GetInt(-2)), int64(ctx.GetInt(-1)))

eth/tracers/js/tracer_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ func testTracer(t *testing.T, newTracer tracerCtor) {
125125
}, { // tests that depth is reported correctly
126126
code: "{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}",
127127
want: `[0,1,2]`,
128+
}, { // tests memory length
129+
code: "{lengths: [], step: function(log) { this.lengths.push(log.memory.length()); }, fault: function() {}, result: function() { return this.lengths; }}",
130+
want: `[0,0,0]`,
128131
}, { // tests to-string of opcodes
129132
code: "{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}",
130133
want: `["PUSH1","PUSH1","STOP"]`,

0 commit comments

Comments
 (0)