-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
124 lines (112 loc) · 3.12 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
<head>
<title>easywasi example</title>
<style>
/* this is not realy needed, but make it look nicer */
body,
html {
margin: 0;
height: 90vh;
}
body {
font-family: sans-serif;
background: #000;
color: #fff;
padding: 20px;
}
#stdio {
height: 100%;
display: flex;
gap: 10px;
}
textarea {
flex-grow: 1;
background: #000;
color: #fff;
padding: 10px;
height: 80%;
}
textarea::-webkit-scrollbar {
width: 1em;
}
textarea::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
}
textarea::-webkit-scrollbar-thumb {
background-color: darkgrey;
outline: 1px solid slategrey;
cursor: grab;
}
</style>
</head>
<body>
<script type="importmap">
{
"imports": {
"@easywasm/wasi": "./easywasm/easywasi.js",
"@zenfs/core": "https://esm.sh/@zenfs/core",
"@zenfs/dom": "https://esm.sh/@zenfs/dom",
"@zenfs/archives": "https://esm.sh/@zenfs/archives"
}
}
</script>
<!-- this is nasty, but there is a bug in CDN/zenfs -->
<script type="importmap">
{
"imports": {
"https://esm.sh/@zenfs/core@%5E2.0.0/path?target=es2022": "https://esm.sh/@zenfs/core/dist/path.js"
}
}
</script>
<p>
This is an example page for
<a href="https://github.com/easywasm/wasi">easywasm/wasi</a>.
</p>
<div id="stdio">
<textarea id="out"></textarea>
<textarea id="err"></textarea>
</div>
<script type="module">
import { WasiPreview1 } from '@easywasm/wasi'
import { configure, InMemory, fs } from '@zenfs/core'
import { IndexedDB } from '@zenfs/dom'
import { Zip } from '@zenfs/archives'
await configure({
mounts: {
'/zip': {
backend: Zip,
data: await fetch('example.zip').then((r) => r.arrayBuffer())
},
'/tmp': InMemory, // goes away on refresh
'/home': IndexedDB // lives between page-loads
}
})
// write persistant counter file, if it does not exist
if (!fs.existsSync('/home/counter')) {
fs.writeFileSync('/home/counter', '\0')
}
const wasi_snapshot_preview1 = new WasiPreview1({
fs,
args: ['coolguy', 'a', 'b', 'c'],
env: {
COOL: 'yep'
}
})
const { instance } = await WebAssembly.instantiateStreaming(fetch('example.wasm'), {
wasi_snapshot_preview1
})
// Here I implement custom stdio. In yours, you don't have to do this, and it will just log/warn
function addText(t, buffer) {
t.value += d.decode(buffer)
t.selectionStart = t.selectionEnd = t.value.length
t.blur()
t.focus()
t.blur()
}
const d = new TextDecoder()
wasi_snapshot_preview1.stdout = (buffer) => addText(document.getElementById('out'), buffer)
wasi_snapshot_preview1.stderr = (buffer) => addText(document.getElementById('err'), buffer)
// this sets up the memory & calls the wasm file's main()
const exitCode = wasi_snapshot_preview1.start(instance.exports)
console.log(`Test run: ${exitCode}`)
</script>
</body>