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+ /* eslint-disable @typescript-eslint/triple-slash-reference */
5+ /// <reference path="../types/sidecar.d.ts" />
6+
47import { exceptions , simd } from "wasm-feature-detect" ;
58
69import gitHash from "consts:gitHash" ;
710
8- import type { AssetEntryInternal , GlobalObjects , LoaderHelpers , RuntimeHelpers } from "../types/internal" ;
11+ import type { AssetEntryInternal , DotnetModuleInternal , GlobalObjects , LoaderHelpers , MonoConfigInternal , RuntimeHelpers } from "../types/internal" ;
912import type { MonoConfig , RuntimeAPI } from "../types" ;
1013import { assert_runtime_running , is_exited , is_runtime_running , mono_exit } from "./exit" ;
1114import { assertIsControllablePromise , createPromiseController , getPromiseController } from "./promise-controller" ;
1215import { mono_download_assets , resolve_single_asset_path , retrieve_asset_download } from "./assets" ;
1316import { setup_proxy_console } from "./logging" ;
1417import { invokeLibraryInitializers } from "./libraryInitializers" ;
15- import { hasDebuggingEnabled } from "./config" ;
18+ import { deep_merge_config , hasDebuggingEnabled } from "./config" ;
1619import { logDownloadStatsToConsole , purgeUnusedCacheEntriesAsync } from "./assetsCache" ;
1720
21+ // if we are the first script loaded in the web worker, we are expected to become the sidecar
22+ if ( typeof importScripts === "function" && ! globalThis . onmessage ) {
23+ ( globalThis as any ) . dotnetSidecar = true ;
24+ }
25+
26+ // keep in sync with src\mono\wasm\runtime\globals.ts and src\mono\wasm\test-main.js
1827export const ENVIRONMENT_IS_NODE = typeof process == "object" && typeof process . versions == "object" && typeof process . versions . node == "string" ;
19- export const ENVIRONMENT_IS_WORKER = typeof importScripts == "function" ;
20- export const ENVIRONMENT_IS_WEB = typeof window == "object" || ( ENVIRONMENT_IS_WORKER && ! ENVIRONMENT_IS_NODE ) ;
21- export const ENVIRONMENT_IS_SHELL = ! ENVIRONMENT_IS_WEB && ! ENVIRONMENT_IS_NODE && ! ENVIRONMENT_IS_WORKER ;
28+ export const ENVIRONMENT_IS_WEB_WORKER = typeof importScripts == "function" ;
29+ export const ENVIRONMENT_IS_SIDECAR = ENVIRONMENT_IS_WEB_WORKER && typeof dotnetSidecar !== "undefined" ; // sidecar is emscripten main running in a web worker
30+ export const ENVIRONMENT_IS_WORKER = ENVIRONMENT_IS_WEB_WORKER && ! ENVIRONMENT_IS_SIDECAR ; // we redefine what ENVIRONMENT_IS_WORKER, we replace it in emscripten internals, so that sidecar works
31+ export const ENVIRONMENT_IS_WEB = typeof window == "object" || ( ENVIRONMENT_IS_WEB_WORKER && ! ENVIRONMENT_IS_NODE ) ;
32+ export const ENVIRONMENT_IS_SHELL = ! ENVIRONMENT_IS_WEB && ! ENVIRONMENT_IS_NODE ;
2233
2334export let runtimeHelpers : RuntimeHelpers = { } as any ;
2435export let loaderHelpers : LoaderHelpers = { } as any ;
2536export let exportedRuntimeAPI : RuntimeAPI = { } as any ;
2637export let INTERNAL : any = { } ;
2738export let _loaderModuleLoaded = false ; // please keep it in place also as rollup guard
2839
40+ export const monoConfig : MonoConfigInternal = { } as any ;
41+ export const emscriptenModule : DotnetModuleInternal = {
42+ config : monoConfig
43+ } as any ;
2944export const globalObjectsRoot : GlobalObjects = {
3045 mono : { } ,
3146 binding : { } ,
3247 internal : INTERNAL ,
33- module : { } ,
48+ module : emscriptenModule ,
3449 loaderHelpers,
3550 runtimeHelpers,
3651 api : exportedRuntimeAPI ,
@@ -56,7 +71,7 @@ export function setLoaderGlobals(
5671
5772 Object . assign ( globalObjects . module , {
5873 disableDotnet6Compatibility : true ,
59- config : { environmentVariables : { } }
74+ config : deep_merge_config ( monoConfig , { environmentVariables : { } } ) ,
6075 } ) ;
6176 Object . assign ( runtimeHelpers , {
6277 mono_wasm_bindings_is_ready : false ,
0 commit comments