|
| 1 | +package goja |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/dop251/goja/parser" |
| 7 | +) |
| 8 | + |
| 9 | +func TestDebuggerSimpleCaseWhereExecAndPrintDontWork(t *testing.T) { |
| 10 | + const SCRIPT = ` |
| 11 | + function test() { |
| 12 | + var a = true; |
| 13 | + debugger; |
| 14 | + return a; |
| 15 | + } |
| 16 | + test() |
| 17 | + ` |
| 18 | + r := &Runtime{} |
| 19 | + r.init() |
| 20 | + debugger := r.EnableDebugMode() |
| 21 | + |
| 22 | + ch := make(chan struct{}) |
| 23 | + go func() { |
| 24 | + defer close(ch) |
| 25 | + defer func() { |
| 26 | + if t.Failed() { |
| 27 | + r.Interrupt("failed test") |
| 28 | + } |
| 29 | + }() |
| 30 | + b, c := debugger.WaitToActivate() |
| 31 | + t.Logf("%d\n", debugger.Line()) |
| 32 | + if b != DebuggerStatementActivation { |
| 33 | + t.Fatalf("Wrong activation %s", b) |
| 34 | + } |
| 35 | + if v, err := debugger.Exec("a = false"); err != nil { |
| 36 | + t.Fatalf("error while executing %s", err) |
| 37 | + } else if v.ToBoolean() { // TODO this is wrong it should be false, but it doesn't work |
| 38 | + t.Fatalf("wrong returned value %+v", v) |
| 39 | + } |
| 40 | + |
| 41 | + if v, err := debugger.Print("a"); err == nil { // this should work and return false ... but it doesn't |
| 42 | + t.Fatalf("no error while executing %s", err) |
| 43 | + } else if v == "false" { // TODO this is wrong it should be false, but it doesn't work |
| 44 | + t.Fatalf("wrong returned value %+v", v) |
| 45 | + } |
| 46 | + c() |
| 47 | + }() |
| 48 | + testScript1WithRuntime(SCRIPT, valueTrue, t, r) // TODO: this should be valueFalse, but it doesn't work |
| 49 | + <-ch // wait for the debugger |
| 50 | +} |
| 51 | + |
| 52 | +func testScript1WithRuntime(script string, expectedResult Value, t *testing.T, r *Runtime) { |
| 53 | + prg, err := parser.ParseFile(nil, "test.js", script, 0) |
| 54 | + if err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + |
| 58 | + c := newCompiler() |
| 59 | + c.compile(prg, false, false, true) |
| 60 | + |
| 61 | + vm := r.vm |
| 62 | + vm.prg = c.p |
| 63 | + vm.prg.dumpCode(t.Logf) |
| 64 | + vm.result = _undefined |
| 65 | + vm.run() |
| 66 | + v := vm.result |
| 67 | + t.Logf("stack size: %d", len(vm.stack)) |
| 68 | + t.Logf("stashAllocs: %d", vm.stashAllocs) |
| 69 | + |
| 70 | + if v == nil && expectedResult != nil || !v.SameAs(expectedResult) { |
| 71 | + t.Fatalf("Result: %+v, expected: %+v", v, expectedResult) |
| 72 | + } |
| 73 | + |
| 74 | + if vm.sp != 0 { |
| 75 | + t.Fatalf("sp: %d", vm.sp) |
| 76 | + } |
| 77 | + |
| 78 | + if l := len(vm.iterStack); l > 0 { |
| 79 | + t.Fatalf("iter stack is not empty: %d", l) |
| 80 | + } |
| 81 | +} |
0 commit comments