Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.1

- Fixed host import arguments arriving as `double` instead of their WebAssembly types (#18) @michaelthyregod

# 1.1.0

Released on Friday, August 21 2026.
Expand Down
47 changes: 47 additions & 0 deletions src/AngleSharp.Wasm.Tests/WasmtimeRuntimeTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace AngleSharp.Wasm.Tests;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;

Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<object?>? received = null;

var imports = new[]
{
new WasmImportFunction(
"host",
"echo",
types,
Array.Empty<WasmValueType>(),
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) }));
}
}
8 changes: 4 additions & 4 deletions src/AngleSharp.Wasm/WasmtimeWasmRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]}'."),
};
}
Expand Down
Loading