-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
45 lines (38 loc) · 888 Bytes
/
main.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
//go:build js && wasm
// +build js,wasm
package main
import (
"encoding/json"
"syscall/js"
"github.com/hsiaosiyuan0/mole/ecma/estree"
"github.com/hsiaosiyuan0/mole/ecma/parser"
"github.com/hsiaosiyuan0/mole/span"
)
func compileToJson(src string) (string, error) {
opts := parser.NewParserOpts()
s := span.NewSource("", src)
p := parser.NewParser(s, opts)
ast, err := p.Prog()
if err != nil {
return "", err
}
b, err := json.Marshal(estree.ConvertProg(ast.(*parser.Prog), estree.NewConvertCtx(p)))
if err != nil {
return "", err
}
return string(b), nil
}
func compile(this js.Value, args []js.Value) interface{} {
src := args[0].String()
json, err := compileToJson(src)
var errMsg string
if err != nil {
errMsg = err.Error()
}
return []interface{}{json, errMsg}
}
func main() {
c := make(chan bool)
js.Global().Set("compile", js.FuncOf(compile))
<-c
}