Skip to content

Commit 82871f4

Browse files
authored
[browser] legacy JS interop optional via WasmEnableLegacyJsInterop - native part (#82834)
1 parent 13d1801 commit 82871f4

24 files changed

+291
-281
lines changed

src/mono/wasi/build/WasiApp.Native.targets

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@
182182
<!--<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />-->
183183
<!--<_EmccCFlags Include="-DENABLE_AOT_PROFILER=1" Condition="$(WasmProfilers.Contains('aot'))" />-->
184184
<!--<_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1" Condition="$(WasmProfilers.Contains('browser'))" />-->
185-
<!--<_EmccCFlags Include="-DCORE_BINDINGS" />-->
186185
<!--<_EmccCFlags Include="-DGEN_PINVOKE=1" />-->
187186
<!--<_EmccCFlags Include="-emit-llvm" />-->
188187

src/mono/wasi/runtime/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ set(CMAKE_EXECUTABLE_SUFFIX ".wasm")
99
add_executable(dotnet driver.c pinvoke.c stubs.c synthetic-pthread.c)
1010

1111
target_include_directories(dotnet PUBLIC ${MONO_INCLUDES} ${MONO_OBJ_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}/include/wasm)
12-
target_compile_options(dotnet PUBLIC @${NATIVE_BIN_DIR}/src/wasi-default.rsp @${NATIVE_BIN_DIR}/src/wasi-compile.rsp -DCORE_BINDINGS -DGEN_PINVOKE=1)
12+
target_compile_options(dotnet PUBLIC @${NATIVE_BIN_DIR}/src/wasi-default.rsp @${NATIVE_BIN_DIR}/src/wasi-compile.rsp -DGEN_PINVOKE=1)
1313

1414
set_target_properties(dotnet PROPERTIES COMPILE_FLAGS ${CONFIGURATION_WASICC_FLAGS})
1515

src/mono/wasm/build/WasmApp.Native.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@
218218
<_EmccCFlags Include="-DLINK_ICALLS=1" Condition="'$(WasmLinkIcalls)' == 'true'" />
219219
<_EmccCFlags Include="-DENABLE_AOT_PROFILER=1" Condition="$(WasmProfilers.Contains('aot'))" />
220220
<_EmccCFlags Include="-DENABLE_BROWSER_PROFILER=1" Condition="$(WasmProfilers.Contains('browser'))" />
221-
<_EmccCFlags Include="-DCORE_BINDINGS" />
222221
<_EmccCFlags Include="-DGEN_PINVOKE=1" />
223222
<_EmccCFlags Include="-emit-llvm" />
224223

@@ -258,6 +257,8 @@
258257
<EmscriptenEnvVars Include="PYTHONPATH=$(EmscriptenPythonToolsPath)" Condition="'$(OS)' == 'Windows_NT'" />
259258
<EmscriptenEnvVars Include="PYTHONHOME=" Condition="'$(OS)' == 'Windows_NT'" />
260259
<EmscriptenEnvVars Include="EM_CACHE=$(WasmCachePath)" Condition="'$(WasmCachePath)' != ''" />
260+
<EmscriptenEnvVars Include="WasmEnableLegacyJsInterop=$(WasmEnableLegacyJsInterop)"/>
261+
<EmscriptenEnvVars Include="MonoWasmThreads=$(MonoWasmThreads)"/>
261262
</ItemGroup>
262263

263264
<ItemGroup Condition="'$(WasmAllowUndefinedSymbols)' == 'true'">

src/mono/wasm/build/WasmApp.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
- $(RunAOTCompilationAfterBuild) - Run AOT compilation even after Build. By default, it is run only for publish.
6464
Defaults to false.
6565
- $(WasmAotProfilePath) - Path to an AOT profile file.
66+
- $(WasmEnableLegacyJsInterop) - Include support for legacy JS interop. Defaults to true.
6667
- $(WasmEnableExceptionHandling) - Enable support for the WASM Exception Handling feature.
6768
- $(WasmEnableSIMD) - Enable support for the WASM SIMD feature.
6869
- $(WasmEnableWebcil) - Enable conversion of assembly .dlls to .webcil
@@ -94,6 +95,7 @@
9495
<WasmDedup Condition="'$(WasmDedup)' == ''">true</WasmDedup>
9596
<WasmEnableExceptionHandling Condition="'$(WasmEnableExceptionHandling)' == ''">false</WasmEnableExceptionHandling>
9697
<WasmEnableSIMD Condition="'$(WasmEnableSIMD)' == ''">true</WasmEnableSIMD>
98+
<WasmEnableLegacyJsInterop Condition="'$(WasmEnableLegacyJsInterop)' == ''">true</WasmEnableLegacyJsInterop>
9799

98100
<!--<WasmStripAOTAssemblies Condition="'$(AOTMode)' == 'LLVMOnlyInterp'">false</WasmStripAOTAssemblies>-->
99101
<!--<WasmStripAOTAssemblies Condition="'$(WasmStripAOTAssemblies)' == ''">$(RunAOTCompilation)</WasmStripAOTAssemblies>-->

src/mono/wasm/runtime/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ project(mono-wasm-runtime C)
44

55
option(DISABLE_THREADS "defined if the build does NOT support multithreading" ON)
66
option(DISABLE_WASM_USER_THREADS "defined if the build does not allow user threads to be created in a multithreaded build" OFF)
7+
option(ENABLE_LEGACY_JS_INTEROP "defined if the build supports legacy JavaScript interop" ON)
78

89
set(CMAKE_EXECUTABLE_SUFFIX ".js")
910
add_executable(dotnet corebindings.c driver.c pinvoke.c)
1011

1112
target_include_directories(dotnet PUBLIC ${MONO_INCLUDES} ${MONO_OBJ_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}/include/wasm)
12-
target_compile_options(dotnet PUBLIC @${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-compile.rsp -DCORE_BINDINGS -DGEN_PINVOKE=1)
13+
target_compile_options(dotnet PUBLIC @${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-compile.rsp -DGEN_PINVOKE=1)
1314

1415
set_target_properties(dotnet PROPERTIES COMPILE_FLAGS ${CONFIGURATION_EMCC_FLAGS})
1516

src/mono/wasm/runtime/assets.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import cwraps from "./cwraps";
55
import { mono_wasm_load_icu_data } from "./icu";
66
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, ENVIRONMENT_IS_WEB, Module, runtimeHelpers } from "./imports";
77
import { mono_wasm_load_bytes_into_heap } from "./memory";
8-
import { MONO } from "./net6-legacy/imports";
98
import { endMeasure, MeasuredBlock, startMeasure } from "./profiler";
109
import { createPromiseController, PromiseAndController } from "./promise-controller";
1110
import { delay } from "./promise-utils";
@@ -548,12 +547,12 @@ export async function wait_for_all_assets() {
548547
if (runtimeHelpers.config.assets) {
549548
mono_assert(actual_downloaded_assets_count == expected_downloaded_assets_count, () => `Expected ${expected_downloaded_assets_count} assets to be downloaded, but only finished ${actual_downloaded_assets_count}`);
550549
mono_assert(actual_instantiated_assets_count == expected_instantiated_assets_count, () => `Expected ${expected_instantiated_assets_count} assets to be in memory, but only instantiated ${actual_instantiated_assets_count}`);
551-
loaded_files.forEach(value => MONO.loaded_files.push(value.url));
550+
loaded_files.forEach(value => runtimeHelpers.loadedFiles.push(value.url));
552551
if (runtimeHelpers.diagnosticTracing) console.debug("MONO_WASM: all assets are loaded in wasm memory");
553552
}
554553
}
555554

556555
// Used by the debugger to enumerate loaded dlls and pdbs
557556
export function mono_wasm_get_loaded_files(): string[] {
558-
return MONO.loaded_files;
557+
return runtimeHelpers.loadedFiles;
559558
}

src/mono/wasm/runtime/corebindings.c

Lines changed: 23 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,35 @@
1717
#include "gc-common.h"
1818

1919
//JS funcs
20+
extern void mono_wasm_release_cs_owned_object (int js_handle);
21+
extern void mono_wasm_bind_js_function(MonoString **function_name, MonoString **module_name, void *signature, int* function_js_handle, int *is_exception, MonoObject **result);
22+
extern void mono_wasm_invoke_bound_function(int function_js_handle, void *data);
23+
extern void mono_wasm_invoke_import(int fn_handle, void *data);
24+
extern void mono_wasm_bind_cs_function(MonoString **fully_qualified_name, int signature_hash, void* signatures, int *is_exception, MonoObject **result);
25+
extern void mono_wasm_marshal_promise(void *data);
26+
27+
typedef void (*background_job_cb)(void);
28+
void mono_threads_schedule_background_job (background_job_cb cb);
29+
30+
#ifdef ENABLE_LEGACY_JS_INTEROP
2031
extern void mono_wasm_invoke_js_with_args_ref (int js_handle, MonoString **method, MonoArray **args, int *is_exception, MonoObject **result);
2132
extern void mono_wasm_get_object_property_ref (int js_handle, MonoString **propertyName, int *is_exception, MonoObject **result);
22-
extern void mono_wasm_get_by_index_ref (int js_handle, int property_index, int *is_exception, MonoObject **result);
2333
extern void mono_wasm_set_object_property_ref (int js_handle, MonoString **propertyName, MonoObject **value, int createIfNotExist, int hasOwnProperty, int *is_exception, MonoObject **result);
34+
extern void mono_wasm_get_by_index_ref (int js_handle, int property_index, int *is_exception, MonoObject **result);
2435
extern void mono_wasm_set_by_index_ref (int js_handle, int property_index, MonoObject **value, int *is_exception, MonoObject **result);
2536
extern void mono_wasm_get_global_object_ref (MonoString **global_name, int *is_exception, MonoObject **result);
26-
extern void mono_wasm_release_cs_owned_object (int js_handle);
27-
extern void mono_wasm_create_cs_owned_object_ref (MonoString **core_name, MonoArray **args, int *is_exception, MonoObject** result);
2837
extern void mono_wasm_typed_array_to_array_ref (int js_handle, int *is_exception, MonoObject **result);
38+
extern void mono_wasm_create_cs_owned_object_ref (MonoString **core_name, MonoArray **args, int *is_exception, MonoObject** result);
2939
extern void mono_wasm_typed_array_from_ref (int ptr, int begin, int end, int bytes_per_element, int type, int *is_exception, MonoObject** result);
3040

31-
extern void mono_wasm_bind_js_function(MonoString **function_name, MonoString **module_name, void *signature, int* function_js_handle, int *is_exception, MonoObject **result);
32-
extern void mono_wasm_invoke_bound_function(int function_js_handle, void *data);
33-
extern void mono_wasm_invoke_import(int fn_handle, void *data);
34-
extern void mono_wasm_bind_cs_function(MonoString **fully_qualified_name, int signature_hash, void* signatures, int *is_exception, MonoObject **result);
35-
extern void mono_wasm_marshal_promise(void *data);
41+
// Blazor specific custom routines - see dotnet_support.js for backing code
42+
extern void* mono_wasm_invoke_js_blazor (MonoString **exceptionMessage, void *callInfo, void* arg0, void* arg1, void* arg2);
43+
#endif /* ENABLE_LEGACY_JS_INTEROP */
3644

37-
38-
void core_initialize_internals (void)
45+
void bindings_initialize_internals (void)
3946
{
47+
mono_add_internal_call ("System.Runtime.InteropServices.JavaScript.JSSynchronizationContext::ScheduleBackgroundJob", mono_threads_schedule_background_job);
48+
4049
mono_add_internal_call ("Interop/Runtime::ReleaseCSOwnedObject", mono_wasm_release_cs_owned_object);
4150
mono_add_internal_call ("Interop/Runtime::BindJSFunction", mono_wasm_bind_js_function);
4251
mono_add_internal_call ("Interop/Runtime::InvokeJSFunction", mono_wasm_invoke_bound_function);
@@ -45,7 +54,7 @@ void core_initialize_internals (void)
4554
mono_add_internal_call ("Interop/Runtime::MarshalPromise", mono_wasm_marshal_promise);
4655
mono_add_internal_call ("Interop/Runtime::RegisterGCRoot", mono_wasm_register_root);
4756
mono_add_internal_call ("Interop/Runtime::DeregisterGCRoot", mono_wasm_deregister_root);
48-
57+
#ifdef ENABLE_LEGACY_JS_INTEROP
4958
// legacy
5059
mono_add_internal_call ("Interop/Runtime::InvokeJSWithArgsRef", mono_wasm_invoke_js_with_args_ref);
5160
mono_add_internal_call ("Interop/Runtime::GetObjectPropertyRef", mono_wasm_get_object_property_ref);
@@ -56,70 +65,8 @@ void core_initialize_internals (void)
5665
mono_add_internal_call ("Interop/Runtime::TypedArrayToArrayRef", mono_wasm_typed_array_to_array_ref);
5766
mono_add_internal_call ("Interop/Runtime::CreateCSOwnedObjectRef", mono_wasm_create_cs_owned_object_ref);
5867
mono_add_internal_call ("Interop/Runtime::TypedArrayFromRef", mono_wasm_typed_array_from_ref);
59-
}
60-
61-
// Int8Array | int8_t | byte or SByte (signed byte)
62-
// Uint8Array | uint8_t | byte or Byte (unsigned byte)
63-
// Uint8ClampedArray| uint8_t | byte or Byte (unsigned byte)
64-
// Int16Array | int16_t | short (signed short)
65-
// Uint16Array | uint16_t | ushort (unsigned short)
66-
// Int32Array | int32_t | int (signed integer)
67-
// Uint32Array | uint32_t | uint (unsigned integer)
68-
// Float32Array | float | float
69-
// Float64Array | double | double
70-
// typed array marshalling
71-
// Keep in sync with driver.c
72-
#define MARSHAL_ARRAY_BYTE 10
73-
#define MARSHAL_ARRAY_UBYTE 11
74-
#define MARSHAL_ARRAY_UBYTE_C 12 // alias of MARSHAL_ARRAY_UBYTE
75-
#define MARSHAL_ARRAY_SHORT 13
76-
#define MARSHAL_ARRAY_USHORT 14
77-
#define MARSHAL_ARRAY_INT 15
78-
#define MARSHAL_ARRAY_UINT 16
79-
#define MARSHAL_ARRAY_FLOAT 17
80-
#define MARSHAL_ARRAY_DOUBLE 18
81-
82-
EMSCRIPTEN_KEEPALIVE void
83-
mono_wasm_typed_array_new_ref (char *arr, int length, int size, int type, PPVOLATILE(MonoArray) result)
84-
{
85-
MONO_ENTER_GC_UNSAFE;
86-
MonoClass * typeClass = mono_get_byte_class(); // default is Byte
87-
switch (type) {
88-
case MARSHAL_ARRAY_BYTE:
89-
typeClass = mono_get_sbyte_class();
90-
break;
91-
case MARSHAL_ARRAY_SHORT:
92-
typeClass = mono_get_int16_class();
93-
break;
94-
case MARSHAL_ARRAY_USHORT:
95-
typeClass = mono_get_uint16_class();
96-
break;
97-
case MARSHAL_ARRAY_INT:
98-
typeClass = mono_get_int32_class();
99-
break;
100-
case MARSHAL_ARRAY_UINT:
101-
typeClass = mono_get_uint32_class();
102-
break;
103-
case MARSHAL_ARRAY_FLOAT:
104-
typeClass = mono_get_single_class();
105-
break;
106-
case MARSHAL_ARRAY_DOUBLE:
107-
typeClass = mono_get_double_class();
108-
break;
109-
case MARSHAL_ARRAY_UBYTE:
110-
case MARSHAL_ARRAY_UBYTE_C:
111-
typeClass = mono_get_byte_class();
112-
break;
113-
default:
114-
printf ("Invalid marshal type %d in mono_wasm_typed_array_new", type);
115-
abort();
116-
}
117-
118-
PVOLATILE(MonoArray) buffer;
119-
120-
buffer = mono_array_new (mono_get_root_domain(), typeClass, length);
121-
memcpy(mono_array_addr_with_size(buffer, sizeof(char), 0), arr, length * size);
12268

123-
store_volatile((PPVOLATILE(MonoObject))result, (MonoObject *)buffer);
124-
MONO_EXIT_GC_UNSAFE;
69+
// Blazor specific custom routines - see dotnet_support.js for backing code
70+
mono_add_internal_call ("WebAssembly.JSInterop.InternalCalls::InvokeJS", mono_wasm_invoke_js_blazor);
71+
#endif /* ENABLE_LEGACY_JS_INTEROP */
12572
}

0 commit comments

Comments
 (0)