-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
166 lines (152 loc) · 5.09 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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>Wit wasi runtime jco generated</title>
<base href="." />
</head>
<body>
<script>
fetch("./out-dir/plugin_wasi.wasm")
.then((resp) => {
console.log("RESP:", resp);
return resp.arrayBuffer();
})
.then((wasmBytes) => {
console.log("WASM BYTES:", wasmBytes);
import("./src/bindgen.js").then(async ({ generate, $init }) => {
console.log("BINDGEN:", { generate, $init });
await $init;
let wasiImportsFiles = [
"cli-base",
"clocks",
"filesystem",
"http",
"io",
"logging",
"poll",
"random",
"sockets",
];
let wasiImports = {};
for (let wasiImportFile of wasiImportsFiles) {
// wasiImports[`@bytecodealliance/preview2-shim/${wasiImportFile}`] =
wasiImports[`${wasiImportFile}`] = await import(
`/src/browser/${wasiImportFile}.js`
);
}
console.log("WASI IMPORTS:", wasiImports);
let name = "hello-world";
let opts = {
name,
// map: Object.entries(map ?? {}),
map: [],
instantiation: true,
validLiftingOptimization: false,
noNodejsCompat: true,
tlaCompat: false,
base64Cutoff: 4096,
};
// pass into generate along with bytes
let generated = generate(wasmBytes, opts);
console.log("WITH jco at runtime:", generated);
let { files } = generated;
let bytes = files.find((file) => file[0] == `${name}.js`)[1];
let text = Utf8ArrayToStr(bytes);
console.log("TEXT:", text);
let bytesTs = files.find((file) => file[0] == `${name}.d.ts`)[1];
let textTs = Utf8ArrayToStr(bytesTs);
console.log("TEXT TS:", textTs);
let blob = new Blob([bytes], { type: "text/javascript" });
let url = URL.createObjectURL(blob);
let { instantiate } = await import(url);
let instance = await instantiate(
(core, imports) => {
console.log("Compiling core ...", core, imports);
let file = files.find((f) => f[0] === core)[1];
console.log("Found file", file);
return WebAssembly.compile(file);
// return fetch(`/out-dir/${core}`)
// .then((resp) => resp.arrayBuffer())
// .then((buffer) => {
// console.log("compile core buffer:", buffer);
// return WebAssembly.compile(buffer);
// });
},
{
"import-point": {
default: {
importPoint(point) {
console.log("IN JCO runtime INSTANTIATED IMPORTS", point);
point.x = point.x + 100;
return point;
},
},
},
print: {
default: {
print(msg) {
console.log("IN JCO runtime INSTANTIATED PRINT:", msg);
},
},
},
...wasiImports,
}
);
console.log("INSTANCE:", instance);
let { movePoint, sayHello } = instance;
sayHello();
console.log(movePoint({ x: 50, y: 50 }));
});
});
// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
/* utf.js - UTF-8 <=> UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo <[email protected]>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free. You can redistribute it and/or modify it.
*/
function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while (i < len) {
c = array[i++];
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12:
case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(
((c & 0x0f) << 12) |
((char2 & 0x3f) << 6) |
((char3 & 0x3f) << 0)
);
break;
}
}
return out;
}
</script>
</body>
</html>