From: Pavel Savara Date: Tue, 25 Jul 2023 19:04:35 +0000 (+0200) Subject: [browser] detect SIMD and EH support (#89387) X-Git-Tag: accepted/tizen/unified/riscv/20231226.055536~788 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=764f77412ef4ec1a7e5dc521608247487a66cd7e;p=platform%2Fupstream%2Fdotnet%2Fruntime.git [browser] detect SIMD and EH support (#89387) Co-authored-by: Ankit Jain --- diff --git a/eng/testing/performance/performance-setup.sh b/eng/testing/performance/performance-setup.sh index 0314580..457dea6 100755 --- a/eng/testing/performance/performance-setup.sh +++ b/eng/testing/performance/performance-setup.sh @@ -403,7 +403,7 @@ if [[ -n "$wasm_bundle_directory" ]]; then wasm_bundle_directory_path=$payload_directory mv $wasm_bundle_directory/* $wasm_bundle_directory_path find $wasm_bundle_directory_path -type d - wasm_args="--expose_wasm" + wasm_args="--experimental-wasm-eh --expose_wasm" if [ "$javascript_engine" == "v8" ]; then # for es6 module support wasm_args="$wasm_args --module" diff --git a/src/mono/sample/wasm/Directory.Build.targets b/src/mono/sample/wasm/Directory.Build.targets index b61806b..7ee7a50 100644 --- a/src/mono/sample/wasm/Directory.Build.targets +++ b/src/mono/sample/wasm/Directory.Build.targets @@ -55,10 +55,10 @@ - + - + diff --git a/src/mono/sample/wasm/wasm.mk b/src/mono/sample/wasm/wasm.mk index 6984136..95f3eb8 100644 --- a/src/mono/sample/wasm/wasm.mk +++ b/src/mono/sample/wasm/wasm.mk @@ -45,7 +45,7 @@ run-console: cd bin/$(CONFIG)/AppBundle && $(V8_PATH) --stack-trace-limit=1000 --single-threaded --expose_wasm --module $(MAIN_JS) -- $(ARGS) run-console-node: - cd bin/$(CONFIG)/AppBundle && node --stack-trace-limit=1000 --single-threaded --expose_wasm $(MAIN_JS) $(ARGS) + cd bin/$(CONFIG)/AppBundle && node --stack-trace-limit=1000 --single-threaded --experimental-wasm-eh --expose_wasm $(MAIN_JS) $(ARGS) debug-console-node: - cd bin/$(CONFIG)/AppBundle && node --inspect=9222 --stack-trace-limit=1000 --single-threaded --expose_wasm $(MAIN_JS) $(ARGS) \ No newline at end of file + cd bin/$(CONFIG)/AppBundle && node --inspect=9222 --stack-trace-limit=1000 --single-threaded --experimental-wasm-eh --expose_wasm $(MAIN_JS) $(ARGS) \ No newline at end of file diff --git a/src/mono/wasm/build/WasmApp.Native.targets b/src/mono/wasm/build/WasmApp.Native.targets index 3473661..2c306db 100644 --- a/src/mono/wasm/build/WasmApp.Native.targets +++ b/src/mono/wasm/build/WasmApp.Native.targets @@ -275,6 +275,10 @@ + + + + diff --git a/src/mono/wasm/features.md b/src/mono/wasm/features.md new file mode 100644 index 0000000..8e2b303 --- /dev/null +++ b/src/mono/wasm/features.md @@ -0,0 +1,37 @@ +# Browser or JS engine features + +dotnet for wasm can be compiled with various MSBuild flags which enable the use of browser features. If you need to target an older version of the browser, then you may need to disable some of the dotnet features or optimizations. + +For full set of MSBuild properties [see top of](src\mono\wasm\build\WasmApp.targets) +For set of [browser WASM features see](https://webassembly.org/roadmap/) + +# Multi-threading +Is enabled by `true`. +It requires HTTP headers similar to `Cross-Origin-Embedder-Policy:require-corp` and `Cross-Origin-Opener-Policy:same-origin`. +See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements + +# SIMD - Single instruction, multiple data +Is performance optimization enabled by default. It requires recent version of browser. +You can disable it by `falsetrue`. +[See also](https://github.com/WebAssembly/simd/blob/master/proposals/simd/SIMD.md) + +# EH - Exception handling +Is performance optimization enabled by default. It requires recent version of browser. +You can disable it by `falsetrue`. +[See also](https://github.com/WebAssembly/exception-handling/blob/master/proposals/exception-handling/Exceptions.md) + +# BigInt +Is required if the application uses Int64 marshaling in JS interop. +[See also](https://github.com/WebAssembly/JS-BigInt-integration) + +# fetch browser API +Is required if the application uses [HttpClient](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient) +NodeJS needs to install `node-fetch` and `node-abort-controller` npm packages. + +# WebSocket browser API +Is required if the application uses [WebSocketClient](https://learn.microsoft.com/en-us/dotnet/api/system.net.websockets.clientwebsocket) +NodeJS needs to install `ws` npm package. + +# Shell environments - NodeJS & V8 +We pass most of the unit tests with NodeJS v 14 but it's not fully supported target platform. We would like to hear about community use-cases. +We also use v8 engine version 11 or higher to run some of the tests. The engine is lacking many APIs and features. \ No newline at end of file diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js index 8ea0b7d..dad64ed 100644 --- a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js +++ b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js @@ -7,6 +7,8 @@ // because we can't pass custom define symbols to acorn optimizer, we use environment variables to pass other build options const DISABLE_LEGACY_JS_INTEROP = process.env.DISABLE_LEGACY_JS_INTEROP === "1"; +const WASM_ENABLE_SIMD = process.env.WASM_ENABLE_SIMD === "1"; +const WASM_ENABLE_EH = process.env.WASM_ENABLE_EH === "1"; const ENABLE_BROWSER_PROFILER = process.env.ENABLE_BROWSER_PROFILER === "1"; const ENABLE_AOT_PROFILER = process.env.ENABLE_AOT_PROFILER === "1"; @@ -62,6 +64,8 @@ function setup(linkerSetup) { const postset = ` DOTNET.setup({ `+ `linkerDisableLegacyJsInterop: ${DISABLE_LEGACY_JS_INTEROP ? "true" : "false"},` + + `linkerWasmEnableSIMD: ${WASM_ENABLE_SIMD ? "true" : "false"},` + + `linkerWasmEnableEH: ${WASM_ENABLE_EH ? "true" : "false"},` + `linkerEnableAotProfiler: ${ENABLE_AOT_PROFILER ? "true" : "false"}, ` + `linkerEnableBrowserProfiler: ${ENABLE_BROWSER_PROFILER ? "true" : "false"}` + `}); diff --git a/src/mono/wasm/runtime/globals.ts b/src/mono/wasm/runtime/globals.ts index d42ba56..9ca3d5d 100644 --- a/src/mono/wasm/runtime/globals.ts +++ b/src/mono/wasm/runtime/globals.ts @@ -24,6 +24,8 @@ export let runtimeHelpers: RuntimeHelpers = null as any; export let loaderHelpers: LoaderHelpers = null as any; // this is when we link with workload tools. The consts:wasmEnableLegacyJsInterop is when we compile with rollup. export let linkerDisableLegacyJsInterop = false; +export let linkerWasmEnableSIMD = true; +export let linkerWasmEnableEH = true; export let linkerEnableAotProfiler = false; export let linkerEnableBrowserProfiler = false; export let _runtimeModuleLoaded = false; // please keep it in place also as rollup guard @@ -31,6 +33,8 @@ export let _runtimeModuleLoaded = false; // please keep it in place also as roll export function passEmscriptenInternals(internals: EmscriptenInternals): void { ENVIRONMENT_IS_PTHREAD = internals.isPThread; linkerDisableLegacyJsInterop = internals.linkerDisableLegacyJsInterop; + linkerWasmEnableSIMD = internals.linkerWasmEnableSIMD; + linkerWasmEnableEH = internals.linkerWasmEnableEH; linkerEnableAotProfiler = internals.linkerEnableAotProfiler; linkerEnableBrowserProfiler = internals.linkerEnableBrowserProfiler; runtimeHelpers.quit = internals.quit_; diff --git a/src/mono/wasm/runtime/http.ts b/src/mono/wasm/runtime/http.ts index 1657710..1ad6a4f 100644 --- a/src/mono/wasm/runtime/http.ts +++ b/src/mono/wasm/runtime/http.ts @@ -10,8 +10,8 @@ import type { VoidPtr } from "./types/emscripten"; function verifyEnvironment() { if (typeof globalThis.fetch !== "function" || typeof globalThis.AbortController !== "function") { const message = ENVIRONMENT_IS_NODE - ? "Please install `node-fetch` and `node-abort-controller` npm packages to enable HTTP client support." - : "This browser doesn't support fetch API. Please use a modern browser."; + ? "Please install `node-fetch` and `node-abort-controller` npm packages to enable HTTP client support. See also https://aka.ms/dotnet-wasm-features" + : "This browser doesn't support fetch API. Please use a modern browser. See also https://aka.ms/dotnet-wasm-features"; throw new Error(message); } } diff --git a/src/mono/wasm/runtime/loader/globals.ts b/src/mono/wasm/runtime/loader/globals.ts index 7fac0d5..5f4e833 100644 --- a/src/mono/wasm/runtime/loader/globals.ts +++ b/src/mono/wasm/runtime/loader/globals.ts @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +import { exceptions, simd } from "wasm-feature-detect"; + import type { AssetEntryInternal, GlobalObjects, LoaderHelpers, RuntimeHelpers } from "../types/internal"; import type { MonoConfig, RuntimeAPI } from "../types"; import { assert_runtime_running, is_exited, is_runtime_running, mono_exit } from "./exit"; @@ -94,6 +96,10 @@ export function setLoaderGlobals( hasDebuggingEnabled, invokeLibraryInitializers, + // from wasm-feature-detect npm package + exceptions, + simd, + } as Partial); } diff --git a/src/mono/wasm/runtime/loader/polyfills.ts b/src/mono/wasm/runtime/loader/polyfills.ts index f9aae41..bf30a52 100644 --- a/src/mono/wasm/runtime/loader/polyfills.ts +++ b/src/mono/wasm/runtime/loader/polyfills.ts @@ -19,19 +19,25 @@ const URLPolyfill = class URL { }; export function verifyEnvironment() { - mono_assert(ENVIRONMENT_IS_SHELL || typeof globalThis.URL === "function", "This browser/engine doesn't support URL API. Please use a modern version."); - mono_assert(typeof globalThis.BigInt64Array === "function", "This browser/engine doesn't support BigInt64Array API. Please use a modern version."); + mono_assert(ENVIRONMENT_IS_SHELL || typeof globalThis.URL === "function", "This browser/engine doesn't support URL API. Please use a modern version. See also https://aka.ms/dotnet-wasm-features"); + mono_assert(typeof globalThis.BigInt64Array === "function", "This browser/engine doesn't support BigInt64Array API. Please use a modern version. See also https://aka.ms/dotnet-wasm-features"); if (MonoWasmThreads) { - mono_assert(!ENVIRONMENT_IS_SHELL && !ENVIRONMENT_IS_NODE, "This build of dotnet is multi-threaded, it doesn't support shell environments like V8 or NodeJS."); - mono_assert(globalThis.SharedArrayBuffer !== undefined, "SharedArrayBuffer is not enabled on this page. Please use a modern browser and set Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy http headers. See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements"); - mono_assert(typeof globalThis.EventTarget === "function", "This browser/engine doesn't support EventTarget API. Please use a modern version."); + mono_assert(!ENVIRONMENT_IS_SHELL && !ENVIRONMENT_IS_NODE, "This build of dotnet is multi-threaded, it doesn't support shell environments like V8 or NodeJS. See also https://aka.ms/dotnet-wasm-features"); + mono_assert(globalThis.SharedArrayBuffer !== undefined, "SharedArrayBuffer is not enabled on this page. Please use a modern browser and set Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy http headers. See also https://aka.ms/dotnet-wasm-features"); + mono_assert(typeof globalThis.EventTarget === "function", "This browser/engine doesn't support EventTarget API. Please use a modern version. See also https://aka.ms/dotnet-wasm-features"); } - - // TODO detect other (WASM) features that are required for the runtime - // See https://github.com/dotnet/runtime/issues/84574 } export async function detect_features_and_polyfill(module: DotnetModuleInternal): Promise { + if (ENVIRONMENT_IS_NODE) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore: + const process = await import(/* webpackIgnore: true */"process"); + const minNodeVersion = 14; + if (process.versions.node.split(".")[0] < minNodeVersion) { + throw new Error(`NodeJS at '${process.execPath}' has too low version '${process.versions.node}', please use at least ${minNodeVersion}. See also https://aka.ms/dotnet-wasm-features`); + } + } const scriptUrlQuery =/* webpackIgnore: true */import.meta.url; const queryIndex = scriptUrlQuery.indexOf("?"); diff --git a/src/mono/wasm/runtime/loader/run.ts b/src/mono/wasm/runtime/loader/run.ts index a05f2e0..1709c99 100644 --- a/src/mono/wasm/runtime/loader/run.ts +++ b/src/mono/wasm/runtime/loader/run.ts @@ -363,14 +363,6 @@ export class HostBuilder implements DotnetHostBuilder { if (ENVIRONMENT_IS_WEB && (module.config! as MonoConfigInternal).forwardConsoleLogsToWS && typeof globalThis.WebSocket != "undefined") { setup_proxy_console("main", globalThis.console, globalThis.location.origin); } - if (ENVIRONMENT_IS_NODE) { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore: - const process = await import(/* webpackIgnore: true */"process"); - if (process.versions.node.split(".")[0] < 14) { - throw new Error(`NodeJS at '${process.execPath}' has too low version '${process.versions.node}'`); - } - } mono_assert(module, "Null moduleConfig"); mono_assert(module.config, "Null moduleConfig.config"); await createEmscripten(module); @@ -452,7 +444,7 @@ function importModules() { ]; } -function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModuleExportsInternal]) { +async function initializeModules(es6Modules: [RuntimeModuleExportsInternal, NativeModuleExportsInternal]) { const { initializeExports, initializeReplacements, configureEmscriptenStartup, configureWorkerStartup, setRuntimeGlobals, passEmscriptenInternals } = es6Modules[0]; const { default: emscriptenFactory } = es6Modules[1]; setRuntimeGlobals(globalObjectsRoot); @@ -491,7 +483,7 @@ async function createEmscriptenMain(): Promise { // TODO call mono_download_assets(); here in parallel ? const es6Modules = await Promise.all(promises); - initializeModules(es6Modules as any); + await initializeModules(es6Modules as any); await runtimeHelpers.dotnetReady.promise; @@ -507,7 +499,7 @@ async function createEmscriptenWorker(): Promise { const promises = importModules(); const es6Modules = await Promise.all(promises); - initializeModules(es6Modules as any); + await initializeModules(es6Modules as any); return module; } diff --git a/src/mono/wasm/runtime/package-lock.json b/src/mono/wasm/runtime/package-lock.json index d40ce11..55c8595 100644 --- a/src/mono/wasm/runtime/package-lock.json +++ b/src/mono/wasm/runtime/package-lock.json @@ -248,6 +248,20 @@ "fastq": "^1.6.0" } }, + "@rollup/plugin-node-resolve": { + "version": "15.1.0", + "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/@rollup/plugin-node-resolve/-/plugin-node-resolve-15.1.0.tgz", + "integrity": "sha1-n/zY6MRXCA26ibufy1g6Z3jcdX4=", + "dev": true, + "requires": { + "@rollup/pluginutils": "^5.0.1", + "@types/resolve": "1.20.2", + "deepmerge": "^4.2.2", + "is-builtin-module": "^3.2.1", + "is-module": "^1.0.0", + "resolve": "^1.22.1" + } + }, "@rollup/plugin-terser": { "version": "0.4.3", "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/@rollup/plugin-terser/-/plugin-terser-0.4.3.tgz", @@ -298,6 +312,12 @@ "integrity": "sha1-1w+rpwOdX8pUyDx9urQQUdK29ss=", "dev": true }, + "@types/resolve": { + "version": "1.20.2", + "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/@types/resolve/-/resolve-1.20.2.tgz", + "integrity": "sha1-l9JuAM1KBCO0r2IKvs8+b0QreXU=", + "dev": true + }, "@types/semver": { "version": "7.5.0", "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/@types/semver/-/semver-7.5.0.tgz", @@ -485,6 +505,12 @@ "integrity": "sha1-KxRqb9cugLT1XSVfNe1Zo6mkG9U=", "dev": true }, + "builtin-modules": { + "version": "3.3.0", + "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/builtin-modules/-/builtin-modules-3.3.0.tgz", + "integrity": "sha1-yuYoEriYAellYzbkYiPgMDhr57Y=", + "dev": true + }, "callsites": { "version": "3.1.0", "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/callsites/-/callsites-3.1.0.tgz", @@ -554,6 +580,12 @@ "integrity": "sha1-pvLc5hL63S7x9Rm3NVHxfoUZmDE=", "dev": true }, + "deepmerge": { + "version": "4.3.1", + "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/deepmerge/-/deepmerge-4.3.1.tgz", + "integrity": "sha1-RLXyFHzTsA1LVhN2hZZvJv0l3Uo=", + "dev": true + }, "dir-glob": { "version": "3.0.1", "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/dir-glob/-/dir-glob-3.0.1.tgz", @@ -1019,6 +1051,15 @@ "integrity": "sha1-D6LGT5MpF8NDOg3tVTY6rjdBa3w=", "dev": true }, + "is-builtin-module": { + "version": "3.2.1", + "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/is-builtin-module/-/is-builtin-module-3.2.1.tgz", + "integrity": "sha1-8DJxcX2GVM/K8HqwRj+qNXFYEWk=", + "dev": true, + "requires": { + "builtin-modules": "^3.3.0" + } + }, "is-core-module": { "version": "2.12.1", "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/is-core-module/-/is-core-module-2.12.1.tgz", @@ -1148,6 +1189,12 @@ "is-extglob": "^2.1.1" } }, + "is-module": { + "version": "1.0.0", + "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/is-module/-/is-module-1.0.0.tgz", + "integrity": "sha1-Mlj7afeMFNW4FdZkM2tM/7ZEFZE=", + "dev": true + }, "is-number": { "version": "7.0.0", "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/is-number/-/is-number-7.0.0.tgz", @@ -1706,6 +1753,11 @@ "punycode": "^2.1.0" } }, + "wasm-feature-detect": { + "version": "1.5.1", + "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/wasm-feature-detect/-/wasm-feature-detect-1.5.1.tgz", + "integrity": "sha1-DbV6fX+MJrdD3ehThiFa4rE154o=" + }, "which": { "version": "2.0.2", "resolved": "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public-npm/npm/registry/which/-/which-2.0.2.tgz", diff --git a/src/mono/wasm/runtime/package.json b/src/mono/wasm/runtime/package.json index d1e4621..7e8cf75 100644 --- a/src/mono/wasm/runtime/package.json +++ b/src/mono/wasm/runtime/package.json @@ -22,19 +22,23 @@ "author": "Microsoft", "license": "MIT", "devDependencies": { + "@rollup/plugin-node-resolve": "^15.1.0", "@rollup/plugin-terser": "0.4.3", "@rollup/plugin-typescript": "11.1.2", "@rollup/plugin-virtual": "3.0.1", "@typescript-eslint/eslint-plugin": "5.59.1", "@typescript-eslint/parser": "5.59.1", - "magic-string": "0.30.1", "eslint": "8.44.0", "fast-glob": "3.3.0", "git-commit-info": "2.0.2", + "magic-string": "0.30.1", "rollup": "3.26.2", "rollup-plugin-dts": "5.3.0", "terser": "5.19.0", "tslib": "2.6.0", "typescript": "5.1.6" + }, + "dependencies": { + "wasm-feature-detect": "1.5.1" } } diff --git a/src/mono/wasm/runtime/rollup.config.js b/src/mono/wasm/runtime/rollup.config.js index d37ba57..94333d6 100644 --- a/src/mono/wasm/runtime/rollup.config.js +++ b/src/mono/wasm/runtime/rollup.config.js @@ -2,6 +2,7 @@ import { defineConfig } from "rollup"; import typescript from "@rollup/plugin-typescript"; import terser from "@rollup/plugin-terser"; import virtual from "@rollup/plugin-virtual"; +import { nodeResolve } from "@rollup/plugin-node-resolve"; import { readFile, writeFile, mkdir } from "fs/promises"; import * as fs from "fs"; import * as path from "path"; @@ -18,6 +19,8 @@ const isContinuousIntegrationBuild = process.env.ContinuousIntegrationBuild === const productVersion = process.env.ProductVersion || "8.0.0-dev"; const nativeBinDir = process.env.NativeBinDir ? process.env.NativeBinDir.replace(/"/g, "") : "bin"; const monoWasmThreads = process.env.MonoWasmThreads === "true" ? true : false; +const wasmEnableSIMD = process.env.WASM_ENABLE_SIMD === "1" ? true : false; +const wasmEnableExceptionHandling = process.env.WASM_ENABLE_EH === "1" ? true : false; const wasmEnableLegacyJsInterop = process.env.DISABLE_LEGACY_JS_INTEROP !== "1" ? true : false; const monoDiagnosticsMock = process.env.MonoDiagnosticsMock === "true" ? true : false; const terserConfig = { @@ -90,6 +93,8 @@ const envConstants = { productVersion, configuration, monoWasmThreads, + wasmEnableSIMD, + wasmEnableExceptionHandling, monoDiagnosticsMock, gitHash, wasmEnableLegacyJsInterop, @@ -154,7 +159,7 @@ const loaderConfig = { } ], external: externalDependencies, - plugins: [regexReplace(inlineAssert), regexCheck([checkAssert, checkNoRuntime]), ...outputCodePlugins], + plugins: [nodeResolve(), regexReplace(inlineAssert), regexCheck([checkAssert, checkNoRuntime]), ...outputCodePlugins], onwarn: onwarn }; const runtimeConfig = { diff --git a/src/mono/wasm/runtime/startup.ts b/src/mono/wasm/runtime/startup.ts index 46f7ac4..12e8341 100644 --- a/src/mono/wasm/runtime/startup.ts +++ b/src/mono/wasm/runtime/startup.ts @@ -5,7 +5,7 @@ import MonoWasmThreads from "consts:monoWasmThreads"; import WasmEnableLegacyJsInterop from "consts:wasmEnableLegacyJsInterop"; import { DotnetModuleInternal, CharPtrNull } from "./types/internal"; -import { linkerDisableLegacyJsInterop, ENVIRONMENT_IS_PTHREAD, exportedRuntimeAPI, INTERNAL, loaderHelpers, Module, runtimeHelpers, createPromiseController, mono_assert } from "./globals"; +import { linkerDisableLegacyJsInterop, ENVIRONMENT_IS_PTHREAD, exportedRuntimeAPI, INTERNAL, loaderHelpers, Module, runtimeHelpers, createPromiseController, mono_assert, linkerWasmEnableSIMD, linkerWasmEnableEH } from "./globals"; import cwraps, { init_c_exports } from "./cwraps"; import { mono_wasm_raise_debug_event, mono_wasm_runtime_ready } from "./debug"; import { toBase64StringImpl } from "./base64"; @@ -165,7 +165,6 @@ function preInit(userPreInit: (() => void)[]) { (async () => { try { // - init the rest of the polyfills - // - download Module.config from configSrc await mono_wasm_pre_init_essential_async(); // - start download assets like DLLs @@ -364,6 +363,13 @@ async function mono_wasm_pre_init_essential_async(): Promise { mono_log_debug("mono_wasm_pre_init_essential_async"); Module.addRunDependency("mono_wasm_pre_init_essential_async"); + if (linkerWasmEnableSIMD) { + mono_assert(await loaderHelpers.simd(), "This browser/engine doesn't support WASM SIMD. Please use a modern version. See also https://aka.ms/dotnet-wasm-features"); + } + if (linkerWasmEnableEH) { + mono_assert(await loaderHelpers.exceptions(), "This browser/engine doesn't support WASM exception handling. Please use a modern version. See also https://aka.ms/dotnet-wasm-features"); + } + await init_polyfills_async(); if (MonoWasmThreads) { diff --git a/src/mono/wasm/runtime/types/internal.ts b/src/mono/wasm/runtime/types/internal.ts index bc91e97..3b54d6b1 100644 --- a/src/mono/wasm/runtime/types/internal.ts +++ b/src/mono/wasm/runtime/types/internal.ts @@ -150,6 +150,10 @@ export type LoaderHelpers = { isChromium: boolean, isFirefox: boolean + + // from wasm-feature-detect npm package + exceptions: () => Promise, + simd: () => Promise, } export type RuntimeHelpers = { config: MonoConfigInternal; @@ -266,6 +270,8 @@ export function is_nullish(value: T | null | undefined): value is null | unde export type EmscriptenInternals = { isPThread: boolean, linkerDisableLegacyJsInterop: boolean, + linkerWasmEnableSIMD: boolean, + linkerWasmEnableEH: boolean, linkerEnableAotProfiler: boolean, linkerEnableBrowserProfiler: boolean, quit_: Function, diff --git a/src/mono/wasm/runtime/web-socket.ts b/src/mono/wasm/runtime/web-socket.ts index 93c5a9f..b085cfe 100644 --- a/src/mono/wasm/runtime/web-socket.ts +++ b/src/mono/wasm/runtime/web-socket.ts @@ -32,8 +32,8 @@ function verifyEnvironment() { } if (typeof globalThis.WebSocket !== "function") { const message = ENVIRONMENT_IS_NODE - ? "Please install `ws` npm package to enable networking support." - : "This browser doesn't support WebSocket API. Please use a modern browser."; + ? "Please install `ws` npm package to enable networking support. See also https://aka.ms/dotnet-wasm-features" + : "This browser doesn't support WebSocket API. Please use a modern browser. See also https://aka.ms/dotnet-wasm-features"; throw new Error(message); } } diff --git a/src/mono/wasm/wasm.proj b/src/mono/wasm/wasm.proj index c479a8c..14d5021 100644 --- a/src/mono/wasm/wasm.proj +++ b/src/mono/wasm/wasm.proj @@ -25,6 +25,7 @@ $([MSBuild]::NormalizeDirectory('$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)', 'runtimes', 'browser-wasm', 'native', 'lib')) $([MSBuild]::NormalizeDirectory('$(PkgMicrosoft_NETCore_Runtime_ICU_Transport)', 'runtimes', 'browser-wasm-threads', 'native', 'lib')) true + true true false emcc @@ -399,9 +400,15 @@ cmake --build . --config $(Configuration) $(CmakeOptions) call "$(RepositoryEngineeringDir)native\init-vs-env.cmd" && call "$([MSBuild]::NormalizePath('$(EMSDK_PATH)', 'emsdk_env.bat'))" && $(CMakeBuildRuntimeCmd) bash -c 'source $(EMSDK_PATH)/emsdk_env.sh 2>&1 && $(CMakeBuildRuntimeCmd)' - <_DisableLegacyJsInterop Condition="'$(WasmEnableLegacyJsInterop)' == 'false'">1 - <_DisableLegacyJsInterop Condition="'$(WasmEnableLegacyJsInterop)' != 'false'">0 + + <_CmakeEnvironmentVariable Include="DISABLE_LEGACY_JS_INTEROP=1" Condition="'$(WasmEnableLegacyJsInterop)' == 'false'"/> + <_CmakeEnvironmentVariable Include="DISABLE_LEGACY_JS_INTEROP=0" Condition="'$(WasmEnableLegacyJsInterop)' != 'false'"/> + <_CmakeEnvironmentVariable Include="WASM_ENABLE_SIMD=1" Condition="'$(WasmEnableSIMD)' != 'false'" /> + <_CmakeEnvironmentVariable Include="WASM_ENABLE_SIMD=0" Condition="'$(WasmEnableSIMD)' == 'false'" /> + <_CmakeEnvironmentVariable Include="WASM_ENABLE_EH=1" Condition="'$(WasmEnableExceptionHandling)' != 'false'" /> + <_CmakeEnvironmentVariable Include="WASM_ENABLE_EH=0" Condition="'$(WasmEnableExceptionHandling)' == 'false'" /> + + EnvironmentVariables="@(_CmakeEnvironmentVariable)" /> + EnvironmentVariables="@(_CmakeEnvironmentVariable)" /> @@ -516,8 +523,23 @@ + + <_MonoRollupEnvironmentVariable Include="Configuration:$(Configuration)" /> + <_MonoRollupEnvironmentVariable Include="NativeBinDir:$(NativeBinDir)" /> + <_MonoRollupEnvironmentVariable Include="ProductVersion:$(ProductVersion)" /> + <_MonoRollupEnvironmentVariable Include="MonoWasmThreads:$(MonoWasmThreads)" /> + <_MonoRollupEnvironmentVariable Include="WASM_ENABLE_SIMD:1" Condition="'$(WasmEnableSIMD)' != 'false'" /> + <_MonoRollupEnvironmentVariable Include="WASM_ENABLE_SIMD:0" Condition="'$(WasmEnableSIMD)' == 'false'" /> + <_MonoRollupEnvironmentVariable Include="WASM_ENABLE_EH:1" Condition="'$(WasmEnableExceptionHandling)' != 'false'" /> + <_MonoRollupEnvironmentVariable Include="WASM_ENABLE_EH:0" Condition="'$(WasmEnableExceptionHandling)' == 'false'" /> + <_MonoRollupEnvironmentVariable Include="DISABLE_LEGACY_JS_INTEROP:1" Condition="'$(WasmEnableLegacyJsInterop)' == 'false'" /> + <_MonoRollupEnvironmentVariable Include="DISABLE_LEGACY_JS_INTEROP:0" Condition="'$(WasmEnableLegacyJsInterop)' != 'false'" /> + <_MonoRollupEnvironmentVariable Include="MonoDiagnosticsMock:$(MonoDiagnosticsMock)" /> + <_MonoRollupEnvironmentVariable Include="ContinuousIntegrationBuild:$(ContinuousIntegrationBuild)" /> + + - Configuration:$(Configuration),NativeBinDir:$(NativeBinDir),ProductVersion:$(ProductVersion),MonoWasmThreads:$(MonoWasmThreads),DISABLE_LEGACY_JS_INTEROP:$(_DisableLegacyJsInterop),MonoDiagnosticsMock:$(MonoDiagnosticsMock),ContinuousIntegrationBuild:$(ContinuousIntegrationBuild) + @(_MonoRollupEnvironmentVariable, ',')