-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[browser][coreCLR] browserhost improvements #122233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pavelsavara
wants to merge
5
commits into
dotnet:main
Choose a base branch
from
pavelsavara:browser_fetch_wasm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−156
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ffb46b6
- rename libSystem.Native.Browser.Utils.js
pavelsavara 0b5de85
Merge branch 'main' into browser_fetch_wasm
pavelsavara 11cf136
- fix nodeJS instantiation
pavelsavara 925609b
Update src/native/libs/System.Native.Browser/ReadMe.md
pavelsavara 5c4c7ab
cleanup
pavelsavara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| import type { LoadBootResourceCallback, JsModuleExports, JsAsset, AssemblyAsset, PdbAsset, WasmAsset, IcuAsset, EmscriptenModuleInternal, InstantiateWasmSuccessCallback } from "./types"; | ||
| import { dotnetAssert, dotnetGetInternals, dotnetBrowserHostExports, dotnetUpdateInternals } from "./cross-module"; | ||
| import { ENVIRONMENT_IS_NODE } from "./per-module"; | ||
| import { getIcuResourceName } from "./icu"; | ||
| import { getLoaderConfig } from "./config"; | ||
| import { BrowserHost_InitializeCoreCLR } from "./run"; | ||
| import { createPromiseCompletionSource } from "./promise-completion-source"; | ||
| import { locateFile } from "./bootstrap"; | ||
|
|
||
| const nativeModulePromiseController = createPromiseCompletionSource<EmscriptenModuleInternal>(() => { | ||
| dotnetUpdateInternals(dotnetGetInternals()); | ||
| }); | ||
| let wasmBinaryPromise: Promise<Response> = undefined as any; | ||
|
|
||
| // WASM-TODO: retry logic | ||
| // WASM-TODO: throttling logic | ||
| // WASM-TODO: invokeLibraryInitializers | ||
| // WASM-TODO: webCIL | ||
| // WASM-TODO: downloadOnly - blazor render mode auto pre-download. Really no start. | ||
| // WASM-TODO: no-cache, force-cache, integrity | ||
| // WASM-TODO: LoadBootResourceCallback | ||
| // WASM-TODO: fail fast for missing WASM features - SIMD, EH, BigInt detection | ||
|
|
||
| export async function createRuntime(downloadOnly: boolean, loadBootResource?: LoadBootResourceCallback): Promise<any> { | ||
| if (loadBootResource) throw new Error("TODO: loadBootResource is not implemented yet"); | ||
| const config = getLoaderConfig(); | ||
| if (!config.resources || !config.resources.coreAssembly || !config.resources.coreAssembly.length) throw new Error("Invalid config, resources is not set"); | ||
|
|
||
| const nativeModulePromise = loadJSModule(config.resources.jsModuleNative[0]); | ||
| const runtimeModulePromise = loadJSModule(config.resources.jsModuleRuntime[0]); | ||
| const wasmNativePromise = fetchWasm(config.resources.wasmNative[0]); | ||
|
|
||
| const coreAssembliesPromise = Promise.all(config.resources.coreAssembly.map(fetchDll)); | ||
| const coreVfsPromise = Promise.all((config.resources.coreVfs || []).map(fetchVfs)); | ||
| const assembliesPromise = Promise.all(config.resources.assembly.map(fetchDll)); | ||
| const vfsPromise = Promise.all((config.resources.vfs || []).map(fetchVfs)); | ||
| const icuResourceName = getIcuResourceName(config); | ||
| const icuDataPromise = icuResourceName ? Promise.all((config.resources.icu || []).filter(asset => asset.name === icuResourceName).map(fetchIcu)) : Promise.resolve([]); | ||
|
|
||
| const nativeModule = await nativeModulePromise; | ||
| const modulePromise = nativeModule.dotnetInitializeModule<EmscriptenModuleInternal>(dotnetGetInternals()); | ||
| nativeModulePromiseController.propagateFrom(modulePromise); | ||
|
|
||
| const runtimeModule = await runtimeModulePromise; | ||
| const runtimeModuleReady = runtimeModule.dotnetInitializeModule<void>(dotnetGetInternals()); | ||
|
|
||
| await nativeModulePromiseController.promise; | ||
| await coreAssembliesPromise; | ||
| await coreVfsPromise; | ||
| await vfsPromise; | ||
| await icuDataPromise; | ||
| await wasmNativePromise; // this is just to propagate errors | ||
| if (!downloadOnly) { | ||
| BrowserHost_InitializeCoreCLR(); | ||
| } | ||
|
|
||
| await assembliesPromise; | ||
| await runtimeModuleReady; | ||
| } | ||
|
|
||
| async function loadJSModule(asset: JsAsset): Promise<JsModuleExports> { | ||
| if (asset.name && !asset.resolvedUrl) { | ||
| asset.resolvedUrl = locateFile(asset.name); | ||
| } | ||
| if (!asset.resolvedUrl) throw new Error("Invalid config, resources is not set"); | ||
| return await import(/* webpackIgnore: true */ asset.resolvedUrl); | ||
| } | ||
|
|
||
| function fetchWasm(asset: WasmAsset): Promise<Response> { | ||
| if (asset.name && !asset.resolvedUrl) { | ||
| asset.resolvedUrl = locateFile(asset.name); | ||
| } | ||
| if (!asset.resolvedUrl) throw new Error("Invalid config, resources is not set"); | ||
| wasmBinaryPromise = fetchLike(asset.resolvedUrl); | ||
| return wasmBinaryPromise; | ||
| } | ||
|
|
||
| export async function instantiateWasm(imports: WebAssembly.Imports, successCallback: InstantiateWasmSuccessCallback): Promise<void> { | ||
| const res = await WebAssembly.instantiateStreaming(wasmBinaryPromise, imports); | ||
| successCallback(res.instance, res.module); | ||
| } | ||
|
|
||
| async function fetchIcu(asset: IcuAsset): Promise<void> { | ||
| if (asset.name && !asset.resolvedUrl) { | ||
| asset.resolvedUrl = locateFile(asset.name); | ||
| } | ||
| const bytes = await fetchBytes(asset); | ||
| await nativeModulePromiseController.promise; | ||
| dotnetBrowserHostExports.loadIcuData(bytes); | ||
| } | ||
|
|
||
| async function fetchDll(asset: AssemblyAsset): Promise<void> { | ||
| if (asset.name && !asset.resolvedUrl) { | ||
| asset.resolvedUrl = locateFile(asset.name); | ||
| } | ||
| const bytes = await fetchBytes(asset); | ||
| await nativeModulePromiseController.promise; | ||
|
|
||
| dotnetBrowserHostExports.registerDllBytes(bytes, asset); | ||
| } | ||
|
|
||
| async function fetchVfs(asset: AssemblyAsset): Promise<void> { | ||
| if (asset.name && !asset.resolvedUrl) { | ||
| asset.resolvedUrl = locateFile(asset.name); | ||
| } | ||
| const bytes = await fetchBytes(asset); | ||
| await nativeModulePromiseController.promise; | ||
|
|
||
| dotnetBrowserHostExports.installVfsFile(bytes, asset); | ||
| } | ||
|
|
||
| async function fetchBytes(asset: WasmAsset | AssemblyAsset | PdbAsset | IcuAsset): Promise<Uint8Array> { | ||
| dotnetAssert.check(asset && asset.resolvedUrl, "Bad asset.resolvedUrl"); | ||
| const response = await fetchLike(asset.resolvedUrl); | ||
| const buffer = await response.arrayBuffer(); | ||
| return new Uint8Array(buffer); | ||
| } | ||
|
|
||
| async function fetchLike(resolvedUrl: string): Promise<Response> { | ||
| dotnetAssert.check(resolvedUrl, "Bad resolvedUrl"); | ||
| if (ENVIRONMENT_IS_NODE) { | ||
| const { promises: fs } = await import(/*! webpackIgnore: true */"fs"); | ||
| const { fileURLToPath } = await import(/*! webpackIgnore: true */"url"); | ||
| const isFileUrl = resolvedUrl.startsWith("file://"); | ||
| if (isFileUrl) { | ||
| resolvedUrl = fileURLToPath(resolvedUrl); | ||
| } | ||
| const arrayBuffer = await fs.readFile(resolvedUrl) as any; | ||
| return <Response><any>{ | ||
| ok: true, | ||
| headers: { | ||
| length: 0, | ||
| get: () => null | ||
| }, | ||
| url: resolvedUrl, | ||
| arrayBuffer: () => arrayBuffer, | ||
| json: () => JSON.parse(arrayBuffer), | ||
| text: () => { | ||
| throw new Error("NotImplementedException"); | ||
| } | ||
| }; | ||
| } else { | ||
| const response = await fetch(resolvedUrl); | ||
| if (!response.ok) { | ||
| throw new Error(`Failed to load ${resolvedUrl} with ${response.status} ${response.statusText}`); | ||
| } | ||
| return response; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.