Skip to content

Commit 9d3dbdd

Browse files
committed
Wasm runtime: make it possible to use a JS shell (such as d8, sm, jsc)
1 parent 4250ebe commit 9d3dbdd

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

runtime/wasm/runtime.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
const { link, src, generated } = args;
2222

2323
const isNode = globalThis.process?.versions?.node;
24+
const isShell = !globalThis.TextDecoder;
2425

2526
const math = {
2627
cos: Math.cos,
@@ -127,8 +128,8 @@
127128
return WebAssembly?.promising && f ? WebAssembly.promising(f) : f;
128129
}
129130

130-
const decoder = new TextDecoder("utf-8", { ignoreBOM: 1 });
131-
const encoder = new TextEncoder();
131+
const decoder = isShell || new TextDecoder("utf-8", { ignoreBOM: 1 });
132+
const encoder = isShell || new TextEncoder();
132133

133134
function hash_int(h, d) {
134135
d = Math.imul(d, 0xcc9e2d51 | 0);
@@ -219,11 +220,21 @@
219220
array_length: (a) => a.length,
220221
array_get: (a, i) => a[i],
221222
array_set: (a, i, v) => (a[i] = v),
222-
read_string: (l) => decoder.decode(new Uint8Array(buffer, 0, l)),
223+
read_string: (l) =>
224+
isShell
225+
? decodeURIComponent(
226+
escape(String.fromCharCode(...new Uint8Array(buffer, 0, l))),
227+
)
228+
: decoder.decode(new Uint8Array(buffer, 0, l)),
223229
read_string_stream: (l, stream) =>
224230
decoder.decode(new Uint8Array(buffer, 0, l), { stream }),
225231
append_string: (s1, s2) => s1 + s2,
226232
write_string: (s) => {
233+
if (isShell) {
234+
s = unescape(encodeURIComponent(s));
235+
for (let i = 0; i < s.length; ++i) out_buffer[i] = s.charCodeAt(i);
236+
return s.length;
237+
}
227238
var start = 0,
228239
len = s.length;
229240
for (;;) {
@@ -458,8 +469,14 @@
458469
write: (fd, b, o, l, p) =>
459470
fs
460471
? fs.writeSync(fd, b, o, l, p === null ? p : Number(p))
461-
: (console[fd === 2 ? "error" : "log"](
462-
typeof b === "string" ? b : decoder.decode(b.slice(o, o + l)),
472+
: ((isShell ? globalThis.print : console[fd === 2 ? "error" : "log"])(
473+
typeof b === "string"
474+
? b
475+
: isShell
476+
? decodeURIComponent(
477+
escape(String.fromCharCode(...b.slice(o, o + l))),
478+
)
479+
: decoder.decode(b.slice(o, o + l)),
463480
),
464481
l),
465482
read: (fd, b, o, l, p) => fs.readSync(fd, b, o, l, p),
@@ -504,7 +521,7 @@
504521
fstat: (fd, l) => alloc_stat(fs.fstatSync(fd), l),
505522
chmod: (p, perms) => fs.chmodSync(p, perms),
506523
fchmod: (p, perms) => fs.fchmodSync(p, perms),
507-
file_exists: (p) => +fs.existsSync(p),
524+
file_exists: (p) => (isShell ? 0 : +fs.existsSync(p)),
508525
is_directory: (p) => +fs.lstatSync(p).isDirectory(),
509526
is_file: (p) => +fs.lstatSync(p).isFile(),
510527
utimes: (p, a, m) => fs.utimesSync(p, a, m),
@@ -586,9 +603,13 @@
586603
const url = fetchBase ? new URL(src, fetchBase) : src;
587604
return fetch(url);
588605
}
589-
const loadCode = isNode ? loadRelative : fetchRelative;
606+
const loadCode = isNode
607+
? loadRelative
608+
: isShell
609+
? (s) => globalThis.read(s, "binary")
610+
: fetchRelative;
590611
async function instantiateModule(code) {
591-
return isNode
612+
return isNode || isShell
592613
? WebAssembly.instantiate(await code, imports, options)
593614
: WebAssembly.instantiateStreaming(code, imports, options);
594615
}

0 commit comments

Comments
 (0)