|
21 | 21 | const { link, src, generated } = args; |
22 | 22 |
|
23 | 23 | const isNode = globalThis.process?.versions?.node; |
| 24 | + const isShell = !globalThis.TextDecoder; |
24 | 25 |
|
25 | 26 | const math = { |
26 | 27 | cos: Math.cos, |
|
127 | 128 | return WebAssembly?.promising && f ? WebAssembly.promising(f) : f; |
128 | 129 | } |
129 | 130 |
|
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(); |
132 | 133 |
|
133 | 134 | function hash_int(h, d) { |
134 | 135 | d = Math.imul(d, 0xcc9e2d51 | 0); |
|
219 | 220 | array_length: (a) => a.length, |
220 | 221 | array_get: (a, i) => a[i], |
221 | 222 | 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)), |
223 | 229 | read_string_stream: (l, stream) => |
224 | 230 | decoder.decode(new Uint8Array(buffer, 0, l), { stream }), |
225 | 231 | append_string: (s1, s2) => s1 + s2, |
226 | 232 | 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 | + } |
227 | 238 | var start = 0, |
228 | 239 | len = s.length; |
229 | 240 | for (;;) { |
|
458 | 469 | write: (fd, b, o, l, p) => |
459 | 470 | fs |
460 | 471 | ? 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)), |
463 | 480 | ), |
464 | 481 | l), |
465 | 482 | read: (fd, b, o, l, p) => fs.readSync(fd, b, o, l, p), |
|
504 | 521 | fstat: (fd, l) => alloc_stat(fs.fstatSync(fd), l), |
505 | 522 | chmod: (p, perms) => fs.chmodSync(p, perms), |
506 | 523 | fchmod: (p, perms) => fs.fchmodSync(p, perms), |
507 | | - file_exists: (p) => +fs.existsSync(p), |
| 524 | + file_exists: (p) => (isShell ? 0 : +fs.existsSync(p)), |
508 | 525 | is_directory: (p) => +fs.lstatSync(p).isDirectory(), |
509 | 526 | is_file: (p) => +fs.lstatSync(p).isFile(), |
510 | 527 | utimes: (p, a, m) => fs.utimesSync(p, a, m), |
|
586 | 603 | const url = fetchBase ? new URL(src, fetchBase) : src; |
587 | 604 | return fetch(url); |
588 | 605 | } |
589 | | - const loadCode = isNode ? loadRelative : fetchRelative; |
| 606 | + const loadCode = isNode |
| 607 | + ? loadRelative |
| 608 | + : isShell |
| 609 | + ? (s) => globalThis.read(s, "binary") |
| 610 | + : fetchRelative; |
590 | 611 | async function instantiateModule(code) { |
591 | | - return isNode |
| 612 | + return isNode || isShell |
592 | 613 | ? WebAssembly.instantiate(await code, imports, options) |
593 | 614 | : WebAssembly.instantiateStreaming(code, imports, options); |
594 | 615 | } |
|
0 commit comments