Skip to content

Commit 600c612

Browse files
authored
manual backport of #80257 (#80312)
1 parent f5f9a70 commit 600c612

File tree

2 files changed

+26
-8
lines changed
  • src
    • libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript
    • mono/wasm/runtime

2 files changed

+26
-8
lines changed

src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportExportTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Runtime.CompilerServices;
77
using System.Threading.Tasks;
8+
using System.Threading;
89
using Xunit;
910
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
1011

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

22+
[Fact]
23+
public async Task CancelableImportAsync()
24+
{
25+
var cts = new CancellationTokenSource();
26+
var exTask = Assert.ThrowsAsync<JSException>(async () => await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs", cts.Token));
27+
cts.Cancel();
28+
var actualEx2 = await exTask;
29+
Assert.Equal("OperationCanceledException", actualEx2.Message);
30+
31+
var actualEx = await Assert.ThrowsAsync<JSException>(async () => await JSHost.ImportAsync("JavaScriptTestHelper", "./JavaScriptTestHelper.mjs", new CancellationToken(true)));
32+
Assert.Equal("OperationCanceledException", actualEx.Message);
33+
}
34+
2135
[Fact]
2236
public unsafe void GlobalThis()
2337
{

src/mono/wasm/runtime/invoke-js.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { IMPORTS, INTERNAL, Module, runtimeHelpers } from "./imports";
1212
import { generate_arg_marshal_to_js } from "./marshal-to-js";
1313
import { mono_wasm_new_external_root } from "./roots";
1414
import { mono_wasm_symbolicate_string } from "./logging";
15+
import { wrap_as_cancelable_promise } from "./cancelable-promise";
1516

1617
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 {
1718
const function_name_root = mono_wasm_new_external_root<MonoString>(function_name),
@@ -174,7 +175,7 @@ export function get_global_this(): any {
174175
export const importedModulesPromises: Map<string, Promise<any>> = new Map();
175176
export const importedModules: Map<string, Promise<any>> = new Map();
176177

177-
export async function dynamic_import(module_name: string, module_url: string): Promise<any> {
178+
export function dynamic_import(module_name: string, module_url: string): Promise<any> {
178179
mono_assert(module_name, "Invalid module_name");
179180
mono_assert(module_url, "Invalid module_name");
180181
let promise = importedModulesPromises.get(module_name);
@@ -185,13 +186,16 @@ export async function dynamic_import(module_name: string, module_url: string): P
185186
promise = import(/* webpackIgnore: true */module_url);
186187
importedModulesPromises.set(module_name, promise);
187188
}
188-
const module = await promise;
189-
if (newPromise) {
190-
importedModules.set(module_name, module);
191-
if (runtimeHelpers.diagnosticTracing)
192-
console.debug(`MONO_WASM: imported ES6 module '${module_name}' from '${module_url}'`);
193-
}
194-
return module;
189+
190+
return wrap_as_cancelable_promise(async () => {
191+
const module = await promise;
192+
if (newPromise) {
193+
importedModules.set(module_name, module);
194+
if (runtimeHelpers.diagnosticTracing)
195+
console.debug(`MONO_WASM: imported ES6 module '${module_name}' from '${module_url}'`);
196+
}
197+
return module;
198+
});
195199
}
196200

197201

0 commit comments

Comments
 (0)