From fdaa5b261b31b50e2323e4feb0900f172b057a95 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sat, 17 Apr 2021 19:50:33 +0100 Subject: [PATCH] Faster, smaller wasm runtime --- src/mono/wasm/Makefile | 8 ++++---- src/mono/wasm/runtime/binding_support.js | 9 ++++----- src/mono/wasm/runtime/corebindings.c | 2 +- src/mono/wasm/runtime/dotnet_support.js | 2 +- src/mono/wasm/runtime/externs.js | 3 +++ src/mono/wasm/runtime/library_mono.js | 4 ++-- src/mono/wasm/wasm.proj | 12 +++++++----- src/tasks/WasmAppBuilder/RunWithEmSdkEnv.cs | 8 ++++++++ 8 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 src/mono/wasm/runtime/externs.js diff --git a/src/mono/wasm/Makefile b/src/mono/wasm/Makefile index f8a44275297ea..7d0b071551778 100644 --- a/src/mono/wasm/Makefile +++ b/src/mono/wasm/Makefile @@ -72,7 +72,7 @@ ifeq ($(ENABLE_METADATA_UPDATE),true) endif EMCC_DEBUG_FLAGS =-g -Os -s ASSERTIONS=1 -DDEBUG=1 -EMCC_RELEASE_FLAGS=-Oz --llvm-opts 2 +EMCC_RELEASE_FLAGS=-O3 --llvm-opts 3 ifeq ($(NOSTRIP),) STRIP_CMD=&& $(EMSDK_PATH)/upstream/bin/wasm-opt --strip-dwarf $(NATIVE_BIN_DIR)/dotnet.wasm -o $(NATIVE_BIN_DIR)/dotnet.wasm @@ -108,13 +108,13 @@ $(BUILDS_OBJ_DIR)/pinvoke-table.h: $(PINVOKE_TABLE) | $(BUILDS_OBJ_DIR) if cmp -s $(PINVOKE_TABLE) $$@ ; then : ; else cp $(PINVOKE_TABLE) $$@ ; fi $(BUILDS_OBJ_DIR)/driver.o: runtime/driver.c | $(BUILDS_OBJ_DIR) - $(EMCC) $(EMCC_FLAGS) $(1) -Oz -DCORE_BINDINGS -I$(BUILDS_OBJ_DIR) -I$(MONO_INCLUDE_DIR) runtime/driver.c -c -o $$@ + $(EMCC) $(EMCC_FLAGS) $(1) -O3 -DCORE_BINDINGS -I$(BUILDS_OBJ_DIR) -I$(MONO_INCLUDE_DIR) runtime/driver.c -c -o $$@ $(BUILDS_OBJ_DIR)/pinvoke.o: runtime/pinvoke.c runtime/pinvoke.h $(BUILDS_OBJ_DIR)/pinvoke-table.h | $(BUILDS_OBJ_DIR) - $(EMCC) $(EMCC_FLAGS) $(1) -Oz -DGEN_PINVOKE=1 -I$(BUILDS_OBJ_DIR) runtime/pinvoke.c -c -o $$@ + $(EMCC) $(EMCC_FLAGS) $(1) -O3 -DGEN_PINVOKE=1 -I$(BUILDS_OBJ_DIR) runtime/pinvoke.c -c -o $$@ $(BUILDS_OBJ_DIR)/corebindings.o: runtime/corebindings.c | $(BUILDS_OBJ_DIR) - $(EMCC) $(EMCC_FLAGS) $(1) -Oz -I$(MONO_INCLUDE_DIR) runtime/corebindings.c -c -o $$@ + $(EMCC) $(EMCC_FLAGS) $(1) -O3 -I$(MONO_INCLUDE_DIR) runtime/corebindings.c -c -o $$@ $(NATIVE_BIN_DIR)/src/emcc-flags.txt: | $(NATIVE_BIN_DIR)/src Makefile echo "$(call escape_quote,$(EMCC_FLAGS)) $(1)" > $$@ diff --git a/src/mono/wasm/runtime/binding_support.js b/src/mono/wasm/runtime/binding_support.js index 3938dc83041b6..8426d2154381b 100644 --- a/src/mono/wasm/runtime/binding_support.js +++ b/src/mono/wasm/runtime/binding_support.js @@ -541,6 +541,7 @@ var BindingSupportLib = { ((typeof js_obj === "object" || typeof js_obj === "function") && typeof js_obj.then === "function") } + var result; switch (true) { case js_obj === null: case typeof js_obj === "undefined": @@ -962,7 +963,7 @@ var BindingSupportLib = { var conv = primitiveConverters.get (key); if (!conv) - throw new Error ("Unknown parameter type " + type); + throw new Error ("Unknown parameter type " + key); var localStep = Object.create (conv.steps[0]); localStep.size = conv.size; @@ -1300,7 +1301,7 @@ var BindingSupportLib = { converter, buffer, resultRoot, exceptionRoot, argsRootBuffer, is_result_marshaled ) { this._handle_exception_for_call (converter, buffer, resultRoot, exceptionRoot, argsRootBuffer); - + var result; if (is_result_marshaled) result = this._unbox_mono_obj_root (resultRoot); else @@ -1443,7 +1444,7 @@ var BindingSupportLib = { "return result;" ); - bodyJs = body.join ("\r\n"); + var bodyJs = body.join ("\r\n"); if (friendly_name) { var escapeRE = /[^A-Za-z0-9_]/g; @@ -1699,7 +1700,6 @@ var BindingSupportLib = { var js_args = BINDING.mono_wasm_parse_args(args); - var res; try { var m = obj [js_name]; if (typeof m === "undefined") @@ -1731,7 +1731,6 @@ var BindingSupportLib = { return BINDING.js_string_to_mono_string ("Invalid property name object '" + js_name + "'"); } - var res; try { var m = obj [js_name]; if (m === Object(m) && obj.__is_mono_proxied__) diff --git a/src/mono/wasm/runtime/corebindings.c b/src/mono/wasm/runtime/corebindings.c index a0263c2a8ba9d..e386f84a9f31d 100644 --- a/src/mono/wasm/runtime/corebindings.c +++ b/src/mono/wasm/runtime/corebindings.c @@ -43,7 +43,7 @@ EM_JS(MonoObject*, compile_function, (int snippet_ptr, int len, int *is_exceptio } catch (e) { - res = e.toString (); + var res = e.toString (); setValue (is_exception, 1, "i32"); if (res === null || res === undefined) res = "unknown exception"; diff --git a/src/mono/wasm/runtime/dotnet_support.js b/src/mono/wasm/runtime/dotnet_support.js index 3272b714eca90..c9552c9a94b7c 100644 --- a/src/mono/wasm/runtime/dotnet_support.js +++ b/src/mono/wasm/runtime/dotnet_support.js @@ -46,7 +46,7 @@ var DotNetSupportLib = { var funcNameJsString = DOTNET.conv_string(functionName); var argsJsonJsString = argsJson && DOTNET.conv_string (argsJson); - var dotNetExports = globaThis.DotNet; + var dotNetExports = globalThis.DotNet; if (!dotNetExports) { throw new Error('The Microsoft.JSInterop.js library is not loaded.'); } diff --git a/src/mono/wasm/runtime/externs.js b/src/mono/wasm/runtime/externs.js new file mode 100644 index 0000000000000..fc5845464feef --- /dev/null +++ b/src/mono/wasm/runtime/externs.js @@ -0,0 +1,3 @@ +// @externs +var ___cxa_is_pointer_type = function(type) {}; +var ___cxa_can_catch = function(caughtType, thrownType, exceptionThrowBuf) {}; diff --git a/src/mono/wasm/runtime/library_mono.js b/src/mono/wasm/runtime/library_mono.js index 685aaaf375d85..a0041801660d7 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -2268,7 +2268,7 @@ var MonoSupportLib = { var manifest; try { - manifestContent = Module.UTF8ArrayToString(data, 8, manifestSize); + var manifestContent = Module.UTF8ArrayToString(data, 8, manifestSize); manifest = JSON.parse(manifestContent); if (!(manifest instanceof Array)) return false; @@ -2295,7 +2295,7 @@ var MonoSupportLib = { Module['FS_createPath'](prefix, folder, true, true); }); - for (row of manifest) { + for (var row of manifest) { var name = row[0]; var length = row[1]; var bytes = data.slice(0, length); diff --git a/src/mono/wasm/wasm.proj b/src/mono/wasm/wasm.proj index dda6fa29f6272..101204f5e9838 100644 --- a/src/mono/wasm/wasm.proj +++ b/src/mono/wasm/wasm.proj @@ -119,12 +119,13 @@ --profiling-funcs -s WASM=1 -s ALLOW_MEMORY_GROWTH=1 -s BINARYEN=1 -s ALIASING_FUNCTION_POINTERS=0 -s NO_EXIT_RUNTIME=1 -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'FS_createPath', 'FS_createDataFile', 'cwrap', 'setValue', 'getValue', 'UTF8ToString', 'UTF8ArrayToString', 'addFunction']" -s "EXPORTED_FUNCTIONS=['_putchar']" --source-map-base http://example.com -emit-llvm -s FORCE_FILESYSTEM=1 -s USE_ZLIB=1 $(EmccFlags) -s MODULARIZE=1 -s EXPORT_ES6=1 -g -Os -s ASSERTIONS=1 -DENABLE_NETCORE=1 -DDEBUG=1 - -Oz --llvm-opts 2 -DENABLE_NETCORE=1 + -O3 --llvm-opts 3 -DENABLE_NETCORE=1 "$(EMSDK_PATH)/upstream/bin/wasm-opt" --strip-dwarf "$(NativeBinDir)/dotnet.wasm" -o "$(NativeBinDir)/dotnet.wasm" $(ArtifactsObjDir)wasm $(WasmObjDir)\emcc-version.txt $(MonoArtifactsPath)include/mono-2.0 $(RepoRoot)src\libraries\Native\Unix\System.Native + --externs runtime/externs.js - - @@ -152,12 +153,13 @@ DestinationFolder="$(MonoObjDir)" SkipUnchangedFiles="true" /> - - /dev/null 2>&1 && {Command}'"; } + if (ClosureArgs != null) + { + Environment.SetEnvironmentVariable("EMCC_CLOSURE_ARGS", ClosureArgs); + } + var workingDir = string.IsNullOrEmpty(WorkingDirectory) ? Directory.GetCurrentDirectory() : WorkingDirectory; Log.LogMessage(MessageImportance.Low, $"Working directory: {workingDir}"); Log.LogMessage(MessageImportance.Low, $"Using Command: {Command}");