|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +import type { LoadBootResourceCallback, JsModuleExports, JsAsset, AssemblyAsset, PdbAsset, WasmAsset, IcuAsset, EmscriptenModuleInternal, InstantiateWasmSuccessCallback } from "./types"; |
| 5 | +import { dotnetAssert, dotnetGetInternals, dotnetBrowserHostExports, dotnetUpdateInternals } from "./cross-module"; |
| 6 | +import { ENVIRONMENT_IS_NODE } from "./per-module"; |
| 7 | +import { getIcuResourceName } from "./icu"; |
| 8 | +import { getLoaderConfig } from "./config"; |
| 9 | +import { BrowserHost_InitializeCoreCLR } from "./run"; |
| 10 | +import { createPromiseCompletionSource } from "./promise-completion-source"; |
| 11 | +import { locateFile } from "./bootstrap"; |
| 12 | + |
| 13 | +const nativeModulePromiseController = createPromiseCompletionSource<EmscriptenModuleInternal>(() => { |
| 14 | + dotnetUpdateInternals(dotnetGetInternals()); |
| 15 | +}); |
| 16 | +let wasmBinaryPromise: Promise<Response> = undefined as any; |
| 17 | + |
| 18 | +// WASM-TODO: retry logic |
| 19 | +// WASM-TODO: throttling logic |
| 20 | +// WASM-TODO: invokeLibraryInitializers |
| 21 | +// WASM-TODO: webCIL |
| 22 | +// WASM-TODO: downloadOnly - blazor render mode auto pre-download. Really no start. |
| 23 | + |
| 24 | +export async function createRuntime(downloadOnly: boolean, loadBootResource?: LoadBootResourceCallback): Promise<any> { |
| 25 | + if (loadBootResource) throw new Error("TODO: loadBootResource is not implemented yet"); |
| 26 | + const config = getLoaderConfig(); |
| 27 | + if (!config.resources || !config.resources.coreAssembly || !config.resources.coreAssembly.length) throw new Error("Invalid config, resources is not set"); |
| 28 | + |
| 29 | + const nativeModulePromise = loadJSModule(config.resources.jsModuleNative[0]); |
| 30 | + const runtimeModulePromise = loadJSModule(config.resources.jsModuleRuntime[0]); |
| 31 | + const wasmNativePromise = fetchWasm(config.resources.wasmNative[0]); |
| 32 | + |
| 33 | + const coreAssembliesPromise = Promise.all(config.resources.coreAssembly.map(fetchDll)); |
| 34 | + const coreVfsPromise = Promise.all((config.resources.coreVfs || []).map(fetchVfs)); |
| 35 | + const assembliesPromise = Promise.all(config.resources.assembly.map(fetchDll)); |
| 36 | + const vfsPromise = Promise.all((config.resources.vfs || []).map(fetchVfs)); |
| 37 | + const icuResourceName = getIcuResourceName(config); |
| 38 | + const icuDataPromise = icuResourceName ? Promise.all((config.resources.icu || []).filter(asset => asset.name === icuResourceName).map(fetchIcu)) : Promise.resolve([]); |
| 39 | + |
| 40 | + const nativeModule = await nativeModulePromise; |
| 41 | + const modulePromise = nativeModule.dotnetInitializeModule<EmscriptenModuleInternal>(dotnetGetInternals()); |
| 42 | + nativeModulePromiseController.propagateFrom(modulePromise); |
| 43 | + |
| 44 | + const runtimeModule = await runtimeModulePromise; |
| 45 | + const runtimeModuleReady = runtimeModule.dotnetInitializeModule<void>(dotnetGetInternals()); |
| 46 | + |
| 47 | + await nativeModulePromiseController.promise; |
| 48 | + await coreAssembliesPromise; |
| 49 | + await coreVfsPromise; |
| 50 | + await vfsPromise; |
| 51 | + await icuDataPromise; |
| 52 | + await wasmNativePromise; // this is just to propagate errors |
| 53 | + if (!downloadOnly) { |
| 54 | + BrowserHost_InitializeCoreCLR(); |
| 55 | + } |
| 56 | + |
| 57 | + await assembliesPromise; |
| 58 | + await runtimeModuleReady; |
| 59 | +} |
| 60 | + |
| 61 | +async function loadJSModule(asset: JsAsset): Promise<JsModuleExports> { |
| 62 | + if (asset.name && !asset.resolvedUrl) { |
| 63 | + asset.resolvedUrl = locateFile(asset.name); |
| 64 | + } |
| 65 | + if (!asset.resolvedUrl) throw new Error("Invalid config, resources is not set"); |
| 66 | + return await import(/* webpackIgnore: true */ asset.resolvedUrl); |
| 67 | +} |
| 68 | + |
| 69 | +function fetchWasm(asset: WasmAsset): Promise<Response> { |
| 70 | + if (asset.name && !asset.resolvedUrl) { |
| 71 | + asset.resolvedUrl = locateFile(asset.name); |
| 72 | + } |
| 73 | + if (!asset.resolvedUrl) throw new Error("Invalid config, resources is not set"); |
| 74 | + wasmBinaryPromise = fetchLike(asset.resolvedUrl); |
| 75 | + return wasmBinaryPromise; |
| 76 | +} |
| 77 | + |
| 78 | +export async function instantiateWasm(imports: WebAssembly.Imports, successCallback: InstantiateWasmSuccessCallback): Promise<void> { |
| 79 | + const res = await WebAssembly.instantiateStreaming(wasmBinaryPromise, imports); |
| 80 | + successCallback(res.instance, res.module); |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +async function fetchIcu(asset: IcuAsset): Promise<void> { |
| 85 | + if (asset.name && !asset.resolvedUrl) { |
| 86 | + asset.resolvedUrl = locateFile(asset.name); |
| 87 | + } |
| 88 | + const bytes = await fetchBytes(asset); |
| 89 | + await nativeModulePromiseController.promise; |
| 90 | + dotnetBrowserHostExports.loadIcuData(bytes); |
| 91 | +} |
| 92 | + |
| 93 | +async function fetchDll(asset: AssemblyAsset): Promise<void> { |
| 94 | + if (asset.name && !asset.resolvedUrl) { |
| 95 | + asset.resolvedUrl = locateFile(asset.name); |
| 96 | + } |
| 97 | + const bytes = await fetchBytes(asset); |
| 98 | + await nativeModulePromiseController.promise; |
| 99 | + |
| 100 | + dotnetBrowserHostExports.registerDllBytes(bytes, asset); |
| 101 | +} |
| 102 | + |
| 103 | +async function fetchVfs(asset: AssemblyAsset): Promise<void> { |
| 104 | + if (asset.name && !asset.resolvedUrl) { |
| 105 | + asset.resolvedUrl = locateFile(asset.name); |
| 106 | + } |
| 107 | + const bytes = await fetchBytes(asset); |
| 108 | + await nativeModulePromiseController.promise; |
| 109 | + |
| 110 | + dotnetBrowserHostExports.installVfsFile(bytes, asset); |
| 111 | +} |
| 112 | + |
| 113 | +async function fetchBytes(asset: WasmAsset | AssemblyAsset | PdbAsset | IcuAsset): Promise<Uint8Array> { |
| 114 | + dotnetAssert.check(asset && asset.resolvedUrl, "Bad asset.resolvedUrl"); |
| 115 | + const response = await fetchLike(asset.resolvedUrl); |
| 116 | + const buffer = await response.arrayBuffer(); |
| 117 | + return new Uint8Array(buffer); |
| 118 | +} |
| 119 | + |
| 120 | +async function fetchLike(resolvedUrl: string): Promise<Response> { |
| 121 | + dotnetAssert.check(resolvedUrl, "Bad resolvedUrl"); |
| 122 | + if (ENVIRONMENT_IS_NODE) { |
| 123 | + const { promises: fs } = await import(/*! webpackIgnore: true */"fs"); |
| 124 | + const { fileURLToPath } = await import(/*! webpackIgnore: true */"url"); |
| 125 | + const isFileUrl = resolvedUrl.startsWith("file://"); |
| 126 | + if (isFileUrl) { |
| 127 | + resolvedUrl = fileURLToPath(resolvedUrl); |
| 128 | + } |
| 129 | + const arrayBuffer = await fs.readFile(resolvedUrl) as any; |
| 130 | + return <Response><any>{ |
| 131 | + ok: true, |
| 132 | + headers: { |
| 133 | + length: 0, |
| 134 | + get: () => null |
| 135 | + }, |
| 136 | + url: resolvedUrl, |
| 137 | + arrayBuffer: () => arrayBuffer, |
| 138 | + json: () => JSON.parse(arrayBuffer), |
| 139 | + text: () => { |
| 140 | + throw new Error("NotImplementedException"); |
| 141 | + } |
| 142 | + }; |
| 143 | + } else { |
| 144 | + const response = await fetch(resolvedUrl); |
| 145 | + if (!response.ok) { |
| 146 | + throw new Error(`Failed to load ${resolvedUrl} with ${response.status} ${response.statusText}`); |
| 147 | + } |
| 148 | + return response; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments