-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.html
176 lines (157 loc) · 7.85 KB
/
index.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<html>
<head>
<meta charset="utf-8">
<title>The WebAssembly Go Playground</title>
<link rel="stylesheet" href="style.css">
<script src="wasm_exec.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/libs/lz-string.min.js"></script>
<script src="jquery-linedtextarea.js"></script>
<script src="playground.js"></script>
<script src="sharing.js"></script>
<script>
$(document).ready(function() {
initSharing({
codeEl: '#code',
shareEl: '#share',
shareURLEl: '#shareURL',
});
$('#code').linedtextarea();
$('#code').attr('wrap', 'off');
if (!WebAssembly || !WebAssembly.instantiate) {
$('#run').val('Unsupported Browser');
$('#controls input').attr('disabled', true);
return;
}
let cmds = {};
const exec = (wasm, args) => new Promise((resolve, reject) => {
const go = new Go();
go.exit = resolve;
go.argv = go.argv.concat(args || []);
WebAssembly.instantiate(wasm, go.importObject).then((result) => go.run(result.instance)).catch(reject);
});
Promise.all(
[
'prebuilt/runtime.a',
'prebuilt/internal/bytealg.a',
'prebuilt/internal/cpu.a',
'prebuilt/runtime/internal/atomic.a',
'prebuilt/runtime/internal/math.a',
'prebuilt/runtime/internal/sys.a',
].map((path) => fetch(path)
.then((response) => response.arrayBuffer())
.then((buf) => writeToGoFilesystem(path, new Uint8Array(buf)))
).concat(
['compile', 'link', 'gofmt']
.map((cmd) => fetch('cmd/' + cmd + '.wasm')
.then((response) => response.arrayBuffer())
.then((buf) => {
cmds[cmd] = new Uint8Array(buf);
})
)
)
).then(() => {
const decoder = new TextDecoder('utf-8');
const encoder = new TextEncoder('utf-8');
writeToGoFilesystem('/importcfg', encoder.encode(
"packagefile runtime=prebuilt/runtime.a"
));
writeToGoFilesystem('/importcfg.link', encoder.encode(
"packagefile command-line-arguments=main.a\n" +
"packagefile runtime=prebuilt/runtime.a\n" +
"packagefile internal/bytealg=prebuilt/internal/bytealg.a\n" +
"packagefile internal/cpu=prebuilt/internal/cpu.a\n" +
"packagefile runtime/internal/atomic=prebuilt/runtime/internal/atomic.a\n" +
"packagefile runtime/internal/math=prebuilt/runtime/internal/math.a\n" +
"packagefile runtime/internal/sys=prebuilt/runtime/internal/sys.a"
));
playground({
codeEl: '#code',
outputEl: '#output',
runEl: '#run',
enableHistory: false,
enableShortcuts: true,
transport: {
Run: (body, output) => {
$('#controls input').attr('disabled', true);
writeToGoFilesystem('/main.go', body);
output({
Kind: 'start',
});
goStderr = (buf) => {
output({
Kind: 'stderr',
Body: decoder.decode(buf),
});
};
goStdout = (buf) => {
output({
Kind: 'stdout',
Body: decoder.decode(buf),
});
};
exec(cmds['compile'], ['-p', 'main', '-complete', '-dwarf=false', '-pack', '-importcfg', 'importcfg', 'main.go'])
.then((code) => code || exec(cmds['link'], ['-importcfg', 'importcfg.link', '-buildmode=exe', 'main.a']))
.then((code) => code || exec(readFromGoFilesystem('a.out')))
.then((code) => {
output({
Kind: 'end',
Body: code ? 'status ' + code + '.' : undefined,
});
})
.catch((err) => {
output({
Kind: 'end',
Body: 'wasm error: ' + (err.message || 'unknown'),
});
})
.finally(() => $('#controls input').attr('disabled', false))
;
return {
Kill: () => {},
};
},
},
});
$('#run').val('Run');
$('#fmt').show();
$('#fmt').click(() => {
$('#controls input').attr('disabled', true);
writeToGoFilesystem('/main.go', $('#code').val());
goStderr = (buf) => console.log(decoder.decode(buf));
goStdout = goStderr;
exec(cmds['gofmt'], ['-w', 'main.go'])
.then((code) => {
if (!code) {
$('#code').val(decoder.decode(readFromGoFilesystem('main.go')));
}
})
.finally(() => $('#controls input').attr('disabled', false))
;
});
$('#controls input').attr('disabled', false);
});
});
</script>
</head>
<body itemscope itemtype="http://schema.org/CreativeWork">
<div id="banner">
<div id="head" itemprop="name">The WebAssembly Go Playground</div>
<div id="controls">
<input type="button" value="Loading..." id="run" disabled />
<input type="button" value="Format" id="fmt" style="display:none;" disabled>
<input type="button" value="Share" id="share">
<input type="text" id="shareURL" style="display:none;">
</div>
<a style="float:right;padding:15px;font-family:sans-serif;" href="https://github.com/ccbrown/wasm-go-playground" target="_blank">github.com/ccbrown/wasm-go-playground</a>
</div>
<div id="wrap">
<textarea itemprop="description" id="code" name="code" autocorrect="off" autocomplete="off" autocapitalize="off" spellcheck="false">package main
func main() {
println("Hello, WebAssembly playground!")
}
</textarea>
</div>
<div id="output"></div>
</body>
</html>