Skip to content

Commit

Permalink
[browser] ts code cleanup (#83210)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara authored Mar 10, 2023
1 parent 9981573 commit a923c64
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 49 deletions.
3 changes: 1 addition & 2 deletions src/mono/wasm/runtime/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ let actual_instantiated_assets_count = 0;
let expected_downloaded_assets_count = 0;
let expected_instantiated_assets_count = 0;
const loaded_files: { url: string, file: string }[] = [];
const loaded_assets: { [id: string]: [VoidPtr, number] } = Object.create(null);
// in order to prevent net::ERR_INSUFFICIENT_RESOURCES if we start downloading too many files at same time
let parallel_count = 0;
let throttlingPromise: PromiseAndController<void> | undefined;
Expand All @@ -29,6 +28,7 @@ const skipDownloadsByAssetTypes: {
[k: string]: boolean
} = {
"js-module-threads": true,
"dotnetwasm": true,
};

// `response.arrayBuffer()` can't be called twice. Some usecases are calling it on response in the instantiation.
Expand Down Expand Up @@ -372,7 +372,6 @@ function _instantiate_asset(asset: AssetEntry, url: string, bytes: Uint8Array) {
case "heap":
case "icu":
offset = mono_wasm_load_bytes_into_heap(bytes);
loaded_assets[virtualName] = [offset, bytes.length];
break;

case "vfs": {
Expand Down
3 changes: 0 additions & 3 deletions src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ let linked_functions = [
"mono_wasm_invoke_import",
"mono_wasm_bind_cs_function",
"mono_wasm_marshal_promise",

// pal_icushim_static.c
"mono_wasm_load_icu_data",
];

if (monoWasmThreads) {
Expand Down
4 changes: 0 additions & 4 deletions src/mono/wasm/runtime/exports-linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import MonoWasmThreads from "consts:monoWasmThreads";
import WasmEnableLegacyJsInterop from "consts:WasmEnableLegacyJsInterop";
import { mono_wasm_debugger_log, mono_wasm_add_dbg_command_received, mono_wasm_set_entrypoint_breakpoint, mono_wasm_fire_debugger_agent_message_with_data, mono_wasm_fire_debugger_agent_message_with_data_to_pause } from "./debug";
import { mono_wasm_release_cs_owned_object } from "./gc-handles";
import { mono_wasm_load_icu_data } from "./icu";
import { mono_wasm_bind_cs_function } from "./invoke-cs";
import { mono_wasm_bind_js_function, mono_wasm_invoke_bound_function, mono_wasm_invoke_import } from "./invoke-js";
import { mono_interp_tier_prepare_jiterpreter } from "./jiterpreter";
Expand Down Expand Up @@ -95,9 +94,6 @@ export function export_linker(): any {
mono_wasm_bind_cs_function,
mono_wasm_marshal_promise,

// pal_icushim_static.c
mono_wasm_load_icu_data,

// threading exports, if threading is enabled
...mono_wasm_threads_exports,
// legacy interop exports, if enabled
Expand Down
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export function set_emscripten_entrypoint(
const initialRuntimeHelpers: Partial<RuntimeHelpers> =
{
javaScriptExports: {} as any,
mono_wasm_load_runtime_done: false,
mono_wasm_bindings_is_ready: false,
maxParallelDownloads: 16,
enableDownloadRetry: true,
Expand Down
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/pthreads/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export function setupPreloadChannelToMainThread() {
const mainPort = channel.port2;
workerPort.addEventListener("message", (event) => {
const config = JSON.parse(event.data.config) as MonoConfig;
console.debug("MONO_WASM: applying mono config from main", event.data.config);
onMonoConfigReceived(config);
workerPort.close();
mainPort.close();
Expand Down
63 changes: 26 additions & 37 deletions src/mono/wasm/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ async function preRunAsync(userPreRun: (() => void)[]) {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: preRunAsync");
const mark = startMeasure();
try {
if (MonoWasmThreads) {
await instantiateWasmPThreadWorkerPool();
}
// all user Module.preRun callbacks
userPreRun.map(fn => fn());
endMeasure(mark, MeasuredBlock.preRun);
Expand All @@ -234,12 +231,23 @@ async function onRuntimeInitializedAsync(userOnRuntimeInitialized: () => void) {
beforeOnRuntimeInitialized.promise_control.resolve();
try {
await wait_for_all_assets();
// load runtime

// load runtime and apply environment settings (if necessary)
await mono_wasm_before_user_runtime_initialized();

if (config.runtimeOptions) {
mono_wasm_set_runtime_options(config.runtimeOptions);
if (MonoWasmThreads) {
await instantiateWasmPThreadWorkerPool();
}

bindings_init();
if (!runtimeHelpers.mono_wasm_runtime_is_ready) mono_wasm_runtime_ready();
if (!runtimeHelpers.mono_wasm_symbols_are_ready) readSymbolMapFile("dotnet.js.symbols");

setTimeout(() => {
// when there are free CPU cycles
string_decoder.init_fields();
});

// call user code
try {
userOnRuntimeInitialized();
Expand All @@ -266,6 +274,11 @@ async function postRunAsync(userpostRun: (() => void)[]) {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: postRunAsync");
try {
const mark = startMeasure();

// create /usr/share folder which is SpecialFolder.CommonApplicationData
Module["FS_createPath"]("/", "usr", true, true);
Module["FS_createPath"]("/", "usr/share", true, true);

// all user Module.postRun callbacks
userpostRun.map(fn => fn());
endMeasure(mark, MeasuredBlock.postRun);
Expand Down Expand Up @@ -339,27 +352,6 @@ async function mono_wasm_pre_init_full(): Promise<void> {
Module.removeRunDependency("mono_wasm_pre_init_full");
}

async function mono_wasm_before_user_runtime_initialized(): Promise<void> {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_before_user_runtime_initialized");

try {
await _apply_configuration_from_args();
mono_wasm_globalization_init();

if (!runtimeHelpers.mono_wasm_load_runtime_done) mono_wasm_load_runtime("unused", config.debugLevel);
if (!runtimeHelpers.mono_wasm_runtime_is_ready) mono_wasm_runtime_ready();
if (!runtimeHelpers.mono_wasm_symbols_are_ready) readSymbolMapFile("dotnet.js.symbols");

setTimeout(() => {
// when there are free CPU cycles
string_decoder.init_fields();
});
} catch (err: any) {
_print_error("MONO_WASM: Error in mono_wasm_before_user_runtime_initialized", err);
throw err;
}
}

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 Down Expand Up @@ -451,14 +443,15 @@ async function instantiate_wasm_module(
): Promise<void> {
// this is called so early that even Module exports like addRunDependency don't exist yet
try {
replace_linker_placeholders(imports, export_linker());
await mono_wasm_load_config(Module.configSrc);
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: instantiate_wasm_module");
const assetToLoad = resolve_asset_path("dotnetwasm");
// FIXME: this would not apply re-try (on connection reset during download) for dotnet.wasm because we could not download the buffer before we pass it to instantiate_wasm_asset
await start_asset_download(assetToLoad);
await beforePreInit.promise;
Module.addRunDependency("instantiate_wasm_module");

replace_linker_placeholders(imports, export_linker());
await instantiate_wasm_asset(assetToLoad, imports, successCallback);
assetToLoad.pendingDownloadInternal = null as any; // GC
assetToLoad.pendingDownload = null as any; // GC
Expand All @@ -474,10 +467,8 @@ async function instantiate_wasm_module(
Module.removeRunDependency("instantiate_wasm_module");
}

async function _apply_configuration_from_args() {
// create /usr/share folder which is SpecialFolder.CommonApplicationData
Module["FS_createPath"]("/", "usr", true, true);
Module["FS_createPath"]("/", "usr/share", true, true);
async function mono_wasm_before_user_runtime_initialized() {
mono_wasm_globalization_init();

for (const k in config.environmentVariables) {
const v = config.environmentVariables![k];
Expand All @@ -500,14 +491,13 @@ async function _apply_configuration_from_args() {
if (MonoWasmThreads) {
await mono_wasm_init_diagnostics();
}

mono_wasm_load_runtime("unused", config.debugLevel);

}

export function mono_wasm_load_runtime(unused?: string, debugLevel?: number): void {
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: mono_wasm_load_runtime");
if (runtimeHelpers.mono_wasm_load_runtime_done) {
return;
}
runtimeHelpers.mono_wasm_load_runtime_done = true;
try {
const mark = startMeasure();
if (debugLevel == undefined) {
Expand All @@ -519,7 +509,6 @@ export function mono_wasm_load_runtime(unused?: string, debugLevel?: number): vo
cwraps.mono_wasm_load_runtime(unused || "unused", debugLevel);
endMeasure(mark, MeasuredBlock.loadRuntime);

if (!runtimeHelpers.mono_wasm_bindings_is_ready) bindings_init();
} catch (err: any) {
_print_error("MONO_WASM: mono_wasm_load_runtime () failed", err);

Expand Down
1 change: 0 additions & 1 deletion src/mono/wasm/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ export type RuntimeHelpers = {
runtime_interop_exports_class: MonoClass;

_i52_error_scratch_buffer: Int32Ptr;
mono_wasm_load_runtime_done: boolean;
mono_wasm_runtime_is_ready: boolean;
mono_wasm_bindings_is_ready: boolean;
mono_wasm_symbols_are_ready: boolean;
Expand Down

0 comments on commit a923c64

Please sign in to comment.