Skip to content

Commit

Permalink
manual backport of #80257 (#80312)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara authored Jan 12, 2023
1 parent f5f9a70 commit 600c612
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Threading;
using Xunit;
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters

Expand All @@ -18,6 +19,19 @@ public unsafe void StructSize()
Assert.Equal(16, sizeof(JSMarshalerArgument));
}

[Fact]
public async Task CancelableImportAsync()
{
var cts = new CancellationTokenSource();
var exTask = Assert.ThrowsAsync<JSException>(async () => await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs", cts.Token));
cts.Cancel();
var actualEx2 = await exTask;
Assert.Equal("OperationCanceledException", actualEx2.Message);

var actualEx = await Assert.ThrowsAsync<JSException>(async () => await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs", new CancellationToken(true)));
Assert.Equal("OperationCanceledException", actualEx.Message);
}

[Fact]
public unsafe void GlobalThis()
{
Expand Down
20 changes: 12 additions & 8 deletions src/mono/wasm/runtime/invoke-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IMPORTS, INTERNAL, Module, runtimeHelpers } from "./imports";
import { generate_arg_marshal_to_js } from "./marshal-to-js";
import { mono_wasm_new_external_root } from "./roots";
import { mono_wasm_symbolicate_string } from "./logging";
import { wrap_as_cancelable_promise } from "./cancelable-promise";

export function mono_wasm_bind_js_function(function_name: MonoStringRef, module_name: MonoStringRef, signature: JSFunctionSignature, function_js_handle: Int32Ptr, is_exception: Int32Ptr, result_address: MonoObjectRef): void {
const function_name_root = mono_wasm_new_external_root<MonoString>(function_name),
Expand Down Expand Up @@ -174,7 +175,7 @@ export function get_global_this(): any {
export const importedModulesPromises: Map<string, Promise<any>> = new Map();
export const importedModules: Map<string, Promise<any>> = new Map();

export async function dynamic_import(module_name: string, module_url: string): Promise<any> {
export function dynamic_import(module_name: string, module_url: string): Promise<any> {
mono_assert(module_name, "Invalid module_name");
mono_assert(module_url, "Invalid module_name");
let promise = importedModulesPromises.get(module_name);
Expand All @@ -185,13 +186,16 @@ export async function dynamic_import(module_name: string, module_url: string): P
promise = import(/* webpackIgnore: true */module_url);
importedModulesPromises.set(module_name, promise);
}
const module = await promise;
if (newPromise) {
importedModules.set(module_name, module);
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: imported ES6 module '${module_name}' from '${module_url}'`);
}
return module;

return wrap_as_cancelable_promise(async () => {
const module = await promise;
if (newPromise) {
importedModules.set(module_name, module);
if (runtimeHelpers.diagnosticTracing)
console.debug(`MONO_WASM: imported ES6 module '${module_name}' from '${module_url}'`);
}
return module;
});
}


Expand Down

0 comments on commit 600c612

Please sign in to comment.