forked from mostafa/goja_debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.go
141 lines (119 loc) · 3.13 KB
/
program.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/dop251/goja"
"github.com/dop251/goja/parser"
"github.com/dop251/goja_nodejs/console"
"github.com/dop251/goja_nodejs/require"
"github.com/evanw/esbuild/pkg/api"
)
var (
runtime *goja.Runtime
dbg *goja.Debugger
)
func debug(inspect bool, liveInfo, filename string) error {
content, err := os.ReadFile(filename)
if err != nil {
log.Fatal(err)
return err
}
if inspect {
if !strings.Contains(string(content), "//# sourceMappingURL=") {
// Generate sourceamp on-the-fly, which will unavoidably remove comments and empty lines
content = generateSourceMap(filename, string(content))
}
fmt.Println("Welcome to Goja debugger")
fmt.Println("Type 'help' or 'h' for list of commands.")
}
printer := &console.StdPrinter{
StdoutPrint: func(s string) {
prefix := ""
if inspect {
prefix = "< "
}
fmt.Printf("%s%s\n", prefix, s)
},
StderrPrint: func(s string) { fmt.Printf("%s\n", s) },
}
loader := func(requestedPath string) ([]byte, error) {
if requestedPath != "" && inspect {
fmt.Printf("%sLoaded sourcemap from: %s%s\n", GrayColor, requestedPath, ResetColor)
}
return nil, nil
}
ast, err := parser.ParseFile(nil, filename, string(content), 0, parser.WithSourceMapLoader(loader))
if err != nil {
log.Fatal(err)
return err
}
prg, err := goja.CompileASTDebug(ast, false)
if err != nil {
log.Fatal(err)
return err
}
runtime = goja.New()
if inspect {
dbg = runtime.AttachDebugger()
}
registry := new(require.Registry)
registry.Enable(runtime)
registry.RegisterNativeModule("console", console.RequireWithPrinter(printer))
console.Enable(runtime)
go func() {
if inspect {
defer dbg.Detach()
reader := bufio.NewReader(os.Stdin)
reason := dbg.Continue()
printDebuggingReason(reason)
for {
fmt.Printf("debug%s> ", getInfo(liveInfo))
userInput, _ := reader.ReadString('\n')
// remove newlines and extra spaces
userInput = strings.TrimSpace(userInput)
if !repl(userInput) {
reason = dbg.Continue()
printDebuggingReason(reason)
}
}
}
}()
_, err = runtime.RunProgram(prg)
if err != nil {
log.Fatal(err)
return err
}
return nil
}
func printDebuggingReason(reason goja.ActivationReason) {
if reason == goja.ProgramStartActivation {
fmt.Printf("Break on start in %s:%d\n", dbg.Filename(), dbg.Line())
} else if reason == goja.BreakpointActivation {
fmt.Printf("Break on breakpoint in %s:%d\ns", dbg.Filename(), dbg.Line())
} else {
fmt.Printf("Break on debugger statement in %s:%d\n", dbg.Filename(), dbg.Line())
}
}
func getInfo(liveInfo string) string {
if liveInfo == "line" {
return fmt.Sprintf("[%d]", dbg.Line())
}
return fmt.Sprintf("[%d]", dbg.PC())
}
func generateSourceMap(filename string, src string) []byte {
result := api.Transform(src, api.TransformOptions{
Sourcemap: api.SourceMapInline,
SourcesContent: api.SourcesContentInclude,
Sourcefile: filename,
MinifyWhitespace: false,
MinifyIdentifiers: false,
MinifySyntax: false,
})
if len(result.Errors) > 0 {
fmt.Println(result.Errors)
}
return result.Code
}