mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
add console.log/error support
This commit is contained in:
parent
16b0d9e982
commit
6b3cf83d16
2 changed files with 32 additions and 1 deletions
|
|
@ -21,7 +21,9 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/dop251/goja"
|
"github.com/dop251/goja"
|
||||||
|
|
@ -470,7 +472,26 @@ func wrapError(context string, err error) error {
|
||||||
// It depends on type converters having been set up.
|
// It depends on type converters having been set up.
|
||||||
func (t *jsTracer) setBuiltinFunctions() {
|
func (t *jsTracer) setBuiltinFunctions() {
|
||||||
vm := t.vm
|
vm := t.vm
|
||||||
// TODO: load console from goja-nodejs
|
// Initialize console object with log and error methods
|
||||||
|
console := vm.NewObject()
|
||||||
|
console.Set("log", func(call goja.FunctionCall) goja.Value {
|
||||||
|
var output []string
|
||||||
|
for _, argument := range call.Arguments {
|
||||||
|
output = append(output, fmt.Sprintf("%v", argument))
|
||||||
|
}
|
||||||
|
fmt.Fprintln(os.Stdout, strings.Join(output, " "))
|
||||||
|
return goja.Undefined()
|
||||||
|
})
|
||||||
|
console.Set("error", func(call goja.FunctionCall) goja.Value {
|
||||||
|
var output []string
|
||||||
|
for _, argument := range call.Arguments {
|
||||||
|
output = append(output, fmt.Sprintf("%v", argument))
|
||||||
|
}
|
||||||
|
fmt.Fprintln(os.Stderr, strings.Join(output, " "))
|
||||||
|
return goja.Undefined()
|
||||||
|
})
|
||||||
|
vm.Set("console", console)
|
||||||
|
|
||||||
vm.Set("toHex", func(v goja.Value) string {
|
vm.Set("toHex", func(v goja.Value) string {
|
||||||
b, err := t.fromBuf(vm, v, false)
|
b, err := t.fromBuf(vm, v, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -146,6 +146,16 @@ func TestTracer(t *testing.T) {
|
||||||
}, { // tests ctx.coinbase
|
}, { // tests ctx.coinbase
|
||||||
code: "{lengths: [], step: function(log) { }, fault: function() {}, result: function(ctx) { var coinbase = ctx.coinbase; return toAddress(coinbase); }}",
|
code: "{lengths: [], step: function(log) { }, fault: function() {}, result: function(ctx) { var coinbase = ctx.coinbase; return toAddress(coinbase); }}",
|
||||||
want: `{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0}`,
|
want: `{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0}`,
|
||||||
|
}, {
|
||||||
|
code: `{
|
||||||
|
step: function() {
|
||||||
|
console.log("test log");
|
||||||
|
console.error("test error");
|
||||||
|
},
|
||||||
|
fault: function() {},
|
||||||
|
result: function() {},
|
||||||
|
}`,
|
||||||
|
want: `{}`,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
if have, err := execTracer(tt.code, tt.contract); tt.want != string(have) || tt.fail != err {
|
if have, err := execTracer(tt.code, tt.contract); tt.want != string(have) || tt.fail != err {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue