forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bytecodealliance#718 from AustinWise/austin/OtherF…
…ixes [dotnet] Some small fixes and unit tests
- Loading branch information
Showing
8 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
*.*~ | ||
.DS_Store | ||
|
||
.vs/ | ||
.vscode | ||
|
||
bin/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.29519.181 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wasmtime", "src\Wasmtime.csproj", "{5EB63C51-5286-4DDF-BF7F-4110CC6D80B8}" | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Wasmtime.Tests", "tests\Wasmtime.Tests.csproj", "{8A200114-1D0B-4F90-9F82-1FFE47C207DD}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{5EB63C51-5286-4DDF-BF7F-4110CC6D80B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{5EB63C51-5286-4DDF-BF7F-4110CC6D80B8}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{5EB63C51-5286-4DDF-BF7F-4110CC6D80B8}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{5EB63C51-5286-4DDF-BF7F-4110CC6D80B8}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{8A200114-1D0B-4F90-9F82-1FFE47C207DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{8A200114-1D0B-4F90-9F82-1FFE47C207DD}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{8A200114-1D0B-4F90-9F82-1FFE47C207DD}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{8A200114-1D0B-4F90-9F82-1FFE47C207DD}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {F5AC35E5-1373-49E6-97DC-68CB5E0369E0} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using FluentAssertions; | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace Wasmtime.Tests | ||
{ | ||
public class FunctionThunkingFixture : ModuleFixture | ||
{ | ||
protected override string ModuleFileName => "FunctionThunking.wasm"; | ||
} | ||
|
||
public class FunctionThunkingTests : IClassFixture<FunctionThunkingFixture> | ||
{ | ||
const string THROW_MESSAGE = "Test error messages for wasmtime dotnet bidnings 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 ItPropegatesExceptionsToCallersViaTraps() | ||
{ | ||
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); | ||
} | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
(module | ||
(type $FUNCSIG$iii (func (param i32 i32) (result i32))) | ||
(type $FUNCSIG$v (func)) | ||
(import "env" "add" (func $add (param i32 i32) (result i32))) | ||
(import "env" "do_throw" (func $do_throw)) | ||
(table 0 anyfunc) | ||
(memory $0 1) | ||
(export "memory" (memory $0)) | ||
(export "add_wrapper" (func $add_wrapper)) | ||
(export "do_throw_wrapper" (func $do_throw_wrapper)) | ||
(func $add_wrapper (; 2 ;) (param $0 i32) (param $1 i32) (result i32) | ||
(call $add | ||
(get_local $0) | ||
(get_local $1) | ||
) | ||
) | ||
(func $do_throw_wrapper (; 3 ;) | ||
(call $do_throw) | ||
) | ||
) |