@ttsc/wasm 0.12.3 → 0.12.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,39 +7,43 @@
7
7
  // The boot helper is parameterized by `apiName` so any wasm built with
8
8
  // `host.Expose(...)` can be loaded the same way. The base wasm uses "ttsc";
9
9
  // downstream consumers pick their own (e.g. "ttscPlayground", "ttscTypia").
10
-
11
- import { createMemFS, type IMemFSHost } from "./MemFS";
10
+ import { type IMemFSHost, createMemFS } from "./MemFS";
12
11
  import type { ITtscApi } from "./api";
13
12
 
14
13
  declare const importScripts: (...urls: string[]) => void;
15
14
 
15
+ /** Options for `bootTtsc`. All fields except `wasmUrl` have sensible defaults. */
16
16
  export interface IBootTtscOptions {
17
17
  /** URL of the .wasm to fetch. */
18
18
  wasmUrl: string;
19
19
  /** URL of wasm_exec.js. Defaults to the same directory as wasmUrl. */
20
20
  wasmExecUrl?: string;
21
21
  /**
22
- * globalThis property name the wasm binds. Must match the value the wasm
23
- * was built with (the `apiName` passed to `host.Expose`). Defaults to
24
- * "ttsc".
22
+ * GlobalThis property name the wasm binds. Must match the value the wasm was
23
+ * built with (the `apiName` passed to `host.Expose`). Defaults to "ttsc".
25
24
  */
26
25
  apiName?: string;
27
26
  /**
28
- * Optional pre-existing MemFS host. When omitted, a fresh one is created
29
- * and stored on the returned BootResult. Pass an existing host when you
30
- * want to boot multiple wasms over the same filesystem (e.g. base ttsc +
31
- * a typia wasm) so they share project sources.
27
+ * Optional pre-existing MemFS host. When omitted, a fresh one is created and
28
+ * stored on the returned BootResult. Pass an existing host when you want to
29
+ * boot multiple wasms over the same filesystem (e.g. base ttsc + a typia
30
+ * wasm) so they share project sources.
32
31
  */
33
32
  host?: IMemFSHost;
34
33
  }
35
34
 
35
+ /** Handle returned by `bootTtsc` once the wasm is ready. */
36
36
  export interface IBootResult {
37
+ /** The typed API proxy bound by the wasm to `globalThis[apiName]`. */
37
38
  api: ITtscApi;
39
+ /** The MemFS instance shared with the wasm's virtual filesystem. */
38
40
  host: IMemFSHost;
39
41
  }
40
42
 
41
43
  /** Boot a host-built wasm. Re-entrant only if you reuse the same `host`. */
42
- export async function bootTtsc(options: IBootTtscOptions): Promise<IBootResult> {
44
+ export async function bootTtsc(
45
+ options: IBootTtscOptions,
46
+ ): Promise<IBootResult> {
43
47
  const wasmUrl = options.wasmUrl;
44
48
  const wasmExecUrl = options.wasmExecUrl ?? defaultWasmExecUrl(wasmUrl);
45
49
  const apiName = options.apiName ?? "ttsc";
@@ -64,11 +68,12 @@ export async function bootTtsc(options: IBootTtscOptions): Promise<IBootResult>
64
68
 
65
69
  const response = await fetch(wasmUrl);
66
70
  if (!response.ok) {
67
- throw new Error(
68
- `bootTtsc: failed to fetch ${wasmUrl}: ${response.status}`,
69
- );
71
+ throw new Error(`bootTtsc: failed to fetch ${wasmUrl}: ${response.status}`);
70
72
  }
71
- const wasm = await WebAssembly.instantiateStreaming(response, go.importObject);
73
+ const wasm = await WebAssembly.instantiateStreaming(
74
+ response,
75
+ go.importObject,
76
+ );
72
77
  // go.run never resolves until the wasm exits; we don't await it.
73
78
  void go.run(wasm.instance);
74
79
  await ready;
@@ -81,17 +86,35 @@ export async function bootTtsc(options: IBootTtscOptions): Promise<IBootResult>
81
86
  return { api, host };
82
87
  }
83
88
 
89
+ /**
90
+ * Derive the `wasm_exec.js` URL from the wasm URL by replacing the filename.
91
+ *
92
+ * If `wasmUrl` has no directory component, returns `"wasm_exec.js"` (same
93
+ * directory as the caller's base URL).
94
+ */
84
95
  function defaultWasmExecUrl(wasmUrl: string): string {
85
96
  const slash = wasmUrl.lastIndexOf("/");
86
97
  if (slash < 0) return "wasm_exec.js";
87
98
  return wasmUrl.slice(0, slash + 1) + "wasm_exec.js";
88
99
  }
89
100
 
101
+ /**
102
+ * Minimal shape of the `Go` constructor that `wasm_exec.js` exports on
103
+ * `globalThis`. Only the members we actually use are typed here.
104
+ */
90
105
  interface IGoInstance {
91
106
  importObject: WebAssembly.Imports;
92
107
  run(instance: WebAssembly.Instance): Promise<void>;
93
108
  }
94
109
 
110
+ /**
111
+ * Minimal `process` shim required by `wasm_exec.js` in non-Node environments.
112
+ *
113
+ * Go's js/wasm bridge reads `process.pid`, `process.ppid`, and calls
114
+ * `process.cwd()`. `getuid`/`getgid` and friends return `-1` (root-less).
115
+ * `umask` and `getgroups` are never exercised by the compiler but are included
116
+ * for completeness so unexpected calls surface as clear errors.
117
+ */
95
118
  function createProcessShim(): Record<string, unknown> {
96
119
  return {
97
120
  getuid: () => -1,