Skip to content

[draft][browser] enable closure compiler for dotnet.js #80178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions src/mono/wasm/build/WasmApp.Native.targets
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
<WasmNativeStrip Condition="'$(WasmNativeStrip)' == '' and '$(Configuration)' == 'Debug' and '$(WasmBuildingForNestedPublish)' != 'true'">false</WasmNativeStrip>
<WasmNativeStrip Condition="'$(WasmNativeStrip)' == ''">true</WasmNativeStrip>
<WasmNativeDebugSymbols Condition="'$(WasmNativeDebugSymbols)' == ''">true</WasmNativeDebugSymbols>
<WasmMinifyJsLevel Condition="'$(WasmMinifyJsLevel)' == '' and '$(Configuration)' == 'Release'">SIMPLE_OPTIMIZATIONS</WasmMinifyJsLevel>
<WasmMinifyJsLevel Condition="'$(WasmMinifyJsLevel)' == ''">NONE</WasmMinifyJsLevel>
<WasmLinkIcalls Condition="'$(WasmLinkIcalls)' == ''">$(WasmBuildNative)</WasmLinkIcalls>

<_WasmICallTablePath>$(_WasmIntermediateOutputPath)icall-table.h</_WasmICallTablePath>
Expand Down Expand Up @@ -232,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)" />


<!-- ILLinker should have removed unused imports, so error for Publish -->
<_EmccLDSFlags Include="-s ERROR_ON_UNDEFINED_SYMBOLS=0" Condition="'$(WasmBuildingForNestedPublish)' != 'true'" />
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/build/WasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- $(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
Expand Down
5 changes: 5 additions & 0 deletions src/mono/wasm/runtime/es6/dotnet.es6.extpost.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
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 };
32 changes: 16 additions & 16 deletions src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,33 +23,33 @@ 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 => {
require = someRequire;
});
}
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;
` : ''}
`,
};

Expand Down
2 changes: 0 additions & 2 deletions src/mono/wasm/runtime/es6/dotnet.es6.pre.js
Original file line number Diff line number Diff line change
@@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/mono/wasm/runtime/logging.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
4 changes: 0 additions & 4 deletions src/mono/wasm/runtime/run-outer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ export interface DotnetHostBuilder {
run(): Promise<number>
}

// 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[];
Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/types/consts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
6 changes: 5 additions & 1 deletion src/mono/wasm/wasm.proj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<_EmccCompileRspPath>$(NativeBinDir)src\emcc-compile.rsp</_EmccCompileRspPath>
<_EmccLinkRspPath>$(NativeBinDir)src\emcc-link.rsp</_EmccLinkRspPath>
<WasmNativeStrip Condition="'$(ContinuousIntegrationBuild)' == 'true'">false</WasmNativeStrip>
<WasmMinifyJsLevel Condition="'$(WasmMinifyJsLevel)' == '' and '$(Configuration)' == 'Release'">SIMPLE_OPTIMIZATIONS</WasmMinifyJsLevel>
<WasmMinifyJsLevel Condition="'$(WasmMinifyJsLevel)' == ''">NONE</WasmMinifyJsLevel>
</PropertyGroup>

<Target Name="CheckEnv">
Expand Down Expand Up @@ -254,10 +256,12 @@
<PInvokeTableFile>$(ArtifactsObjDir)wasm/pinvoke-table.h</PInvokeTableFile>
<InterpToNativeTableFile>$(ArtifactsObjDir)wasm/wasm_m2n_invoke.g.h</InterpToNativeTableFile>
<CMakeConfigurationEmccFlags Condition="'$(Configuration)' == 'Debug'">-g -Os -s -DDEBUG=1 -DENABLE_AOT_PROFILER=1 -DENABLE_BROWSER_PROFILER=1</CMakeConfigurationEmccFlags>
<CMakeConfigurationEmccFlags Condition="'$(Configuration)' == 'Release'">-Oz -DENABLE_BROWSER_PROFILER=1</CMakeConfigurationEmccFlags>
<CMakeConfigurationEmccFlags Condition="'$(Configuration)' == 'Release'">-Oz -DENABLE_BROWSER_PROFILER=1</CMakeConfigurationEmccFlags>

<CMakeConfigurationLinkFlags Condition="'$(Configuration)' == 'Debug'" >$(CMakeConfigurationEmccFlags)</CMakeConfigurationLinkFlags>
<CMakeConfigurationLinkFlags Condition="'$(Configuration)' == 'Release'">-O2</CMakeConfigurationLinkFlags>

<CMakeConfigurationLinkFlags Condition="'$(WasmMinifyJsLevel)' != 'NONE'">$(CMakeConfigurationLinkFlags) --closure 1 --closure-args=--compilation_level=$(WasmMinifyJsLevel)</CMakeConfigurationLinkFlags>
<CMakeConfigurationLinkFlags>$(CMakeConfigurationLinkFlags) -s EXPORT_ES6=1</CMakeConfigurationLinkFlags>
<CMakeConfigurationLinkFlags Condition="'$(MonoWasmThreads)' == 'true'">$(CMakeConfigurationLinkFlags) -Wno-pthreads-mem-growth</CMakeConfigurationLinkFlags>
<CMakeConfigurationLinkFlags >$(CMakeConfigurationLinkFlags) --emit-symbol-map</CMakeConfigurationLinkFlags>
Expand Down
15 changes: 8 additions & 7 deletions src/native/libs/System.Native/pal_random.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}
}
},
Expand Down