-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
224 lines (172 loc) · 4.83 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<html>
<head>
<title>Try Starlark - Online Starlark interpreter</title>
<meta charset="utf-8">
<style>
.renderjson a { text-decoration: none; color: #C00 }
.renderjson .disclosure { color: crimson;
font-size: 150%;
}
.renderjson .syntax { color: #CCC; }
.renderjson .string { color: #444; }
.renderjson .number { color: #808; }
.renderjson .boolean { color: plum; }
.renderjson .key { color: #888; }
.renderjson .keyword { color: lightgoldenrodyellow; }
.renderjson .object.syntax { color: lightseagreen; }
.renderjson .array.syntax { color: lightsalmon; }
.codebox {
outline: none;
height: 600px;
border-radius: 10px;
}
.error {
padding: 20px 0 0;
color: #F00;
}
code {
white-space: pre-wrap;
}
.code-window {
width: 662px;
background: #fff;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.16);
border: 1px solid #000;
margin: 1rem;
overflow: hidden;
border-radius: 0.8rem;
float: left;
}
.code-editor {
position: relative;
width: 100%;
height: 600px;
}
/* Tabs */
#tab1:checked ~ #content1,
#tab2:checked ~ #content2,
#tab3:checked ~ #content3 {
display: block;
}
section {
display: none;
}
input {
display: none;
}
label {
display: inline-block;
margin: 0 0 -1px;
padding: 15px 25px;
font-weight: 600;
text-align: center;
color: #abc;
border: 1px solid #000;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.16);
}
h1 { color: #666; text-align: center; }
</style>
<script type="text/javascript" src="third_party/codeflask.min.js"></script>
<script type="text/javascript" src="third_party/prism-python.js"></script>
<script type="text/javascript" src="third_party/renderjson.js"></script>
<script type="text/javascript" src="wasm_exec.js"></script>
<script type="text/javascript">
async function run(fileUrl) {
try {
const go = new Go();
const file = await fetch(fileUrl, {cache: "no-cache"});
const buffer = await file.arrayBuffer();
const { instance } = await WebAssembly.instantiate(buffer, go.importObject);
go.run(instance);
init();
} catch (err) {
console.error(err);
}
}
setTimeout(() => run("./starlark-web"));
</script>
</head>
<body>
<h1>Starlark Playground</h1>
<p>When you modify this <a href="https://github.com/bazelbuild/starlark">Starlark</a> code, it is
executed in the background. The result of the evaluation is shown as JSON on the right. Values
starting with underscore are hidden. If the values contain booleans, None, or non-JSON objects, you
may get a JSON parse error.</p>
<p>The code uses the <a href="https://github.com/google/starlark-go">Starlark interpreter in Go</a>,
compiled to WebAssembly.</p>
<div class="code-window">
<div class="code-editor"></div>
</div>
<input id="tab1" type="radio" name="tabs" checked>
<label for="tab1">Show output</label>
<input id="tab2" type="radio" name="tabs">
<label for="tab2">Show JSON</label>
<div class="error" id="error"></div>
<section id="content1"><code id="output"></code></section>
<section id="content2">
<div id="wrapper" style="float:left;"> <div id="tree"></div> </div>
</section>
<script>
const flask = new CodeFlask('.code-editor', {
language: 'py',
lineNumbers: true
});
flask.addLanguage('py', Prism.languages.python)
var error = document.getElementById("error");
var output = document.getElementById("output");
var wrapper = document.getElementById("wrapper");
renderjson.set_show_to_level(10);
renderjson.set_max_string_length(50);
// Called from main.go
function setError(msg) {
error.textContent = msg;
}
// Called from main.go
function updateOutput(msg) {
output.textContent += msg + "\n";
}
// Called from main.go
function updateJSON(text) {
var dataStr = text;
try {
var data = JSON.parse(dataStr);
} catch (e) {
console.log("invalid JSON: " + data);
error.textContent = "invalid JSON: " + e;
return;
}
while (wrapper.firstChild) {
wrapper.removeChild(wrapper.firstChild);
}
wrapper.appendChild(renderjson(data));
}
function init() {
flask.updateCode(`def sqr(x): return x * x
a = [sqr(x) for x in range(7)]
_string_methods = dir("")
first_string_methods = _string_methods[0:5]
hashes = { str(x): hash(str(x)) for x in range(4) }
empty = {}
esc = "\\x42"
data = {
"a dict": {
"title": "example",
"count": 2,
"nested": {
"more nested": "yes",
}
},
}
name = "world"
print("Hello {}!".format(name))
`);
flask.onUpdate((code) => {
// Triggered whenever the code is updated.
error.textContent = "";
output.textContent = "";
evalStarlark(flask.getCode()); // wasm function (in main.go)
});
}
</script>
</body>
</html>