Skip to content

Commit b8bd915

Browse files
PackageToJS: Make some options optional in option setup functions
1 parent 4b327dc commit b8bd915

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

Plugins/PackageToJS/Templates/platforms/browser.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { InstantiateOptions, ModuleSource/* #if HAS_IMPORTS */, Imports/* #endif */ } from "../instantiate.js"
22

3-
export function defaultBrowserSetup(options: {
3+
export function defaultBrowserSetup(options?: {
44
module: ModuleSource,
55
/* #if IS_WASI */
66
args?: string[],
@@ -11,7 +11,7 @@ export function defaultBrowserSetup(options: {
1111
getImports: () => Imports,
1212
/* #endif */
1313
/* #if USE_SHARED_MEMORY */
14-
spawnWorker: (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker,
14+
spawnWorker?: (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker,
1515
/* #endif */
1616
}): Promise<InstantiateOptions>
1717

Plugins/PackageToJS/Templates/platforms/browser.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ class DefaultBrowserThreadRegistry {
102102
/** @type {import('./browser.d.ts').defaultBrowserSetup} */
103103
export async function defaultBrowserSetup(options) {
104104
/* #if IS_WASI */
105-
const args = options.args ?? []
106-
const onStdoutLine = options.onStdoutLine ?? ((line) => console.log(line))
107-
const onStderrLine = options.onStderrLine ?? ((line) => console.error(line))
105+
const args = options?.args ?? []
106+
const onStdoutLine = options?.onStdoutLine ?? ((line) => console.log(line))
107+
const onStderrLine = options?.onStderrLine ?? ((line) => console.error(line))
108108
const wasi = new WASI(/* args */[MODULE_PATH, ...args], /* env */[], /* fd */[
109109
new OpenFile(new File([])), // stdin
110110
ConsoleStdout.lineBuffered((stdout) => {
@@ -118,13 +118,13 @@ export async function defaultBrowserSetup(options) {
118118
/* #endif */
119119
/* #if USE_SHARED_MEMORY */
120120
const memory = new WebAssembly.Memory(MEMORY_TYPE);
121-
const threadChannel = new DefaultBrowserThreadRegistry(options.spawnWorker)
121+
const threadChannel = new DefaultBrowserThreadRegistry(options?.spawnWorker || createDefaultWorkerFactory())
122122
/* #endif */
123123

124124
return {
125-
module: options.module,
125+
module: options?.module,
126126
/* #if HAS_IMPORTS */
127-
getImports() { return options.getImports() },
127+
getImports() { return options?.getImports() },
128128
/* #endif */
129129
/* #if IS_WASI */
130130
wasi: Object.assign(wasi, {

Plugins/PackageToJS/Templates/platforms/node.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export type DefaultNodeSetupOptions = {
77
/* #endif */
88
onExit?: (code: number) => void,
99
/* #if USE_SHARED_MEMORY */
10-
spawnWorker: (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker,
10+
spawnWorker?: (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker,
1111
/* #endif */
1212
}
1313

14-
export function defaultNodeSetup(options: DefaultNodeSetupOptions): Promise<InstantiateOptions>
14+
export function defaultNodeSetup(options?: DefaultNodeSetupOptions): Promise<InstantiateOptions>
1515

1616
export function createDefaultWorkerFactory(preludeScript?: string): (module: WebAssembly.Module, memory: WebAssembly.Memory, startArg: any) => Worker

Plugins/PackageToJS/Templates/platforms/node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class DefaultNodeThreadRegistry {
113113
/* #endif */
114114

115115
/** @type {import('./node.d.ts').defaultNodeSetup} */
116-
export async function defaultNodeSetup(options) {
116+
export async function defaultNodeSetup(options = {}) {
117117
const path = await import("node:path");
118118
const { fileURLToPath } = await import("node:url");
119119
const { readFile } = await import("node:fs/promises")
@@ -134,7 +134,7 @@ export async function defaultNodeSetup(options) {
134134
const module = await WebAssembly.compile(new Uint8Array(await readFile(path.join(pkgDir, MODULE_PATH))))
135135
/* #if USE_SHARED_MEMORY */
136136
const memory = new WebAssembly.Memory(MEMORY_TYPE);
137-
const threadChannel = new DefaultNodeThreadRegistry(options.spawnWorker)
137+
const threadChannel = new DefaultNodeThreadRegistry(options.spawnWorker || createDefaultWorkerFactory())
138138
/* #endif */
139139

140140
return {

0 commit comments

Comments
 (0)