diff options
author | Marek FiĊĦera <mara@neptuo.com> | 2022-04-29 11:24:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-29 11:24:27 +0300 |
commit | 8058bbef8b7f2279f19289fccd3f3f4fa4ceb4cb (patch) | |
tree | b80e54dbab4bacade98a8438dfc83928c4b5acf6 /src/mono/wasm/runtime | |
parent | 7ae227a5aa60dc337b8fd195e96e380123b76a1a (diff) |
[wasm] Migrate rest of the JS interop functions to out params (#68642)
Migrate rest of the JS interop functions to out params instead of return.
A follow up on the https://github.com/dotnet/runtime/pull/65994.
- `mono_wasm_invoke_js_with_args`
- `mono_wasm_cancel_promise`
- `mono_wasm_compile_function`
Diffstat (limited to 'src/mono/wasm/runtime')
-rw-r--r-- | src/mono/wasm/runtime/cancelable-promise.ts | 14 | ||||
-rw-r--r-- | src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js | 6 | ||||
-rw-r--r-- | src/mono/wasm/runtime/corebindings.c | 12 | ||||
-rw-r--r-- | src/mono/wasm/runtime/es6/dotnet.es6.lib.js | 6 | ||||
-rw-r--r-- | src/mono/wasm/runtime/exports.ts | 12 | ||||
-rw-r--r-- | src/mono/wasm/runtime/method-calls.ts | 48 |
6 files changed, 57 insertions, 41 deletions
diff --git a/src/mono/wasm/runtime/cancelable-promise.ts b/src/mono/wasm/runtime/cancelable-promise.ts index 83fd769f57c..ebd01a86e5b 100644 --- a/src/mono/wasm/runtime/cancelable-promise.ts +++ b/src/mono/wasm/runtime/cancelable-promise.ts @@ -2,8 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. import { mono_wasm_get_jsobj_from_js_handle } from "./gc-handles"; -import { wrap_error } from "./method-calls"; -import { JSHandle, MonoString } from "./types"; +import { wrap_error_root } from "./method-calls"; +import { mono_wasm_new_external_root } from "./roots"; +import { JSHandle, MonoObject, MonoObjectRef, MonoString } from "./types"; import { Int32Ptr } from "./types/emscripten"; export const _are_promises_supported = ((typeof Promise === "object") || (typeof Promise === "function")) && (typeof Promise.resolve === "function"); @@ -17,14 +18,19 @@ export function isThenable(js_obj: any): boolean { ((typeof js_obj === "object" || typeof js_obj === "function") && typeof js_obj.then === "function"); } -export function mono_wasm_cancel_promise(thenable_js_handle: JSHandle, is_exception: Int32Ptr): void | MonoString { +export function mono_wasm_cancel_promise_ref(thenable_js_handle: JSHandle, is_exception: Int32Ptr, result_address: MonoObjectRef): void | MonoString { + const resultRoot = mono_wasm_new_external_root<MonoObject>(result_address); try { const promise = mono_wasm_get_jsobj_from_js_handle(thenable_js_handle); const promise_control = promise[promise_control_symbol]; promise_control.reject("OperationCanceledException"); } catch (ex) { - return wrap_error(is_exception, ex); + wrap_error_root(is_exception, ex, resultRoot); + return; + } + finally { + resultRoot.release(); } } diff --git a/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js b/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js index fd89a822b74..de07811bdd6 100644 --- a/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js +++ b/src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js @@ -43,7 +43,7 @@ const linked_functions = [ "mono_wasm_trace_logger", // corebindings.c - "mono_wasm_invoke_js_with_args", + "mono_wasm_invoke_js_with_args_ref", "mono_wasm_get_object_property_ref", "mono_wasm_set_object_property_ref", "mono_wasm_get_by_index_ref", @@ -55,13 +55,13 @@ const linked_functions = [ "mono_wasm_typed_array_copy_to_ref", "mono_wasm_typed_array_from_ref", "mono_wasm_typed_array_copy_from_ref", - "mono_wasm_cancel_promise", + "mono_wasm_cancel_promise_ref", "mono_wasm_web_socket_open_ref", "mono_wasm_web_socket_send", "mono_wasm_web_socket_receive", "mono_wasm_web_socket_close_ref", "mono_wasm_web_socket_abort", - "mono_wasm_compile_function", + "mono_wasm_compile_function_ref", // pal_icushim_static.c "mono_wasm_load_icu_data", diff --git a/src/mono/wasm/runtime/corebindings.c b/src/mono/wasm/runtime/corebindings.c index 8d6d29cd947..35e48a5b4f2 100644 --- a/src/mono/wasm/runtime/corebindings.c +++ b/src/mono/wasm/runtime/corebindings.c @@ -17,7 +17,7 @@ #include "gc-common.h" //JS funcs -extern MonoObject* mono_wasm_invoke_js_with_args (int js_handle, MonoString *method, MonoArray *args, int *is_exception); +extern void mono_wasm_invoke_js_with_args_ref (int js_handle, MonoString **method, MonoArray **args, int *is_exception, MonoObject **result); extern void mono_wasm_get_object_property_ref (int js_handle, MonoString **propertyName, int *is_exception, MonoObject **result); extern void mono_wasm_get_by_index_ref (int js_handle, int property_index, int *is_exception, MonoObject **result); extern void mono_wasm_set_object_property_ref (int js_handle, MonoString **propertyName, MonoObject **value, int createIfNotExist, int hasOwnProperty, int *is_exception, MonoObject **result); @@ -29,17 +29,17 @@ extern void mono_wasm_typed_array_to_array_ref (int js_handle, int *is_exception extern void mono_wasm_typed_array_copy_to_ref (int js_handle, int ptr, int begin, int end, int bytes_per_element, int *is_exception, MonoObject** result); 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); extern void mono_wasm_typed_array_copy_from_ref (int js_handle, int ptr, int begin, int end, int bytes_per_element, int *is_exception, MonoObject** result); -extern MonoString* mono_wasm_cancel_promise (int thenable_js_handle, int *is_exception); +extern void mono_wasm_cancel_promise_ref (int thenable_js_handle, int *is_exception, MonoString** result); extern void mono_wasm_web_socket_open_ref (MonoString **uri, MonoArray **subProtocols, MonoDelegate **on_close, int *web_socket_js_handle, int *thenable_js_handle, int *is_exception, MonoObject **result); extern void mono_wasm_web_socket_send (int webSocket_js_handle, void* buffer_ptr, int offset, int length, int message_type, int end_of_message, int *thenable_js_handle, int *is_exception, MonoObject **result); extern void mono_wasm_web_socket_receive (int webSocket_js_handle, void* buffer_ptr, int offset, int length, void* response_ptr, int *thenable_js_handle, int *is_exception, MonoObject **result); extern void mono_wasm_web_socket_close_ref (int webSocket_js_handle, int code, MonoString **reason, int wait_for_close_received, int *thenable_js_handle, int *is_exception, MonoObject **result); extern void mono_wasm_web_socket_abort (int webSocket_js_handle, int *is_exception, MonoString **result); -extern MonoObject* mono_wasm_compile_function (MonoString *str, int *is_exception); +extern void mono_wasm_compile_function_ref (MonoString *str, int *is_exception, MonoObject **result); void core_initialize_internals () { - mono_add_internal_call ("Interop/Runtime::InvokeJSWithArgs", mono_wasm_invoke_js_with_args); + mono_add_internal_call ("Interop/Runtime::InvokeJSWithArgsRef", mono_wasm_invoke_js_with_args_ref); mono_add_internal_call ("Interop/Runtime::GetObjectPropertyRef", mono_wasm_get_object_property_ref); mono_add_internal_call ("Interop/Runtime::GetByIndexRef", mono_wasm_get_by_index_ref); mono_add_internal_call ("Interop/Runtime::SetObjectPropertyRef", mono_wasm_set_object_property_ref); @@ -51,13 +51,13 @@ void core_initialize_internals () mono_add_internal_call ("Interop/Runtime::TypedArrayCopyToRef", mono_wasm_typed_array_copy_to_ref); mono_add_internal_call ("Interop/Runtime::TypedArrayFromRef", mono_wasm_typed_array_from_ref); mono_add_internal_call ("Interop/Runtime::TypedArrayCopyFromRef", mono_wasm_typed_array_copy_from_ref); - mono_add_internal_call ("Interop/Runtime::CompileFunction", mono_wasm_compile_function); + mono_add_internal_call ("Interop/Runtime::CompileFunctionRef", mono_wasm_compile_function_ref); mono_add_internal_call ("Interop/Runtime::WebSocketOpenRef", mono_wasm_web_socket_open_ref); mono_add_internal_call ("Interop/Runtime::WebSocketSend", mono_wasm_web_socket_send); mono_add_internal_call ("Interop/Runtime::WebSocketReceive", mono_wasm_web_socket_receive); mono_add_internal_call ("Interop/Runtime::WebSocketCloseRef", mono_wasm_web_socket_close_ref); mono_add_internal_call ("Interop/Runtime::WebSocketAbort", mono_wasm_web_socket_abort); - mono_add_internal_call ("Interop/Runtime::CancelPromise", mono_wasm_cancel_promise); + mono_add_internal_call ("Interop/Runtime::CancelPromiseRef", mono_wasm_cancel_promise_ref); } // Int8Array | int8_t | byte or SByte (signed byte) diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js index 1b8586b0332..a110268beeb 100644 --- a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js +++ b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js @@ -80,7 +80,7 @@ const linked_functions = [ "mono_wasm_trace_logger", // corebindings.c - "mono_wasm_invoke_js_with_args", + "mono_wasm_invoke_js_with_args_ref", "mono_wasm_get_object_property_ref", "mono_wasm_set_object_property_ref", "mono_wasm_get_by_index_ref", @@ -92,13 +92,13 @@ const linked_functions = [ "mono_wasm_typed_array_copy_to_ref", "mono_wasm_typed_array_from_ref", "mono_wasm_typed_array_copy_from_ref", - "mono_wasm_cancel_promise", + "mono_wasm_cancel_promise_ref", "mono_wasm_web_socket_open_ref", "mono_wasm_web_socket_send", "mono_wasm_web_socket_receive", "mono_wasm_web_socket_close_ref", "mono_wasm_web_socket_abort", - "mono_wasm_compile_function", + "mono_wasm_compile_function_ref", // pal_icushim_static.c "mono_wasm_load_icu_data", diff --git a/src/mono/wasm/runtime/exports.ts b/src/mono/wasm/runtime/exports.ts index 45124fc219a..064ddcfa214 100644 --- a/src/mono/wasm/runtime/exports.ts +++ b/src/mono/wasm/runtime/exports.ts @@ -46,14 +46,14 @@ import { import { call_static_method, mono_bind_static_method, mono_call_assembly_entry_point, mono_method_resolve, - mono_wasm_compile_function, + mono_wasm_compile_function_ref, mono_wasm_get_by_index_ref, mono_wasm_get_global_object_ref, mono_wasm_get_object_property_ref, mono_wasm_invoke_js, mono_wasm_invoke_js_blazor, - mono_wasm_invoke_js_with_args, mono_wasm_set_by_index_ref, mono_wasm_set_object_property_ref + mono_wasm_invoke_js_with_args_ref, mono_wasm_set_by_index_ref, mono_wasm_set_object_property_ref } from "./method-calls"; import { mono_wasm_typed_array_copy_to_ref, mono_wasm_typed_array_from_ref, mono_wasm_typed_array_copy_from_ref, mono_wasm_load_bytes_into_heap } from "./buffers"; -import { mono_wasm_cancel_promise } from "./cancelable-promise"; +import { mono_wasm_cancel_promise_ref } from "./cancelable-promise"; import { mono_wasm_release_cs_owned_object } from "./gc-handles"; import { mono_wasm_web_socket_open_ref, mono_wasm_web_socket_send, mono_wasm_web_socket_receive, mono_wasm_web_socket_close_ref, mono_wasm_web_socket_abort } from "./web-socket"; import cwraps from "./cwraps"; @@ -328,7 +328,7 @@ export const __linker_exports: any = { mono_wasm_trace_logger, // also keep in sync with corebindings.c - mono_wasm_invoke_js_with_args, + mono_wasm_invoke_js_with_args_ref, mono_wasm_get_object_property_ref, mono_wasm_set_object_property_ref, mono_wasm_get_by_index_ref, @@ -340,13 +340,13 @@ export const __linker_exports: any = { mono_wasm_typed_array_copy_to_ref, mono_wasm_typed_array_from_ref, mono_wasm_typed_array_copy_from_ref, - mono_wasm_cancel_promise, + mono_wasm_cancel_promise_ref, mono_wasm_web_socket_open_ref, mono_wasm_web_socket_send, mono_wasm_web_socket_receive, mono_wasm_web_socket_close_ref, mono_wasm_web_socket_abort, - mono_wasm_compile_function, + mono_wasm_compile_function_ref, // also keep in sync with pal_icushim_static.c mono_wasm_load_icu_data, diff --git a/src/mono/wasm/runtime/method-calls.ts b/src/mono/wasm/runtime/method-calls.ts index 52e9d74afee..76ced9b613f 100644 --- a/src/mono/wasm/runtime/method-calls.ts +++ b/src/mono/wasm/runtime/method-calls.ts @@ -13,7 +13,7 @@ import { mono_array_root_to_js_array, unbox_mono_obj_root } from "./cs-to-js"; import { get_js_obj, mono_wasm_get_jsobj_from_js_handle } from "./gc-handles"; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore used by unsafe export -import { js_array_to_mono_array, _js_to_mono_obj_unsafe, js_to_mono_obj_root } from "./js-to-cs"; +import { js_array_to_mono_array, js_to_mono_obj_root } from "./js-to-cs"; import { mono_bind_method, Converter, _compile_converter_for_marshal_string, @@ -301,17 +301,21 @@ export function mono_call_assembly_entry_point(assembly: string, args?: any[], s return mono_bind_assembly_entry_point(assembly, signature)(...args); } -export function mono_wasm_invoke_js_with_args(js_handle: JSHandle, method_name: MonoString, args: MonoArray, is_exception: Int32Ptr): any { - const argsRoot = mono_wasm_new_root(args), nameRoot = mono_wasm_new_root(method_name); +export function mono_wasm_invoke_js_with_args_ref(js_handle: JSHandle, method_name: MonoStringRef, args: MonoObjectRef, is_exception: Int32Ptr, result_address: MonoObjectRef): any { + const argsRoot = mono_wasm_new_external_root<MonoArray>(args), + nameRoot = mono_wasm_new_external_root<MonoString>(method_name), + resultRoot = mono_wasm_new_external_root<MonoObject>(result_address); try { const js_name = conv_string_root(nameRoot); if (!js_name || (typeof (js_name) !== "string")) { - return wrap_error(is_exception, "ERR12: Invalid method name object @" + nameRoot.value); + wrap_error_root(is_exception, "ERR12: Invalid method name object @" + nameRoot.value, resultRoot); + return; } const obj = get_js_obj(js_handle); if (!obj) { - return wrap_error(is_exception, "ERR13: Invalid JS object handle '" + js_handle + "' while invoking '" + js_name + "'"); + wrap_error_root(is_exception, "ERR13: Invalid JS object handle '" + js_handle + "' while invoking '" + js_name + "'", resultRoot); + return; } const js_args = mono_array_root_to_js_array(argsRoot); @@ -322,15 +326,14 @@ export function mono_wasm_invoke_js_with_args(js_handle: JSHandle, method_name: throw new Error("Method: '" + js_name + "' not found for: '" + Object.prototype.toString.call(obj) + "'"); const res = m.apply(obj, js_args); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore caller is unsafe also - return _js_to_mono_obj_unsafe(true, res); + js_to_mono_obj_root(res, resultRoot, true); } catch (ex) { - return wrap_error(is_exception, ex); + wrap_error_root(is_exception, ex, resultRoot); } } finally { argsRoot.release(); nameRoot.release(); + resultRoot.release(); } } @@ -617,11 +620,15 @@ export function mono_wasm_invoke_js(code: MonoString, is_exception: Int32Ptr): M // Compiles a JavaScript function from the function data passed. // Note: code snippet is not a function definition. Instead it must create and return a function instance. // code like `return function() { App.call_test_method(); };` -export function mono_wasm_compile_function(code: MonoString, is_exception: Int32Ptr): MonoObject { - if (code === MonoStringNull) - return MonoStringNull; +export function mono_wasm_compile_function_ref(code: MonoStringRef, is_exception: Int32Ptr, result_address: MonoObjectRef): void { + const codeRoot = mono_wasm_new_external_root<MonoString>(code), + resultRoot = mono_wasm_new_external_root<MonoObject>(result_address); - const js_code = conv_string(code); + const js_code = conv_string_root(codeRoot); + if (!js_code) { + js_to_mono_obj_root(MonoStringNull, resultRoot, true); + return; + } try { const closure = { @@ -630,13 +637,16 @@ export function mono_wasm_compile_function(code: MonoString, is_exception: Int32 const fn_body_template = `const {Module, MONO, BINDING, INTERNAL} = __closure; ${js_code} ;`; const fn_defn = new Function("__closure", fn_body_template); const res = fn_defn(closure); - if (!res || typeof res !== "function") - return wrap_error(is_exception, "Code must return an instance of a JavaScript function. Please use `return` statement to return a function."); + if (!res || typeof res !== "function") { + wrap_error_root(is_exception, "Code must return an instance of a JavaScript function. Please use `return` statement to return a function.", resultRoot); + return; + } Module.setValue(is_exception, 0, "i32"); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore caller is unsafe also - return _js_to_mono_obj_unsafe(true, res); + + js_to_mono_obj_root(res, resultRoot, true); } catch (ex) { - return wrap_error(is_exception, ex); + wrap_error_root(is_exception, ex, resultRoot); + } finally { + resultRoot.release(); } }
\ No newline at end of file |