-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
111 lines (102 loc) · 2.57 KB
/
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
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
package main
import (
"strings"
"syscall/js"
)
var (
output []string
)
func main() {
jsFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
switch args[0].String() {
case "/compile":
output = nil
source := args[1].Get("data").Get("body").String()
enabeGop := args[1].Get("data").Get("goplus").Bool()
go func() {
_, err := runCode(source, enabeGop)
v := js.Global().Get("Object").New()
if err != nil {
v.Set("Error", err.Error())
} else {
v.Set("Body", strings.Join(output, ""))
}
args[1].Get("success").Invoke(v)
}()
case "/fmt":
source := args[1].Get("data").Get("body").String()
enabeGop := args[1].Get("data").Get("goplus").Bool()
dst, err := formatCode([]byte(source), enabeGop)
v := js.Global().Get("Object").New()
if err != nil {
v.Set("Error", err.Error())
} else {
v.Set("Body", string(dst))
}
args[1].Get("success").Invoke(v)
case "/doc/play":
}
return nil
})
js.Global().Set("gop_ajax", jsFunc)
if supportWebWork() {
jsOnMessage := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
data := args[0].Get("data")
method := data.Get("method").String()
source := data.Get("body").String()
enabeGop := data.Get("goplus").Bool()
switch method {
case "/compile":
output = nil
_, err := runCode(source, enabeGop)
v := js.Global().Get("Object").New()
v.Set("Method", method)
if err != nil {
v.Set("Error", err.Error())
} else {
v.Set("Body", strings.Join(output, ""))
}
js.Global().Get("self").Call("postMessage", v)
case "/fmt":
dst, err := formatCode([]byte(source), enabeGop)
v := js.Global().Get("Object").New()
v.Set("Method", method)
if err != nil {
v.Set("Error", err.Error())
} else {
v.Set("Body", string(dst))
}
js.Global().Get("self").Call("postMessage", v)
}
return nil
})
js.Global().Get("self").Call("addEventListener", "message", jsOnMessage)
}
}
func supportWebWork() bool {
proto := js.Global().Get("location").Get("protocol").String()
return proto == "http:" || proto == "https:"
}
func jsLog(args ...interface{}) {
js.Global().Get("console").Call("log", args...)
}
var index = `<html>
<meta charset="UTF-8">
<head>
<title>The iGo+ Playground</title>
<script type="text/javascript" src="./playground.js"></script>
<style>
.edit {
font-size:16px;
width:400px;
height:400px;
}
</style>
</head>
<body>
<textarea id="editor" class="edit"></textarea>
<button id="run" type="button">Run</button>
<textarea id="output" class="edit"></textarea>
</body>
</html>
`