Skip to content

Commit fc406dd

Browse files
committed
Merge branch 'browser_fetch_wasm' into browser_all
# Conflicts: # src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs # src/native/corehost/browserhost/loader/assets.ts # src/native/corehost/browserhost/loader/bootstrap.ts # src/native/corehost/browserhost/loader/index.ts # src/native/libs/Common/JavaScript/CMakeLists.txt
2 parents 6e1d89c + 5c4c7ab commit fc406dd

File tree

15 files changed

+51
-54
lines changed

15 files changed

+51
-54
lines changed

src/coreclr/hosts/corerun/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ else()
6161
set(JS_SYSTEM_NATIVE_BROWSER
6262
"${STATIC_LIB_DESTINATION}/libSystem.Native.Browser.js")
6363
set(JS_SYSTEM_BROWSER_UTILS
64-
"${STATIC_LIB_DESTINATION}/libSystem.Browser.Utils.js")
64+
"${STATIC_LIB_DESTINATION}/libSystem.Native.Browser.Utils.js")
6565
set(JS_CORE_RUN
6666
"${CMAKE_CURRENT_SOURCE_DIR}/wasm/libCorerun.extpost.js")
6767
set_target_properties(corerun PROPERTIES

src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@
271271
<PlatformManifestFileEntry Include="libnativeresourcestring.a" IsNative="true" />
272272
<PlatformManifestFileEntry Include="libBrowserHost.js" IsNative="true" />
273273
<PlatformManifestFileEntry Include="libBrowserHost.js.map" IsNative="true" />
274-
<PlatformManifestFileEntry Include="libSystem.Browser.Utils.js" IsNative="true" />
275-
<PlatformManifestFileEntry Include="libSystem.Browser.Utils.js.map" IsNative="true" />
274+
<PlatformManifestFileEntry Include="libSystem.Native.Browser.Utils.js" IsNative="true" />
275+
<PlatformManifestFileEntry Include="libSystem.Native.Browser.Utils.js.map" IsNative="true" />
276276
<PlatformManifestFileEntry Include="libSystem.Native.Browser.js" IsNative="true" />
277277
<PlatformManifestFileEntry Include="libSystem.Native.Browser.js.map" IsNative="true" />
278278
<PlatformManifestFileEntry Include="libSystem.Runtime.InteropServices.JavaScript.Native.js" IsNative="true" />

src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ internal static partial class Libraries
1616
internal const string JavaScriptNative = "libSystem.Runtime.InteropServices.JavaScript.Native";
1717
internal const string IOPortsNative = "libSystem.IO.Ports.Native";
1818
internal const string HostPolicy = "libhostpolicy";
19-
internal const string SystemBrowserNative = "libSystem.JavaScript.Native";
19+
internal const string SystemBrowserNative = "libSystem.Native.Browser";
2020
}
2121
}

src/native/corehost/browserhost/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ LIST(APPEND NATIVE_LIBS
7979
set(JS_SYSTEM_NATIVE_BROWSER
8080
"${SHARED_LIB_DESTINATION}/libSystem.Native.Browser.js")
8181
set(JS_SYSTEM_BROWSER_UTILS
82-
"${SHARED_LIB_DESTINATION}/libSystem.Browser.Utils.js")
82+
"${SHARED_LIB_DESTINATION}/libSystem.Native.Browser.Utils.js")
8383
set(JS_SYSTEM_RUNTIME_INTEROPSERVICES_JAVASCRIPT_NATIVE
8484
"${SHARED_LIB_DESTINATION}/libSystem.Runtime.InteropServices.JavaScript.Native.js")
8585
set(JS_BROWSER_HOST

src/native/corehost/browserhost/ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Implements native part of the CoreCLR host and exposes it as an internal JavaScr
1717
It is **Emscripten application** statically linked from libraries.
1818

1919
- `libSystem.Native.Browser.js` linked -> `dotnet.native.js`
20-
- `libSystem.Browser.Utils.js` linked -> `dotnet.native.js`
20+
- `libSystem.Native.Browser.Utils.js` linked -> `dotnet.native.js`
2121
- `libSystem.Runtime.InteropServices.JavaScript.Native.js` linked -> `dotnet.native.js`
2222
- `libSystem.Native.Browser.a` linked -> `dotnet.native.wasm`
2323
- `libSystem.Runtime.InteropServices.JavaScript.Native.a` linked -> `dotnet.native.wasm`

src/native/corehost/browserhost/browserhost.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static const void* pinvoke_override(const char* library_name, const char* entry_
6868
{
6969
return SystemResolveDllImport(entry_point_name);
7070
}
71-
if (strcmp(library_name, "libSystem.JavaScript.Native") == 0)
71+
if (strcmp(library_name, "libSystem.Native.Browser") == 0)
7272
{
7373
return SystemJSResolveDllImport(entry_point_name);
7474
}

src/native/corehost/browserhost/loader/assets.ts

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
import type { LoadBootResourceCallback, JsModuleExports, JsAsset, AssemblyAsset, PdbAsset, WasmAsset, IcuAsset, EmscriptenModuleInternal, InstantiateWasmSuccessCallback } from "./types";
5+
56
import { dotnetAssert, dotnetGetInternals, dotnetBrowserHostExports, dotnetUpdateInternals } from "./cross-module";
6-
import { ENVIRONMENT_IS_NODE } from "./per-module";
77
import { getIcuResourceName } from "./icu";
88
import { getLoaderConfig } from "./config";
99
import { BrowserHost_InitializeCoreCLR } from "./run";
1010
import { createPromiseCompletionSource } from "./promise-completion-source";
1111
import { locateFile } from "./bootstrap";
12+
import { fetchLike } from "./polyfills";
1213

1314
const nativeModulePromiseController = createPromiseCompletionSource<EmscriptenModuleInternal>(() => {
1415
dotnetUpdateInternals(dotnetGetInternals());
1516
});
16-
let wasmBinaryPromise: Promise<Response> = undefined as any;
17+
let wasmBinaryPromise: any = undefined;
1718

1819
// WASM-TODO: retry logic
1920
// WASM-TODO: throttling logic
2021
// WASM-TODO: invokeLibraryInitializers
2122
// WASM-TODO: webCIL
2223
// WASM-TODO: downloadOnly - blazor render mode auto pre-download. Really no start.
24+
// WASM-TODO: no-cache, force-cache, integrity
25+
// WASM-TODO: LoadBootResourceCallback
26+
// WASM-TODO: fail fast for missing WASM features - SIMD, EH, BigInt detection
2327

2428
export async function createRuntime(downloadOnly: boolean, loadBootResource?: LoadBootResourceCallback): Promise<any> {
2529
if (loadBootResource) throw new Error("TODO: loadBootResource is not implemented yet");
@@ -76,11 +80,18 @@ function fetchWasm(asset: WasmAsset): Promise<Response> {
7680
}
7781

7882
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);
83+
if (wasmBinaryPromise instanceof globalThis.Response === false || !WebAssembly.instantiateStreaming) {
84+
const res = await wasmBinaryPromise;
85+
const data = await res.arrayBuffer();
86+
const module = await WebAssembly.compile(data);
87+
const instance = await WebAssembly.instantiate(module, imports);
88+
successCallback(instance, module);
89+
} else {
90+
const res = await WebAssembly.instantiateStreaming(wasmBinaryPromise, imports);
91+
successCallback(res.instance, res.module);
92+
}
8193
}
8294

83-
8495
async function fetchIcu(asset: IcuAsset): Promise<void> {
8596
if (asset.name && !asset.resolvedUrl) {
8697
asset.resolvedUrl = locateFile(asset.name);
@@ -116,36 +127,3 @@ async function fetchBytes(asset: WasmAsset | AssemblyAsset | PdbAsset | IcuAsset
116127
const buffer = await response.arrayBuffer();
117128
return new Uint8Array(buffer);
118129
}
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-
}

src/native/corehost/browserhost/loader/bootstrap.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
import type { LoaderConfig, DotnetHostBuilder } from "./types";
4+
import { type LoaderConfig, type DotnetHostBuilder, GlobalizationMode } from "./types";
55
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL } from "./per-module";
66
import { nodeFs } from "./polyfills";
77

@@ -84,17 +84,34 @@ export async function findResources(dotnet: DotnetHostBuilder): Promise<void> {
8484
const json = await fs.promises.readFile(runtimeConfigName, { encoding: "utf8" });
8585
runtimeConfig = JSON.parse(json);
8686
}
87+
const icus = files
88+
.filter(file => file.startsWith("icudt") && file.endsWith(".dat"))
89+
.map(filename => {
90+
// filename without path
91+
const name = filename.substring(filename.lastIndexOf("/") + 1);
92+
return { virtualPath: name, name };
93+
});
94+
95+
const environmentVariables: { [key: string]: string } = {};
96+
let globalizationMode = GlobalizationMode.All;
97+
if (!icus.length) {
98+
globalizationMode = GlobalizationMode.Invariant;
99+
environmentVariables["DOTNET_SYSTEM_GLOBALIZATION_INVARIANT"] = "1";
100+
}
87101

88102
const config: LoaderConfig = {
89103
mainAssemblyName,
90104
runtimeConfig,
105+
globalizationMode,
91106
virtualWorkingDirectory: mountedDir,
107+
environmentVariables,
92108
resources: {
93109
jsModuleNative: [{ name: "dotnet.native.js" }],
94110
jsModuleRuntime: [{ name: "dotnet.runtime.js" }],
95111
wasmNative: [{ name: "dotnet.native.wasm", }],
96112
coreAssembly: [{ virtualPath: mountedDir + "/System.Private.CoreLib.dll", name: "System.Private.CoreLib.dll" },],
97113
assembly: assemblies,
114+
icu: icus,
98115
}
99116
};
100117
dotnet.withConfig(config);

src/native/corehost/browserhost/loader/icu.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export function getIcuResourceName(config: LoaderConfig): string | null {
3333
}
3434

3535
config.globalizationMode = GlobalizationMode.Invariant;
36+
config.environmentVariables!["DOTNET_SYSTEM_GLOBALIZATION_INVARIANT"] = "1";
3637
return null;
3738
}
3839

src/native/corehost/browserhost/loader/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export function dotnetInitializeModule(): RuntimeAPI {
7373
};
7474
Object.assign(dotnetAssert, assert);
7575

76+
// emscripten extension point
7677
const localModule: Partial<EmscriptenModuleInternal> = {
7778
instantiateWasm,
7879
};

0 commit comments

Comments
 (0)