forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionThunkingTests.cs
More file actions
69 lines (54 loc) · 2.09 KB
/
FunctionThunkingTests.cs
File metadata and controls
69 lines (54 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using FluentAssertions;
using System;
using System.Linq;
using Xunit;
namespace Wasmtime.Tests
{
public class FunctionThunkingFixture : ModuleFixture
{
protected override string ModuleFileName => "FunctionThunking.wat";
}
public class FunctionThunkingTests : IClassFixture<FunctionThunkingFixture>
{
const string THROW_MESSAGE = "Test error message for wasmtime dotnet unit tests.";
class MyHost : IHost
{
public Instance Instance { get; set; }
[Import("add", Module = "env")]
public int Add(int x, int y) => x + y;
[Import("do_throw", Module = "env")]
public void Throw() => throw new Exception(THROW_MESSAGE);
}
public FunctionThunkingTests(FunctionThunkingFixture fixture)
{
Fixture = fixture;
}
private FunctionThunkingFixture Fixture { get; }
[Fact]
public void ItBindsImportMethodsAndCallsThemCorrectly()
{
var host = new MyHost();
using var instance = Fixture.Module.Instantiate(host);
var add_func = instance.Externs.Functions.Where(f => f.Name == "add_wrapper").Single();
int invoke_add(int x, int y) => (int)add_func.Invoke(new object[] { x, y });
invoke_add(40, 2).Should().Be(42);
invoke_add(22, 5).Should().Be(27);
//Collect garbage to make sure delegate function pointers pasted to wasmtime are rooted.
GC.Collect();
GC.WaitForPendingFinalizers();
invoke_add(1970, 50).Should().Be(2020);
}
[Fact]
public void ItPropagatesExceptionsToCallersViaTraps()
{
var host = new MyHost();
using var instance = Fixture.Module.Instantiate(host);
var throw_func = instance.Externs.Functions.Where(f => f.Name == "do_throw_wrapper").Single();
Action action = () => throw_func.Invoke();
action
.Should()
.Throw<TrapException>()
.WithMessage(THROW_MESSAGE);
}
}
}