Skip to content

Commit

Permalink
[WASM] Fix third parameter marshaling on callbacks (dotnet#101638)
Browse files Browse the repository at this point in the history
* Fix typo with get_signature_arg3_type incrorrecty arg2 instead of arg3

get_signature_arg3_type method is expected to read argument type by Arg3MarshalerType (=28) offset instead of Arg2MarshalerType (=24)

* Add another callback tests to cover different parameter types
  • Loading branch information
maxkatz6 authored and matouskozak committed Apr 30, 2024
1 parent 15568fd commit cbf2031
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,23 @@ public void JsImportCallback_ActionIntLong()
Assert.Equal(43, calledB);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))]
public void JsImportCallback_ActionIntLongDouble()
{
int calledA = -1;
long calledB = -1;
double calledC = -1;
JavaScriptTestHelper.back4_ActionIntLongDouble((a, b, c) =>
{
calledA = a;
calledB = b;
calledC = c;
}, 42, 43, 44.5);
Assert.Equal(42, calledA);
Assert.Equal(43, calledB);
Assert.Equal(44.5, calledC);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))]
public void JsImportCallback_ActionIntThrow()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ public static async Task<object> AwaitTaskOfObject([JSMarshalAs<JSType.Promise<J
[return: JSMarshalAs<JSType.Number>]
internal static partial int back3_FunctionIntInt([JSMarshalAs<JSType.Function<JSType.Number, JSType.Number>>] Func<int, int>? fun, [JSMarshalAs<JSType.Number>] int a);

[JSImport("back4", "JavaScriptTestHelper")]
internal static partial void back4_ActionIntLongDouble([JSMarshalAs<JSType.Function<JSType.Number, JSType.Number, JSType.Number>>] Action<int, long, double>? action, [JSMarshalAs<JSType.Number>] int a, [JSMarshalAs<JSType.Number>] long b, [JSMarshalAs<JSType.Number>] double c);

[JSImport("invoke1", "JavaScriptTestHelper")]
[return: JSMarshalAs<JSType.Function<JSType.Number, JSType.Number>>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,21 @@ export function back3(arg1, arg2, arg3) {
}
}

export function back4(arg1, arg2, arg3, arg4) {
if (globalThis.gc) {
// console.log('globalThis.gc');
globalThis.gc();
}
try {
if (!(arg1 instanceof Function)) throw new Error('expecting Function!')

return arg1(arg2, arg3, arg4);
}
catch (ex) {
throw ex;
}
}

export function backback(arg1, arg2, arg3) {
if (globalThis.gc) {
// console.log('globalThis.gc');
Expand Down Expand Up @@ -432,4 +447,4 @@ export async function setup() {

export function delay(ms) {
return new Promise(resolve => globalThis.setTimeout(resolve, ms));
}
}
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/marshal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function get_signature_arg2_type (sig: JSMarshalerType): MarshalerType {

export function get_signature_arg3_type (sig: JSMarshalerType): MarshalerType {
mono_assert(sig, "Null sig");
return <any>getU8(<any>sig + JSBindingTypeOffsets.Arg2MarshalerType);
return <any>getU8(<any>sig + JSBindingTypeOffsets.Arg3MarshalerType);
}

export function get_signature_argument_count (signature: JSFunctionSignature): number {
Expand Down

0 comments on commit cbf2031

Please sign in to comment.