Skip to content

Commit cda3569

Browse files
Strip WASI polyfill from non-WASI builds
1 parent f1e3540 commit cda3569

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

Plugins/PackageToJS/Sources/PackageToJS.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ struct PackagingPlanner {
282282
]
283283
let (buildConfiguration, triple) = deriveBuildConfiguration()
284284
let conditions = [
285-
"USE_SHARED_MEMORY": triple == "wasm32-unknown-wasip1-threads"
285+
"USE_SHARED_MEMORY": triple == "wasm32-unknown-wasip1-threads",
286+
"IS_WASI": triple.hasPrefix("wasm32-unknown-wasi"),
286287
]
287288
return make.addTask(
288289
inputFiles: [selfPath, inputPath.path], inputTasks: [outputDirTask] + inputs,

Plugins/PackageToJS/Templates/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// @ts-check
2-
import { WASI, WASIProcExit, File, OpenFile, ConsoleStdout, PreopenDirectory } from '@bjorn3/browser_wasi_shim';
2+
/* #if IS_WASI */
3+
import { WASI, File, OpenFile, ConsoleStdout, PreopenDirectory } from '@bjorn3/browser_wasi_shim';
4+
/* #endif */
35
import { instantiate, MODULE_PATH } from './instantiate.js';
46

57
/** @type {import('./index.d').init} */
@@ -8,6 +10,7 @@ export async function init(
810
imports,
911
options
1012
) {
13+
/* #if IS_WASI */
1114
const wasi = new WASI(/* args */[MODULE_PATH, ...(options?.args ?? [])], /* env */[], /* fd */[
1215
new OpenFile(new File([])), // stdin
1316
ConsoleStdout.lineBuffered((stdout) => {
@@ -18,8 +21,11 @@ export async function init(
1821
}),
1922
new PreopenDirectory("/", new Map()),
2023
], { debug: false })
24+
/* #endif */
2125
const { instance, exports, swift } = await instantiate(moduleSource, imports, {
26+
/* #if IS_WASI */
2227
wasi,
28+
/* #endif */
2329
/* #if USE_SHARED_MEMORY */
2430
memory: options.memory,
2531
/* #endif */

Plugins/PackageToJS/Templates/instantiate.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ export declare function instantiate(
6060
moduleSource: WebAssembly.Module | ArrayBufferView | ArrayBuffer | Response | PromiseLike<Response>,
6161
imports: Import,
6262
options: {
63+
/* #if IS_WASI */
6364
wasi: WASI,
65+
/* #endif */
6466
/* #if USE_SHARED_MEMORY */
6567
memory: WebAssembly.Memory
6668
/* #endif */

Plugins/PackageToJS/Templates/instantiate.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,22 @@ export async function instantiate(
2323
imports,
2424
options
2525
) {
26+
/* #if IS_WASI */
2627
const { wasi } = options;
28+
/* #endif */
2729
const instantiator = await createInstantiator(imports, options);
2830
const swift = new SwiftRuntime();
2931

3032
/** @type {WebAssembly.Imports} */
3133
const importObject = {
3234
javascript_kit: swift.wasmImports,
35+
/* #if IS_WASI */
3336
wasi_snapshot_preview1: wasi.wasiImport,
3437
/* #if USE_SHARED_MEMORY */ env: {
3538
memory: options.memory,
3639
},
3740
/* #endif */
41+
/* #endif */
3842
};
3943
instantiator.addImports(importObject);
4044

@@ -60,7 +64,9 @@ export async function instantiate(
6064
}
6165

6266
swift.setInstance(instance);
67+
/* #if IS_WASI */
6368
wasi.initialize(instance);
69+
/* #endif */
6470

6571
return {
6672
instance,

0 commit comments

Comments
 (0)