From b6564c98096a71688d138df682226334553bfdac Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Wed, 4 Jan 2023 15:31:01 +0100 Subject: [PATCH 1/7] wip --- .../Directory.Build.props | 1 + src/mono/wasm/build/WasmApp.Native.targets | 11 +++- src/mono/wasm/runtime/CMakeLists.txt | 4 +- .../wasm/runtime/es6/dotnet.es6.externs.js | 54 +++++++++++++++++++ .../wasm/runtime/es6/dotnet.es6.extpost.js | 7 +++ src/mono/wasm/runtime/es6/dotnet.es6.lib.js | 32 +++++------ src/mono/wasm/runtime/es6/dotnet.es6.pre.js | 2 - src/mono/wasm/runtime/logging.ts | 4 +- src/mono/wasm/runtime/run-outer.ts | 4 -- src/mono/wasm/runtime/types/consts.d.ts | 3 ++ src/mono/wasm/wasm.proj | 3 +- 11 files changed, 96 insertions(+), 29 deletions(-) create mode 100644 src/mono/wasm/runtime/es6/dotnet.es6.externs.js diff --git a/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props b/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props index d260fa689f48b1..8ca44c4d83ec08 100644 --- a/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props +++ b/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props @@ -239,6 +239,7 @@ + diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index fc637efdd64fa2..02e0adb70d04ee 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -254,9 +254,11 @@ + <_WasmExtraJSFile Include="$(_WasmRuntimePackSrcDir)\*.%(JSFileType.Identity)" Kind="%(JSFileType.Kind)" /> <_WasmExtraJSFile Include="$(_WasmRuntimePackSrcDir)\es6\*.%(JSFileType.Identity)" Kind="%(JSFileType.Kind)" /> + <_WasmExtraExtFile Include="$(_WasmRuntimePackSrcDir)/es6/*.%(ExtFileType.Identity)" Kind="%(ExtFileType.Kind)" /> <_WasmNativeFileForLinking Include="@(NativeFileReference)" /> @@ -401,6 +403,9 @@ <_EmccLinkStepArgs Include="--%(_WasmExtraJSFile.Kind) "%(_WasmExtraJSFile.Identity)"" Condition="'%(_WasmExtraJSFile.Kind)' != ''" /> <_WasmLinkDependencies Include="@(_WasmExtraJSFile)" /> + <_EmccLinkStepArgs Include="--closure 1" /> + <_EmccLinkStepArgs Include="--%(_WasmExtraExtFile.Kind)"%(_WasmExtraExtFile.Identity)"" Condition="'%(_WasmExtraExtFile.Kind)' != ''" /> + <_EmccLinkStepArgs Include=""%(_WasmNativeFileForLinking.Identity)"" /> <_WasmLinkDependencies Include="@(_WasmNativeFileForLinking)" /> @@ -412,8 +417,10 @@ <_EmccLinkStepArgs Include="$(EmccExtraLDFlags)" /> - - + + <_EmccLinkStepArgs2>@(_EmccLinkStepArgs, ' ') + + diff --git a/src/mono/wasm/runtime/CMakeLists.txt b/src/mono/wasm/runtime/CMakeLists.txt index dab32f84e060c2..eeb2a9a2eab018 100644 --- a/src/mono/wasm/runtime/CMakeLists.txt +++ b/src/mono/wasm/runtime/CMakeLists.txt @@ -30,8 +30,8 @@ target_link_libraries(dotnet ${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a) set_target_properties(dotnet PROPERTIES - LINK_DEPENDS "${NATIVE_BIN_DIR}/src/emcc-default.rsp;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.pre.js;${NATIVE_BIN_DIR}/src/es6/runtime.es6.iffe.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js;${NATIVE_BIN_DIR}/src/pal_random.lib.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.post.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js;" - LINK_FLAGS "@${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-link.rsp ${CONFIGURATION_LINK_FLAGS} --extern-pre-js ${NATIVE_BIN_DIR}/src/es6/runtime.es6.iffe.js --pre-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.pre.js --js-library ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js --js-library ${NATIVE_BIN_DIR}/src/pal_random.lib.js --post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.post.js --extern-post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js " + LINK_DEPENDS "${NATIVE_BIN_DIR}/src/emcc-default.rsp;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.pre.js;${NATIVE_BIN_DIR}/src/es6/runtime.es6.iffe.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js;${NATIVE_BIN_DIR}/src/pal_random.lib.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.post.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.externs.js;" + LINK_FLAGS "@${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-link.rsp ${CONFIGURATION_LINK_FLAGS} --extern-pre-js ${NATIVE_BIN_DIR}/src/es6/runtime.es6.iffe.js --pre-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.pre.js --js-library ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js --js-library ${NATIVE_BIN_DIR}/src/pal_random.lib.js --post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.post.js --extern-post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js --closure-args=--externs=${NATIVE_BIN_DIR}/src/es6/dotnet.es6.externs.js" RUNTIME_OUTPUT_DIRECTORY "${NATIVE_BIN_DIR}") set(ignoreMeWasmOptFlags "${CONFIGURATION_WASM_OPT_FLAGS}") diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.externs.js b/src/mono/wasm/runtime/es6/dotnet.es6.externs.js new file mode 100644 index 00000000000000..0985ccf41bfee5 --- /dev/null +++ b/src/mono/wasm/runtime/es6/dotnet.es6.externs.js @@ -0,0 +1,54 @@ +/** + * @fileoverview https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler + * @externs + */ + + +const MONO = {}, BINDING = {}, INTERNAL = {}, IMPORTS = {}; + +const __dotnet_runtime = {}; + +/** @interface */ +function EarlyImports() { } +EarlyImports.prototype.isGlobal = false; +EarlyImports.prototype.isNode = false; +EarlyImports.prototype.isWorker = false; +EarlyImports.prototype.isShell = false; +EarlyImports.prototype.isPThread = false; +EarlyImports.prototype.quit_ = function () { }; +EarlyImports.prototype.ExitStatus = {}; +EarlyImports.prototype.requirePromise = {}; + + +/** @interface */ +function EarlyExports() { } +EarlyExports.prototype.mono = {}; +EarlyExports.prototype.binding = {}; +EarlyExports.prototype.internal = {}; +EarlyExports.prototype.module = {}; +EarlyExports.prototype.marshaled_imports = {}; + +/** @interface */ +function EarlyReplacements() { } +EarlyReplacements.prototype.fetch = function () { }; +EarlyReplacements.prototype.require = function () { }; +EarlyReplacements.prototype.requirePromise = {}; +EarlyReplacements.prototype.noExitRuntime = false; +EarlyReplacements.prototype.updateGlobalBufferAndViews = function () { }; +EarlyReplacements.prototype.pthreadReplacements = {}; +EarlyReplacements.prototype.scriptDirectory = ""; +EarlyReplacements.prototype.scriptUrl = ""; + + +/** + * @param {EarlyImports} imports + * @param {EarlyExports} exports + * @param {EarlyReplacements} replacements + * @param {Object} callbackAPI: any + */ +const __initializeImportsAndExports = function ( + imports, + exports, + replacements, + callbackAPI) { }; +const __requirePromise = {}; \ No newline at end of file diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.extpost.js b/src/mono/wasm/runtime/es6/dotnet.es6.extpost.js index be0f6311733c3e..892cd4b2522232 100644 --- a/src/mono/wasm/runtime/es6/dotnet.es6.extpost.js +++ b/src/mono/wasm/runtime/es6/dotnet.es6.extpost.js @@ -1,5 +1,12 @@ +const ENVIRONMENT_IS_WEB = typeof window == "object"; +const ENVIRONMENT_IS_NODE = typeof process == "object" && typeof process.versions == "object" && typeof process.versions.node == "string"; const MONO = {}, BINDING = {}, INTERNAL = {}, IMPORTS = {}; +var fetch = fetch || undefined; +var require = require || undefined; +var __dirname = __dirname || ''; __dotnet_runtime.__setEmscriptenEntrypoint(createDotnetRuntime); +const __initializeImportsAndExports = __dotnet_runtime.__initializeImportsAndExports; +const __requirePromise = ENVIRONMENT_IS_NODE ? import(/* webpackIgnore: true */'module').then(mod => mod.createRequire(import.meta.url)) : undefined; const dotnet = __dotnet_runtime.moduleExports.dotnet; const exit = __dotnet_runtime.moduleExports.exit; export { dotnet, exit, INTERNAL }; diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js index fb184f0533ff08..756c7b91f48f5a 100644 --- a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js +++ b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js @@ -5,16 +5,16 @@ "use strict"; #if USE_PTHREADS -const usePThreads = `true`; +const usePThreads = true; const isPThread = `ENVIRONMENT_IS_PTHREAD`; #else -const usePThreads = `false`; +const usePThreads = false; const isPThread = `false`; #endif const DotnetSupportLib = { $DOTNET: {}, - // this line will be placed early on emscripten runtime creation, passing import and export objects into __dotnet_runtime IFFE + // this line will be placed early on emscripten runtime creation, passing import and export objects into __dotnet_runtime IIFE // Emscripten uses require function for nodeJS even in ES6 module. We need https://nodejs.org/api/module.html#modulecreaterequirefilename // We use dynamic import because there is no "module" module in the browser. // This is async init of it, note it would become available only after first tick. @@ -23,21 +23,21 @@ const DotnetSupportLib = { // We also replace implementation of fetch $DOTNET__postset: ` let __dotnet_replacement_PThread = ${usePThreads} ? {} : undefined; -if (${usePThreads}) { - __dotnet_replacement_PThread.loadWasmModuleToWorker = PThread.loadWasmModuleToWorker; - __dotnet_replacement_PThread.threadInitTLS = PThread.threadInitTLS; - __dotnet_replacement_PThread.allocateUnusedWorker = PThread.allocateUnusedWorker; -} +${usePThreads ? ` +__dotnet_replacement_PThread.loadWasmModuleToWorker = PThread.loadWasmModuleToWorker; +__dotnet_replacement_PThread.threadInitTLS = PThread.threadInitTLS; +__dotnet_replacement_PThread.allocateUnusedWorker = PThread.allocateUnusedWorker; +` : ''} let __dotnet_replacements = {scriptUrl: import.meta.url, fetch: globalThis.fetch, require, updateGlobalBufferAndViews, pthreadReplacements: __dotnet_replacement_PThread}; if (ENVIRONMENT_IS_NODE) { - __dotnet_replacements.requirePromise = import(/* webpackIgnore: true */'module').then(mod => mod.createRequire(import.meta.url)); + __dotnet_replacements.requirePromise = __requirePromise; } -let __dotnet_exportedAPI = __dotnet_runtime.__initializeImportsAndExports( +let __dotnet_exportedAPI = __initializeImportsAndExports( { isGlobal:false, isNode:ENVIRONMENT_IS_NODE, isWorker:ENVIRONMENT_IS_WORKER, isShell:ENVIRONMENT_IS_SHELL, isWeb:ENVIRONMENT_IS_WEB, isPThread:${isPThread}, quit_, ExitStatus, requirePromise:__dotnet_replacements.requirePromise }, { mono:MONO, binding:BINDING, internal:INTERNAL, module:Module, marshaled_imports: IMPORTS }, __dotnet_replacements, __callbackAPI); updateGlobalBufferAndViews = __dotnet_replacements.updateGlobalBufferAndViews; -var fetch = __dotnet_replacements.fetch; +fetch = __dotnet_replacements.fetch; _scriptDir = __dirname = scriptDirectory = __dotnet_replacements.scriptDirectory; if (ENVIRONMENT_IS_NODE) { __dotnet_replacements.requirePromise.then(someRequire => { @@ -45,11 +45,11 @@ if (ENVIRONMENT_IS_NODE) { }); } var noExitRuntime = __dotnet_replacements.noExitRuntime; -if (${usePThreads}) { - PThread.loadWasmModuleToWorker = __dotnet_replacements.pthreadReplacements.loadWasmModuleToWorker; - PThread.threadInitTLS = __dotnet_replacements.pthreadReplacements.threadInitTLS; - PThread.allocateUnusedWorker = __dotnet_replacements.pthreadReplacements.allocateUnusedWorker; -} +${usePThreads ? ` +PThread.loadWasmModuleToWorker = __dotnet_replacements.pthreadReplacements.loadWasmModuleToWorker; +PThread.threadInitTLS = __dotnet_replacements.pthreadReplacements.threadInitTLS; +PThread.allocateUnusedWorker = __dotnet_replacements.pthreadReplacements.allocateUnusedWorker; +` : ''} `, }; diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.pre.js b/src/mono/wasm/runtime/es6/dotnet.es6.pre.js index c7c64de2b0e7c9..67102e3458d41f 100644 --- a/src/mono/wasm/runtime/es6/dotnet.es6.pre.js +++ b/src/mono/wasm/runtime/es6/dotnet.es6.pre.js @@ -1,5 +1,3 @@ -var require = require || undefined; -var __dirname = __dirname || ''; var __callbackAPI = { MONO, BINDING, INTERNAL, IMPORTS }; if (typeof createDotnetRuntime === "function") { __callbackAPI.Module = Module = { ready: Module.ready }; diff --git a/src/mono/wasm/runtime/logging.ts b/src/mono/wasm/runtime/logging.ts index cdb2d00dba723d..bb06aaa68beca2 100644 --- a/src/mono/wasm/runtime/logging.ts +++ b/src/mono/wasm/runtime/logging.ts @@ -1,5 +1,5 @@ -//! Licensed to the .NET Foundation under one or more agreements. -//! The .NET Foundation licenses this file to you under the MIT license. +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. import BuildConfiguration from "consts:configuration"; import { INTERNAL, Module, runtimeHelpers } from "./imports"; diff --git a/src/mono/wasm/runtime/run-outer.ts b/src/mono/wasm/runtime/run-outer.ts index 6c4115a292eef9..7f6902e2d9712c 100644 --- a/src/mono/wasm/runtime/run-outer.ts +++ b/src/mono/wasm/runtime/run-outer.ts @@ -22,10 +22,6 @@ export interface DotnetHostBuilder { run(): Promise } -// these constants duplicate detection inside emscripten internals, but happen earlier -const ENVIRONMENT_IS_WEB = typeof window == "object"; -const ENVIRONMENT_IS_NODE = typeof process == "object" && typeof process.versions == "object" && typeof process.versions.node == "string"; - class HostBuilder implements DotnetHostBuilder { private instance?: RuntimeAPI; private applicationArguments?: string[]; diff --git a/src/mono/wasm/runtime/types/consts.d.ts b/src/mono/wasm/runtime/types/consts.d.ts index ff10feaf3a6f60..8d32d52110133e 100644 --- a/src/mono/wasm/runtime/types/consts.d.ts +++ b/src/mono/wasm/runtime/types/consts.d.ts @@ -17,3 +17,6 @@ declare module "consts:monoDiagnosticsMock" { const constant: boolean; export default constant; } + +declare const ENVIRONMENT_IS_NODE: boolean; +declare const ENVIRONMENT_IS_WEB: boolean; \ No newline at end of file diff --git a/src/mono/wasm/wasm.proj b/src/mono/wasm/wasm.proj index 5069285979c98a..1f4c93dbf52785 100644 --- a/src/mono/wasm/wasm.proj +++ b/src/mono/wasm/wasm.proj @@ -257,7 +257,7 @@ -Oz -DENABLE_BROWSER_PROFILER=1 $(CMakeConfigurationEmccFlags) - -O2 + -O2 --closure 1 $(CMakeConfigurationLinkFlags) -s EXPORT_ES6=1 $(CMakeConfigurationLinkFlags) -Wno-pthreads-mem-growth $(CMakeConfigurationLinkFlags) --emit-symbol-map @@ -306,6 +306,7 @@ From 400f4c4c1a0451c72466ab1e9195d46af578b704 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Wed, 4 Jan 2023 18:25:40 +0100 Subject: [PATCH 2/7] fix --- .../wasm/runtime/es6/dotnet.es6.externs.js | 34 ++++++++++++++++++- src/mono/wasm/runtime/exports.ts | 2 +- .../libs/System.Native/pal_random.lib.js | 15 ++++---- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.externs.js b/src/mono/wasm/runtime/es6/dotnet.es6.externs.js index 0985ccf41bfee5..1f96c4a82ebab1 100644 --- a/src/mono/wasm/runtime/es6/dotnet.es6.externs.js +++ b/src/mono/wasm/runtime/es6/dotnet.es6.externs.js @@ -51,4 +51,36 @@ const __initializeImportsAndExports = function ( exports, replacements, callbackAPI) { }; -const __requirePromise = {}; \ No newline at end of file +const __requirePromise = {}; + + +/** @interface */ +function t_FS() { } +t_FS.prototype.isDir = function () { }; + +/** + * @type {t_FS} + * @suppress {duplicate} + */ +var FS = {} + +/** @interface */ +function CreateDotnetRuntime() { } +CreateDotnetRuntime.prototype.locateFile = function () { }; +CreateDotnetRuntime.prototype.__locateFile = function () { }; +CreateDotnetRuntime.prototype.ready = {}; +CreateDotnetRuntime.prototype.FS = FS; +CreateDotnetRuntime.prototype.HEAPU8 = {}; +CreateDotnetRuntime.prototype.HEAP8 = {}; +CreateDotnetRuntime.prototype.HEAPU16 = {}; +CreateDotnetRuntime.prototype.HEAP16 = {}; +CreateDotnetRuntime.prototype.HEAPU32 = {}; +CreateDotnetRuntime.prototype.HEAP32 = {}; +CreateDotnetRuntime.prototype.HEAPF32 = {}; +CreateDotnetRuntime.prototype.HEAPF64 = {}; + +/** + * @type {CreateDotnetRuntime} + * @suppress {duplicate} + */ +var createDotnetRuntime = {} diff --git a/src/mono/wasm/runtime/exports.ts b/src/mono/wasm/runtime/exports.ts index 1950eb7ae692b7..0b440c05b8c12d 100644 --- a/src/mono/wasm/runtime/exports.ts +++ b/src/mono/wasm/runtime/exports.ts @@ -37,7 +37,7 @@ function initializeImportsAndExports( const module = exports.module as DotnetModule; const globalThisAny = globalThis as any; - // we want to have same instance of MONO, BINDING and Module in dotnet iffe + // we want to have same instance of MONO, BINDING and Module in dotnet iife set_imports_exports(imports, exports); set_legacy_exports(exports); init_polyfills(replacements); diff --git a/src/native/libs/System.Native/pal_random.lib.js b/src/native/libs/System.Native/pal_random.lib.js index bcaba4d0c31dfc..66cd390cccf268 100644 --- a/src/native/libs/System.Native/pal_random.lib.js +++ b/src/native/libs/System.Native/pal_random.lib.js @@ -3,11 +3,12 @@ const DotNetEntropyLib = { $DOTNETENTROPY: { - // batchedQuotaMax is the max number of bytes as specified by the api spec. - // If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm. - // https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues - batchedQuotaMax: 65536, getBatchedRandomValues: function (buffer, bufferLength) { + // batchedQuotaMax is the max number of bytes as specified by the api spec. + // If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm. + // https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues + const batchedQuotaMax = 65536; + // Chrome doesn't want SharedArrayBuffer to be passed to crypto APIs const needTempBuf = typeof SharedArrayBuffer !== 'undefined' && Module.HEAPU8.buffer instanceof SharedArrayBuffer; // if we need a temporary buffer, make one that is big enough and write into it from the beginning @@ -16,14 +17,14 @@ const DotNetEntropyLib = { const offset = needTempBuf ? 0 : buffer; // for modern web browsers // map the work array to the memory buffer passed with the length - for (let i = 0; i < bufferLength; i += this.batchedQuotaMax) { - const view = new Uint8Array(buf, offset + i, Math.min(bufferLength - i, this.batchedQuotaMax)); + for (let i = 0; i < bufferLength; i += batchedQuotaMax) { + const view = new Uint8Array(buf, offset + i, Math.min(bufferLength - i, batchedQuotaMax)); crypto.getRandomValues(view) } if (needTempBuf) { // copy data out of the temporary buffer into the wasm instance memory const heapView = new Uint8Array(Module.HEAPU8.buffer, buffer, bufferLength); - heapView.set(new Uint8Array (buf)); + heapView.set(new Uint8Array(buf)); } } }, From b3ee6db69ec0327a1dcab686532d577c4294d91c Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Thu, 5 Jan 2023 19:42:42 +0100 Subject: [PATCH 3/7] fix --- src/mono/wasm/build/WasmApp.Native.targets | 6 ++++-- src/mono/wasm/build/WasmApp.targets | 1 + src/mono/wasm/wasm.proj | 7 +++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 02e0adb70d04ee..a811d3bc23257e 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -156,6 +156,8 @@ false true true + true + false $(WasmBuildNative) <_WasmICallTablePath>$(_WasmIntermediateOutputPath)icall-table.h @@ -403,8 +405,8 @@ <_EmccLinkStepArgs Include="--%(_WasmExtraJSFile.Kind) "%(_WasmExtraJSFile.Identity)"" Condition="'%(_WasmExtraJSFile.Kind)' != ''" /> <_WasmLinkDependencies Include="@(_WasmExtraJSFile)" /> - <_EmccLinkStepArgs Include="--closure 1" /> - <_EmccLinkStepArgs Include="--%(_WasmExtraExtFile.Kind)"%(_WasmExtraExtFile.Identity)"" Condition="'%(_WasmExtraExtFile.Kind)' != ''" /> + <_EmccLinkStepArgs Condition="'$(WasmAggressivelyMinifyJs)' == 'true'" Include="--closure 1" /> + <_EmccLinkStepArgs Include="--%(_WasmExtraExtFile.Kind)"%(_WasmExtraExtFile.Identity)"" Condition="'$(WasmAggressivelyMinifyJs)' == 'true' and '%(_WasmExtraExtFile.Kind)' != ''" /> <_EmccLinkStepArgs Include=""%(_WasmNativeFileForLinking.Identity)"" /> <_WasmLinkDependencies Include="@(_WasmNativeFileForLinking)" /> diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index aa9832ada35215..99a83ef71ed381 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -24,6 +24,7 @@ - $(WasmNativeDebugSymbols) - Build with native debug symbols, useful only with `$(RunAOTCompilation)`, or `$(WasmBuildNative)` Defaults to true. - $(WasmEmitSymbolMap) - Generates a `dotnet.js.symbols` file with a map of wasm function number to name. + - $(WasmAggressivelyMinifyJs) - Use closure compiler to aggressively minify dotnet.js. - $(WasmDedup) - Whenever to dedup generic instances when using AOT. Defaults to true. - $(WasmProfilers) - Profilers to use diff --git a/src/mono/wasm/wasm.proj b/src/mono/wasm/wasm.proj index 1f4c93dbf52785..68db08f5f5409e 100644 --- a/src/mono/wasm/wasm.proj +++ b/src/mono/wasm/wasm.proj @@ -33,6 +33,7 @@ <_EmccCompileRspPath>$(NativeBinDir)src\emcc-compile.rsp <_EmccLinkRspPath>$(NativeBinDir)src\emcc-link.rsp false + true @@ -254,10 +255,12 @@ $(ArtifactsObjDir)wasm/pinvoke-table.h $(ArtifactsObjDir)wasm/wasm_m2n_invoke.g.h -g -Os -s -DDEBUG=1 -DENABLE_AOT_PROFILER=1 -DENABLE_BROWSER_PROFILER=1 - -Oz -DENABLE_BROWSER_PROFILER=1 + -Oz -DENABLE_BROWSER_PROFILER=1 $(CMakeConfigurationEmccFlags) - -O2 --closure 1 + -O2 + + $(CMakeConfigurationLinkFlags) --closure 1 $(CMakeConfigurationLinkFlags) -s EXPORT_ES6=1 $(CMakeConfigurationLinkFlags) -Wno-pthreads-mem-growth $(CMakeConfigurationLinkFlags) --emit-symbol-map From f64eec912d8609d7d0ca1419f8f9968938630a5a Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Thu, 5 Jan 2023 20:03:58 +0100 Subject: [PATCH 4/7] fix --- src/mono/wasm/build/WasmApp.Native.targets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index a811d3bc23257e..b03fdcae34f932 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -156,7 +156,7 @@ false true true - true + true false $(WasmBuildNative) From e2fc9d0b9d802ec2877c15dc55f1b46b20d4e64b Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Thu, 5 Jan 2023 20:15:27 +0100 Subject: [PATCH 5/7] simple --- src/mono/wasm/build/WasmApp.Native.targets | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index b03fdcae34f932..50c6259b9e41fa 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -406,6 +406,9 @@ <_WasmLinkDependencies Include="@(_WasmExtraJSFile)" /> <_EmccLinkStepArgs Condition="'$(WasmAggressivelyMinifyJs)' == 'true'" Include="--closure 1" /> + <_EmccLinkStepArgs Include="--%(_WasmExtraExtFile.Kind)"%(_WasmExtraExtFile.Identity)"" Condition="'$(WasmAggressivelyMinifyJs)' == 'true' and '%(_WasmExtraExtFile.Kind)' != ''" /> <_EmccLinkStepArgs Include=""%(_WasmNativeFileForLinking.Identity)"" /> From b4f07599aa87cd3f1a6699be41016a723cf7ffd2 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Thu, 19 Jan 2023 15:51:20 +0100 Subject: [PATCH 6/7] WasmMinifyJsLevel SIMPLE_OPTIMIZATIONS and NONE --- .../Directory.Build.props | 1 - src/mono/wasm/build/WasmApp.Native.targets | 19 ++-- src/mono/wasm/build/WasmApp.targets | 2 +- src/mono/wasm/runtime/CMakeLists.txt | 4 +- .../wasm/runtime/es6/dotnet.es6.externs.js | 86 ------------------- .../wasm/runtime/es6/dotnet.es6.extpost.js | 4 +- src/mono/wasm/wasm.proj | 6 +- 7 files changed, 13 insertions(+), 109 deletions(-) delete mode 100644 src/mono/wasm/runtime/es6/dotnet.es6.externs.js diff --git a/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props b/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props index 6cadc81feb038c..ec204db5d1df49 100644 --- a/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props +++ b/src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props @@ -241,7 +241,6 @@ - diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 50ce85525a2aa8..fb386365430220 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -156,8 +156,8 @@ false true true - true - false + SIMPLE_OPTIMIZATIONS + NONE $(WasmBuildNative) <_WasmICallTablePath>$(_WasmIntermediateOutputPath)icall-table.h @@ -256,11 +256,9 @@ - <_WasmExtraJSFile Include="$(_WasmRuntimePackSrcDir)\*.%(JSFileType.Identity)" Kind="%(JSFileType.Kind)" /> <_WasmExtraJSFile Include="$(_WasmRuntimePackSrcDir)\es6\*.%(JSFileType.Identity)" Kind="%(JSFileType.Kind)" /> - <_WasmExtraExtFile Include="$(_WasmRuntimePackSrcDir)/es6/*.%(ExtFileType.Identity)" Kind="%(ExtFileType.Kind)" /> <_WasmNativeFileForLinking Include="@(NativeFileReference)" /> @@ -405,11 +403,8 @@ <_EmccLinkStepArgs Include="--%(_WasmExtraJSFile.Kind) "%(_WasmExtraJSFile.Identity)"" Condition="'%(_WasmExtraJSFile.Kind)' != ''" /> <_WasmLinkDependencies Include="@(_WasmExtraJSFile)" /> - <_EmccLinkStepArgs Condition="'$(WasmAggressivelyMinifyJs)' == 'true'" Include="--closure 1" /> - - <_EmccLinkStepArgs Include="--%(_WasmExtraExtFile.Kind)"%(_WasmExtraExtFile.Identity)"" Condition="'$(WasmAggressivelyMinifyJs)' == 'true' and '%(_WasmExtraExtFile.Kind)' != ''" /> + <_EmccLinkStepArgs Condition="'$(WasmMinifyJsLevel)' != 'NONE'" Include="--closure 1" /> + <_EmccLinkStepArgs Condition="'$(WasmMinifyJsLevel)' != 'NONE'" Include="--closure-args=--compilation_level=$(WasmMinifyJsLevel)" /> <_EmccLinkStepArgs Include=""%(_WasmNativeFileForLinking.Identity)"" /> <_WasmLinkDependencies Include="@(_WasmNativeFileForLinking)" /> @@ -422,10 +417,8 @@ <_EmccLinkStepArgs Include="$(EmccExtraLDFlags)" /> - - <_EmccLinkStepArgs2>@(_EmccLinkStepArgs, ' ') - - + + diff --git a/src/mono/wasm/build/WasmApp.targets b/src/mono/wasm/build/WasmApp.targets index 9ab244b00199c0..3d1f652fff3a81 100644 --- a/src/mono/wasm/build/WasmApp.targets +++ b/src/mono/wasm/build/WasmApp.targets @@ -24,7 +24,7 @@ - $(WasmNativeDebugSymbols) - Build with native debug symbols, useful only with `$(RunAOTCompilation)`, or `$(WasmBuildNative)` Defaults to true. - $(WasmEmitSymbolMap) - Generates a `dotnet.js.symbols` file with a map of wasm function number to name. - - $(WasmAggressivelyMinifyJs) - Use closure compiler to aggressively minify dotnet.js. + - $(WasmMinifyJsLevel) - Use closure compiler to minify dotnet.js after linking. Default:`SIMPLE_OPTIMIZATIONS`. Other option is `NONE`. - $(WasmDedup) - Whenever to dedup generic instances when using AOT. Defaults to true. - $(WasmProfilers) - Profilers to use diff --git a/src/mono/wasm/runtime/CMakeLists.txt b/src/mono/wasm/runtime/CMakeLists.txt index eeb2a9a2eab018..dab32f84e060c2 100644 --- a/src/mono/wasm/runtime/CMakeLists.txt +++ b/src/mono/wasm/runtime/CMakeLists.txt @@ -30,8 +30,8 @@ target_link_libraries(dotnet ${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a) set_target_properties(dotnet PROPERTIES - LINK_DEPENDS "${NATIVE_BIN_DIR}/src/emcc-default.rsp;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.pre.js;${NATIVE_BIN_DIR}/src/es6/runtime.es6.iffe.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js;${NATIVE_BIN_DIR}/src/pal_random.lib.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.post.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.externs.js;" - LINK_FLAGS "@${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-link.rsp ${CONFIGURATION_LINK_FLAGS} --extern-pre-js ${NATIVE_BIN_DIR}/src/es6/runtime.es6.iffe.js --pre-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.pre.js --js-library ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js --js-library ${NATIVE_BIN_DIR}/src/pal_random.lib.js --post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.post.js --extern-post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js --closure-args=--externs=${NATIVE_BIN_DIR}/src/es6/dotnet.es6.externs.js" + LINK_DEPENDS "${NATIVE_BIN_DIR}/src/emcc-default.rsp;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.pre.js;${NATIVE_BIN_DIR}/src/es6/runtime.es6.iffe.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js;${NATIVE_BIN_DIR}/src/pal_random.lib.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.post.js;${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js;" + LINK_FLAGS "@${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-link.rsp ${CONFIGURATION_LINK_FLAGS} --extern-pre-js ${NATIVE_BIN_DIR}/src/es6/runtime.es6.iffe.js --pre-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.pre.js --js-library ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.lib.js --js-library ${NATIVE_BIN_DIR}/src/pal_random.lib.js --post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.post.js --extern-post-js ${NATIVE_BIN_DIR}/src/es6/dotnet.es6.extpost.js " RUNTIME_OUTPUT_DIRECTORY "${NATIVE_BIN_DIR}") set(ignoreMeWasmOptFlags "${CONFIGURATION_WASM_OPT_FLAGS}") diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.externs.js b/src/mono/wasm/runtime/es6/dotnet.es6.externs.js deleted file mode 100644 index 1f96c4a82ebab1..00000000000000 --- a/src/mono/wasm/runtime/es6/dotnet.es6.externs.js +++ /dev/null @@ -1,86 +0,0 @@ -/** - * @fileoverview https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler - * @externs - */ - - -const MONO = {}, BINDING = {}, INTERNAL = {}, IMPORTS = {}; - -const __dotnet_runtime = {}; - -/** @interface */ -function EarlyImports() { } -EarlyImports.prototype.isGlobal = false; -EarlyImports.prototype.isNode = false; -EarlyImports.prototype.isWorker = false; -EarlyImports.prototype.isShell = false; -EarlyImports.prototype.isPThread = false; -EarlyImports.prototype.quit_ = function () { }; -EarlyImports.prototype.ExitStatus = {}; -EarlyImports.prototype.requirePromise = {}; - - -/** @interface */ -function EarlyExports() { } -EarlyExports.prototype.mono = {}; -EarlyExports.prototype.binding = {}; -EarlyExports.prototype.internal = {}; -EarlyExports.prototype.module = {}; -EarlyExports.prototype.marshaled_imports = {}; - -/** @interface */ -function EarlyReplacements() { } -EarlyReplacements.prototype.fetch = function () { }; -EarlyReplacements.prototype.require = function () { }; -EarlyReplacements.prototype.requirePromise = {}; -EarlyReplacements.prototype.noExitRuntime = false; -EarlyReplacements.prototype.updateGlobalBufferAndViews = function () { }; -EarlyReplacements.prototype.pthreadReplacements = {}; -EarlyReplacements.prototype.scriptDirectory = ""; -EarlyReplacements.prototype.scriptUrl = ""; - - -/** - * @param {EarlyImports} imports - * @param {EarlyExports} exports - * @param {EarlyReplacements} replacements - * @param {Object} callbackAPI: any - */ -const __initializeImportsAndExports = function ( - imports, - exports, - replacements, - callbackAPI) { }; -const __requirePromise = {}; - - -/** @interface */ -function t_FS() { } -t_FS.prototype.isDir = function () { }; - -/** - * @type {t_FS} - * @suppress {duplicate} - */ -var FS = {} - -/** @interface */ -function CreateDotnetRuntime() { } -CreateDotnetRuntime.prototype.locateFile = function () { }; -CreateDotnetRuntime.prototype.__locateFile = function () { }; -CreateDotnetRuntime.prototype.ready = {}; -CreateDotnetRuntime.prototype.FS = FS; -CreateDotnetRuntime.prototype.HEAPU8 = {}; -CreateDotnetRuntime.prototype.HEAP8 = {}; -CreateDotnetRuntime.prototype.HEAPU16 = {}; -CreateDotnetRuntime.prototype.HEAP16 = {}; -CreateDotnetRuntime.prototype.HEAPU32 = {}; -CreateDotnetRuntime.prototype.HEAP32 = {}; -CreateDotnetRuntime.prototype.HEAPF32 = {}; -CreateDotnetRuntime.prototype.HEAPF64 = {}; - -/** - * @type {CreateDotnetRuntime} - * @suppress {duplicate} - */ -var createDotnetRuntime = {} diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.extpost.js b/src/mono/wasm/runtime/es6/dotnet.es6.extpost.js index 892cd4b2522232..f852ad05419b2c 100644 --- a/src/mono/wasm/runtime/es6/dotnet.es6.extpost.js +++ b/src/mono/wasm/runtime/es6/dotnet.es6.extpost.js @@ -1,9 +1,7 @@ const ENVIRONMENT_IS_WEB = typeof window == "object"; const ENVIRONMENT_IS_NODE = typeof process == "object" && typeof process.versions == "object" && typeof process.versions.node == "string"; const MONO = {}, BINDING = {}, INTERNAL = {}, IMPORTS = {}; -var fetch = fetch || undefined; -var require = require || undefined; -var __dirname = __dirname || ''; +var fetch = fetch || undefined; var require = require || undefined; var __dirname = __dirname || ''; __dotnet_runtime.__setEmscriptenEntrypoint(createDotnetRuntime); const __initializeImportsAndExports = __dotnet_runtime.__initializeImportsAndExports; const __requirePromise = ENVIRONMENT_IS_NODE ? import(/* webpackIgnore: true */'module').then(mod => mod.createRequire(import.meta.url)) : undefined; diff --git a/src/mono/wasm/wasm.proj b/src/mono/wasm/wasm.proj index 67f41164d06bf3..28731033af919f 100644 --- a/src/mono/wasm/wasm.proj +++ b/src/mono/wasm/wasm.proj @@ -33,7 +33,8 @@ <_EmccCompileRspPath>$(NativeBinDir)src\emcc-compile.rsp <_EmccLinkRspPath>$(NativeBinDir)src\emcc-link.rsp false - true + SIMPLE_OPTIMIZATIONS + NONE @@ -260,7 +261,7 @@ $(CMakeConfigurationEmccFlags) -O2 - $(CMakeConfigurationLinkFlags) --closure 1 + $(CMakeConfigurationLinkFlags) --closure 1 --closure-args=--compilation_level=$(WasmMinifyJsLevel) $(CMakeConfigurationLinkFlags) -s EXPORT_ES6=1 $(CMakeConfigurationLinkFlags) -Wno-pthreads-mem-growth $(CMakeConfigurationLinkFlags) --emit-symbol-map @@ -309,7 +310,6 @@ From f650386b4d7b46209df3f169d2c1bcf4f3a41271 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Thu, 19 Jan 2023 17:03:29 +0100 Subject: [PATCH 7/7] not with -g --- src/mono/wasm/build/WasmApp.Native.targets | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index fb386365430220..d7d3d0f45ecccb 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -234,6 +234,9 @@ <_EmccLDFlags Include="@(_EmccCommonFlags)" /> <_EmccLDSFlags Include="-Wl,--allow-undefined" /> <_EmccLDSFlags Include="-s INITIAL_MEMORY=$(EmccInitialHeapSize)" /> + <_EmccLDSFlags Condition="'$(WasmNativeStrip)' != 'false' and '$(WasmMinifyJsLevel)' != 'NONE'" Include="--closure 1" /> + <_EmccLDSFlags Condition="'$(WasmNativeStrip)' != 'false' and '$(WasmMinifyJsLevel)' != 'NONE'" Include="--closure-args=--compilation_level=$(WasmMinifyJsLevel)" /> + <_EmccLDSFlags Include="-s ERROR_ON_UNDEFINED_SYMBOLS=0" Condition="'$(WasmBuildingForNestedPublish)' != 'true'" /> @@ -403,9 +406,6 @@ <_EmccLinkStepArgs Include="--%(_WasmExtraJSFile.Kind) "%(_WasmExtraJSFile.Identity)"" Condition="'%(_WasmExtraJSFile.Kind)' != ''" /> <_WasmLinkDependencies Include="@(_WasmExtraJSFile)" /> - <_EmccLinkStepArgs Condition="'$(WasmMinifyJsLevel)' != 'NONE'" Include="--closure 1" /> - <_EmccLinkStepArgs Condition="'$(WasmMinifyJsLevel)' != 'NONE'" Include="--closure-args=--compilation_level=$(WasmMinifyJsLevel)" /> - <_EmccLinkStepArgs Include=""%(_WasmNativeFileForLinking.Identity)"" /> <_WasmLinkDependencies Include="@(_WasmNativeFileForLinking)" />