Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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