Skip to content

Commit ab9e1d5

Browse files
committed
add instantiateNapiModule(Sync)
1 parent ad4c0bb commit ab9e1d5

File tree

5 files changed

+116
-111
lines changed

5 files changed

+116
-111
lines changed

packages/core/index.d.ts

Lines changed: 61 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
import type { Context } from '@emnapi/runtime'
22

3-
export declare interface BaseCreateOptions {
4-
filename?: string
5-
nodeBinding?: {
6-
node: {
7-
emitAsyncInit: Function
8-
emitAsyncDestroy: Function
9-
makeCallback: Function
10-
}
11-
napi: {
12-
asyncInit: Function
13-
asyncDestroy: Function
14-
makeCallback: Function
15-
}
3+
export declare interface NodeBinding {
4+
node: {
5+
emitAsyncInit: Function
6+
emitAsyncDestroy: Function
7+
makeCallback: Function
8+
}
9+
napi: {
10+
asyncInit: Function
11+
asyncDestroy: Function
12+
makeCallback: Function
1613
}
14+
}
15+
16+
export declare type BaseCreateOptions = {
17+
filename?: string
18+
nodeBinding?: NodeBinding
1719
onCreateWorker?: () => any
1820
print?: (str: string) => void
1921
printErr?: (str: string) => void
22+
postMessage?: (msg: any) => any
2023
}
2124

2225
export declare type CreateOptions = BaseCreateOptions & ({
2326
context: Context
24-
childThread?: false
27+
childThread?: boolean
2528
} | {
2629
context?: Context
27-
postMessage?: (msg: any) => any
2830
childThread: true
2931
})
3032

@@ -41,7 +43,7 @@ export declare interface InitOptions {
4143
table?: WebAssembly.Table
4244
}
4345

44-
export declare interface NapiModule<ChildThread extends boolean> {
46+
export declare interface NapiModule {
4547
imports: {
4648
env: any
4749
napi: any
@@ -50,7 +52,7 @@ export declare interface NapiModule<ChildThread extends boolean> {
5052
exports: any
5153
loaded: boolean
5254
filename: string
53-
childThread: ChildThread
55+
childThread: boolean
5456
emnapi: {
5557
syncMemory<T extends ArrayBuffer | ArrayBufferView> (
5658
js_to_wasm: boolean,
@@ -66,78 +68,55 @@ export declare interface NapiModule<ChildThread extends boolean> {
6668
postMessage?: (msg: any) => any
6769
}
6870

69-
export declare type ToBoolean<T> = [T] extends [never]
70-
? false
71-
: [T] extends [boolean]
72-
? T
73-
: T extends 0
74-
? false
75-
: T extends ''
76-
? false
77-
: T extends null
78-
? false
79-
: T extends undefined
80-
? false
81-
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
82-
: T extends void
83-
? false
84-
: true
85-
86-
export declare function createNapiModule<T extends CreateOptions> (
87-
options: T
88-
): NapiModule<ToBoolean<T['childThread']>>
89-
90-
export declare interface BaseLoadOptions {
91-
wasi?: {
92-
readonly wasiImport?: Record<string, any>
93-
initialize (instance: object): void
94-
getImportObject? (): any
95-
}
71+
export declare function createNapiModule (
72+
options: CreateOptions
73+
): NapiModule
74+
75+
export declare interface ReactorWASI {
76+
readonly wasiImport?: Record<string, any>
77+
initialize (instance: object): void
78+
getImportObject? (): any
79+
}
80+
81+
export declare interface LoadOptions {
82+
wasi?: ReactorWASI
9683
overwriteImports?: (importObject: WebAssembly.Imports) => WebAssembly.Imports
84+
/** Required if in child thread */
85+
tid?: number
86+
/** Required if in child thread */
87+
arg?: number
9788
}
9889

99-
export declare type LoadOptions = BaseLoadOptions & (
100-
(BaseCreateOptions & ({
101-
context: Context
102-
childThread?: false
103-
} | {
104-
context?: Context
105-
postMessage?: (msg: any) => any
106-
childThread: true
107-
tid: number
108-
arg: number
109-
})) |
110-
({
111-
napiModule: NapiModule<false>
112-
} | {
113-
napiModule: NapiModule<true>
114-
tid: number
115-
arg: number
116-
})
117-
)
118-
119-
export declare type LoadInChildThread<T> = T extends LoadOptions
120-
? 'napiModule' extends keyof T
121-
? T['napiModule'] extends NapiModule<infer R>
122-
? R
123-
: never
124-
: 'childThread' extends keyof T
125-
? T['childThread']
126-
: never
127-
: never
128-
129-
export declare interface LoadResult<ChildThread extends boolean> extends WebAssembly.WebAssemblyInstantiatedSource {
130-
napiModule: NapiModule<ChildThread>
90+
export declare type InstantiateOptions = CreateOptions & LoadOptions
91+
92+
export declare interface InstantiatedSource extends WebAssembly.WebAssemblyInstantiatedSource {
93+
napiModule: NapiModule
13194
}
13295

133-
export declare function loadNapiModule<T extends LoadOptions> (
96+
export declare function loadNapiModule (
97+
napiModule: NapiModule,
98+
/** Only support `BufferSource` or `WebAssembly.Module` on Node.js */
99+
wasmInput: string | URL | BufferSource | WebAssembly.Module,
100+
options?: LoadOptions
101+
): Promise<WebAssembly.WebAssemblyInstantiatedSource>
102+
103+
export declare function loadNapiModuleSync (
104+
napiModule: NapiModule,
105+
/** Only support `BufferSource` or `WebAssembly.Module` on Node.js */
106+
wasmInput: string | URL | BufferSource | WebAssembly.Module,
107+
options?: LoadOptions
108+
): WebAssembly.WebAssemblyInstantiatedSource
109+
110+
export declare function instantiateNapiModule (
111+
/** Only support `BufferSource` or `WebAssembly.Module` on Node.js */
134112
wasmInput: string | URL | BufferSource | WebAssembly.Module,
135-
options: T
136-
): Promise<LoadResult<ToBoolean<LoadInChildThread<T>>>>
113+
options: InstantiateOptions
114+
): Promise<InstantiatedSource>
137115

138-
export declare function loadNapiModuleSync<T extends LoadOptions> (
116+
export declare function instantiateNapiModuleSync (
117+
/** Only support `BufferSource` or `WebAssembly.Module` on Node.js */
139118
wasmInput: string | URL | BufferSource | WebAssembly.Module,
140-
options: T
141-
): LoadResult<ToBoolean<LoadInChildThread<T>>>
119+
options: InstantiateOptions
120+
): InstantiatedSource
142121

143122
export declare function handleMessage (msg: { data: any }, callback: (type: string, payload: any) => any): void

packages/core/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
export { createNapiModule } from './module.js'
2-
export { loadNapiModule, loadNapiModuleSync } from './load.js'
2+
export {
3+
loadNapiModule,
4+
loadNapiModuleSync,
5+
instantiateNapiModule,
6+
instantiateNapiModuleSync
7+
} from './load.js'
8+
39
export { handleMessage } from './worker.js'
410

511
export const version = __VERSION__

packages/core/src/load.js

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { load, loadSync } from '@tybys/wasm-util'
22
import { createNapiModule } from './module.js'
33

4-
function loadNapiModuleImpl (loadFn, wasmInput, options) {
4+
function loadNapiModuleImpl (loadFn, userNapiModule, wasmInput, options) {
55
options = options == null ? {} : options
6-
const userNapiModule = options.napiModule
76
let napiModule
8-
if (typeof userNapiModule === 'object' && userNapiModule !== null) {
7+
const isLoad = typeof userNapiModule === 'object' && userNapiModule !== null
8+
if (isLoad) {
99
if (userNapiModule.loaded) {
1010
throw new Error('napiModule has already loaded')
1111
}
@@ -137,28 +137,50 @@ function loadNapiModuleImpl (loadFn, wasmInput, options) {
137137
})
138138
}
139139

140-
return { instance, module, napiModule }
140+
const ret = { instance, module }
141+
if (!isLoad) {
142+
ret.napiModule = napiModule
143+
}
144+
return ret
141145
})
142146
}
143147

144-
export function loadNapiModule (wasmInput, options) {
145-
return loadNapiModuleImpl((wasmInput, importObject, callback) => {
146-
return load(wasmInput, importObject).then((source) => {
147-
return callback(null, source)
148-
}, err => {
149-
return callback(err)
150-
})
151-
}, wasmInput, options)
148+
function loadCallback (wasmInput, importObject, callback) {
149+
return load(wasmInput, importObject).then((source) => {
150+
return callback(null, source)
151+
}, err => {
152+
return callback(err)
153+
})
152154
}
153155

154-
export function loadNapiModuleSync (wasmInput, options) {
155-
return loadNapiModuleImpl((wasmInput, importObject, callback) => {
156-
let source
157-
try {
158-
source = loadSync(wasmInput, importObject)
159-
} catch (err) {
160-
return callback(err)
161-
}
162-
return callback(null, source)
163-
}, wasmInput, options)
156+
function loadSyncCallback (wasmInput, importObject, callback) {
157+
let source
158+
try {
159+
source = loadSync(wasmInput, importObject)
160+
} catch (err) {
161+
return callback(err)
162+
}
163+
return callback(null, source)
164+
}
165+
166+
export function loadNapiModule (napiModule, wasmInput, options) {
167+
if (typeof napiModule !== 'object' || napiModule === null) {
168+
throw new TypeError('Invalid napiModule')
169+
}
170+
return loadNapiModuleImpl(loadCallback, napiModule, wasmInput, options)
171+
}
172+
173+
export function loadNapiModuleSync (napiModule, wasmInput, options) {
174+
if (typeof napiModule !== 'object' || napiModule === null) {
175+
throw new TypeError('Invalid napiModule')
176+
}
177+
return loadNapiModuleImpl(loadSyncCallback, napiModule, wasmInput, options)
178+
}
179+
180+
export function instantiateNapiModule (wasmInput, options) {
181+
return loadNapiModuleImpl(loadCallback, undefined, wasmInput, options)
182+
}
183+
184+
export function instantiateNapiModuleSync (wasmInput, options) {
185+
return loadNapiModuleImpl(loadSyncCallback, undefined, wasmInput, options)
164186
}

packages/test/util.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ function loadPath (request, options) {
4646
}
4747

4848
const p = new Promise((resolve, reject) => {
49-
loadNapiModule(fs.readFileSync(request), {
50-
napiModule,
49+
loadNapiModule(napiModule, fs.readFileSync(request), {
5150
wasi,
5251
overwriteImports (importObject) {
5352
if (process.env.EMNAPI_TEST_WASI_THREADS) {
@@ -79,8 +78,7 @@ function loadPath (request, options) {
7978
const shared = (typeof SharedArrayBuffer === 'function') && (wasmMemory.buffer instanceof SharedArrayBuffer)
8079
return new TextDecoder().decode(shared ? HEAPU8.slice(ptr, end) : HEAPU8.subarray(ptr, end))
8180
}
82-
loadNapiModule(fs.readFileSync(request), {
83-
napiModule,
81+
loadNapiModule(napiModule, fs.readFileSync(request), {
8482
overwriteImports (importObject) {
8583
importObject.env.console_log = function (fmt, ...args) {
8684
const fmtString = UTF8ToString(fmt)

packages/test/worker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
importScripts('../../node_modules/@emnapi/core/dist/emnapi-core.js')
5757

58-
const { loadNapiModuleSync, handleMessage } = globalThis.emnapiCore
58+
const { instantiateNapiModuleSync, handleMessage } = globalThis.emnapiCore
5959

6060
function onLoad (payload) {
6161
const wasi = new WASI({
@@ -68,7 +68,7 @@
6868
: function () { console.log.apply(console, arguments) }
6969
})
7070

71-
loadNapiModuleSync(payload.wasmModule, {
71+
instantiateNapiModuleSync(payload.wasmModule, {
7272
childThread: true,
7373
wasi,
7474
overwriteImports (importObject) {

0 commit comments

Comments
 (0)