Skip to content

Commit 16732c1

Browse files
committed
loading api change
1 parent fd9a332 commit 16732c1

File tree

4 files changed

+90
-57
lines changed

4 files changed

+90
-57
lines changed

packages/core/index.d.ts

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

3-
export declare type CreateOptions = {
3+
export declare interface BaseCreateOptions {
44
filename?: string
55
nodeBinding?: {
66
node: {
@@ -17,7 +17,9 @@ export declare type CreateOptions = {
1717
onCreateWorker?: () => any
1818
print?: () => void
1919
printErr?: () => void
20-
} & ({
20+
}
21+
22+
export declare type CreateOptions = BaseCreateOptions & ({
2123
context: Context
2224
childThread?: false
2325
} | {
@@ -64,45 +66,78 @@ export declare interface NapiModule<ChildThread extends boolean> {
6466
postMessage?: (msg: any) => any
6567
}
6668

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+
6786
export declare function createNapiModule<T extends CreateOptions> (
6887
options: T
69-
): NapiModule<[T['childThread']] extends [boolean] ? T['childThread'] : false>
88+
): NapiModule<ToBoolean<T['childThread']>>
7089

71-
export declare type LoadOptions<ChildThread extends boolean> = {
90+
export declare interface BaseLoadOptions {
7291
wasi?: {
7392
readonly wasiImport?: Record<string, any>
7493
initialize (instance: object): void
7594
getImportObject? (): any
7695
}
7796
overwriteImports?: (importObject: WebAssembly.Imports) => WebAssembly.Imports
78-
} & (
79-
[ChildThread] extends [true]
80-
? {
81-
tid: number
82-
arg: number
83-
}
84-
: {})
97+
}
8598

86-
export declare function loadNapiModule (
87-
napiModule: NapiModule<false>,
88-
wasmInput: string | URL | BufferSource | WebAssembly.Module,
89-
options?: LoadOptions<false>
90-
): Promise<WebAssembly.WebAssemblyInstantiatedSource>
91-
export declare function loadNapiModule (
92-
napiModule: NapiModule<true>,
93-
wasmInput: string | URL | BufferSource | WebAssembly.Module,
94-
options: LoadOptions<true>
95-
): Promise<WebAssembly.WebAssemblyInstantiatedSource>
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+
)
96118

97-
export declare function loadNapiModuleSync (
98-
napiModule: NapiModule<false>,
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>
131+
}
132+
133+
export declare function loadNapiModule<T extends LoadOptions> (
99134
wasmInput: string | URL | BufferSource | WebAssembly.Module,
100-
options?: LoadOptions<false>
101-
): WebAssembly.WebAssemblyInstantiatedSource
102-
export declare function loadNapiModuleSync (
103-
napiModule: NapiModule<true>,
135+
options: T
136+
): Promise<LoadResult<ToBoolean<LoadInChildThread<T>>>>
137+
138+
export declare function loadNapiModuleSync<T extends LoadOptions> (
104139
wasmInput: string | URL | BufferSource | WebAssembly.Module,
105-
options: LoadOptions<true>
106-
): WebAssembly.WebAssemblyInstantiatedSource
140+
options: T
141+
): LoadResult<ToBoolean<LoadInChildThread<T>>>
107142

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

packages/core/src/load.js

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

3-
function loadNapiModuleImpl (loadFn, napiModule, wasmInput, options) {
4-
if (!napiModule) {
5-
throw new TypeError('Invalid napiModule')
4+
function loadNapiModuleImpl (loadFn, wasmInput, options) {
5+
options = options == null ? {} : options
6+
const userNapiModule = options.napiModule
7+
let napiModule
8+
if (typeof userNapiModule === 'object' && userNapiModule !== null) {
9+
if (userNapiModule.loaded) {
10+
throw new Error('napiModule has already loaded')
11+
}
12+
napiModule = userNapiModule
13+
} else {
14+
napiModule = createNapiModule(options)
615
}
716

8-
options = options == null ? {} : options
917
const wasi = options.wasi
1018
const env = Object.assign({}, napiModule.imports.env, napiModule.imports.napi, napiModule.imports.emnapi)
1119
let importObject = {
1220
env,
1321
napi: napiModule.imports.napi,
1422
emnapi: napiModule.imports.emnapi,
1523
wasi: {
24+
// eslint-disable-next-line camelcase
1625
'thread-spawn': function __imported_wasi_thread_spawn (startArg) {
1726
return napiModule.spawnThread(startArg, undefined)
1827
}
@@ -128,33 +137,21 @@ function loadNapiModuleImpl (loadFn, napiModule, wasmInput, options) {
128137
})
129138
}
130139

131-
return { instance, module }
140+
return { instance, module, napiModule }
132141
})
133142
}
134143

135-
/**
136-
* @param {import('@emnapi/core').NapiModule} napiModule
137-
* @param {string | URL | BufferSource | WebAssembly.Module} wasmInput
138-
* @param {any} options
139-
* @returns {Promise<WebAssembly.WebAssemblyInstantiatedSource>}
140-
*/
141-
export function loadNapiModule (napiModule, wasmInput, options) {
144+
export function loadNapiModule (wasmInput, options) {
142145
return loadNapiModuleImpl((wasmInput, importObject, callback) => {
143146
return load(wasmInput, importObject).then((source) => {
144147
return callback(null, source)
145148
}, err => {
146149
return callback(err)
147150
})
148-
}, napiModule, wasmInput, options)
151+
}, wasmInput, options)
149152
}
150153

151-
/**
152-
* @param {import('@emnapi/core').NapiModule} napiModule
153-
* @param {BufferSource | WebAssembly.Module} wasmInput
154-
* @param {any} options
155-
* @returns {WebAssembly.WebAssemblyInstantiatedSource}
156-
*/
157-
export function loadNapiModuleSync (napiModule, wasmInput, options) {
154+
export function loadNapiModuleSync (wasmInput, options) {
158155
return loadNapiModuleImpl((wasmInput, importObject, callback) => {
159156
let source
160157
try {
@@ -163,5 +160,5 @@ export function loadNapiModuleSync (napiModule, wasmInput, options) {
163160
return callback(err)
164161
}
165162
return callback(null, source)
166-
}, napiModule, wasmInput, options)
163+
}, wasmInput, options)
167164
}

packages/test/util.js

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

4848
const p = new Promise((resolve, reject) => {
49-
loadNapiModule(napiModule, fs.readFileSync(request), {
49+
loadNapiModule(fs.readFileSync(request), {
50+
napiModule,
5051
wasi,
5152
overwriteImports (importObject) {
5253
if (process.env.EMNAPI_TEST_WASI_THREADS) {
@@ -78,7 +79,8 @@ function loadPath (request, options) {
7879
const shared = (typeof SharedArrayBuffer === 'function') && (wasmMemory.buffer instanceof SharedArrayBuffer)
7980
return new TextDecoder().decode(shared ? HEAPU8.slice(ptr, end) : HEAPU8.subarray(ptr, end))
8081
}
81-
loadNapiModule(napiModule, fs.readFileSync(request), {
82+
loadNapiModule(fs.readFileSync(request), {
83+
napiModule,
8284
overwriteImports (importObject) {
8385
importObject.env.console_log = function (fmt, ...args) {
8486
const fmtString = UTF8ToString(fmt)

packages/test/worker.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Object.assign(global, {
4242
})
4343

4444
const { WASI } = require('./wasi')
45-
const { createNapiModule, loadNapiModuleSync, handleMessage } = require('@emnapi/core')
45+
const { loadNapiModuleSync, handleMessage } = require('@emnapi/core')
4646

4747
function instantiate (wasmMemory, wasmModule, tid, arg) {
4848
const wasi = new WASI({
@@ -52,10 +52,9 @@ function instantiate (wasmMemory, wasmModule, tid, arg) {
5252
fs.writeSync(1, str + '\n')
5353
}
5454
})
55-
const napiModule = createNapiModule({
56-
childThread: true
57-
})
58-
loadNapiModuleSync(napiModule, wasmModule, {
55+
56+
loadNapiModuleSync(wasmModule, {
57+
childThread: true,
5958
wasi,
6059
overwriteImports (importObject) {
6160
importObject.env.memory = wasmMemory

0 commit comments

Comments
 (0)