-
Notifications
You must be signed in to change notification settings - Fork 88
/
editor.html
892 lines (844 loc) · 35.9 KB
/
editor.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
<!DOCTYPE html>
<html lang="en">
<head>
<title>AssemblyScript Editor</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Try out AssemblyScript right in your browser." />
<link rel="preconnect" href="https://cdn.jsdelivr.net" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap" rel="stylesheet">
<style>
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
font-size: 16px;
color: #aaa;
}
/* Header (not visible if sandboxed) */
#header {
display: none;
background: #007acc;
font-size: 12px;
color: #fff;
padding: 4px 10px;
height: 25px;
box-sizing: border-box;
text-align: center;
}
#header h1, #header p {
margin: 0;
padding: 0;
font-size: 1em;
display: inline-block;
padding: 0 5px;
}
#header a {
color: #fff;
}
/* Loading indicator */
#loading {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(30, 30, 30, 0.8) url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMDAgMTAwIj48cGF0aCBmaWxsPSIjMDA3YWNjIiBkPSJNNzMgNTBjMC0xMi43LTEwLjMtMjMtMjMtMjNTMjcgMzcuMyAyNyA1MG0zLjkgMGMwLTEwLjUgOC41LTE5LjEgMTkuMS0xOS4xUzY5LjEgMzkuNSA2OS4xIDUwIj48YW5pbWF0ZVRyYW5zZm9ybSBhdHRyaWJ1dGVOYW1lPSJ0cmFuc2Zvcm0iIGF0dHJpYnV0ZVR5cGU9IlhNTCIgZHVyPSIxcyIgZnJvbT0iMCA1MCA1MCIgcmVwZWF0Q291bnQ9ImluZGVmaW5pdGUiIHRvPSIzNjAgNTAgNTAiIHR5cGU9InJvdGF0ZSIvPjwvcGF0aD48L3N2Zz4=') center center no-repeat;
background-size: 100px 100px;
z-index: 9000;
user-select: none;
}
/* Editor tabs */
#tabs {
color: #fff;
background: #2d2d2d;
padding: 0 8px 0 8px;
user-select: none;
height: 42px;
box-sizing: border-box;
}
#tabs .tab {
display: inline-block;
font-size: 0.9rem;
font-weight: 300;
padding: 12px 24px 13px 52px;
background: #2d2d2d;
background-size: 20px 20px;
background-repeat: no-repeat;
background-position: 21px center;
cursor: pointer;
text-decoration: none;
filter: grayscale(100%);
opacity: 0.6;
}
#tabs .tab:hover {
opacity: 0.8;
}
#tabs .tab.active {
background-color: #1e1e1e;
filter: none;
opacity: 1;
}
#tabs .tab.disabled {
display: none;
}
#tabs .tab.source {
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAzMiAzMiI+PHRpdGxlPmZpbGVfdHlwZV90eXBlc2NyaXB0X29mZmljaWFsPC90aXRsZT48cG9seWdvbiBwb2ludHM9IjIgMTYgMiAzMCAxNiAzMCAzMCAzMCAzMCAxNiAzMCAyIDE2IDIgMiAyIDIgMTYiIHN0eWxlPSJmaWxsOiMwMDdhY2MiLz48cGF0aCBkPSJNMjQuNTY0LDE0Ljg4NGEzLjQ4NSwzLjQ4NSwwLDAsMSwxLjc1MSwxLjAwOSw0LjYxMSw0LjYxMSwwLDAsMSwuNjcxLjljLjAwOS4wMzYtMS4yMDkuODUzLTEuOTQ3LDEuMzExLS4wMjcuMDE4LS4xMzMtLjEtLjI1My0uMjc2YTEuNTg3LDEuNTg3LDAsMCwwLTEuMzE2LS43OTFjLS44NDktLjA1OC0xLjQuMzg3LTEuMzkxLDEuMTI5YTEuMDI3LDEuMDI3LDAsMCwwLC4xMi41MjRjLjE4Ny4zODcuNTMzLjYxOCwxLjYyMiwxLjA4OSwyLC44NjIsMi44NjIsMS40MzEsMy40LDIuMjRhNC4wNjMsNC4wNjMsMCwwLDEsLjMyNCwzLjQxMywzLjc1MywzLjc1MywwLDAsMS0zLjEsMi4yMTgsOC41ODQsOC41ODQsMCwwLDEtMi4xMzMtLjAyMiw1LjE0NSw1LjE0NSwwLDAsMS0yLjg0OS0xLjQ4NCw0Ljk0Nyw0Ljk0NywwLDAsMS0uNzI5LTEuMDgsMi4wOTIsMi4wOT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uNjUgMTYgMjEuNDM0IiBzdHlsZT0iZmlsbDojZWJlYmViIi8+PHBvbHlnb24gcG9pbnRzPSIxNS45ODkgMTMuNDA3IDE1Ljk4OSAxNi40OTggMTkuNzk1IDE2LjQ5OCAxOS40MzcgMjAuNTA3IDE1Ljk4OSAyMS40MzcgMTUuOTg5IDI0LjY1MyAyMi4zMjYgMjIuODk2IDIyLjM3MiAyMi4zNzQgMjMuMDk4IDE0LjIzNyAyMy4xNzQgMTMuNDA3IDIyLjM0MSAxMy40MDcgMTUuOTg5IDEzLjQwNyIgc3R5bGU9ImZpbGw6I2ZmZiIvPjxwb2x5Z29uIHBvaW50cz0iMTUuOTg5IDcuMTUxIDE1Ljk4OSA5LjA3MSAxNS45ODkgMTAuMjM1IDE1Ljk4OSAxMC4yNDIgMjMuNDQ1IDEwLjI0MiAyMy40NDUgMTAuMjQyIDIzLjQ1NSAxMC4yNDIgMjMuNTE3IDkuNTQ4IDIzLjY1OCA3Ljk4MSAyMy43MzIgNy4xNTEgMTUuOTg5IDcuMTUxIiBzdHlsZT0iZmlsbDojZmZmIi8+PC9zdmc+');
}
#tabs .tab.play {
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0OCA0OCI+PHBhdGggZmlsbD0iI2RkZCIgZD0iTTE2IDEwdjI4bDIyLTE0eiIvPjwvc3ZnPg==')
}
@media only screen and (max-width: 700px) {
#tabs .tab span {
visibility: hidden;
}
#tabs .tab {
padding-right: 10px;
width: 0;
}
}
/* Editor buttons */
#buttons {
user-select: none;
float: right;
height: 42px;
padding: 8px;
margin-right: 31px;
box-sizing: border-box;
}
#buttons a {
color: #fff;
padding: 1px 0.4rem;
text-align: center;
vertical-align: middle;
text-decoration: none !important;
cursor: pointer;
opacity: 0.5;
}
#buttons a:hover {
opacity: 1.0;
}
/* Popups */
.popup {
position: absolute;
background: #3c3c3c;
padding: 0.5rem;
z-index: 2501;
animation: fadeIn 83ms linear;
box-shadow: 0 3px 3px 1px rgba(0,0,0,0.3);
display: none;
font-size: 0.8rem;
user-select: none;
}
.popup:before {
content: "";
position: absolute;
top: -21px;
right: 12px;
border: solid 12px transparent;
border-bottom-color: #3c3c3c;
z-index: 2502;
pointer-events: none;
}
/* Options */
#options .columns {
display: flex;
min-width: 520px;
}
#options .column {
width: 50%;
}
#options h5 {
padding: 5px 0;
margin: 0;
color: #aaa;
font-size: 0.85em;
}
#options p {
margin: 0;
padding: 0.2em 0;
color: #eee;
font-size: 0.95em;
}
#options p:first-child {
padding-top: 0;
}
#options a {
color: #fff;
}
#options label {
position: relative;
top: -2px;
cursor: pointer;
}
#options input {
box-sizing: border-box;
}
#options input[type="number"] {
width: calc(50% - 6px);
margin-left: 3px;
}
#options input[type="text"] {
width: calc(100% - 6px);
margin-left: 3px;
}
/* Editor panes */
.pane {
position: absolute;
width: 100%;
height: calc(100% - 42px);
z-index: 0;
background: #1e1e1e;
}
.pane.active {
z-index: 1;
}
.pane.play iframe {
width: 100%;
height: 100%;
border: 0;
background: #fff;
}
#clipboardHelper {
position: absolute;
z-index: 0;
width: 100%;
opacity: 0;
}
</style>
</head>
<body>
<div id="header">
<h1>AssemblyScript Editor</h1>
<p><a href="introduction.html" target="_blank" rel="noopener">Language documentation</a></p>
</div>
<div id="loading"></div>
<div id="buttons">
<a class="button share" title="Copy shareable Link">🔗</a>
<a class="button options" title="Compiler Options">⚙️</a>
</div>
<div id="options" class="popup">
<div class="columns">
<div class="column">
<h5>Optimize</h5>
<div class="option">
<p>
<input type="radio" name="option-optimize" id="option-optimize-speed" checked /><label for="option-optimize-speed"> For speed</label>
<input type="radio" name="option-optimize" id="option-optimize-size" /><label for="option-optimize-size"> For size</label>
<input type="radio" name="option-optimize" id="option-optimize-none" /><label for="option-optimize-none"> None</label>
</p>
<p><input id="option-noAssert" type="checkbox" /><label for="option-noAssert"> Disable assertions</label></p>
<p><input id="option-debug" type="checkbox" /><label for="option-debug"> Include debug information</label></p>
</div>
<h5>Runtime</h5>
<div class="option">
<p>
<input type="radio" name="option-runtime" id="option-runtime-incremental" checked /><label for="option-runtime-incremental"> incremental</label>
<input type="radio" name="option-runtime" id="option-runtime-minimal" /><label for="option-runtime-minimal"> minimal</label>
<input type="radio" name="option-runtime" id="option-runtime-stub" /><label for="option-runtime-stub"> stub</label>
</p>
<p><input id="option-exportRuntime" type="checkbox" /><label for="option-exportRuntime"> Export runtime helpers to host</label></p>
</div>
<h5>Features</h5>
<div class="option">
<p><input id="option-noExportMemory" type="checkbox" /><label for="option-noExportMemory"> Don't export memory to host</label></p>
<p><input id="option-importMemory" type="checkbox" /><label for="option-importMemory"> Import memory from host</label></p>
<p><input id="option-zeroFilledMemory" type="checkbox" disabled/><label for="option-zeroFilledMemory"> Assume imported memory is zeroed</label></p>
<p><input id="option-exportTable" type="checkbox" /><label for="option-exportTable"> Export function table to host</label></p>
<p><input id="option-importTable" type="checkbox" /><label for="option-importTable"> Import function table from host</label></p>
<p><input id="option-exportStart" type="checkbox" /><label for="option-exportStart"> Export start function (as _start)</label></p>
</div>
</div>
<div class="column">
<h5>Advanced features</h5>
<div class="option">
<p style="white-space: nowrap"><input id="option-memoryBase" type="number" step="1" min="0" max="4294967295" placeholder="Memory base" /> <input id="option-tableBase" type="number" step="1" min="0" max="10000000" placeholder="Table base" /></p>
<p><input id="option-use" type="text" placeholder="Use directives, comma-separated" /></p>
</div>
<h5>Future features</h5>
<div class="option">
<p><input id="option-feature-simd" type="checkbox" /><label for="option-feature-simd"> Enable SIMD</label></p>
<p><input id="option-feature-threads" type="checkbox" /><label for="option-feature-threads"> Enable Threads</label></p>
<p><input id="option-feature-referencetypes" type="checkbox" /><label for="option-feature-referencetypes"> Enable Reference Types</label></p>
<p><input id="option-feature-wasm-gc" type="checkbox" /><label for="option-feature-wasm-gc"> Enable WebAssembly GC</label></p>
</div>
<h5>Text format</h5>
<div class="option">
<p><input id="option-use-wast" type="checkbox" /><label for="option-use-wast"> Use s-expression text format (non-standard)</label></p>
</div>
<h5>Documentation</h5>
<div class="option">
<p><a href="compiler.html#command-line-options" target="_blank" rel="noopener">Command line options</a></p>
<p><a href="garbage-collection.html#runtime-variants" target="_blank" rel="noopener">Runtime variants</a></p>
</div>
</div>
</div>
</div>
<div id="clipboard" class="popup">
Shareable link copied to clipboard.
</div>
<div id="tabs">
<a id="sourceTab" class="tab source active" title="AssemblyScript source code"><span>module.ts</span></a>
<a id="binaryTab" class="tab binary" title="Click to Compile"><span>module.wat</span></a>
<a id="htmlTab" class="tab html" title="JavaScript host code"><span>index.html</span></a>
<a id="playTab" class="tab play" title="Click to Compile & Run"><span>Run</span></a>
</div>
<div id="panes">
<div id="source" class="pane source active" aria-labelledby="sourceTab"></div>
<div id="binary" class="pane binary" aria-labelledby="binaryTab"></div>
<div id="html" class="pane html" aria-labelledby="htmlTab"></div>
<div id="play" class="pane play" aria-labelledby="htmlTab">
<iframe id="play-frame" title="Play frame" src="data:text/html;base64," sandbox="allow-scripts allow-pointer-lock"></iframe>
</div>
</div>
<input type="text" id="clipboardHelper" aria-hidden="true" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/min/vs/loader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/assemblyscript@latest/dist/web.js"></script>
<script type="module">
import asc from "assemblyscript/asc";
const MONACO_VERSION = '0.34.0'
const MONACO_EDITOR_FONT = 'JetBrains Mono'
function crossDomainWorker() {
if (!window.MonacoEnvironment) return
const oldGetWorkerUrl = window.MonacoEnvironment.getWorkerUrl
if (oldGetWorkerUrl.$) return
oldGetWorkerUrl.$ = true
const isSameOrigin = url => {
const loc = window.location
const a = document.createElement('a')
a.href = url
return {
isSame: a.hostname === loc.hostname && a.port === loc.port && a.protocol === loc.protocol,
// maybe the protocol is relative
url: a.href,
}
}
window.MonacoEnvironment.getWorkerUrl = (moduleId, label) => {
const workerUrl = oldGetWorkerUrl(moduleId, label)
const { isSame, url } = isSameOrigin(workerUrl)
/* istanbul ignore else */
if (isSame) return workerUrl
else {
let blob
try {
blob = new Blob([`importScripts('${url}');`], { type: 'application/javascript' })
} catch (e) {
const blobBuilder = new window.BlobBuilder()
blobBuilder.append(`importScripts('${url}');`)
blob = blobBuilder.getBlob('application/javascript')
}
return window.URL.createObjectURL(blob)
}
}
}
require.config({
paths: {
'vs': `https://cdn.jsdelivr.net/npm/monaco-editor@${MONACO_VERSION}/min/vs`
}
})
crossDomainWorker()
const isIframe = window.parent !== window
if (!isIframe) {
document.getElementById('header').style.display = 'block'
document.getElementById('buttons').style.paddingRight = '15px'
document.querySelectorAll('.pane').forEach(element => {
element.style.height = 'calc(100% - 42px - 25px)'
})
}
const sourcePane = document.getElementById('source')
const binaryPane = document.getElementById('binary')
const htmlPane = document.getElementById('html')
const playFrame = document.getElementById('play-frame')
const optionsPopup = document.getElementById('options')
const clipboardPopup = document.getElementById('clipboard')
const optionOptimizeSpeed = document.getElementById('option-optimize-speed')
const optionOptimizeSize = document.getElementById('option-optimize-size')
const optionOptimizeNone = document.getElementById('option-optimize-none')
const optionNoAssert = document.getElementById('option-noAssert')
const optionDebug = document.getElementById('option-debug')
const optionRuntimeIncremental = document.getElementById('option-runtime-incremental')
const optionRuntimeMinimal = document.getElementById('option-runtime-minimal')
const optionRuntimeStub = document.getElementById('option-runtime-stub')
const optionExportRuntime = document.getElementById('option-exportRuntime')
const optionNoExportMemory = document.getElementById('option-noExportMemory')
const optionImportMemory = document.getElementById('option-importMemory')
const optionZeroFilledMemory = document.getElementById('option-zeroFilledMemory')
const optionExportTable = document.getElementById('option-exportTable')
const optionImportTable = document.getElementById('option-importTable')
const optionExportStart = document.getElementById('option-exportStart')
const optionMemoryBase = document.getElementById('option-memoryBase')
const optionTableBase = document.getElementById('option-tableBase')
const optionUse = document.getElementById('option-use')
const optionFeatureSimd = document.getElementById('option-feature-simd')
const optionFeatureThreads = document.getElementById('option-feature-threads')
const optionFeatureReferencetypes = document.getElementById('option-feature-referencetypes')
const optionFeatureWasmGC = document.getElementById('option-feature-wasm-gc')
const optionUseWast = document.getElementById('option-use-wast')
const binaryTabElement = document.getElementById('binaryTab')
const optionCheckboxes = {
'noAssert': optionNoAssert,
'debug': optionDebug,
'exportRuntime': optionExportRuntime,
'noExportMemory': optionNoExportMemory,
'importMemory': optionImportMemory,
'zeroFilledMemory': optionZeroFilledMemory,
'exportTable': optionExportTable,
'importTable': optionImportTable,
'exportStart': optionExportStart,
'useWast': optionUseWast,
}
const optionNumerical = {
'memoryBase': optionMemoryBase,
'tableBase': optionTableBase
}
const optionFeatures = {
'simd': optionFeatureSimd,
'threads': optionFeatureThreads,
'reference-types': optionFeatureReferencetypes,
'gc': optionFeatureWasmGC
}
const optionUseRe = /^(\w+)=(\w+(\/\w+|\.\w+)*)$/
var module_wast = '(module)\n'
var module_wasm = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0])
var module_js = '\n';
var didCompile = false
require([
'vs/editor/editor.main',
'scripts/wat.js'
], async (
{ languages, editor },
{ config: watLanguageConfig, tokens: watLanguageTokens }
) => {
// FIXME: https://github.com/microsoft/monaco-editor/issues/2962
// Add WebAssembly Text Format language
languages.register({ id: 'wat' })
languages.setLanguageConfiguration('wat', watLanguageConfig)
languages.setMonarchTokensProvider('wat', watLanguageTokens)
// Extend the default theme with WebAssembly rules
editor.defineTheme('vs-wasm', {
base: 'vs-dark',
inherit: true,
rules: [
// Common
{ token: 'number', foreground: 'ffc37a' },
{ token: 'comment', foreground: '595959', fontStyle: 'italic' },
// WebAssembly
{ token: 'instruction', foreground: 'dcdcaa' },
{ token: 'controlInstruction', foreground: 'c586c0' },
{ token: 'identifier', foreground: '9cdcf0' },
// TypeScript (reset)
{ token: 'identifier.ts', foreground: 'd4d4d4' },
// Diagnostics
{ token: 'error', foreground: 'd16969' },
{ token: 'warning', foreground: 'dcdcaa' },
{ token: 'info', foreground: '11a8cd' },
{ token: 'pedantic', foreground: 'c586c0' },
{ token: 'underline', foreground: 'd16969' }
],
colors: {
'scrollbar.shadow': '#1e1e1eff',
'editorLineNumber.foreground': '#444444',
'editorLineNumber.activeForeground': '#888888'
}
})
// Add AssemblyScript Standard Library definition
languages.typescript.typescriptDefaults.addExtraLib(
asc.definitionFiles.assembly,
"assemblyscript/std/assembly/index.d.ts"
)
// Obtain the source provided via the location's hash
let source = document.location.hash
let html = ''
if (source.length > 1) {
source = source.substr(1)
try {
source = atob(source)
} catch (e) {
source = `/* ${e.message} */`
}
if (source.startsWith('#!') && !source.startsWith('#!html')) {
let end = source.indexOf('\n')
if (!end) end = source.length
deserializeCompilerOptions(source.substring(2, end))
source = source.substring(end + 1)
}
const htmlMatch = /^#!html$/m.exec(source)
if (htmlMatch) {
html = source.substring(htmlMatch.index + htmlMatch[0].length).trim() + '\n'
source = source.substring(0, htmlMatch.index).trimRight() + '\n'
}
} else {
source = ''
}
// Waiting for EDITOR_FONT to load before set it in editor
await document.fonts.load(`1em ${MONACO_EDITOR_FONT}`)
// Common editor options
const commonEditorOptions = {
value: '',
theme: 'vs-wasm',
automaticLayout: true,
scrollBeyondLastLine: false,
tabSize: 2,
fontSize: 13,
fontFamily: MONACO_EDITOR_FONT,
fontLigatures: true,
padding: {
bottom: 18,
top: 18
},
scrollbar: {
alwaysConsumeMouseWheel: false
},
minimap: {
enabled: false
},
matchBrackets: 'near',
renderLineHighlight: 'none',
cursorStyle: 'line-thin',
cursorBlinking: 'smooth'
}
// Create editor panes
const sourceModel = editor.createModel(source, 'typescript')
const sourceEditor = editor.create(sourcePane, Object.assign({}, commonEditorOptions, {
model: sourceModel,
}))
const binaryModel = editor.createModel('(module\n 🚀\n)\n', 'wat')
binaryModel.updateOptions({ tabSize: 1 });
const binaryEditor = editor.create(binaryPane, Object.assign({}, commonEditorOptions, {
model: binaryModel,
readOnly: true
}))
const htmlModel = editor.createModel(html, 'html')
const htmlEditor = editor.create(htmlPane, Object.assign({}, commonEditorOptions, {
model: htmlModel
}))
// Intercept scroll event when focused
window.addEventListener('wheel', evt => {
if (document.hasFocus()) {
evt.stopPropagation()
evt.preventDefault()
}
}, { passive: false })
// Make tabs switchable
document.querySelectorAll('.tab').forEach(element => {
element.addEventListener('click', () => {
document.querySelectorAll('.tab').forEach(element => element.classList.remove('active'))
element.classList.add('active')
document.querySelectorAll('.pane').forEach(element => element.classList.remove('active'))
const pane = document.getElementById(element.id.substring(0, element.id.length - 3))
pane.classList.add('active')
if (element.id == 'binaryTab') {
binaryEditor.setValue('(module\n 🚀\n)\n')
setTimeout(() => {
compile()
}, 10)
} else if (element.id == 'playTab') {
function serialize() {
return 'data:text/html;base64,' + btoa([
'<script>\n',
'async function compile() {\n',
' return await WebAssembly.compile(new Uint8Array([', module_wasm.join(','), ']));\n',
'}\n',
module_js.replace(/^export async function instantiate\(/m, 'async function instantiate('), '<', '/script>\n\n',
htmlEditor.getValue().trimRight()
].join(''))
}
if (didCompile) {
playFrame.src = serialize()
} else {
playFrame.src = 'data:text/html;base64,Q29tcGlsaW5nLi4u' // Compiling...
compile(() => {
playFrame.src = serialize()
})
}
}
})
})
// Make options clickable
let optionsVisible = false
function toggleOptions(evt) {
if (optionsVisible = !optionsVisible) {
optionsPopup.style.display = 'block'
const bounds = evt.target.getBoundingClientRect()
optionsPopup.style.top = (bounds.y + bounds.height + 12) + 'px'
optionsPopup.style.right = (window.innerWidth - (bounds.x + bounds.width / 2) - 24) + 'px'
} else {
optionsPopup.style.display = 'none'
}
}
document.querySelector('.button.options').addEventListener('click', toggleOptions)
document.querySelector('#tabs').addEventListener('click', () => {
if (optionsVisible) toggleOptions(null)
})
Array.prototype.slice.call(document.querySelectorAll('.pane'))
.map(pane => pane.addEventListener('click', () => {
if (optionsVisible) toggleOptions(null)
}))
// --zeroFilledMemory depends on --importMemory
optionImportMemory.addEventListener('change', evt => {
optionZeroFilledMemory.disabled = !evt.target.checked
if (!evt.target.checked) optionZeroFilledMemory.checked = false
})
// WebAssembly GC depends on Reference Types
let optionFeatureReferencetypesEnabled = optionFeatureReferencetypes.checked
optionFeatureWasmGC.addEventListener('change', evt => {
if (evt.target.checked) {
optionFeatureReferencetypesEnabled = optionFeatureReferencetypes.checked
optionFeatureReferencetypes.checked = true
optionFeatureReferencetypes.disabled = true
} else {
optionFeatureReferencetypes.disabled = false
optionFeatureReferencetypes.checked = optionFeatureReferencetypesEnabled
}
})
let useWast = optionUseWast.checked
binaryTabElement.innerHTML = useWast ? 'module.wast' : 'module.wat'
optionUseWast.addEventListener('change', evt => {
useWast = evt.target.checked
binaryTabElement.innerHTML = useWast ? 'module.wast' : 'module.wat'
})
// Make contents shareable
let clipboardTimer = null
document.querySelector('.button.share').addEventListener('click', evt => {
const helper = document.getElementById('clipboardHelper')
const serial = btoa([
'#!', serializeCompilerOptions(), '\n',
sourceEditor.getValue().trim(),
'\n#!html\n',
htmlEditor.getValue().trim()
].join(''))
document.location.hash = serial
helper.value = document.location
helper.select()
document.execCommand('copy')
helper.value = ''
helper.focus()
clipboardPopup.style.display = 'block'
const button = evt.target
button.setAttribute('href', document.location)
const bounds = button.getBoundingClientRect()
clipboardPopup.style.top = (bounds.y + bounds.height + 12) + 'px'
clipboardPopup.style.right = (window.innerWidth - (bounds.x + bounds.width / 2) - 12) + 'px'
if (clipboardTimer) clearTimeout(clipboardTimer)
clipboardTimer = setTimeout(() => {
clipboardPopup.style.display = 'none'
clipboardTimer = null
}, 3000)
})
function deserializeCompilerOptions(query) {
const parts = query.split('&')
const options = {}
for (const part of parts) {
const pos = part.indexOf('=')
if (~pos) {
options[decodeURIComponent(part.substring(0, pos))] = part.substring(pos + 1)
} else {
options[decodeURIComponent(part)] = ''
}
}
switch (options.optimize) {
case 'speed': optionOptimizeSpeed.checked = true; break
case 'size': optionOptimizeSize.checked = true; break
case 'none': optionOptimizeNone.checked = true; break
}
switch (options.runtime) {
case 'incremental': optionRuntimeIncremental.checked = true; break
case 'minimal': optionRuntimeMinimal.checked = true; break
case 'stub': optionRuntimeStub.checked = true; break
}
for (let [key, element] of Object.entries(optionCheckboxes)) {
if (options[key] != null) element.checked = true
}
for (let [key, element] of Object.entries(optionNumerical)) {
const value = parseInt(options[key], 10)
if (value > 0) element.value = value
}
let useValue = options['use']
if (typeof useValue === 'string') {
useValue = useValue.trim()
if (useValue.length) {
const use = []
for (const value of useValue.split(',')) {
let match = optionUseRe.exec(value.trim())
if (match) use.push(match[1] + '=' + match[2])
}
if (use.length) optionUse.value = use.join(',')
}
}
if (options.enable && options.enable.length) {
const features = options.enable.split(',')
for (let feature of features) {
if (optionFeatures.hasOwnProperty(feature)) {
optionFeatures[feature].checked = true
}
}
}
}
function serializeCompilerOptions() {
let query = []
if (optionOptimizeSpeed.checked) {
query.push('optimize=speed')
} else if (optionOptimizeSize.checked) {
query.push('optimize=size')
} else if (optionOptimizeNone.checked) {
query.push('optimize=none')
}
if (optionRuntimeIncremental.checked) {
query.push('runtime=incremental')
} else if (optionRuntimeMinimal.checked) {
query.push('runtime=minimal')
} else if (optionRuntimeStub.checked) {
query.push('runtime=stub')
}
for (let [key, element] of Object.entries(optionCheckboxes)) {
if (element.checked) query.push(key)
}
for (let [key, element] of Object.entries(optionNumerical)) {
let value = parseInt(element.value, 10)
if (value > 0) query.push(key + '=' + value)
}
const useValue = optionUse.value.trim()
if (useValue.length) {
const use = []
for (const value of useValue.split(',')) {
const match = optionUseRe.exec(value.trim())
if (match) use.push(match[1] + '=' + match[2])
}
if (use.length) query.push('use=' + use.join(','))
}
const features = []
for (let [key, element] of Object.entries(optionFeatures)) {
if (element.checked) features.push(key)
}
if (features.length) {
query.push('enable=' + features.join(','))
}
return query.join('&')
}
// Gets user specified compiler options
function getCompilerOptions() {
const options = []
if (optionOptimizeSpeed.checked) {
options.push('-O3')
} else if (optionOptimizeSize.checked) {
options.push('-O3z', '--converge')
}
if (optionDebug.checked) {
options.push('--debug')
}
if (optionNoAssert.checked) {
options.push('--noAssert')
}
if (optionRuntimeIncremental.checked) {
options.push('--runtime', 'incremental')
} else if (optionRuntimeMinimal.checked) {
options.push('--runtime', 'minimal')
} else if (optionRuntimeStub.checked) {
options.push('--runtime', 'stub')
}
if (optionExportRuntime.checked) {
options.push('--exportRuntime')
}
if (optionNoExportMemory.checked) {
options.push('--noExportMemory')
}
if (optionImportMemory.checked) {
options.push('--importMemory')
}
if (optionZeroFilledMemory.checked) {
options.push('--zeroFilledMemory')
}
if (optionExportTable.checked) {
options.push('--exportTable')
}
if (optionImportTable.checked) {
options.push('--importTable')
}
if (optionExportStart.checked) {
options.push('--exportStart', '_start')
}
const memoryBase = parseInt(optionMemoryBase.value, 10)
if (memoryBase > 0 && memoryBase <= 4294967295) {
options.push('--memoryBase', memoryBase.toString())
}
const tableBase = parseInt(optionTableBase.value, 10)
if (tableBase > 0 && tableBase <= 10000000) {
options.push('--tableBase', tableBase.toString())
}
const useValue = optionUse.value.trim()
if (useValue.length) {
const use = []
for (const value of useValue.split(',')) {
const match = optionUseRe.exec(value.trim())
if (match) use.push(match[1] + '=' + match[2])
}
if (use.length) options.push('--use', use.join(','))
}
const enable = []
for (let [key, element] of Object.entries(optionFeatures)) {
if (element.checked) enable.push(key)
}
if (enable.length) {
options.push('--enable', enable.join(','))
}
return options
}
// Compiles the source to WebAssembly
function compile(cb) {
const tsModule = 'module.ts'
const jsModule = 'module.js'
const textModule = `module.${useWast ? 'wast' : 'wat'}`
const wasmModule = 'module.wasm'
const stdout = asc.createMemoryStream()
const sources = {
'module.ts': sourceEditor.getValue()
}
const outputs = {}
const config = {
stdout,
stderr: stdout,
readFile: name => Object.prototype.hasOwnProperty.call(sources, name) ? sources[name] : null,
writeFile: (name, contents) => { outputs[name] = contents },
listFiles: () => []
}
const options = [
tsModule,
'--textFile', textModule,
'--outFile', wasmModule,
'--bindings', 'raw',
...getCompilerOptions()
]
asc.main(options, config).then(({ error, stdout }) => {
let output = stdout.toString().trim()
if (output.length) {
output = ';; ' + output.replace(/\n/g, '\n;; ') + '\n'
}
output = ';; INFO asc ' + options.join(' ') + '\n' + output
if (error) {
binaryEditor.setValue(output + `(module\n ;; FAILURE ${error.message}\n)\n`)
} else {
module_wast = outputs[textModule]
module_wasm = outputs[wasmModule]
module_js = outputs[jsModule]
binaryEditor.setValue(output + module_wast)
}
didCompile = true
if (cb) cb(error)
})
}
// Finally, hide the loading animation
document.getElementById('loading').style.display = 'none'
})
</script>
</body>
</html>