Skip to content

[browser][MT] fix error propagation when calling JSImport of missing JS function #100408

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

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public void MissingImport()
Assert.Contains("intentionallyMissingImport must be a Function but was undefined", ex.Message);
}

[Fact]
public async Task MissingImportAsync()
{
var ex = await Assert.ThrowsAsync<JSException>(() => JavaScriptTestHelper.IntentionallyMissingImportAsync());
Assert.Contains("intentionallyMissingImportAsync must be a Function but was undefined", ex.Message);
}

#if !FEATURE_WASM_MANAGED_THREADS // because in MT JSHost.ImportAsync is really async, it will finish before the caller could cancel it
[Fact]
public async Task CancelableImportAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static void ConsoleWriteLine([JSMarshalAs<JSType.String>] string message)
[JSImport("intentionallyMissingImport", "JavaScriptTestHelper")]
public static partial void IntentionallyMissingImport();

[JSImport("intentionallyMissingImportAsync", "JavaScriptTestHelper")]
public static partial Task IntentionallyMissingImportAsync();

[JSImport("catch1toString", "JavaScriptTestHelper")]
public static partial string catch1toString(string message, string functionName);

Expand Down
27 changes: 21 additions & 6 deletions src/mono/browser/runtime/invoke-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import WasmEnableThreads from "consts:wasmEnableThreads";
import BuildConfiguration from "consts:configuration";

import { marshal_exception_to_cs, bind_arg_marshal_to_cs } from "./marshal-to-cs";
import { get_signature_argument_count, bound_js_function_symbol, get_sig, get_signature_version, get_signature_type, imported_js_function_symbol, get_signature_handle, get_signature_function_name, get_signature_module_name, is_receiver_should_free, get_caller_native_tid, get_sync_done_semaphore_ptr } from "./marshal";
import { marshal_exception_to_cs, bind_arg_marshal_to_cs, marshal_task_to_cs } from "./marshal-to-cs";
import { get_signature_argument_count, bound_js_function_symbol, get_sig, get_signature_version, get_signature_type, imported_js_function_symbol, get_signature_handle, get_signature_function_name, get_signature_module_name, is_receiver_should_free, get_caller_native_tid, get_sync_done_semaphore_ptr, get_arg } from "./marshal";
import { forceThreadMemoryViewRefresh } from "./memory";
import { JSFunctionSignature, JSMarshalerArguments, BoundMarshalerToJs, JSFnHandle, BoundMarshalerToCs, JSHandle, MarshalerType, VoidPtrNull } from "./types/internal";
import { VoidPtr } from "./types/emscripten";
Expand All @@ -28,7 +28,6 @@ export function mono_wasm_bind_js_import_ST (signature: JSFunctionSignature): Vo
bind_js_import(signature);
return VoidPtrNull;
} catch (ex: any) {
Module.err(ex.toString());
return stringToUTF16Ptr(normalize_exception(ex));
}
}
Expand All @@ -45,9 +44,25 @@ export function mono_wasm_invoke_jsimport_MT (signature: JSFunctionSignature, ar
try {
bound_fn = bind_js_import(signature);
} catch (ex: any) {
Module.err(ex.toString());
marshal_exception_to_cs(<any>args, ex);
return;
// propagate the exception back to caller, which could be on different thread. Handle both sync and async signatures.
try {
const res_sig = get_sig(signature, 1);
const res_type = get_signature_type(res_sig);
if (res_type === MarshalerType.Task) {
const res = get_arg(args, 1);
marshal_task_to_cs(res, Promise.reject(ex));
} else {
marshal_exception_to_cs(<any>args, ex);
if (monoThreadInfo.isUI) {
const done_semaphore = get_sync_done_semaphore_ptr(args);
tcwraps.mono_threads_wasm_sync_run_in_target_thread_done(done_semaphore);
}
}
return;
} catch (ex2: any) {
runtimeHelpers.nativeExit(ex2);
return;
}
}
}
mono_assert(bound_fn, () => `Imported function handle expected ${function_handle}`);
Expand Down
10 changes: 5 additions & 5 deletions src/mono/browser/runtime/marshal-to-cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export function initialize_marshalers_to_cs (): void {
js_to_cs_marshalers.set(MarshalerType.JSException, marshal_exception_to_cs);
js_to_cs_marshalers.set(MarshalerType.JSObject, marshal_js_object_to_cs);
js_to_cs_marshalers.set(MarshalerType.Object, marshal_cs_object_to_cs);
js_to_cs_marshalers.set(MarshalerType.Task, _marshal_task_to_cs);
js_to_cs_marshalers.set(MarshalerType.TaskResolved, _marshal_task_to_cs);
js_to_cs_marshalers.set(MarshalerType.TaskRejected, _marshal_task_to_cs);
js_to_cs_marshalers.set(MarshalerType.Task, marshal_task_to_cs);
js_to_cs_marshalers.set(MarshalerType.TaskResolved, marshal_task_to_cs);
js_to_cs_marshalers.set(MarshalerType.TaskRejected, marshal_task_to_cs);
js_to_cs_marshalers.set(MarshalerType.Action, _marshal_function_to_cs);
js_to_cs_marshalers.set(MarshalerType.Function, _marshal_function_to_cs);
js_to_cs_marshalers.set(MarshalerType.None, _marshal_null_to_cs);// also void
Expand Down Expand Up @@ -295,7 +295,7 @@ function _marshal_function_to_cs (arg: JSMarshalerArgument, value: Function, _?:
}


function _marshal_task_to_cs (arg: JSMarshalerArgument, value: Promise<any>, _?: MarshalerType, res_converter?: MarshalerToCs) {
export function marshal_task_to_cs (arg: JSMarshalerArgument, value: Promise<any>, _?: MarshalerType, res_converter?: MarshalerToCs) {
const handleIsPreallocated = get_arg_type(arg) == MarshalerType.TaskPreCreated;
if (value === null || value === undefined) {
if (WasmEnableThreads && handleIsPreallocated) {
Expand Down Expand Up @@ -415,7 +415,7 @@ export function marshal_cs_object_to_cs (arg: JSMarshalerArgument, value: any):
) {
throw new Error("NotImplementedException: TypedArray");
} else if (isThenable(value)) {
_marshal_task_to_cs(arg, value);
marshal_task_to_cs(arg, value);
} else if (value instanceof Span) {
throw new Error("NotImplementedException: Span");
} else if (js_type == "object") {
Expand Down