diff --git a/crates/misc/dotnet/docs/articles/intro.md b/crates/misc/dotnet/docs/articles/intro.md index f0e572adb3..510c924325 100644 --- a/crates/misc/dotnet/docs/articles/intro.md +++ b/crates/misc/dotnet/docs/articles/intro.md @@ -165,10 +165,8 @@ Alternatively, the `run` function could be invoked without using the runtime bin ```c# ... -using (var instance = module.Instantiate(new Host())) -{ - instance.Externs.Functions[0].Invoke(); -} +using var instance = module.Instantiate(new Host()); +instance.Externs.Functions[0].Invoke(); ... ``` diff --git a/crates/misc/dotnet/src/Bindings/FunctionBinding.cs b/crates/misc/dotnet/src/Bindings/FunctionBinding.cs index 6d574f2412..5d5d781750 100644 --- a/crates/misc/dotnet/src/Bindings/FunctionBinding.cs +++ b/crates/misc/dotnet/src/Bindings/FunctionBinding.cs @@ -52,14 +52,12 @@ namespace Wasmtime.Bindings { var parameters = Interop.ToValueTypeVec(Import.Parameters); var results = Interop.ToValueTypeVec(Import.Results); - using (var funcType = Interop.wasm_functype_new(ref parameters, ref results)) - { - var callback = CreateCallback(store, host); - var func = Interop.wasm_func_new(store.Handle, funcType, callback); - // Store the callback with the safe handle to keep the delegate GC reachable - func.Callback = callback; - return func; - } + using var funcType = Interop.wasm_functype_new(ref parameters, ref results); + var callback = CreateCallback(store, host); + var func = Interop.wasm_func_new(store.Handle, funcType, callback); + // Store the callback with the safe handle to keep the delegate GC reachable + func.Callback = callback; + return func; } } diff --git a/crates/misc/dotnet/src/Bindings/GlobalBinding.cs b/crates/misc/dotnet/src/Bindings/GlobalBinding.cs index 69df579868..da75e8148b 100644 --- a/crates/misc/dotnet/src/Bindings/GlobalBinding.cs +++ b/crates/misc/dotnet/src/Bindings/GlobalBinding.cs @@ -59,14 +59,14 @@ namespace Wasmtime.Bindings var valueTypeHandle = valueType.DangerousGetHandle(); valueType.SetHandleAsInvalid(); - using (var globalType = Interop.wasm_globaltype_new( + using var globalType = Interop.wasm_globaltype_new( valueTypeHandle, - 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; - return handle; - } + 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; + return handle; } } diff --git a/crates/misc/dotnet/src/Bindings/MemoryBinding.cs b/crates/misc/dotnet/src/Bindings/MemoryBinding.cs index f2a10c7061..30e9f18cde 100644 --- a/crates/misc/dotnet/src/Bindings/MemoryBinding.cs +++ b/crates/misc/dotnet/src/Bindings/MemoryBinding.cs @@ -68,12 +68,11 @@ namespace Wasmtime.Bindings Interop.wasm_limits_t limits = new Interop.wasm_limits_t(); limits.min = min; limits.max = max; - using (var memoryType = Interop.wasm_memorytype_new(&limits)) - { - var handle = Interop.wasm_memory_new(store.Handle, memoryType); - memory.Handle = handle; - return handle; - } + + using var memoryType = Interop.wasm_memorytype_new(&limits); + var handle = Interop.wasm_memory_new(store.Handle, memoryType); + memory.Handle = handle; + return handle; } } diff --git a/crates/misc/dotnet/tests/FunctionThunkingTests.cs b/crates/misc/dotnet/tests/FunctionThunkingTests.cs index f05afbc1df..97eba2a299 100644 --- a/crates/misc/dotnet/tests/FunctionThunkingTests.cs +++ b/crates/misc/dotnet/tests/FunctionThunkingTests.cs @@ -36,36 +36,34 @@ namespace Wasmtime.Tests 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 }); + using var instance = Fixture.Module.Instantiate(host); - invoke_add(40, 2).Should().Be(42); - invoke_add(22, 5).Should().Be(27); + 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 }); - //Collect garbage to make sure delegate function pointers pasted to wasmtime are rooted. - GC.Collect(); - GC.WaitForPendingFinalizers(); + invoke_add(40, 2).Should().Be(42); + invoke_add(22, 5).Should().Be(27); - 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] 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(); + using var instance = Fixture.Module.Instantiate(host); - action - .Should() - .Throw() - .WithMessage(THROW_MESSAGE); - } + var throw_func = instance.Externs.Functions.Where(f => f.Name == "do_throw_wrapper").Single(); + Action action = () => throw_func.Invoke(); + + action + .Should() + .Throw() + .WithMessage(THROW_MESSAGE); } } } diff --git a/crates/misc/dotnet/tests/GlobalExportsTests.cs b/crates/misc/dotnet/tests/GlobalExportsTests.cs index 63b6250679..ca143f7286 100644 --- a/crates/misc/dotnet/tests/GlobalExportsTests.cs +++ b/crates/misc/dotnet/tests/GlobalExportsTests.cs @@ -46,124 +46,123 @@ namespace Wasmtime.Tests [Fact] public void ItCreatesExternsForTheGlobals() { - using (var instance = Fixture.Module.Instantiate(new Host())) - { - dynamic dyn = instance; - var globals = instance.Externs.Globals; - globals.Count.Should().Be(8); + using var instance = Fixture.Module.Instantiate(new Host()); - var i32 = globals[0]; - i32.Name.Should().Be("global_i32"); - i32.Kind.Should().Be(ValueKind.Int32); - i32.IsMutable.Should().Be(false); - i32.Value.Should().Be(0); + dynamic dyn = instance; + var globals = instance.Externs.Globals; + globals.Count.Should().Be(8); - var i32Mut = globals[1]; - i32Mut.Name.Should().Be("global_i32_mut"); - i32Mut.Kind.Should().Be(ValueKind.Int32); - i32Mut.IsMutable.Should().Be(true); - 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 i32 = globals[0]; + i32.Name.Should().Be("global_i32"); + i32.Kind.Should().Be(ValueKind.Int32); + i32.IsMutable.Should().Be(false); + i32.Value.Should().Be(0); - var i64 = globals[2]; - i64.Name.Should().Be("global_i64"); - i64.Kind.Should().Be(ValueKind.Int64); - i64.IsMutable.Should().Be(false); - i64.Value.Should().Be(2); + var i32Mut = globals[1]; + i32Mut.Name.Should().Be("global_i32_mut"); + i32Mut.Kind.Should().Be(ValueKind.Int32); + i32Mut.IsMutable.Should().Be(true); + 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]; - i64Mut.Name.Should().Be("global_i64_mut"); - i64Mut.Kind.Should().Be(ValueKind.Int64); - i64Mut.IsMutable.Should().Be(true); - 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 i64 = globals[2]; + i64.Name.Should().Be("global_i64"); + i64.Kind.Should().Be(ValueKind.Int64); + i64.IsMutable.Should().Be(false); + i64.Value.Should().Be(2); - var f32 = globals[4]; - f32.Name.Should().Be("global_f32"); - f32.Kind.Should().Be(ValueKind.Float32); - f32.IsMutable.Should().Be(false); - f32.Value.Should().Be(4); + var i64Mut = globals[3]; + i64Mut.Name.Should().Be("global_i64_mut"); + i64Mut.Kind.Should().Be(ValueKind.Int64); + i64Mut.IsMutable.Should().Be(true); + 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]; - f32Mut.Name.Should().Be("global_f32_mut"); - f32Mut.Kind.Should().Be(ValueKind.Float32); - f32Mut.IsMutable.Should().Be(true); - 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 f32 = globals[4]; + f32.Name.Should().Be("global_f32"); + f32.Kind.Should().Be(ValueKind.Float32); + f32.IsMutable.Should().Be(false); + f32.Value.Should().Be(4); - var f64 = globals[6]; - f64.Name.Should().Be("global_f64"); - f64.Kind.Should().Be(ValueKind.Float64); - f64.IsMutable.Should().Be(false); - f64.Value.Should().Be(6); + var f32Mut = globals[5]; + f32Mut.Name.Should().Be("global_f32_mut"); + f32Mut.Kind.Should().Be(ValueKind.Float32); + f32Mut.IsMutable.Should().Be(true); + 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]; - f64Mut.Name.Should().Be("global_f64_mut"); - f64Mut.Kind.Should().Be(ValueKind.Float64); - f64Mut.IsMutable.Should().Be(true); - f64Mut.Value.Should().Be(7); - 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); + var f64 = globals[6]; + f64.Name.Should().Be("global_f64"); + f64.Kind.Should().Be(ValueKind.Float64); + f64.IsMutable.Should().Be(false); + f64.Value.Should().Be(6); - Action action = () => i32.Value = 0; - action - .Should() - .Throw() - .WithMessage("The value of global 'global_i32' cannot be modified."); - action = () => dyn.global_i32 = 0; - action - .Should() - .Throw() - .WithMessage("The value of global 'global_i32' cannot be modified."); + var f64Mut = globals[7]; + f64Mut.Name.Should().Be("global_f64_mut"); + f64Mut.Kind.Should().Be(ValueKind.Float64); + f64Mut.IsMutable.Should().Be(true); + f64Mut.Value.Should().Be(7); + 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 = () => i64.Value = 0; - action - .Should() - .Throw() - .WithMessage("The value of global 'global_i64' cannot be modified."); - action = () => dyn.global_i64 = 0; - action - .Should() - .Throw() - .WithMessage("The value of global 'global_i64' cannot be modified."); + Action action = () => i32.Value = 0; + action + .Should() + .Throw() + .WithMessage("The value of global 'global_i32' cannot be modified."); + action = () => dyn.global_i32 = 0; + action + .Should() + .Throw() + .WithMessage("The value of global 'global_i32' cannot be modified."); - action = () => f32.Value = 0; - action - .Should() - .Throw() - .WithMessage("The value of global 'global_f32' cannot be modified."); - action = () => dyn.global_f32 = 0; - action - .Should() - .Throw() - .WithMessage("The value of global 'global_f32' cannot be modified."); + action = () => i64.Value = 0; + action + .Should() + .Throw() + .WithMessage("The value of global 'global_i64' cannot be modified."); + action = () => dyn.global_i64 = 0; + action + .Should() + .Throw() + .WithMessage("The value of global 'global_i64' cannot be modified."); - action = () => f64.Value = 0; - action - .Should() - .Throw() - .WithMessage("The value of global 'global_f64' cannot be modified."); - action = () => dyn.global_f64 = 0; - action - .Should() - .Throw() - .WithMessage("The value of global 'global_f64' cannot be modified."); - } + action = () => f32.Value = 0; + action + .Should() + .Throw() + .WithMessage("The value of global 'global_f32' cannot be modified."); + action = () => dyn.global_f32 = 0; + action + .Should() + .Throw() + .WithMessage("The value of global 'global_f32' cannot be modified."); + + action = () => f64.Value = 0; + action + .Should() + .Throw() + .WithMessage("The value of global 'global_f64' cannot be modified."); + action = () => dyn.global_f64 = 0; + action + .Should() + .Throw() + .WithMessage("The value of global 'global_f64' cannot be modified."); } public static IEnumerable GetGlobalExports() diff --git a/crates/misc/dotnet/tests/GlobalImportBindingTests.cs b/crates/misc/dotnet/tests/GlobalImportBindingTests.cs index f6b92d6e76..7969a90641 100644 --- a/crates/misc/dotnet/tests/GlobalImportBindingTests.cs +++ b/crates/misc/dotnet/tests/GlobalImportBindingTests.cs @@ -118,7 +118,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWithMissingImport() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new NoImportsHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new NoImportsHost()); }; action .Should() @@ -129,7 +129,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWithStaticField() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new GlobalIsStaticHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new GlobalIsStaticHost()); }; action .Should() @@ -140,7 +140,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWithNonReadOnlyField() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new GlobalIsNotReadOnlyHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new GlobalIsNotReadOnlyHost()); }; action .Should() @@ -151,7 +151,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWithInvalidType() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new NotAGlobalHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new NotAGlobalHost()); }; action .Should() @@ -162,7 +162,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWithInvalidGlobalType() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new NotAValidGlobalTypeHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new NotAValidGlobalTypeHost()); }; action .Should() @@ -173,7 +173,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWithGlobalTypeMismatch() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new TypeMismatchHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new TypeMismatchHost()); }; action .Should() @@ -184,7 +184,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWhenGlobalIsNotMut() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new NotMutHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new NotMutHost()); }; action .Should() @@ -195,7 +195,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWhenGlobalIsMut() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new MutHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new MutHost()); }; action .Should() @@ -207,53 +207,52 @@ namespace Wasmtime.Tests public void ItBindsTheGlobalsCorrectly() { var host = new ValidHost(); - 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); + using dynamic instance = Fixture.Module.Instantiate(host); - host.Int32Mut.Value = 10; - host.Int32Mut.Value.Should().Be(10); - ((int)instance.get_global_i32_mut()).Should().Be(10); - instance.set_global_i32_mut(11); - host.Int32Mut.Value.Should().Be(11); - ((int)instance.get_global_i32_mut()).Should().Be(11); + 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.Int64Mut.Value = 12; - host.Int64Mut.Value.Should().Be(12); - ((long)instance.get_global_i64_mut()).Should().Be(12); - instance.set_global_i64_mut(13); - host.Int64Mut.Value.Should().Be(13); - ((long)instance.get_global_i64_mut()).Should().Be(13); + host.Int32Mut.Value = 10; + host.Int32Mut.Value.Should().Be(10); + ((int)instance.get_global_i32_mut()).Should().Be(10); + instance.set_global_i32_mut(11); + host.Int32Mut.Value.Should().Be(11); + ((int)instance.get_global_i32_mut()).Should().Be(11); - host.Float32Mut.Value = 14; - host.Float32Mut.Value.Should().Be(14); - ((float)instance.get_global_f32_mut()).Should().Be(14); - instance.set_global_f32_mut(15); - host.Float32Mut.Value.Should().Be(15); - ((float)instance.get_global_f32_mut()).Should().Be(15); + host.Int64Mut.Value = 12; + host.Int64Mut.Value.Should().Be(12); + ((long)instance.get_global_i64_mut()).Should().Be(12); + instance.set_global_i64_mut(13); + host.Int64Mut.Value.Should().Be(13); + ((long)instance.get_global_i64_mut()).Should().Be(13); - 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); - } + host.Float32Mut.Value = 14; + host.Float32Mut.Value.Should().Be(14); + ((float)instance.get_global_f32_mut()).Should().Be(14); + instance.set_global_f32_mut(15); + host.Float32Mut.Value.Should().Be(15); + ((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); } } } diff --git a/crates/misc/dotnet/tests/MemoryExportsTests.cs b/crates/misc/dotnet/tests/MemoryExportsTests.cs index 1a91ea18a3..08242e75c7 100644 --- a/crates/misc/dotnet/tests/MemoryExportsTests.cs +++ b/crates/misc/dotnet/tests/MemoryExportsTests.cs @@ -45,43 +45,42 @@ namespace Wasmtime.Tests public void ItCreatesExternsForTheMemories() { var host = new Host(); - using (var instance = Fixture.Module.Instantiate(host)) - { - instance.Externs.Memories.Count.Should().Be(1); + using var instance = Fixture.Module.Instantiate(host); - var memory = instance.Externs.Memories[0]; - memory.ReadString(0, 11).Should().Be("Hello World"); - int written = memory.WriteString(0, "WebAssembly Rocks!"); - memory.ReadString(0, written).Should().Be("WebAssembly Rocks!"); + instance.Externs.Memories.Count.Should().Be(1); - memory.ReadByte(20).Should().Be(1); - memory.WriteByte(20, 11); - memory.ReadByte(20).Should().Be(11); + var memory = instance.Externs.Memories[0]; + memory.ReadString(0, 11).Should().Be("Hello World"); + int written = memory.WriteString(0, "WebAssembly Rocks!"); + memory.ReadString(0, written).Should().Be("WebAssembly Rocks!"); - memory.ReadInt16(21).Should().Be(2); - memory.WriteInt16(21, 12); - memory.ReadInt16(21).Should().Be(12); + memory.ReadByte(20).Should().Be(1); + memory.WriteByte(20, 11); + memory.ReadByte(20).Should().Be(11); - memory.ReadInt32(23).Should().Be(3); - memory.WriteInt32(23, 13); - memory.ReadInt32(23).Should().Be(13); + memory.ReadInt16(21).Should().Be(2); + memory.WriteInt16(21, 12); + memory.ReadInt16(21).Should().Be(12); - memory.ReadInt64(27).Should().Be(4); - memory.WriteInt64(27, 14); - memory.ReadInt64(27).Should().Be(14); + memory.ReadInt32(23).Should().Be(3); + memory.WriteInt32(23, 13); + memory.ReadInt32(23).Should().Be(13); - memory.ReadSingle(35).Should().Be(5); - memory.WriteSingle(35, 15); - memory.ReadSingle(35).Should().Be(15); + memory.ReadInt64(27).Should().Be(4); + memory.WriteInt64(27, 14); + memory.ReadInt64(27).Should().Be(14); - memory.ReadDouble(39).Should().Be(6); - memory.WriteDouble(39, 16); - memory.ReadDouble(39).Should().Be(16); + memory.ReadSingle(35).Should().Be(5); + memory.WriteSingle(35, 15); + memory.ReadSingle(35).Should().Be(15); - memory.ReadIntPtr(48).Should().Be((IntPtr)7); - memory.WriteIntPtr(48, (IntPtr)17); - memory.ReadIntPtr(48).Should().Be((IntPtr)17); - } + memory.ReadDouble(39).Should().Be(6); + memory.WriteDouble(39, 16); + 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 GetMemoryExports() diff --git a/crates/misc/dotnet/tests/MemoryImportBindingTests.cs b/crates/misc/dotnet/tests/MemoryImportBindingTests.cs index cbe9efaae9..cab85e2434 100644 --- a/crates/misc/dotnet/tests/MemoryImportBindingTests.cs +++ b/crates/misc/dotnet/tests/MemoryImportBindingTests.cs @@ -74,7 +74,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWithMissingImport() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new MissingImportsHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new MissingImportsHost()); }; action .Should() @@ -85,7 +85,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWithStaticField() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new MemoryIsStaticHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new MemoryIsStaticHost()); }; action .Should() @@ -96,7 +96,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWithNonReadOnlyField() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new MemoryIsNotReadOnlyHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new MemoryIsNotReadOnlyHost()); }; action .Should() @@ -107,7 +107,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWithInvalidType() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new NotAMemoryHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new NotAMemoryHost()); }; action .Should() @@ -118,7 +118,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWhenMemoryHasInvalidMinimum() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new InvalidMinimumHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new InvalidMinimumHost()); }; action .Should() @@ -129,7 +129,7 @@ namespace Wasmtime.Tests [Fact] public void ItFailsToInstantiateWhenMemoryHasInvalidMaximum() { - Action action = () => { using (var instance = Fixture.Module.Instantiate(new InvalidMaximumHost())) { } }; + Action action = () => { using var instance = Fixture.Module.Instantiate(new InvalidMaximumHost()); }; action .Should() @@ -141,47 +141,46 @@ namespace Wasmtime.Tests public void ItBindsTheGlobalsCorrectly() { var host = new ValidHost(); - 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!"); + using dynamic instance = Fixture.Module.Instantiate(host); - host.Mem.ReadByte(20).Should().Be(1); - host.Mem.WriteByte(20, 11); - host.Mem.ReadByte(20).Should().Be(11); - ((byte)instance.ReadByte()).Should().Be(11); + 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.ReadInt16(21).Should().Be(2); - host.Mem.WriteInt16(21, 12); - host.Mem.ReadInt16(21).Should().Be(12); - ((short)instance.ReadInt16()).Should().Be(12); + host.Mem.ReadByte(20).Should().Be(1); + host.Mem.WriteByte(20, 11); + host.Mem.ReadByte(20).Should().Be(11); + ((byte)instance.ReadByte()).Should().Be(11); - host.Mem.ReadInt32(23).Should().Be(3); - host.Mem.WriteInt32(23, 13); - host.Mem.ReadInt32(23).Should().Be(13); - ((int)instance.ReadInt32()).Should().Be(13); + host.Mem.ReadInt16(21).Should().Be(2); + host.Mem.WriteInt16(21, 12); + host.Mem.ReadInt16(21).Should().Be(12); + ((short)instance.ReadInt16()).Should().Be(12); - host.Mem.ReadInt64(27).Should().Be(4); - host.Mem.WriteInt64(27, 14); - host.Mem.ReadInt64(27).Should().Be(14); - ((long)instance.ReadInt64()).Should().Be(14); + host.Mem.ReadInt32(23).Should().Be(3); + host.Mem.WriteInt32(23, 13); + host.Mem.ReadInt32(23).Should().Be(13); + ((int)instance.ReadInt32()).Should().Be(13); - host.Mem.ReadSingle(35).Should().Be(5); - host.Mem.WriteSingle(35, 15); - host.Mem.ReadSingle(35).Should().Be(15); - ((float)instance.ReadFloat32()).Should().Be(15); + host.Mem.ReadInt64(27).Should().Be(4); + host.Mem.WriteInt64(27, 14); + host.Mem.ReadInt64(27).Should().Be(14); + ((long)instance.ReadInt64()).Should().Be(14); - host.Mem.ReadDouble(39).Should().Be(6); - host.Mem.WriteDouble(39, 16); - host.Mem.ReadDouble(39).Should().Be(16); - ((double)instance.ReadFloat64()).Should().Be(16); + host.Mem.ReadSingle(35).Should().Be(5); + host.Mem.WriteSingle(35, 15); + host.Mem.ReadSingle(35).Should().Be(15); + ((float)instance.ReadFloat32()).Should().Be(15); - 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); - } + host.Mem.ReadDouble(39).Should().Be(6); + host.Mem.WriteDouble(39, 16); + host.Mem.ReadDouble(39).Should().Be(16); + ((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); } } } diff --git a/crates/misc/dotnet/tests/WasiTests.cs b/crates/misc/dotnet/tests/WasiTests.cs index 5739a5c438..06d332634d 100644 --- a/crates/misc/dotnet/tests/WasiTests.cs +++ b/crates/misc/dotnet/tests/WasiTests.cs @@ -145,25 +145,23 @@ namespace Wasmtime.Tests { const string MESSAGE = "WASM IS VERY COOL"; - using (var file = new TempFile()) - { - File.WriteAllText(file.Path, MESSAGE); + using var file = new TempFile(); + File.WriteAllText(file.Path, MESSAGE); - var wasi = new WasiBuilder() - .WithStandardInput(file.Path) - .Build(Fixture.Module.Store); + var wasi = new WasiBuilder() + .WithStandardInput(file.Path) + .Build(Fixture.Module.Store); - using var instance = Fixture.Module.Instantiate(wasi); - dynamic inst = instance; + 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); + var memory = instance.Externs.Memories[0]; + memory.WriteInt32(0, 8); + memory.WriteInt32(4, MESSAGE.Length); - Assert.Equal(0, inst.call_fd_read(0, 0, 1, 32)); - Assert.Equal(MESSAGE.Length, memory.ReadInt32(32)); - Assert.Equal(MESSAGE, memory.ReadString(8, MESSAGE.Length)); - } + Assert.Equal(0, inst.call_fd_read(0, 0, 1, 32)); + Assert.Equal(MESSAGE.Length, memory.ReadInt32(32)); + Assert.Equal(MESSAGE, memory.ReadString(8, MESSAGE.Length)); } [Theory] @@ -173,33 +171,32 @@ namespace Wasmtime.Tests { 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(); - 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)); + 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)); } [Fact] @@ -207,44 +204,43 @@ namespace Wasmtime.Tests { const string MESSAGE = "WASM IS VERY COOL"; - using (var file = new TempFile()) - { - var wasi = new WasiBuilder() - .WithPreopenedDirectory(Path.GetDirectoryName(file.Path), "/foo") - .Build(Fixture.Module.Store); + using var file = new TempFile(); - using var instance = Fixture.Module.Instantiate(wasi); - dynamic inst = instance; + var wasi = new WasiBuilder() + .WithPreopenedDirectory(Path.GetDirectoryName(file.Path), "/foo") + .Build(Fixture.Module.Store); - var memory = instance.Externs.Memories[0]; - var fileName = Path.GetFileName(file.Path); - memory.WriteString(0, fileName); + using var instance = Fixture.Module.Instantiate(wasi); + dynamic inst = instance; - Assert.Equal(0, inst.call_path_open( - 3, - 0, - 0, - fileName.Length, - 0, - 0x40 /* RIGHTS_FD_WRITE */, - 0, - 0, - 64 - ) - ); + var memory = instance.Externs.Memories[0]; + var fileName = Path.GetFileName(file.Path); + memory.WriteString(0, fileName); - var fileFd = (int) memory.ReadInt32(64); - Assert.True(fileFd > 3); + Assert.Equal(0, inst.call_path_open( + 3, + 0, + 0, + fileName.Length, + 0, + 0x40 /* RIGHTS_FD_WRITE */, + 0, + 0, + 64 + ) + ); - memory.WriteInt32(0, 8); - memory.WriteInt32(4, MESSAGE.Length); - memory.WriteString(8, MESSAGE); + var fileFd = (int) memory.ReadInt32(64); + Assert.True(fileFd > 3); - 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)); - } + memory.WriteInt32(0, 8); + memory.WriteInt32(4, MESSAGE.Length); + memory.WriteString(8, MESSAGE); + + 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)); } } }