Favor using non-braced using statement.

This commit is contained in:
Peter Huene
2020-02-24 18:44:37 -08:00
parent a6ec8f85a6
commit 4e1d2a2fc1
10 changed files with 328 additions and 343 deletions

View File

@@ -165,10 +165,8 @@ Alternatively, the `run` function could be invoked without using the runtime bin
```c# ```c#
... ...
using (var instance = module.Instantiate(new Host())) using var instance = module.Instantiate(new Host());
{ instance.Externs.Functions[0].Invoke();
instance.Externs.Functions[0].Invoke();
}
... ...
``` ```

View File

@@ -52,14 +52,12 @@ namespace Wasmtime.Bindings
{ {
var parameters = Interop.ToValueTypeVec(Import.Parameters); var parameters = Interop.ToValueTypeVec(Import.Parameters);
var results = Interop.ToValueTypeVec(Import.Results); var results = Interop.ToValueTypeVec(Import.Results);
using (var funcType = Interop.wasm_functype_new(ref parameters, ref results)) using var funcType = Interop.wasm_functype_new(ref parameters, ref results);
{ var callback = CreateCallback(store, host);
var callback = CreateCallback(store, host); var func = Interop.wasm_func_new(store.Handle, funcType, callback);
var func = Interop.wasm_func_new(store.Handle, funcType, callback); // Store the callback with the safe handle to keep the delegate GC reachable
// Store the callback with the safe handle to keep the delegate GC reachable func.Callback = callback;
func.Callback = callback; return func;
return func;
}
} }
} }

View File

@@ -59,14 +59,14 @@ namespace Wasmtime.Bindings
var valueTypeHandle = valueType.DangerousGetHandle(); var valueTypeHandle = valueType.DangerousGetHandle();
valueType.SetHandleAsInvalid(); valueType.SetHandleAsInvalid();
using (var globalType = Interop.wasm_globaltype_new( using var globalType = Interop.wasm_globaltype_new(
valueTypeHandle, valueTypeHandle,
Import.IsMutable ? Interop.wasm_mutability_t.WASM_VAR : Interop.wasm_mutability_t.WASM_CONST)) Import.IsMutable ? Interop.wasm_mutability_t.WASM_VAR : Interop.wasm_mutability_t.WASM_CONST
{ );
var handle = Interop.wasm_global_new(store.Handle, globalType, &v);
global.Handle = handle; var handle = Interop.wasm_global_new(store.Handle, globalType, &v);
return handle; global.Handle = handle;
} return handle;
} }
} }

View File

@@ -68,12 +68,11 @@ namespace Wasmtime.Bindings
Interop.wasm_limits_t limits = new Interop.wasm_limits_t(); Interop.wasm_limits_t limits = new Interop.wasm_limits_t();
limits.min = min; limits.min = min;
limits.max = max; limits.max = max;
using (var memoryType = Interop.wasm_memorytype_new(&limits))
{ using var memoryType = Interop.wasm_memorytype_new(&limits);
var handle = Interop.wasm_memory_new(store.Handle, memoryType); var handle = Interop.wasm_memory_new(store.Handle, memoryType);
memory.Handle = handle; memory.Handle = handle;
return handle; return handle;
}
} }
} }

View File

@@ -36,36 +36,34 @@ namespace Wasmtime.Tests
public void ItBindsImportMethodsAndCallsThemCorrectly() public void ItBindsImportMethodsAndCallsThemCorrectly()
{ {
var host = new MyHost(); var host = new MyHost();
using (var instance = Fixture.Module.Instantiate(host)) 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); var add_func = instance.Externs.Functions.Where(f => f.Name == "add_wrapper").Single();
invoke_add(22, 5).Should().Be(27); int invoke_add(int x, int y) => (int)add_func.Invoke(new object[] { x, y });
//Collect garbage to make sure delegate function pointers pasted to wasmtime are rooted. invoke_add(40, 2).Should().Be(42);
GC.Collect(); invoke_add(22, 5).Should().Be(27);
GC.WaitForPendingFinalizers();
invoke_add(1970, 50).Should().Be(2020); //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] [Fact]
public void ItPropagatesExceptionsToCallersViaTraps() public void ItPropagatesExceptionsToCallersViaTraps()
{ {
var host = new MyHost(); var host = new MyHost();
using (var instance = Fixture.Module.Instantiate(host)) 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 var throw_func = instance.Externs.Functions.Where(f => f.Name == "do_throw_wrapper").Single();
.Should() Action action = () => throw_func.Invoke();
.Throw<TrapException>()
.WithMessage(THROW_MESSAGE); action
} .Should()
.Throw<TrapException>()
.WithMessage(THROW_MESSAGE);
} }
} }
} }

View File

@@ -46,124 +46,123 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItCreatesExternsForTheGlobals() public void ItCreatesExternsForTheGlobals()
{ {
using (var instance = Fixture.Module.Instantiate(new Host())) using var instance = Fixture.Module.Instantiate(new Host());
{
dynamic dyn = instance;
var globals = instance.Externs.Globals;
globals.Count.Should().Be(8);
var i32 = globals[0]; dynamic dyn = instance;
i32.Name.Should().Be("global_i32"); var globals = instance.Externs.Globals;
i32.Kind.Should().Be(ValueKind.Int32); globals.Count.Should().Be(8);
i32.IsMutable.Should().Be(false);
i32.Value.Should().Be(0);
var i32Mut = globals[1]; var i32 = globals[0];
i32Mut.Name.Should().Be("global_i32_mut"); i32.Name.Should().Be("global_i32");
i32Mut.Kind.Should().Be(ValueKind.Int32); i32.Kind.Should().Be(ValueKind.Int32);
i32Mut.IsMutable.Should().Be(true); i32.IsMutable.Should().Be(false);
i32Mut.Value.Should().Be(1); i32.Value.Should().Be(0);
i32Mut.Value = 11;
i32Mut.Value.Should().Be(11);
dyn.global_i32_mut = 12;
((int)dyn.global_i32_mut).Should().Be(12);
i32Mut.Value.Should().Be(12);
var i64 = globals[2]; var i32Mut = globals[1];
i64.Name.Should().Be("global_i64"); i32Mut.Name.Should().Be("global_i32_mut");
i64.Kind.Should().Be(ValueKind.Int64); i32Mut.Kind.Should().Be(ValueKind.Int32);
i64.IsMutable.Should().Be(false); i32Mut.IsMutable.Should().Be(true);
i64.Value.Should().Be(2); i32Mut.Value.Should().Be(1);
i32Mut.Value = 11;
i32Mut.Value.Should().Be(11);
dyn.global_i32_mut = 12;
((int)dyn.global_i32_mut).Should().Be(12);
i32Mut.Value.Should().Be(12);
var i64Mut = globals[3]; var i64 = globals[2];
i64Mut.Name.Should().Be("global_i64_mut"); i64.Name.Should().Be("global_i64");
i64Mut.Kind.Should().Be(ValueKind.Int64); i64.Kind.Should().Be(ValueKind.Int64);
i64Mut.IsMutable.Should().Be(true); i64.IsMutable.Should().Be(false);
i64Mut.Value.Should().Be(3); i64.Value.Should().Be(2);
i64Mut.Value = 13;
i64Mut.Value.Should().Be(13);
dyn.global_i64_mut = 14;
((long)dyn.global_i64_mut).Should().Be(14);
i64Mut.Value.Should().Be(14);
var f32 = globals[4]; var i64Mut = globals[3];
f32.Name.Should().Be("global_f32"); i64Mut.Name.Should().Be("global_i64_mut");
f32.Kind.Should().Be(ValueKind.Float32); i64Mut.Kind.Should().Be(ValueKind.Int64);
f32.IsMutable.Should().Be(false); i64Mut.IsMutable.Should().Be(true);
f32.Value.Should().Be(4); i64Mut.Value.Should().Be(3);
i64Mut.Value = 13;
i64Mut.Value.Should().Be(13);
dyn.global_i64_mut = 14;
((long)dyn.global_i64_mut).Should().Be(14);
i64Mut.Value.Should().Be(14);
var f32Mut = globals[5]; var f32 = globals[4];
f32Mut.Name.Should().Be("global_f32_mut"); f32.Name.Should().Be("global_f32");
f32Mut.Kind.Should().Be(ValueKind.Float32); f32.Kind.Should().Be(ValueKind.Float32);
f32Mut.IsMutable.Should().Be(true); f32.IsMutable.Should().Be(false);
f32Mut.Value.Should().Be(5); f32.Value.Should().Be(4);
f32Mut.Value = 15;
f32Mut.Value.Should().Be(15);
dyn.global_f32_mut = 16;
((float)dyn.global_f32_mut).Should().Be(16);
f32Mut.Value.Should().Be(16);
var f64 = globals[6]; var f32Mut = globals[5];
f64.Name.Should().Be("global_f64"); f32Mut.Name.Should().Be("global_f32_mut");
f64.Kind.Should().Be(ValueKind.Float64); f32Mut.Kind.Should().Be(ValueKind.Float32);
f64.IsMutable.Should().Be(false); f32Mut.IsMutable.Should().Be(true);
f64.Value.Should().Be(6); f32Mut.Value.Should().Be(5);
f32Mut.Value = 15;
f32Mut.Value.Should().Be(15);
dyn.global_f32_mut = 16;
((float)dyn.global_f32_mut).Should().Be(16);
f32Mut.Value.Should().Be(16);
var f64Mut = globals[7]; var f64 = globals[6];
f64Mut.Name.Should().Be("global_f64_mut"); f64.Name.Should().Be("global_f64");
f64Mut.Kind.Should().Be(ValueKind.Float64); f64.Kind.Should().Be(ValueKind.Float64);
f64Mut.IsMutable.Should().Be(true); f64.IsMutable.Should().Be(false);
f64Mut.Value.Should().Be(7); f64.Value.Should().Be(6);
f64Mut.Value = 17;
f64Mut.Value.Should().Be(17);
dyn.global_f64_mut = 17;
((double)dyn.global_f64_mut).Should().Be(17);
f64Mut.Value.Should().Be(17);
Action action = () => i32.Value = 0; var f64Mut = globals[7];
action f64Mut.Name.Should().Be("global_f64_mut");
.Should() f64Mut.Kind.Should().Be(ValueKind.Float64);
.Throw<InvalidOperationException>() f64Mut.IsMutable.Should().Be(true);
.WithMessage("The value of global 'global_i32' cannot be modified."); f64Mut.Value.Should().Be(7);
action = () => dyn.global_i32 = 0; f64Mut.Value = 17;
action f64Mut.Value.Should().Be(17);
.Should() dyn.global_f64_mut = 17;
.Throw<InvalidOperationException>() ((double)dyn.global_f64_mut).Should().Be(17);
.WithMessage("The value of global 'global_i32' cannot be modified."); f64Mut.Value.Should().Be(17);
action = () => i64.Value = 0; Action action = () => i32.Value = 0;
action action
.Should() .Should()
.Throw<InvalidOperationException>() .Throw<InvalidOperationException>()
.WithMessage("The value of global 'global_i64' cannot be modified."); .WithMessage("The value of global 'global_i32' cannot be modified.");
action = () => dyn.global_i64 = 0; action = () => dyn.global_i32 = 0;
action action
.Should() .Should()
.Throw<InvalidOperationException>() .Throw<InvalidOperationException>()
.WithMessage("The value of global 'global_i64' cannot be modified."); .WithMessage("The value of global 'global_i32' cannot be modified.");
action = () => f32.Value = 0; action = () => i64.Value = 0;
action action
.Should() .Should()
.Throw<InvalidOperationException>() .Throw<InvalidOperationException>()
.WithMessage("The value of global 'global_f32' cannot be modified."); .WithMessage("The value of global 'global_i64' cannot be modified.");
action = () => dyn.global_f32 = 0; action = () => dyn.global_i64 = 0;
action action
.Should() .Should()
.Throw<InvalidOperationException>() .Throw<InvalidOperationException>()
.WithMessage("The value of global 'global_f32' cannot be modified."); .WithMessage("The value of global 'global_i64' cannot be modified.");
action = () => f64.Value = 0; action = () => f32.Value = 0;
action action
.Should() .Should()
.Throw<InvalidOperationException>() .Throw<InvalidOperationException>()
.WithMessage("The value of global 'global_f64' cannot be modified."); .WithMessage("The value of global 'global_f32' cannot be modified.");
action = () => dyn.global_f64 = 0; action = () => dyn.global_f32 = 0;
action action
.Should() .Should()
.Throw<InvalidOperationException>() .Throw<InvalidOperationException>()
.WithMessage("The value of global 'global_f64' cannot be modified."); .WithMessage("The value of global 'global_f32' cannot be modified.");
}
action = () => f64.Value = 0;
action
.Should()
.Throw<InvalidOperationException>()
.WithMessage("The value of global 'global_f64' cannot be modified.");
action = () => dyn.global_f64 = 0;
action
.Should()
.Throw<InvalidOperationException>()
.WithMessage("The value of global 'global_f64' cannot be modified.");
} }
public static IEnumerable<object[]> GetGlobalExports() public static IEnumerable<object[]> GetGlobalExports()

View File

@@ -118,7 +118,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWithMissingImport() public void ItFailsToInstantiateWithMissingImport()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new NoImportsHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new NoImportsHost()); };
action action
.Should() .Should()
@@ -129,7 +129,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWithStaticField() public void ItFailsToInstantiateWithStaticField()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new GlobalIsStaticHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new GlobalIsStaticHost()); };
action action
.Should() .Should()
@@ -140,7 +140,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWithNonReadOnlyField() public void ItFailsToInstantiateWithNonReadOnlyField()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new GlobalIsNotReadOnlyHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new GlobalIsNotReadOnlyHost()); };
action action
.Should() .Should()
@@ -151,7 +151,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWithInvalidType() public void ItFailsToInstantiateWithInvalidType()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new NotAGlobalHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new NotAGlobalHost()); };
action action
.Should() .Should()
@@ -162,7 +162,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWithInvalidGlobalType() public void ItFailsToInstantiateWithInvalidGlobalType()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new NotAValidGlobalTypeHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new NotAValidGlobalTypeHost()); };
action action
.Should() .Should()
@@ -173,7 +173,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWithGlobalTypeMismatch() public void ItFailsToInstantiateWithGlobalTypeMismatch()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new TypeMismatchHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new TypeMismatchHost()); };
action action
.Should() .Should()
@@ -184,7 +184,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWhenGlobalIsNotMut() public void ItFailsToInstantiateWhenGlobalIsNotMut()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new NotMutHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new NotMutHost()); };
action action
.Should() .Should()
@@ -195,7 +195,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWhenGlobalIsMut() public void ItFailsToInstantiateWhenGlobalIsMut()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new MutHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new MutHost()); };
action action
.Should() .Should()
@@ -207,53 +207,52 @@ namespace Wasmtime.Tests
public void ItBindsTheGlobalsCorrectly() public void ItBindsTheGlobalsCorrectly()
{ {
var host = new ValidHost(); var host = new ValidHost();
using (dynamic instance = Fixture.Module.Instantiate(host)) using dynamic instance = Fixture.Module.Instantiate(host);
{
host.Int32Mut.Value.Should().Be(0);
((int)instance.get_global_i32_mut()).Should().Be(0);
host.Int32.Value.Should().Be(1);
((int)instance.get_global_i32()).Should().Be(1);
host.Int64Mut.Value.Should().Be(2);
((long)instance.get_global_i64_mut()).Should().Be(2);
host.Int64.Value.Should().Be(3);
((long)instance.get_global_i64()).Should().Be(3);
host.Float32Mut.Value.Should().Be(4);
((float)instance.get_global_f32_mut()).Should().Be(4);
host.Float32.Value.Should().Be(5);
((float)instance.get_global_f32()).Should().Be(5);
host.Float64Mut.Value.Should().Be(6);
((double)instance.get_global_f64_mut()).Should().Be(6);
host.Float64.Value.Should().Be(7);
((double)instance.get_global_f64()).Should().Be(7);
host.Int32Mut.Value = 10; host.Int32Mut.Value.Should().Be(0);
host.Int32Mut.Value.Should().Be(10); ((int)instance.get_global_i32_mut()).Should().Be(0);
((int)instance.get_global_i32_mut()).Should().Be(10); host.Int32.Value.Should().Be(1);
instance.set_global_i32_mut(11); ((int)instance.get_global_i32()).Should().Be(1);
host.Int32Mut.Value.Should().Be(11); host.Int64Mut.Value.Should().Be(2);
((int)instance.get_global_i32_mut()).Should().Be(11); ((long)instance.get_global_i64_mut()).Should().Be(2);
host.Int64.Value.Should().Be(3);
((long)instance.get_global_i64()).Should().Be(3);
host.Float32Mut.Value.Should().Be(4);
((float)instance.get_global_f32_mut()).Should().Be(4);
host.Float32.Value.Should().Be(5);
((float)instance.get_global_f32()).Should().Be(5);
host.Float64Mut.Value.Should().Be(6);
((double)instance.get_global_f64_mut()).Should().Be(6);
host.Float64.Value.Should().Be(7);
((double)instance.get_global_f64()).Should().Be(7);
host.Int64Mut.Value = 12; host.Int32Mut.Value = 10;
host.Int64Mut.Value.Should().Be(12); host.Int32Mut.Value.Should().Be(10);
((long)instance.get_global_i64_mut()).Should().Be(12); ((int)instance.get_global_i32_mut()).Should().Be(10);
instance.set_global_i64_mut(13); instance.set_global_i32_mut(11);
host.Int64Mut.Value.Should().Be(13); host.Int32Mut.Value.Should().Be(11);
((long)instance.get_global_i64_mut()).Should().Be(13); ((int)instance.get_global_i32_mut()).Should().Be(11);
host.Float32Mut.Value = 14; host.Int64Mut.Value = 12;
host.Float32Mut.Value.Should().Be(14); host.Int64Mut.Value.Should().Be(12);
((float)instance.get_global_f32_mut()).Should().Be(14); ((long)instance.get_global_i64_mut()).Should().Be(12);
instance.set_global_f32_mut(15); instance.set_global_i64_mut(13);
host.Float32Mut.Value.Should().Be(15); host.Int64Mut.Value.Should().Be(13);
((float)instance.get_global_f32_mut()).Should().Be(15); ((long)instance.get_global_i64_mut()).Should().Be(13);
host.Float64Mut.Value = 16; host.Float32Mut.Value = 14;
host.Float64Mut.Value.Should().Be(16); host.Float32Mut.Value.Should().Be(14);
((double)instance.get_global_f64_mut()).Should().Be(16); ((float)instance.get_global_f32_mut()).Should().Be(14);
instance.set_global_f64_mut(17); instance.set_global_f32_mut(15);
host.Float64Mut.Value.Should().Be(17); host.Float32Mut.Value.Should().Be(15);
((double)instance.get_global_f64_mut()).Should().Be(17); ((float)instance.get_global_f32_mut()).Should().Be(15);
}
host.Float64Mut.Value = 16;
host.Float64Mut.Value.Should().Be(16);
((double)instance.get_global_f64_mut()).Should().Be(16);
instance.set_global_f64_mut(17);
host.Float64Mut.Value.Should().Be(17);
((double)instance.get_global_f64_mut()).Should().Be(17);
} }
} }
} }

View File

@@ -45,43 +45,42 @@ namespace Wasmtime.Tests
public void ItCreatesExternsForTheMemories() public void ItCreatesExternsForTheMemories()
{ {
var host = new Host(); var host = new Host();
using (var instance = Fixture.Module.Instantiate(host)) using var instance = Fixture.Module.Instantiate(host);
{
instance.Externs.Memories.Count.Should().Be(1);
var memory = instance.Externs.Memories[0]; instance.Externs.Memories.Count.Should().Be(1);
memory.ReadString(0, 11).Should().Be("Hello World");
int written = memory.WriteString(0, "WebAssembly Rocks!");
memory.ReadString(0, written).Should().Be("WebAssembly Rocks!");
memory.ReadByte(20).Should().Be(1); var memory = instance.Externs.Memories[0];
memory.WriteByte(20, 11); memory.ReadString(0, 11).Should().Be("Hello World");
memory.ReadByte(20).Should().Be(11); int written = memory.WriteString(0, "WebAssembly Rocks!");
memory.ReadString(0, written).Should().Be("WebAssembly Rocks!");
memory.ReadInt16(21).Should().Be(2); memory.ReadByte(20).Should().Be(1);
memory.WriteInt16(21, 12); memory.WriteByte(20, 11);
memory.ReadInt16(21).Should().Be(12); memory.ReadByte(20).Should().Be(11);
memory.ReadInt32(23).Should().Be(3); memory.ReadInt16(21).Should().Be(2);
memory.WriteInt32(23, 13); memory.WriteInt16(21, 12);
memory.ReadInt32(23).Should().Be(13); memory.ReadInt16(21).Should().Be(12);
memory.ReadInt64(27).Should().Be(4); memory.ReadInt32(23).Should().Be(3);
memory.WriteInt64(27, 14); memory.WriteInt32(23, 13);
memory.ReadInt64(27).Should().Be(14); memory.ReadInt32(23).Should().Be(13);
memory.ReadSingle(35).Should().Be(5); memory.ReadInt64(27).Should().Be(4);
memory.WriteSingle(35, 15); memory.WriteInt64(27, 14);
memory.ReadSingle(35).Should().Be(15); memory.ReadInt64(27).Should().Be(14);
memory.ReadDouble(39).Should().Be(6); memory.ReadSingle(35).Should().Be(5);
memory.WriteDouble(39, 16); memory.WriteSingle(35, 15);
memory.ReadDouble(39).Should().Be(16); memory.ReadSingle(35).Should().Be(15);
memory.ReadIntPtr(48).Should().Be((IntPtr)7); memory.ReadDouble(39).Should().Be(6);
memory.WriteIntPtr(48, (IntPtr)17); memory.WriteDouble(39, 16);
memory.ReadIntPtr(48).Should().Be((IntPtr)17); memory.ReadDouble(39).Should().Be(16);
}
memory.ReadIntPtr(48).Should().Be((IntPtr)7);
memory.WriteIntPtr(48, (IntPtr)17);
memory.ReadIntPtr(48).Should().Be((IntPtr)17);
} }
public static IEnumerable<object[]> GetMemoryExports() public static IEnumerable<object[]> GetMemoryExports()

View File

@@ -74,7 +74,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWithMissingImport() public void ItFailsToInstantiateWithMissingImport()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new MissingImportsHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new MissingImportsHost()); };
action action
.Should() .Should()
@@ -85,7 +85,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWithStaticField() public void ItFailsToInstantiateWithStaticField()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new MemoryIsStaticHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new MemoryIsStaticHost()); };
action action
.Should() .Should()
@@ -96,7 +96,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWithNonReadOnlyField() public void ItFailsToInstantiateWithNonReadOnlyField()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new MemoryIsNotReadOnlyHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new MemoryIsNotReadOnlyHost()); };
action action
.Should() .Should()
@@ -107,7 +107,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWithInvalidType() public void ItFailsToInstantiateWithInvalidType()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new NotAMemoryHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new NotAMemoryHost()); };
action action
.Should() .Should()
@@ -118,7 +118,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWhenMemoryHasInvalidMinimum() public void ItFailsToInstantiateWhenMemoryHasInvalidMinimum()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new InvalidMinimumHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new InvalidMinimumHost()); };
action action
.Should() .Should()
@@ -129,7 +129,7 @@ namespace Wasmtime.Tests
[Fact] [Fact]
public void ItFailsToInstantiateWhenMemoryHasInvalidMaximum() public void ItFailsToInstantiateWhenMemoryHasInvalidMaximum()
{ {
Action action = () => { using (var instance = Fixture.Module.Instantiate(new InvalidMaximumHost())) { } }; Action action = () => { using var instance = Fixture.Module.Instantiate(new InvalidMaximumHost()); };
action action
.Should() .Should()
@@ -141,47 +141,46 @@ namespace Wasmtime.Tests
public void ItBindsTheGlobalsCorrectly() public void ItBindsTheGlobalsCorrectly()
{ {
var host = new ValidHost(); var host = new ValidHost();
using (dynamic instance = Fixture.Module.Instantiate(host)) using dynamic instance = Fixture.Module.Instantiate(host);
{
host.Mem.ReadString(0, 11).Should().Be("Hello World");
int written = host.Mem.WriteString(0, "WebAssembly Rocks!");
host.Mem.ReadString(0, written).Should().Be("WebAssembly Rocks!");
host.Mem.ReadByte(20).Should().Be(1); host.Mem.ReadString(0, 11).Should().Be("Hello World");
host.Mem.WriteByte(20, 11); int written = host.Mem.WriteString(0, "WebAssembly Rocks!");
host.Mem.ReadByte(20).Should().Be(11); host.Mem.ReadString(0, written).Should().Be("WebAssembly Rocks!");
((byte)instance.ReadByte()).Should().Be(11);
host.Mem.ReadInt16(21).Should().Be(2); host.Mem.ReadByte(20).Should().Be(1);
host.Mem.WriteInt16(21, 12); host.Mem.WriteByte(20, 11);
host.Mem.ReadInt16(21).Should().Be(12); host.Mem.ReadByte(20).Should().Be(11);
((short)instance.ReadInt16()).Should().Be(12); ((byte)instance.ReadByte()).Should().Be(11);
host.Mem.ReadInt32(23).Should().Be(3); host.Mem.ReadInt16(21).Should().Be(2);
host.Mem.WriteInt32(23, 13); host.Mem.WriteInt16(21, 12);
host.Mem.ReadInt32(23).Should().Be(13); host.Mem.ReadInt16(21).Should().Be(12);
((int)instance.ReadInt32()).Should().Be(13); ((short)instance.ReadInt16()).Should().Be(12);
host.Mem.ReadInt64(27).Should().Be(4); host.Mem.ReadInt32(23).Should().Be(3);
host.Mem.WriteInt64(27, 14); host.Mem.WriteInt32(23, 13);
host.Mem.ReadInt64(27).Should().Be(14); host.Mem.ReadInt32(23).Should().Be(13);
((long)instance.ReadInt64()).Should().Be(14); ((int)instance.ReadInt32()).Should().Be(13);
host.Mem.ReadSingle(35).Should().Be(5); host.Mem.ReadInt64(27).Should().Be(4);
host.Mem.WriteSingle(35, 15); host.Mem.WriteInt64(27, 14);
host.Mem.ReadSingle(35).Should().Be(15); host.Mem.ReadInt64(27).Should().Be(14);
((float)instance.ReadFloat32()).Should().Be(15); ((long)instance.ReadInt64()).Should().Be(14);
host.Mem.ReadDouble(39).Should().Be(6); host.Mem.ReadSingle(35).Should().Be(5);
host.Mem.WriteDouble(39, 16); host.Mem.WriteSingle(35, 15);
host.Mem.ReadDouble(39).Should().Be(16); host.Mem.ReadSingle(35).Should().Be(15);
((double)instance.ReadFloat64()).Should().Be(16); ((float)instance.ReadFloat32()).Should().Be(15);
host.Mem.ReadIntPtr(48).Should().Be((IntPtr)7); host.Mem.ReadDouble(39).Should().Be(6);
host.Mem.WriteIntPtr(48, (IntPtr)17); host.Mem.WriteDouble(39, 16);
host.Mem.ReadIntPtr(48).Should().Be((IntPtr)17); host.Mem.ReadDouble(39).Should().Be(16);
((IntPtr)instance.ReadIntPtr()).Should().Be((IntPtr)17); ((double)instance.ReadFloat64()).Should().Be(16);
}
host.Mem.ReadIntPtr(48).Should().Be((IntPtr)7);
host.Mem.WriteIntPtr(48, (IntPtr)17);
host.Mem.ReadIntPtr(48).Should().Be((IntPtr)17);
((IntPtr)instance.ReadIntPtr()).Should().Be((IntPtr)17);
} }
} }
} }

View File

@@ -145,25 +145,23 @@ namespace Wasmtime.Tests
{ {
const string MESSAGE = "WASM IS VERY COOL"; const string MESSAGE = "WASM IS VERY COOL";
using (var file = new TempFile()) using var file = new TempFile();
{ File.WriteAllText(file.Path, MESSAGE);
File.WriteAllText(file.Path, MESSAGE);
var wasi = new WasiBuilder() var wasi = new WasiBuilder()
.WithStandardInput(file.Path) .WithStandardInput(file.Path)
.Build(Fixture.Module.Store); .Build(Fixture.Module.Store);
using var instance = Fixture.Module.Instantiate(wasi); using var instance = Fixture.Module.Instantiate(wasi);
dynamic inst = instance; dynamic inst = instance;
var memory = instance.Externs.Memories[0]; var memory = instance.Externs.Memories[0];
memory.WriteInt32(0, 8); memory.WriteInt32(0, 8);
memory.WriteInt32(4, MESSAGE.Length); memory.WriteInt32(4, MESSAGE.Length);
Assert.Equal(0, inst.call_fd_read(0, 0, 1, 32)); Assert.Equal(0, inst.call_fd_read(0, 0, 1, 32));
Assert.Equal(MESSAGE.Length, memory.ReadInt32(32)); Assert.Equal(MESSAGE.Length, memory.ReadInt32(32));
Assert.Equal(MESSAGE, memory.ReadString(8, MESSAGE.Length)); Assert.Equal(MESSAGE, memory.ReadString(8, MESSAGE.Length));
}
} }
[Theory] [Theory]
@@ -173,33 +171,32 @@ namespace Wasmtime.Tests
{ {
const string MESSAGE = "WASM IS VERY COOL"; const string MESSAGE = "WASM IS VERY COOL";
using (var file = new TempFile()) using var file = new TempFile();
var builder = new WasiBuilder();
if (fd == 1)
{ {
var builder = new WasiBuilder(); builder.WithStandardOutput(file.Path);
if (fd == 1)
{
builder.WithStandardOutput(file.Path);
}
else if (fd == 2)
{
builder.WithStandardError(file.Path);
}
var wasi = builder.Build(Fixture.Module.Store);
using var instance = Fixture.Module.Instantiate(wasi);
dynamic inst = instance;
var memory = instance.Externs.Memories[0];
memory.WriteInt32(0, 8);
memory.WriteInt32(4, MESSAGE.Length);
memory.WriteString(8, MESSAGE);
Assert.Equal(0, inst.call_fd_write(fd, 0, 1, 32));
Assert.Equal(MESSAGE.Length, memory.ReadInt32(32));
Assert.Equal(0, inst.call_fd_close(fd));
Assert.Equal(MESSAGE, File.ReadAllText(file.Path));
} }
else if (fd == 2)
{
builder.WithStandardError(file.Path);
}
var wasi = builder.Build(Fixture.Module.Store);
using var instance = Fixture.Module.Instantiate(wasi);
dynamic inst = instance;
var memory = instance.Externs.Memories[0];
memory.WriteInt32(0, 8);
memory.WriteInt32(4, MESSAGE.Length);
memory.WriteString(8, MESSAGE);
Assert.Equal(0, inst.call_fd_write(fd, 0, 1, 32));
Assert.Equal(MESSAGE.Length, memory.ReadInt32(32));
Assert.Equal(0, inst.call_fd_close(fd));
Assert.Equal(MESSAGE, File.ReadAllText(file.Path));
} }
[Fact] [Fact]
@@ -207,44 +204,43 @@ namespace Wasmtime.Tests
{ {
const string MESSAGE = "WASM IS VERY COOL"; const string MESSAGE = "WASM IS VERY COOL";
using (var file = new TempFile()) using var file = new TempFile();
{
var wasi = new WasiBuilder()
.WithPreopenedDirectory(Path.GetDirectoryName(file.Path), "/foo")
.Build(Fixture.Module.Store);
using var instance = Fixture.Module.Instantiate(wasi); var wasi = new WasiBuilder()
dynamic inst = instance; .WithPreopenedDirectory(Path.GetDirectoryName(file.Path), "/foo")
.Build(Fixture.Module.Store);
var memory = instance.Externs.Memories[0]; using var instance = Fixture.Module.Instantiate(wasi);
var fileName = Path.GetFileName(file.Path); dynamic inst = instance;
memory.WriteString(0, fileName);
Assert.Equal(0, inst.call_path_open( var memory = instance.Externs.Memories[0];
3, var fileName = Path.GetFileName(file.Path);
0, memory.WriteString(0, fileName);
0,
fileName.Length,
0,
0x40 /* RIGHTS_FD_WRITE */,
0,
0,
64
)
);
var fileFd = (int) memory.ReadInt32(64); Assert.Equal(0, inst.call_path_open(
Assert.True(fileFd > 3); 3,
0,
0,
fileName.Length,
0,
0x40 /* RIGHTS_FD_WRITE */,
0,
0,
64
)
);
memory.WriteInt32(0, 8); var fileFd = (int) memory.ReadInt32(64);
memory.WriteInt32(4, MESSAGE.Length); Assert.True(fileFd > 3);
memory.WriteString(8, MESSAGE);
Assert.Equal(0, inst.call_fd_write(fileFd, 0, 1, 64)); memory.WriteInt32(0, 8);
Assert.Equal(MESSAGE.Length, memory.ReadInt32(64)); memory.WriteInt32(4, MESSAGE.Length);
Assert.Equal(0, inst.call_fd_close(fileFd)); memory.WriteString(8, MESSAGE);
Assert.Equal(MESSAGE, File.ReadAllText(file.Path));
} Assert.Equal(0, inst.call_fd_write(fileFd, 0, 1, 64));
Assert.Equal(MESSAGE.Length, memory.ReadInt32(64));
Assert.Equal(0, inst.call_fd_close(fileFd));
Assert.Equal(MESSAGE, File.ReadAllText(file.Path));
} }
} }
} }