Skip to content

Commit

Permalink
test for failed bind of JSImport
Browse files Browse the repository at this point in the history
make it exception instead of abort
  • Loading branch information
pavelsavara committed Mar 26, 2024
1 parent 7d0a8c3 commit 9f5d587
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public async Task MultipleImportAsync()
instance1.Dispose();
}

[Fact]
public void MissingImport()
{
var ex = Assert.Throws<JSException>(() => JavaScriptTestHelper.IntentionallyMissingImport());
Assert.Contains("intentionallyMissingImport 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 @@ -37,6 +37,9 @@ public static void ConsoleWriteLine([JSMarshalAs<JSType.String>] string message)
[JSImport("delay", "JavaScriptTestHelper")]
public static partial Task Delay(int ms);

[JSImport("intentionallyMissingImport", "JavaScriptTestHelper")]
public static partial void IntentionallyMissingImport();

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

Expand Down
16 changes: 13 additions & 3 deletions src/mono/browser/runtime/invoke-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ export function mono_wasm_invoke_jsimport_MT(signature: JSFunctionSignature, arg
let bound_fn = js_import_wrapper_by_fn_handle[function_handle];
if (bound_fn == undefined) {
// it was not bound yet, let's do it now
bound_fn = bind_js_import(signature);
try {
bound_fn = bind_js_import(signature);
} catch (ex: any) {
Module.err(ex.toString());
marshal_exception_to_cs(<any>args, ex);
return;
}
}
mono_assert(bound_fn, () => `Imported function handle expected ${function_handle}`);

Expand Down Expand Up @@ -389,14 +395,18 @@ function mono_wasm_lookup_js_import(function_name: string, js_module_name: strin
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
const newscope = scope[part];
mono_assert(newscope, () => `${part} not found while looking up ${function_name}`);
if (!newscope) {
throw new Error(`${part} not found while looking up ${function_name}`);
}
scope = newscope;
}

const fname = parts[parts.length - 1];
const fn = scope[fname];

mono_assert(typeof (fn) === "function", () => `${function_name} must be a Function but was ${typeof fn}`);
if (typeof (fn) !== "function") {
throw new Error(`${function_name} must be a Function but was ${typeof fn}`);
}

// if the function was already bound to some object it would stay bound to original object. That's good.
return fn.bind(scope);
Expand Down

0 comments on commit 9f5d587

Please sign in to comment.