Skip to content

Commit af162ff

Browse files
committed
move postMessage to napiModule
1 parent b59421e commit af162ff

File tree

6 files changed

+24
-140
lines changed

6 files changed

+24
-140
lines changed

packages/core/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export declare type CreateOptions = {
2222
childThread?: false
2323
} | {
2424
context?: Context
25+
postMessage?: (msg: any) => any
2526
childThread: true
2627
})
2728

@@ -59,6 +60,8 @@ export declare interface NapiModule<ChildThread extends boolean> {
5960
}
6061

6162
init (options: InitOptions): any
63+
spawnThread (startArg: number): number
64+
postMessage?: (msg: any) => any
6265
}
6366

6467
export declare function createNapiModule<T extends CreateOptions> (
@@ -72,7 +75,6 @@ export declare type LoadOptions<ChildThread extends boolean> = {
7275
getImportObject? (): any
7376
}
7477
overwriteImports?: (importObject: WebAssembly.Imports) => WebAssembly.Imports
75-
postMessage?: (msg: any) => any
7678
} & (
7779
[ChildThread] extends [true]
7880
? {

packages/core/src/load.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,11 @@ function loadNapiModuleImpl (loadFn, napiModule, wasmInput, options) {
136136
}
137137
}
138138

139-
const postMsg = typeof options.postMessage === 'function'
140-
? typeof options.postMessage
141-
: typeof postMessage === 'function'
142-
? postMessage
143-
: function () {}
144-
145139
return loadFn(wasmInput, importObject, (err, source) => {
146140
if (err) {
147141
if (napiModule.childThread) {
148-
postMsg({
142+
const postMessage = napiModule.postMessage
143+
postMessage({
149144
__emnapi__: {
150145
type: 'loaded',
151146
payload: {
@@ -176,6 +171,7 @@ function loadNapiModuleImpl (loadFn, napiModule, wasmInput, options) {
176171
if (napiModule.childThread) {
177172
// https://github.com/nodejs/help/issues/4102
178173
const noop = () => {}
174+
const exports = instance.exports
179175
const exportsProxy = new Proxy({}, {
180176
get (t, p, r) {
181177
if (p === 'memory') {
@@ -184,7 +180,7 @@ function loadNapiModuleImpl (loadFn, napiModule, wasmInput, options) {
184180
if (p === '_initialize') {
185181
return noop
186182
}
187-
return Reflect.get(instance.exports, p, r)
183+
return Reflect.get(exports, p, r)
188184
}
189185
})
190186
instance = new Proxy(instance, {
@@ -200,7 +196,8 @@ function loadNapiModuleImpl (loadFn, napiModule, wasmInput, options) {
200196
}
201197

202198
if (napiModule.childThread) {
203-
postMsg({
199+
const postMessage = napiModule.postMessage
200+
postMessage({
204201
__emnapi__: {
205202
type: 'loaded',
206203
payload: {

packages/emnapi/src/core/async.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function emnapiAddSendListener (worker: any): boolean {
1717
const __emnapi__ = data.__emnapi__
1818
if (__emnapi__ && __emnapi__.type === 'async-send') {
1919
if (ENVIRONMENT_IS_PTHREAD) {
20+
const postMessage = napiModule.postMessage!
2021
postMessage({ __emnapi__ })
2122
} else {
2223
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -44,6 +45,7 @@ function emnapiAddSendListener (worker: any): boolean {
4445

4546
function __emnapi_async_send_js (type: number, callback: number, data: number): void {
4647
if (ENVIRONMENT_IS_PTHREAD) {
48+
const postMessage = napiModule.postMessage!
4749
postMessage({
4850
__emnapi__: {
4951
type: 'async-send',
@@ -72,6 +74,7 @@ function spawnThread (startArg: number, threadId?: Int32Array): number {
7274
const threadIdBuffer = new SharedArrayBuffer(4)
7375
const id = new Int32Array(threadIdBuffer)
7476
Atomics.store(id, 0, -1)
77+
const postMessage = napiModule.postMessage!
7578
postMessage({
7679
__emnapi__: {
7780
type: 'thread-spawn',

packages/emnapi/src/core/init.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare interface CreateOptions {
1010
onCreateWorker?: () => any
1111
print?: () => void
1212
printErr?: () => void
13+
postMessage?: (msg: any) => any
1314
}
1415

1516
declare interface InitOptions {
@@ -40,6 +41,7 @@ declare interface INapiModule {
4041

4142
init (options: InitOptions): any
4243
spawnThread (startArg: number): number
44+
postMessage?: (msg: any) => any
4345
}
4446

4547
var ENVIRONMENT_IS_NODE = typeof process === 'object' && process !== null && typeof process.versions === 'object' && process.versions !== null && typeof process.versions.node === 'string'
@@ -139,6 +141,16 @@ if (!ENVIRONMENT_IS_PTHREAD) {
139141
emnapiCtx = context
140142
} else {
141143
emnapiCtx = options?.context
144+
145+
const postMsg = typeof options.postMessage === 'function'
146+
? options.postMessage
147+
: typeof postMessage === 'function'
148+
? postMessage
149+
: undefined
150+
if (typeof postMsg !== 'function') {
151+
throw new TypeError('No postMessage found')
152+
}
153+
napiModule.postMessage = postMsg
142154
}
143155

144156
if (typeof options.filename === 'string') {

packages/test/util.js

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -56,46 +56,6 @@ function loadPath (request, options) {
5656
}).then(() => {
5757
resolve(napiModule.exports)
5858
}).catch(reject)
59-
// WebAssembly.instantiate(require('fs').readFileSync(request), {
60-
// wasi_snapshot_preview1: wasi.wasiImport,
61-
// env: {
62-
// ...(process.env.EMNAPI_TEST_WASI_THREADS ? { memory: wasmMemory } : {}),
63-
// ...napiModule.imports.env
64-
// },
65-
// napi: napiModule.imports.napi,
66-
// emnapi: napiModule.imports.emnapi,
67-
// wasi: {
68-
// 'thread-spawn': __imported_wasi_thread_spawn
69-
// }
70-
// })
71-
// .then(({ instance, module }) => {
72-
// if (process.env.EMNAPI_TEST_WASI_THREADS) {
73-
// instance = {
74-
// exports: {
75-
// ...instance.exports,
76-
// memory: wasmMemory
77-
// }
78-
// }
79-
// // Object.defineProperty(instance.exports, 'memory', { value: wasmMemory })
80-
// } else {
81-
// wasmMemory = instance.exports.memory
82-
// }
83-
// wasi.initialize(instance)
84-
// let exports
85-
// try {
86-
// exports = napiModule.init({
87-
// instance,
88-
// module,
89-
// memory: instance.exports.memory,
90-
// table: instance.exports.__indirect_function_table
91-
// })
92-
// } catch (err) {
93-
// reject(err)
94-
// return
95-
// }
96-
// resolve(exports)
97-
// })
98-
// .catch(reject)
9959
})
10060
p.Module = napiModule
10161
return p
@@ -130,35 +90,6 @@ function loadPath (request, options) {
13090
wasmMemory = instance.exports.memory
13191
resolve(napiModule.exports)
13292
}).catch(reject)
133-
// WebAssembly.instantiate(require('fs').readFileSync(request), {
134-
// env: {
135-
// ...napiModule.imports.env,
136-
// console_log (fmt, ...args) {
137-
// const fmtString = UTF8ToString(fmt)
138-
// console.log(fmtString, ...args)
139-
// return 0
140-
// }
141-
// },
142-
// napi: napiModule.imports.napi,
143-
// emnapi: napiModule.imports.emnapi
144-
// })
145-
// .then(({ instance, module }) => {
146-
// wasmMemory = instance.exports.memory
147-
// let exports
148-
// try {
149-
// exports = napiModule.init({
150-
// instance,
151-
// module,
152-
// memory: instance.exports.memory,
153-
// table: instance.exports.__indirect_function_table
154-
// })
155-
// } catch (err) {
156-
// reject(err)
157-
// return
158-
// }
159-
// resolve(exports)
160-
// })
161-
// .catch(reject)
16293
})
16394
p.Module = napiModule
16495
return p

packages/test/worker.js

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -66,51 +66,6 @@ function instantiate (wasmMemory, wasmModule, tid, arg) {
6666
parentPort.postMessage(msg)
6767
}
6868
})
69-
// const instance = new WebAssembly.Instance(wasmModule, {
70-
// wasi_snapshot_preview1: wasi.wasiImport,
71-
// env: {
72-
// memory: wasmMemory,
73-
// ...napiModule.imports.env
74-
// },
75-
// napi: napiModule.imports.napi,
76-
// emnapi: napiModule.imports.emnapi,
77-
// wasi: {
78-
// 'thread-spawn': function (startArg) {
79-
// return napiModule.spawnThread(startArg)
80-
// }
81-
// }
82-
// })
83-
// const noop = () => {}
84-
// const exportsProxy = new Proxy({}, {
85-
// get (t, p, r) {
86-
// if (p === 'memory') {
87-
// return wasmMemory
88-
// }
89-
// if (p === '_initialize') {
90-
// return noop
91-
// }
92-
// return Reflect.get(instance.exports, p, r)
93-
// }
94-
// })
95-
// const instanceProxy = new Proxy(instance, {
96-
// get (target, p, receiver) {
97-
// if (p === 'exports') {
98-
// return exportsProxy
99-
// }
100-
// return Reflect.get(target, p, receiver)
101-
// }
102-
// })
103-
104-
// wasi.initialize(instanceProxy)
105-
// postMessage({
106-
// __emnapi__: {
107-
// type: 'loaded',
108-
// payload: {
109-
// err: null
110-
// }
111-
// }
112-
// })
113-
// instance.exports.wasi_thread_start(tid, arg)
11469
}
11570

11671
self.onmessage = function (e) {
@@ -119,20 +74,4 @@ self.onmessage = function (e) {
11974
instantiate(payload.wasmMemory, payload.wasmModule, payload.tid, payload.arg)
12075
}
12176
})
122-
// if (e.data.__emnapi__) {
123-
// const type = e.data.__emnapi__.type
124-
// const payload = e.data.__emnapi__.payload
125-
// if (type === 'load') {
126-
// try {
127-
// instantiate(payload.wasmMemory, payload.wasmModule, payload.tid, payload.arg)
128-
// } catch (err) {
129-
// postMessage({
130-
// __emnapi__: {
131-
// type: 'loaded',
132-
// payload: { err }
133-
// }
134-
// })
135-
// }
136-
// }
137-
// }
13877
}

0 commit comments

Comments
 (0)