|
7 | 7 | // USE_PTHREADS is emscripten's define symbol, which is passed to acorn optimizer, so we could use it here |
8 | 8 | #if USE_PTHREADS |
9 | 9 | const monoWasmThreads = true; |
10 | | -const isPThread = `ENVIRONMENT_IS_PTHREAD`; |
11 | 10 | #else |
12 | 11 | const monoWasmThreads = false; |
13 | | -const isPThread = "false"; |
14 | 12 | #endif |
15 | 13 |
|
16 | 14 | // because we can't pass custom define symbols to acorn optimizer, we use environment variables to pass other build options |
17 | 15 | const WasmEnableLegacyJsInterop = process.env.WasmEnableLegacyJsInterop !== "false"; |
18 | 16 |
|
19 | | -const DotnetSupportLib = { |
20 | | - $DOTNET: {}, |
21 | | - // this line will be placed early on emscripten runtime creation, passing import and export objects into __dotnet_runtime IIFE |
22 | | - // Emscripten uses require function for nodeJS even in ES6 module. We need https://nodejs.org/api/module.html#modulecreaterequirefilename |
23 | | - // We use dynamic import because there is no "module" module in the browser. |
24 | | - // This is async init of it, note it would become available only after first tick. |
25 | | - // Also fix of scriptDirectory would be delayed |
26 | | - // Emscripten's getBinaryPromise is not async for NodeJs, but we would like to have it async, so we replace it. |
27 | | - // We also replace implementation of fetch |
28 | | - $DOTNET__postset: ` |
29 | | -let __dotnet_replacement_PThread = ${monoWasmThreads} ? {} : undefined; |
30 | | -${monoWasmThreads ? ` |
31 | | -__dotnet_replacement_PThread.loadWasmModuleToWorker = PThread.loadWasmModuleToWorker; |
32 | | -__dotnet_replacement_PThread.threadInitTLS = PThread.threadInitTLS; |
33 | | -__dotnet_replacement_PThread.allocateUnusedWorker = PThread.allocateUnusedWorker; |
34 | | -` : ''} |
35 | | -let __dotnet_replacements = {scriptUrl: import.meta.url, fetch: globalThis.fetch, require, updateMemoryViews, pthreadReplacements: __dotnet_replacement_PThread}; |
36 | | -if (ENVIRONMENT_IS_NODE) { |
37 | | - __dotnet_replacements.requirePromise = __requirePromise; |
38 | | -} |
39 | | -let __dotnet_exportedAPI = __initializeImportsAndExports( |
40 | | - { isGlobal:false, isNode:ENVIRONMENT_IS_NODE, isWorker:ENVIRONMENT_IS_WORKER, isShell:ENVIRONMENT_IS_SHELL, isWeb:ENVIRONMENT_IS_WEB, isPThread:${isPThread}, quit_, ExitStatus, requirePromise:__dotnet_replacements.requirePromise }, |
41 | | - { mono:MONO, binding:BINDING, internal:INTERNAL, module:Module, marshaled_imports: IMPORTS }, |
42 | | - __dotnet_replacements, __callbackAPI); |
43 | | -updateMemoryViews = __dotnet_replacements.updateMemoryViews; |
44 | | -fetch = __dotnet_replacements.fetch; |
45 | | -_scriptDir = __dirname = scriptDirectory = __dotnet_replacements.scriptDirectory; |
46 | | -if (ENVIRONMENT_IS_NODE) { |
47 | | - __dotnet_replacements.requirePromise.then(someRequire => { |
48 | | - require = someRequire; |
49 | | - }); |
| 17 | +function setup() { |
| 18 | + const initializeImportsAndExports = Module['initializeImportsAndExports']; |
| 19 | + const pthreadReplacements = {}; |
| 20 | + const dotnet_replacements = { |
| 21 | + scriptUrl: import.meta.url, |
| 22 | + fetch: globalThis.fetch, |
| 23 | + require, |
| 24 | + updateMemoryViews, |
| 25 | + pthreadReplacements, |
| 26 | + scriptDirectory |
| 27 | + }; |
| 28 | + #if USE_PTHREADS |
| 29 | + dotnet_replacements.loadWasmModuleToWorker = PThread.loadWasmModuleToWorker; |
| 30 | + dotnet_replacements.threadInitTLS = PThread.threadInitTLS; |
| 31 | + dotnet_replacements.allocateUnusedWorker = PThread.allocateUnusedWorker; |
| 32 | + #else |
| 33 | + const ENVIRONMENT_IS_PTHREAD = false; |
| 34 | + #endif |
| 35 | + if (ENVIRONMENT_IS_NODE) { |
| 36 | + dotnet_replacements.requirePromise = import(/* webpackIgnore: true */'module').then(mod => mod.createRequire(import.meta.url)); |
| 37 | + __dotnet_replacements.requirePromise.then(someRequire => { |
| 38 | + require = someRequire; |
| 39 | + }); |
| 40 | + } |
| 41 | + Module['noExitRuntime'] = ENVIRONMENT_IS_WEB; |
| 42 | + if (!Module['locateFile']) Module['locateFile'] = Module['__locateFile'] = (path) => dotnet_replacements.scriptDirectory + path; |
| 43 | + |
| 44 | + // call runtime |
| 45 | + initializeImportsAndExports( |
| 46 | + { |
| 47 | + isNode: ENVIRONMENT_IS_NODE, isWorker: ENVIRONMENT_IS_WORKER, isShell: ENVIRONMENT_IS_SHELL, isWeb: ENVIRONMENT_IS_WEB, isPThread: ENVIRONMENT_IS_PTHREAD, |
| 48 | + quit_, ExitStatus |
| 49 | + }, dotnet_replacements); |
| 50 | + |
| 51 | + updateMemoryViews = dotnet_replacements.updateMemoryViews; |
| 52 | + fetch = dotnet_replacements.fetch; |
| 53 | + _scriptDir = __dirname = scriptDirectory = dotnet_replacements.scriptDirectory; |
| 54 | + #if USE_PTHREADS |
| 55 | + PThread.loadWasmModuleToWorker = dotnet_replacements.loadWasmModuleToWorker; |
| 56 | + PThread.threadInitTLS = dotnet_replacements.threadInitTLS; |
| 57 | + PThread.allocateUnusedWorker = dotnet_replacements.allocateUnusedWorker; |
| 58 | + #endif |
50 | 59 | } |
51 | | -var noExitRuntime = __dotnet_replacements.noExitRuntime; |
52 | | -${monoWasmThreads ? ` |
53 | | -PThread.loadWasmModuleToWorker = __dotnet_replacements.pthreadReplacements.loadWasmModuleToWorker; |
54 | | -PThread.threadInitTLS = __dotnet_replacements.pthreadReplacements.threadInitTLS; |
55 | | -PThread.allocateUnusedWorker = __dotnet_replacements.pthreadReplacements.allocateUnusedWorker; |
56 | | -` : ''} |
57 | | -`, |
| 60 | + |
| 61 | +const postset = ` |
| 62 | + DOTNET.setup(); |
| 63 | +`; |
| 64 | + |
| 65 | +const DotnetSupportLib = { |
| 66 | + $DOTNET: { setup }, |
| 67 | + $DOTNET__postset: postset |
58 | 68 | }; |
59 | 69 |
|
60 | 70 | // the methods would be visible to EMCC linker |
@@ -97,7 +107,7 @@ let linked_functions = [ |
97 | 107 | "mono_wasm_invoke_import", |
98 | 108 | "mono_wasm_bind_cs_function", |
99 | 109 | "mono_wasm_marshal_promise", |
100 | | - |
| 110 | + |
101 | 111 | "icudt68_dat", |
102 | 112 | ]; |
103 | 113 |
|
|
0 commit comments