-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathinstantiate.js
130 lines (121 loc) · 3.85 KB
/
instantiate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// @ts-check
import { SwiftRuntime } from "./runtime.js"
export const MODULE_PATH = "@PACKAGE_TO_JS_MODULE_PATH@";
/* #if USE_SHARED_MEMORY */
export const MEMORY_TYPE = {
// @ts-ignore
initial: import.meta.PACKAGE_TO_JS_MEMORY_INITIAL,
// @ts-ignore
maximum: import.meta.PACKAGE_TO_JS_MEMORY_MAXIMUM,
// @ts-ignore
shared: import.meta.PACKAGE_TO_JS_MEMORY_SHARED,
}
/* #endif */
/* #if HAS_BRIDGE */
// @ts-ignore
import { createInstantiator } from "./bridge.js"
/* #else */
/**
* @param {import('./instantiate.d').InstantiateOptions} options
* @param {any} swift
*/
async function createInstantiator(options, swift) {
return {
/** @param {WebAssembly.Imports} importObject */
addImports: (importObject) => {},
/** @param {WebAssembly.Instance} instance */
setInstance: (instance) => {},
/** @param {WebAssembly.Instance} instance */
createExports: (instance) => {
return {};
},
}
}
/* #endif */
/** @type {import('./instantiate.d').instantiate} */
export async function instantiate(
options
) {
const result = await _instantiate(options);
/* #if IS_WASI */
options.wasi.initialize(result.instance);
/* #endif */
result.swift.main();
return result;
}
/** @type {import('./instantiate.d').instantiateForThread} */
export async function instantiateForThread(
tid, startArg, options
) {
const result = await _instantiate(options);
/* #if IS_WASI */
options.wasi.setInstance(result.instance);
/* #endif */
result.swift.startThread(tid, startArg)
return result;
}
/** @type {import('./instantiate.d').instantiate} */
async function _instantiate(
options
) {
const _WebAssembly = options.WebAssembly || WebAssembly;
const moduleSource = options.module;
/* #if IS_WASI */
const { wasi } = options;
/* #endif */
const swift = new SwiftRuntime({
/* #if USE_SHARED_MEMORY */
sharedMemory: true,
threadChannel: options.threadChannel,
/* #endif */
});
const instantiator = await createInstantiator(options, swift);
/** @type {WebAssembly.Imports} */
const importObject = {
javascript_kit: swift.wasmImports,
/* #if IS_WASI */
wasi_snapshot_preview1: wasi.wasiImport,
/* #if USE_SHARED_MEMORY */
env: {
memory: options.memory,
},
wasi: {
"thread-spawn": (startArg) => {
return options.threadChannel.spawnThread(module, options.memory, startArg);
}
}
/* #endif */
/* #endif */
};
instantiator.addImports(importObject);
options.addToCoreImports?.(importObject, () => instance, () => exports);
let module;
let instance;
let exports;
if (moduleSource instanceof _WebAssembly.Module) {
module = moduleSource;
instance = await _WebAssembly.instantiate(module, importObject);
} else if (typeof Response === "function" && (moduleSource instanceof Response || moduleSource instanceof Promise)) {
if (typeof _WebAssembly.instantiateStreaming === "function") {
const result = await _WebAssembly.instantiateStreaming(moduleSource, importObject);
module = result.module;
instance = result.instance;
} else {
const moduleBytes = await (await moduleSource).arrayBuffer();
module = await _WebAssembly.compile(moduleBytes);
instance = await _WebAssembly.instantiate(module, importObject);
}
} else {
// @ts-expect-error: Type 'Response' is not assignable to type 'BufferSource'
module = await _WebAssembly.compile(moduleSource);
instance = await _WebAssembly.instantiate(module, importObject);
}
swift.setInstance(instance);
instantiator.setInstance(instance);
exports = instantiator.createExports(instance);
return {
instance,
swift,
exports,
}
}