diff --git a/CHANGELOG.md b/CHANGELOG.md index 5245b8a..0827da6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # 1.1.1 +Released on Monday, November 9 2026. + +- Fixed host import arguments arriving as `double` instead of their WebAssembly types (#18) @michaelthyregod - Added trimming and Native AOT compatibility (`IsAotCompatible`) for the .NET 8 and .NET 10 targets (#17) @michaelthyregod # 1.1.0 diff --git a/src/AngleSharp.Wasm.Tests/WasmtimeRuntimeTests.cs b/src/AngleSharp.Wasm.Tests/WasmtimeRuntimeTests.cs index c9e77b0..f071e98 100644 --- a/src/AngleSharp.Wasm.Tests/WasmtimeRuntimeTests.cs +++ b/src/AngleSharp.Wasm.Tests/WasmtimeRuntimeTests.cs @@ -1,6 +1,8 @@ namespace AngleSharp.Wasm.Tests; using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using NUnit.Framework; @@ -43,6 +45,21 @@ public sealed class WasmtimeRuntimeTests 0x0A, 0x09, 0x01, 0x07, 0x00, 0x10, 0x00, 0x41, 0x01, 0x6A, 0x0B, }; + // (module + // (import "host" "echo" (func $echo (param i32 i64 f32 f64))) + // (func (export "run") (param i32 i64 f32 f64) + // local.get 0 local.get 1 local.get 2 local.get 3 call $echo)) + private static readonly byte[] HostImportArgumentsModule = + { + 0x00, 0x61, 0x73, 0x6D, + 0x01, 0x00, 0x00, 0x00, + 0x01, 0x08, 0x01, 0x60, 0x04, 0x7F, 0x7E, 0x7D, 0x7C, 0x00, + 0x02, 0x0D, 0x01, 0x04, 0x68, 0x6F, 0x73, 0x74, 0x04, 0x65, 0x63, 0x68, 0x6F, 0x00, 0x00, + 0x03, 0x02, 0x01, 0x00, + 0x07, 0x07, 0x01, 0x03, 0x72, 0x75, 0x6E, 0x00, 0x01, + 0x0A, 0x0E, 0x01, 0x0C, 0x00, 0x20, 0x00, 0x20, 0x01, 0x20, 0x02, 0x20, 0x03, 0x10, 0x00, 0x0B, + }; + [Test] public async Task CanCompileInstantiateAndInvokeExport() { @@ -97,4 +114,34 @@ public async Task CanInstantiateWithHostImport() Assert.That(Convert.ToInt32(result), Is.EqualTo(42)); } + + [Test] + public async Task HostImportReceivesArgumentsWithTheirWasmTypes() + { + using var runtime = new WasmtimeWasmRuntime(); + using var module = await runtime.CompileAsync(HostImportArgumentsModule).ConfigureAwait(false); + var types = new[] { WasmValueType.Int32, WasmValueType.Int64, WasmValueType.Float32, WasmValueType.Float64 }; + IReadOnlyList? received = null; + + var imports = new[] + { + new WasmImportFunction( + "host", + "echo", + types, + Array.Empty(), + arguments => + { + received = arguments; + return null; + }), + }; + + using var instance = await runtime.InstantiateAsync(module, imports).ConfigureAwait(false); + await instance.InvokeAsync("run", new object?[] { 7, 9007199254740993L, 1.5f, 2.25 }).ConfigureAwait(false); + + Assert.That(received, Is.EqualTo(new object?[] { 7, 9007199254740993L, 1.5f, 2.25 })); + Assert.That(received!.Select(argument => argument?.GetType()), + Is.EqualTo(new[] { typeof(int), typeof(long), typeof(float), typeof(double) })); + } } diff --git a/src/AngleSharp.Wasm/WasmtimeWasmRuntime.cs b/src/AngleSharp.Wasm/WasmtimeWasmRuntime.cs index 51dd0da..82f3edf 100644 --- a/src/AngleSharp.Wasm/WasmtimeWasmRuntime.cs +++ b/src/AngleSharp.Wasm/WasmtimeWasmRuntime.cs @@ -126,10 +126,10 @@ private static void BindImport(Linker linker, WasmImportFunction import) { converted[i] = types[i] switch { - WasmValueType.Int32 => arguments[i].AsInt32(), - WasmValueType.Int64 => arguments[i].AsInt64(), - WasmValueType.Float32 => arguments[i].AsSingle(), - WasmValueType.Float64 => arguments[i].AsDouble(), + WasmValueType.Int32 => (object)arguments[i].AsInt32(), + WasmValueType.Int64 => (object)arguments[i].AsInt64(), + WasmValueType.Float32 => (object)arguments[i].AsSingle(), + WasmValueType.Float64 => (object)arguments[i].AsDouble(), _ => throw new NotSupportedException($"Unsupported argument type '{types[i]}'."), }; }