Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
- moved get_preferred_icu_asset
- moved TZ detection
- moved runtimeHelpers.waitForDebugger
- anyModule refactoring
- added runtimeHelpers.updateGlobalBufferAndViews
- removed obsolete comments about blazor startup sequence
- removed diplicate calls mono_wasm_init_diagnostics (also previously blazor startup path)
  • Loading branch information
pavelsavara committed Mar 7, 2023
commit 64eb318ecf92189753a3f62ce8ae28b9e7ae3428
40 changes: 6 additions & 34 deletions src/mono/wasm/runtime/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,8 @@ const skipInstantiateByAssetTypes: {
"dotnetwasm": true,
};

export function get_preferred_icu_asset(): string | null {
if (!runtimeHelpers.config.assets)
return null;

// By setting <WasmIcuDataFileName> user can define what ICU source file they want to load.
// There is no need to check application's culture when <WasmIcuDataFileName> is set.
// If it was not set, then we have 3 "icu" assets in config and we should choose
// only one for loading, the one that matches the application's locale.
const icuAssets = runtimeHelpers.config.assets.filter(a => a["behavior"] == "icu");
if (icuAssets.length === 1)
return icuAssets[0].name;

// reads the browsers locale / the OS's locale
const preferredCulture = ENVIRONMENT_IS_WEB ? navigator.language : Intl.DateTimeFormat().resolvedOptions().locale;
const prefix = preferredCulture.split("-")[0];
const CJK = "icudt_CJK.dat";
const EFIGS = "icudt_EFIGS.dat";
const OTHERS = "icudt_no_CJK.dat";

// not all "fr-*", "it-*", "de-*", "es-*" are in EFIGS, only the one that is mostly used
if (prefix == "en" || ["fr", "fr-FR", "it", "it-IT", "de", "de-DE", "es", "es-ES"].includes(preferredCulture))
return EFIGS;
if (["zh", "ko", "ja"].includes(prefix))
return CJK;
return OTHERS;
}

export function shouldLoadIcuAsset(asset: AssetEntryInternal, preferredIcuAsset: string | null): boolean {
return !(asset.behavior == "icu" && asset.name != preferredIcuAsset);
export function shouldLoadIcuAsset(asset: AssetEntryInternal): boolean {
return !(asset.behavior == "icu" && asset.name != runtimeHelpers.preferredIcuAsset);
}

export function resolve_asset_path(behavior: AssetBehaviours) {
Expand All @@ -86,7 +59,6 @@ export function resolve_asset_path(behavior: AssetBehaviours) {
return asset;
}
export async function mono_download_assets(): Promise<void> {
const preferredIcuAsset = get_preferred_icu_asset();
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_download_assets");
runtimeHelpers.maxParallelDownloads = runtimeHelpers.config.maxParallelDownloads || runtimeHelpers.maxParallelDownloads;
runtimeHelpers.enableDownloadRetry = runtimeHelpers.config.enableDownloadRetry || runtimeHelpers.enableDownloadRetry;
Expand All @@ -101,10 +73,10 @@ export async function mono_download_assets(): Promise<void> {
mono_assert(!asset.resolvedUrl || typeof asset.resolvedUrl === "string", "asset resolvedUrl could be string");
mono_assert(!asset.hash || typeof asset.hash === "string", "asset resolvedUrl could be string");
mono_assert(!asset.pendingDownload || typeof asset.pendingDownload === "object", "asset pendingDownload could be object");
if (!skipInstantiateByAssetTypes[asset.behavior] && shouldLoadIcuAsset(asset, preferredIcuAsset)) {
if (!skipInstantiateByAssetTypes[asset.behavior] && shouldLoadIcuAsset(asset)) {
expected_instantiated_assets_count++;
}
if (!skipDownloadsByAssetTypes[asset.behavior] && shouldLoadIcuAsset(asset, preferredIcuAsset)) {
if (!skipDownloadsByAssetTypes[asset.behavior] && shouldLoadIcuAsset(asset)) {
expected_downloaded_assets_count++;
promises_of_assets.push(start_asset_download(asset));
}
Expand Down Expand Up @@ -132,10 +104,10 @@ export async function mono_download_assets(): Promise<void> {
const headersOnly = skipBufferByAssetTypes[asset.behavior];
if (!headersOnly) {
mono_assert(asset.isOptional, "Expected asset to have the downloaded buffer");
if (!skipDownloadsByAssetTypes[asset.behavior] && shouldLoadIcuAsset(asset, preferredIcuAsset)) {
if (!skipDownloadsByAssetTypes[asset.behavior] && shouldLoadIcuAsset(asset)) {
expected_downloaded_assets_count--;
}
if (!skipInstantiateByAssetTypes[asset.behavior] && shouldLoadIcuAsset(asset, preferredIcuAsset)) {
if (!skipInstantiateByAssetTypes[asset.behavior] && shouldLoadIcuAsset(asset)) {
expected_instantiated_assets_count--;
}
} else {
Expand Down
26 changes: 26 additions & 0 deletions src/mono/wasm/runtime/icu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,29 @@ export function mono_wasm_globalization_init(): void {
}
}

export function get_preferred_icu_asset(): string | null {
if (!runtimeHelpers.config.assets)
return null;

// By setting <WasmIcuDataFileName> user can define what ICU source file they want to load.
// There is no need to check application's culture when <WasmIcuDataFileName> is set.
// If it was not set, then we have 3 "icu" assets in config and we should choose
// only one for loading, the one that matches the application's locale.
const icuAssets = runtimeHelpers.config.assets.filter(a => a["behavior"] == "icu");
if (icuAssets.length === 1)
return icuAssets[0].name;

// reads the browsers locale / the OS's locale
const preferredCulture = ENVIRONMENT_IS_WEB ? navigator.language : Intl.DateTimeFormat().resolvedOptions().locale;
const prefix = preferredCulture.split("-")[0];
const CJK = "icudt_CJK.dat";
const EFIGS = "icudt_EFIGS.dat";
const OTHERS = "icudt_no_CJK.dat";

// not all "fr-*", "it-*", "de-*", "es-*" are in EFIGS, only the one that is mostly used
if (prefix == "en" || ["fr", "fr-FR", "it", "it-IT", "de", "de-DE", "es", "es-ES"].includes(preferredCulture))
return EFIGS;
if (["zh", "ko", "ja"].includes(prefix))
return CJK;
return OTHERS;
}
3 changes: 2 additions & 1 deletion src/mono/wasm/runtime/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { EmscriptenModule } from "./types/emscripten";

// these are our public API (except internal)
export let Module: EmscriptenModule & DotnetModule;
export let anyModule: any = undefined;
export let INTERNAL: any;
export let IMPORTS: any;

Expand All @@ -29,7 +30,7 @@ export function set_imports_exports(
): void {
INTERNAL = exports.internal;
IMPORTS = exports.marshaled_imports;
Module = exports.module;
anyModule = Module = exports.module;

ENVIRONMENT_IS_NODE = imports.isNode;
ENVIRONMENT_IS_SHELL = imports.isShell;
Expand Down
3 changes: 1 addition & 2 deletions src/mono/wasm/runtime/managed-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

import { GCHandle, MarshalerToCs, MarshalerToJs, MonoMethod, mono_assert } from "./types";
import cwraps from "./cwraps";
import { Module, runtimeHelpers, ENVIRONMENT_IS_PTHREAD } from "./imports";
import { runtimeHelpers, ENVIRONMENT_IS_PTHREAD, anyModule } from "./imports";
import { alloc_stack_frame, get_arg, get_arg_gc_handle, MarshalerType, set_arg_type, set_gc_handle } from "./marshal";
import { invoke_method_and_handle_exception } from "./invoke-cs";
import { marshal_array_to_cs_impl, marshal_exception_to_cs, marshal_intptr_to_cs } from "./marshal-to-cs";
import { marshal_int32_to_js, marshal_string_to_js, marshal_task_to_js } from "./marshal-to-js";

export function init_managed_exports(): void {
const anyModule = Module as any;
const exports_fqn_asm = "System.Runtime.InteropServices.JavaScript";
runtimeHelpers.runtime_interop_module = cwraps.mono_wasm_assembly_load(exports_fqn_asm);
if (!runtimeHelpers.runtime_interop_module)
Expand Down
3 changes: 1 addition & 2 deletions src/mono/wasm/runtime/marshal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

import { js_owned_gc_handle_symbol, teardown_managed_proxy } from "./gc-handles";
import { Module, runtimeHelpers } from "./imports";
import { anyModule, Module, runtimeHelpers } from "./imports";
import { getF32, getF64, getI16, getI32, getI64Big, getU16, getU32, getU8, setF32, setF64, setI16, setI32, setI64Big, setU16, setU32, setU8 } from "./memory";
import { mono_wasm_new_external_root } from "./roots";
import { mono_assert, GCHandle, JSHandle, MonoObject, MonoString, GCHandleNull, JSMarshalerArguments, JSFunctionSignature, JSMarshalerType, JSMarshalerArgument, MarshalerToJs, MarshalerToCs, WasmRoot } from "./types";
Expand Down Expand Up @@ -41,7 +41,6 @@ export const JSMarshalerTypeSize = 32;
export const JSMarshalerSignatureHeaderSize = 4 + 4; // without Exception and Result

export function alloc_stack_frame(size: number): JSMarshalerArguments {
const anyModule = Module as any;
const args = anyModule.stackAlloc(JavaScriptMarshalerArgSize * size);
mono_assert(args && (<any>args) % 8 == 0, "Arg alignment");
const exc = get_arg(args, 0);
Expand Down
5 changes: 2 additions & 3 deletions src/mono/wasm/runtime/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import BuildConfiguration from "consts:configuration";
import MonoWasmThreads from "consts:monoWasmThreads";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, ENVIRONMENT_IS_WORKER, INTERNAL, Module, runtimeHelpers } from "./imports";
import { anyModule, ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, ENVIRONMENT_IS_WORKER, INTERNAL, Module, runtimeHelpers } from "./imports";
import { afterUpdateGlobalBufferAndViews } from "./memory";
import { replaceEmscriptenPThreadLibrary } from "./pthreads/shared/emscripten-replacements";
import { DotnetModuleConfigImports, EarlyReplacements } from "./types";
Expand All @@ -13,7 +13,6 @@ let node_fs: any | undefined = undefined;
let node_url: any | undefined = undefined;

export function init_polyfills(replacements: EarlyReplacements): void {
const anyModule = Module as any;

// performance.now() is used by emscripten and doesn't work in JSC
if (typeof globalThis.performance === "undefined") {
Expand Down Expand Up @@ -177,7 +176,7 @@ export function init_polyfills(replacements: EarlyReplacements): void {

// memory
const originalUpdateGlobalBufferAndViews = replacements.updateGlobalBufferAndViews;
replacements.updateGlobalBufferAndViews = (buffer: ArrayBufferLike) => {
runtimeHelpers.updateGlobalBufferAndViews = replacements.updateGlobalBufferAndViews = (buffer: ArrayBufferLike) => {
originalUpdateGlobalBufferAndViews(buffer);
afterUpdateGlobalBufferAndViews(buffer);
};
Expand Down
47 changes: 19 additions & 28 deletions src/mono/wasm/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import BuildConfiguration from "consts:configuration";
import MonoWasmThreads from "consts:monoWasmThreads";
import WasmEnableLegacyJsInterop from "consts:WasmEnableLegacyJsInterop";
import { CharPtrNull, DotnetModule, RuntimeAPI, MonoConfig, MonoConfigInternal } from "./types";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, INTERNAL, Module, runtimeHelpers } from "./imports";
import { anyModule, ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, INTERNAL, Module, runtimeHelpers } from "./imports";
import cwraps, { init_c_exports } from "./cwraps";
import { mono_wasm_raise_debug_event, mono_wasm_runtime_ready } from "./debug";
import { mono_wasm_globalization_init } from "./icu";
import { get_preferred_icu_asset, mono_wasm_globalization_init } from "./icu";
import { toBase64StringImpl } from "./base64";
import { mono_wasm_init_aot_profiler, mono_wasm_init_browser_profiler } from "./profiler";
import { mono_on_abort, mono_exit } from "./run";
Expand Down Expand Up @@ -125,7 +125,6 @@ async function instantiateWasmWorker(
// wait for the config to arrive by message from the main thread
await afterConfigLoaded.promise;

const anyModule = Module as any;
normalizeConfig();
replace_linker_placeholders(imports, export_linker());

Expand Down Expand Up @@ -295,7 +294,6 @@ export function abort_startup(reason: any, should_exit: boolean): void {
}
}

// runs in both blazor and non-blazor
function mono_wasm_pre_init_essential(isWorker: boolean): void {
if (!isWorker)
Module.addRunDependency("mono_wasm_pre_init_essential");
Expand All @@ -318,7 +316,6 @@ function mono_wasm_pre_init_essential(isWorker: boolean): void {
Module.removeRunDependency("mono_wasm_pre_init_essential");
}

// runs in both blazor and non-blazor
async function mono_wasm_pre_init_essential_async(): Promise<void> {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_pre_init_essential_async");
Module.addRunDependency("mono_wasm_pre_init_essential_async");
Expand All @@ -333,7 +330,6 @@ async function mono_wasm_pre_init_essential_async(): Promise<void> {
Module.removeRunDependency("mono_wasm_pre_init_essential_async");
}

// runs just in non-blazor
async function mono_wasm_pre_init_full(): Promise<void> {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_pre_init_full");
Module.addRunDependency("mono_wasm_pre_init_full");
Expand All @@ -343,7 +339,6 @@ async function mono_wasm_pre_init_full(): Promise<void> {
Module.removeRunDependency("mono_wasm_pre_init_full");
}

// runs just in non-blazor
async function mono_wasm_before_user_runtime_initialized(): Promise<void> {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_before_user_runtime_initialized");

Expand All @@ -365,7 +360,6 @@ async function mono_wasm_before_user_runtime_initialized(): Promise<void> {
}
}

// runs in both blazor and non-blazor
async function mono_wasm_after_user_runtime_initialized(): Promise<void> {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_after_user_runtime_initialized");
try {
Expand All @@ -386,10 +380,6 @@ async function mono_wasm_after_user_runtime_initialized(): Promise<void> {
}
}
}
// for Blazor, init diagnostics after their "onRuntimeInitalized" sets env variables, but before their postRun callback (which calls mono_wasm_load_runtime)
if (MonoWasmThreads) {
await mono_wasm_init_diagnostics();
}

if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: Initializing mono runtime");

Expand Down Expand Up @@ -484,15 +474,7 @@ async function instantiate_wasm_module(
Module.removeRunDependency("instantiate_wasm_module");
}

// runs just in non-blazor
async function _apply_configuration_from_args() {
try {
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (tz) mono_wasm_setenv("TZ", tz);
} catch {
console.info("MONO_WASM: failed to detect timezone, will fallback to UTC");
}

// create /usr/share folder which is SpecialFolder.CommonApplicationData
Module["FS_createPath"]("/", "usr", true, true);
Module["FS_createPath"]("/", "usr/share", true, true);
Expand All @@ -514,7 +496,7 @@ async function _apply_configuration_from_args() {
if (config.browserProfilerOptions)
mono_wasm_init_browser_profiler(config.browserProfilerOptions);

// for non-Blazor, init diagnostics after environment variables are set
// init diagnostics after environment variables are set
if (MonoWasmThreads) {
await mono_wasm_init_diagnostics();
}
Expand All @@ -536,7 +518,6 @@ export function mono_wasm_load_runtime(unused?: string, debugLevel?: number): vo
}
cwraps.mono_wasm_load_runtime(unused || "unused", debugLevel);
endMeasure(mark, MeasuredBlock.loadRuntime);
runtimeHelpers.waitForDebugger = config.waitForDebugger;

if (!runtimeHelpers.mono_wasm_bindings_is_ready) bindings_init();
} catch (err: any) {
Expand All @@ -552,10 +533,10 @@ export function mono_wasm_load_runtime(unused?: string, debugLevel?: number): vo
}

export function bindings_init(): void {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: bindings_init");
if (runtimeHelpers.mono_wasm_bindings_is_ready) {
return;
}
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: bindings_init");
runtimeHelpers.mono_wasm_bindings_is_ready = true;
try {
const mark = startMeasure();
Expand Down Expand Up @@ -588,7 +569,7 @@ export async function mono_wasm_load_config(configFilePath?: string): Promise<vo
configLoaded = true;
if (!configFilePath) {
normalizeConfig();
afterConfigLoaded.promise_control.resolve(runtimeHelpers.config);
afterConfigLoaded.promise_control.resolve(config);
return;
}
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_load_config");
Expand All @@ -609,15 +590,15 @@ export async function mono_wasm_load_config(configFilePath?: string): Promise<vo

if (Module.onConfigLoaded) {
try {
await Module.onConfigLoaded(<MonoConfig>runtimeHelpers.config);
await Module.onConfigLoaded(<MonoConfig>config);
normalizeConfig();
}
catch (err: any) {
_print_error("MONO_WASM: onConfigLoaded() failed", err);
throw err;
}
}
afterConfigLoaded.promise_control.resolve(runtimeHelpers.config);
afterConfigLoaded.promise_control.resolve(config);
} catch (err) {
const errMessage = `Failed to load config file ${configFilePath} ${err}`;
abort_startup(errMessage, true);
Expand All @@ -640,13 +621,23 @@ function normalizeConfig() {
if (config.diagnosticTracing === undefined && BuildConfiguration === "Debug") {
config.diagnosticTracing = true;
}
runtimeHelpers.diagnosticTracing = !!runtimeHelpers.config.diagnosticTracing;
runtimeHelpers.diagnosticTracing = !!config.diagnosticTracing;

runtimeHelpers.enablePerfMeasure = !!config.browserProfilerOptions
&& globalThis.performance
&& typeof globalThis.performance.measure === "function";
}
runtimeHelpers.preferredIcuAsset = get_preferred_icu_asset();

if (runtimeHelpers.timezone === undefined && config.environmentVariables["TZ"] === undefined) {
try {
runtimeHelpers.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone || null;
if (runtimeHelpers.timezone) config.environmentVariables["TZ"] = runtimeHelpers.timezone;
} catch {
console.info("MONO_WASM: failed to detect timezone, will fallback to UTC");
}
}
runtimeHelpers.waitForDebugger = config.waitForDebugger;
}

export function mono_wasm_asm_loaded(assembly_name: CharPtr, assembly_ptr: number, assembly_len: number, pdb_ptr: number, pdb_len: number): void {
// Only trigger this codepath for assemblies loaded after app is ready
Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ export type RuntimeHelpers = {
locateFile: (path: string, prefix?: string) => string,
javaScriptExports: JavaScriptExports,
loadedFiles: string[],
preferredIcuAsset: string | null,
timezone: string | null,
updateGlobalBufferAndViews: (buffer: ArrayBufferLike) => void
}

export type GlobalizationMode =
Expand Down