@solidjs/compiler-wasm32-wasi 2.0.0-rc.11 → 2.0.0-rc.14

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.
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solidjs/compiler-wasm32-wasi",
3
- "version": "2.0.0-rc.11",
3
+ "version": "2.0.0-rc.14",
4
4
  "main": "compiler.wasi.cjs",
5
5
  "files": [
6
6
  "compiler.wasm32-wasi.wasm",
@@ -26,4 +26,4 @@
26
26
  "@emnapi/core": "1.11.3",
27
27
  "@emnapi/runtime": "1.11.3"
28
28
  }
29
- }
29
+ }
@@ -15,10 +15,9 @@ const handler = new MessageHandler({
15
15
  // eslint-disable-next-line no-console
16
16
  console.log.apply(console, arguments)
17
17
  },
18
- printErr: function() {
18
+ printErr: function () {
19
19
  // eslint-disable-next-line no-console
20
20
  console.error.apply(console, arguments)
21
-
22
21
  errorOutputs.push([...arguments])
23
22
  },
24
23
  })
@@ -46,6 +45,30 @@ const handler = new MessageHandler({
46
45
  }
47
46
  })
48
47
 
48
+ // When this wasm thread dies, raise the addon's crash flag before emnapi reports
49
+ // it. The loader thread may be inside wasm, in a cleanup call that waits on this
50
+ // thread; the flag is what those waits check between short slices, and once it
51
+ // is up they trap instead of waiting for good. It is a word in the shared wasm
52
+ // memory, and the loader posts a view of it (see
53
+ // napi_wasm_thread_crash_flag_address) after it instantiated the wasm, or right
54
+ // after it created this worker. No view, no flag: an addon built with an older
55
+ // napi, or a failure before the view arrived.
56
+ let __addonCrashFlag
57
+ const __beforeReportError = handler.beforeReportError
58
+ handler.beforeReportError = function (...args) {
59
+ if (__addonCrashFlag !== undefined) {
60
+ try {
61
+ Atomics.store(__addonCrashFlag, 0, 1)
62
+ } catch {}
63
+ }
64
+ return __beforeReportError.apply(this, args)
65
+ }
66
+
49
67
  globalThis.onmessage = function (e) {
68
+ const data = e && e.data
69
+ if (data && data.__napiRsAddonCrashFlag instanceof Int32Array) {
70
+ __addonCrashFlag = data.__napiRsAddonCrashFlag
71
+ return
72
+ }
50
73
  handler.handle(e)
51
74
  }
package/wasi-worker.mjs CHANGED
@@ -1,74 +1,186 @@
1
- import fs from "node:fs";
2
- import { createRequire } from "node:module";
3
- import { parse } from "node:path";
4
- import { WASI } from "node:wasi";
5
- import { parentPort, Worker } from "node:worker_threads";
1
+ import fs from 'node:fs'
2
+ import { createRequire } from 'node:module'
3
+ import { parse } from 'node:path'
4
+ import { WASI } from 'node:wasi'
5
+ import { parentPort, threadId, Worker, workerData } from 'node:worker_threads'
6
6
 
7
- const require = createRequire(import.meta.url);
7
+ const require = createRequire(import.meta.url)
8
8
 
9
- const {
10
- instantiateNapiModuleSync,
11
- MessageHandler,
12
- getDefaultContext,
13
- emnapiAsyncWorkPlugin,
14
- emnapiTSFNPlugin,
15
- } = require("@napi-rs/wasm-runtime");
9
+ // The thread spawn that created this worker has already returned, so the loader
10
+ // thread may be waiting on this thread. A failure before the crash hook below is
11
+ // in place (a missing '@napi-rs/wasm-runtime', say) has to raise the crash flags
12
+ // too, then fail the worker as before.
13
+ let handler
14
+ try {
15
+ const {
16
+ instantiateNapiModuleSync,
17
+ MessageHandler,
18
+ getDefaultContext,
19
+ emnapiAsyncWorkPlugin,
20
+ emnapiTSFNPlugin,
21
+ } = require('@napi-rs/wasm-runtime')
16
22
 
17
- if (parentPort) {
18
- parentPort.on("message", (data) => {
19
- globalThis.onmessage({ data });
20
- });
21
- }
23
+ if (parentPort) {
24
+ parentPort.on('message', (data) => {
25
+ globalThis.onmessage({ data })
26
+ })
27
+ }
22
28
 
23
- Object.assign(globalThis, {
24
- self: globalThis,
25
- require,
26
- Worker,
27
- importScripts: function (f) {
28
- ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
29
- },
30
- postMessage: function (msg) {
31
- if (parentPort) {
32
- parentPort.postMessage(msg);
33
- }
34
- },
35
- });
29
+ Object.assign(globalThis, {
30
+ self: globalThis,
31
+ require,
32
+ Worker,
33
+ importScripts: function (f) {
34
+ // oxlint-disable-next-line no-eval -- WASI importScripts polyfill
35
+ ;(0, eval)(fs.readFileSync(f, 'utf8') + '//# sourceURL=' + f)
36
+ },
37
+ postMessage: function (msg) {
38
+ if (parentPort) {
39
+ parentPort.postMessage(msg)
40
+ }
41
+ },
42
+ })
36
43
 
37
- const emnapiContext = getDefaultContext();
44
+ const emnapiContext = getDefaultContext()
38
45
 
39
- const __rootDir = parse(process.cwd()).root;
46
+ const __cwd = process.cwd()
47
+ const __rootDir =
48
+ (workerData && typeof workerData.rootDir === 'string' && workerData.rootDir) ||
49
+ parse(__cwd).root
50
+ const __hostRoot =
51
+ (workerData && typeof workerData.hostRoot === 'string' && workerData.hostRoot) ||
52
+ (process.platform === 'android' ? __cwd : __rootDir)
40
53
 
41
- const handler = new MessageHandler({
42
- onLoad({ wasmModule, wasmMemory }) {
43
- const wasi = new WASI({
44
- version: 'preview1',
45
- env: process.env,
46
- preopens: {
47
- [__rootDir]: __rootDir,
48
- },
49
- });
54
+ handler = new MessageHandler({
55
+ onLoad({ wasmModule, wasmMemory }) {
56
+ const wasi = new WASI({
57
+ version: 'preview1',
58
+ env: process.env,
59
+ preopens: {
60
+ [__rootDir]: __hostRoot,
61
+ [__hostRoot]: __hostRoot,
62
+ },
63
+ })
50
64
 
51
- return instantiateNapiModuleSync(wasmModule, {
52
- childThread: true,
53
- wasi,
54
- context: emnapiContext,
55
- // The wasm links a "basic" emnapi archive (no C async-work /
56
- // threadsafe-function implementations), so every thread that
57
- // instantiates it must provide the JavaScript implementations
58
- // through the emnapi plugins.
59
- plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin],
60
- overwriteImports(importObject) {
61
- importObject.env = {
62
- ...importObject.env,
63
- ...importObject.napi,
64
- ...importObject.emnapi,
65
- memory: wasmMemory
66
- };
67
- },
68
- });
69
- },
70
- });
65
+ return instantiateNapiModuleSync(wasmModule, {
66
+ childThread: true,
67
+ wasi,
68
+ context: emnapiContext,
69
+ // The wasm links a "basic" emnapi archive (no C async-work /
70
+ // threadsafe-function implementations), so every thread that
71
+ // instantiates it must provide the JavaScript implementations
72
+ // through the emnapi plugins.
73
+ plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin],
74
+ overwriteImports(importObject) {
75
+ importObject.env = {
76
+ ...importObject.env,
77
+ ...importObject.napi,
78
+ ...importObject.emnapi,
79
+ memory: wasmMemory,
80
+ }
81
+ },
82
+ })
83
+ },
84
+ })
85
+ } catch (error) {
86
+ if (workerData && workerData.crashFlag instanceof Int32Array) {
87
+ __raiseWasiThreadCrashFlags(error)
88
+ }
89
+ throw error
90
+ }
91
+
92
+ // Tell the loader thread synchronously that this wasm thread died, so its 'exit'
93
+ // teardown does not re-enter wasm and wait on this thread forever. emnapi calls
94
+ // beforeReportError (which runs emnapi_thread_crashed, so the main thread may
95
+ // throw 'unwind' and start exiting) before the 'error' event is even posted.
96
+ // A loader that predates the flag passes none.
97
+ if (workerData && workerData.crashFlag instanceof Int32Array) {
98
+ const __beforeReportError = handler.beforeReportError
99
+ handler.beforeReportError = function (...args) {
100
+ if (!__raiseWasiThreadCrashFlags(args[0])) {
101
+ // No view (the thread was spawned before the loader read the address,
102
+ // say): the export stores into the same word, but only a worker that
103
+ // loaded has an instance to call it on.
104
+ try {
105
+ const __napiThreadCrashed = this.instance?.exports?.napi_wasm_thread_crashed
106
+ if (typeof __napiThreadCrashed === 'function') {
107
+ __napiThreadCrashed()
108
+ }
109
+ } catch {}
110
+ }
111
+ return __beforeReportError.apply(this, args)
112
+ }
113
+ }
114
+
115
+ // The error goes into the shared crash report first. The 'error' event that
116
+ // would carry it is dropped when the loader terminates this worker before the
117
+ // event is sent, so the report is the only copy the loader can count on.
118
+ // Layout: three Int32 words — state (0 empty, 1 writing, 2 written), byte
119
+ // length, threadId — then the UTF-8 JSON of { name, message, stack }. Only the
120
+ // first worker to crash writes it, and it is complete before the flag is raised.
121
+ //
122
+ // Then the loader's flag, then the addon's. The loader thread may already be
123
+ // inside wasm, in a cleanup call that waits on this thread; the addon's flag is
124
+ // what those waits check between short slices. It is a word in the shared wasm
125
+ // memory (the loader passes a view of it, see napi_wasm_thread_crash_flag_address),
126
+ // so no instance is needed: a worker that failed while loading raises it too.
127
+ // Once it is up the waits trap, and the loader, whose flag is already up, turns
128
+ // the throw into its crash rejection. Returns whether the addon's flag was raised.
129
+ function __raiseWasiThreadCrashFlags(error) {
130
+ try {
131
+ __writeCrashReport(workerData.crashReport, error)
132
+ } catch {}
133
+ try {
134
+ Atomics.store(workerData.crashFlag, 0, 1)
135
+ } catch {}
136
+ const addonCrashFlag = workerData.addonCrashFlag
137
+ if (!(addonCrashFlag instanceof Int32Array)) {
138
+ return false
139
+ }
140
+ try {
141
+ Atomics.store(addonCrashFlag, 0, 1)
142
+ return true
143
+ } catch {
144
+ return false
145
+ }
146
+ }
147
+
148
+ function __writeCrashReport(report, error) {
149
+ if (!(report instanceof SharedArrayBuffer) || report.byteLength <= 12) {
150
+ return
151
+ }
152
+ const header = new Int32Array(report, 0, 3)
153
+ if (Atomics.compareExchange(header, 0, 0, 1) !== 0) {
154
+ return
155
+ }
156
+ let length = 0
157
+ try {
158
+ const body = new Uint8Array(report, 12)
159
+ const isObject =
160
+ error !== null && (typeof error === 'object' || typeof error === 'function')
161
+ const name = isObject && typeof error.name === 'string' ? error.name : 'Error'
162
+ const message = isObject && typeof error.message === 'string'
163
+ ? error.message
164
+ : String(error)
165
+ const stack = isObject && typeof error.stack === 'string' ? error.stack : undefined
166
+ const encoder = new TextEncoder()
167
+ let bytes = encoder.encode(JSON.stringify({ name, message, stack }))
168
+ if (bytes.length > body.length) {
169
+ bytes = encoder.encode(
170
+ JSON.stringify({ name, message: message.slice(0, body.length >> 3) }),
171
+ )
172
+ }
173
+ if (bytes.length <= body.length) {
174
+ body.set(bytes)
175
+ length = bytes.length
176
+ }
177
+ Atomics.store(header, 2, threadId)
178
+ } finally {
179
+ Atomics.store(header, 1, length)
180
+ Atomics.store(header, 0, 2)
181
+ }
182
+ }
71
183
 
72
184
  globalThis.onmessage = function (e) {
73
- handler.handle(e);
74
- };
185
+ handler.handle(e)
186
+ }