forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (54 loc) · 1.92 KB
/
index.js
File metadata and controls
59 lines (54 loc) · 1.92 KB
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
// @ts-check
import { instantiate } from './instantiate.js';
/* #if TARGET_DEFAULT_PLATFORM_NODE */
import { defaultNodeSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory as createDefaultWorkerFactoryForNode /* #endif */} from './platforms/node.js';
/* #else */
import { defaultBrowserSetup /* #if USE_SHARED_MEMORY */, createDefaultWorkerFactory as createDefaultWorkerFactoryForBrowser /* #endif */} from './platforms/browser.js';
/* #endif */
/* #if TARGET_DEFAULT_PLATFORM_NODE */
/** @type {import('./index.d').init} */
async function initNode(_options) {
/** @type {import('./platforms/node.d.ts').DefaultNodeSetupOptions} */
const options = {
...(_options || {}),
/* #if USE_SHARED_MEMORY */
spawnWorker: createDefaultWorkerFactoryForNode(),
/* #endif */
};
const instantiateOptions = await defaultNodeSetup(options);
return await instantiate(instantiateOptions);
}
/* #else */
/** @type {import('./index.d').init} */
async function initBrowser(_options) {
/** @type {import('./index.d').Options} */
const options = _options || {
/* #if HAS_IMPORTS */
/** @returns {import('./instantiate.d').Imports} */
getImports() { (() => { throw new Error("No imports provided") })() }
/* #endif */
};
let module = options.module;
if (!module) {
module = fetch(new URL("@PACKAGE_TO_JS_MODULE_PATH@", import.meta.url))
}
const instantiateOptions = await defaultBrowserSetup({
module,
/* #if HAS_IMPORTS */
getImports: () => options.getImports(),
/* #endif */
/* #if USE_SHARED_MEMORY */
spawnWorker: createDefaultWorkerFactoryForBrowser()
/* #endif */
})
return await instantiate(instantiateOptions);
}
/* #endif */
/** @type {import('./index.d').init} */
export async function init(options) {
/* #if TARGET_DEFAULT_PLATFORM_NODE */
return initNode(options);
/* #else */
return initBrowser(options);
/* #endif */
}