mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-16 18:00:46 +00:00
parent
bfc00e775b
commit
67efb14ff3
2 changed files with 42 additions and 1 deletions
|
|
@ -124,7 +124,7 @@ type jsTracer struct {
|
||||||
// The methods `result` and `fault` are required to be present.
|
// The methods `result` and `fault` are required to be present.
|
||||||
// The methods `step`, `enter`, and `exit` are optional, but note that
|
// The methods `step`, `enter`, and `exit` are optional, but note that
|
||||||
// `enter` and `exit` always go together.
|
// `enter` and `exit` always go together.
|
||||||
func newJsTracer(code string, ctx *tracers.Context, _ json.RawMessage) (tracers.Tracer, error) {
|
func newJsTracer(code string, ctx *tracers.Context, cfg json.RawMessage) (tracers.Tracer, error) {
|
||||||
if c, ok := assetTracers[code]; ok {
|
if c, ok := assetTracers[code]; ok {
|
||||||
code = c
|
code = c
|
||||||
}
|
}
|
||||||
|
|
@ -176,6 +176,17 @@ func newJsTracer(code string, ctx *tracers.Context, _ json.RawMessage) (tracers.
|
||||||
t.exit = exit
|
t.exit = exit
|
||||||
t.result = result
|
t.result = result
|
||||||
t.fault = fault
|
t.fault = fault
|
||||||
|
|
||||||
|
// Pass in config
|
||||||
|
if setup, ok := goja.AssertFunction(obj.Get("setup")); ok {
|
||||||
|
cfgStr := "{}"
|
||||||
|
if cfg != nil {
|
||||||
|
cfgStr = string(cfg)
|
||||||
|
}
|
||||||
|
if _, err := setup(obj, vm.ToValue(cfgStr)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
// Setup objects carrying data to JS. These are created once and re-used.
|
// Setup objects carrying data to JS. These are created once and re-used.
|
||||||
t.log = &steplog{
|
t.log = &steplog{
|
||||||
vm: vm,
|
vm: vm,
|
||||||
|
|
|
||||||
|
|
@ -292,3 +292,33 @@ func TestEnterExit(t *testing.T) {
|
||||||
t.Errorf("Number of invocations of enter() and exit() is wrong. Have %s, want %s\n", have, want)
|
t.Errorf("Number of invocations of enter() and exit() is wrong. Have %s, want %s\n", have, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetup(t *testing.T) {
|
||||||
|
// Test empty config
|
||||||
|
_, err := newJsTracer(`{setup: function(cfg) { if (cfg !== "{}") { throw("invalid empty config") } }, fault: function() {}, result: function() {}}`, new(tracers.Context), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := json.Marshal(map[string]string{"foo": "bar"})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Test no setup func
|
||||||
|
_, err = newJsTracer(`{fault: function() {}, result: function() {}}`, new(tracers.Context), cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Test config value
|
||||||
|
tracer, err := newJsTracer("{config: null, setup: function(cfg) { this.config = JSON.parse(cfg) }, step: function() {}, fault: function() {}, result: function() { return this.config.foo }}", new(tracers.Context), cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
have, err := tracer.GetResult()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(have) != `"bar"` {
|
||||||
|
t.Errorf("tracer returned wrong result. have: %s, want: \"bar\"\n", string(have))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue