Skip to content

Commit

Permalink
Converted Memory into a readonly struct
Browse files Browse the repository at this point in the history
  • Loading branch information
martindevans committed Feb 15, 2023
1 parent 885fc4e commit 617be6f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Wasmtime
/// <summary>
/// Represents a WebAssembly memory.
/// </summary>
public class Memory : IExternal
public readonly struct Memory : IExternal
{
/// <summary>
/// Creates a new WebAssembly memory.
Expand Down Expand Up @@ -553,6 +553,7 @@ internal Memory(Store store, ExternMemory memory)
{
this.memory = memory;
this.store = store;
this.Maximum = null;

var typeHandle = Native.wasmtime_memory_type(store.Context.handle, this.memory);
try
Expand Down Expand Up @@ -580,7 +581,7 @@ internal static class Native
public static extern IntPtr wasmtime_memory_new(IntPtr context, IntPtr typeHandle, out ExternMemory memory);

[DllImport(Engine.LibraryName)]
public static unsafe extern byte* wasmtime_memory_data(IntPtr context, in ExternMemory memory);
public static extern unsafe byte* wasmtime_memory_data(IntPtr context, in ExternMemory memory);

[DllImport(Engine.LibraryName)]
public static extern nuint wasmtime_memory_data_size(IntPtr context, in ExternMemory memory);
Expand Down
6 changes: 3 additions & 3 deletions tests/CallerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void ItCanReadAndWriteMemoryFromCaller()
{
Linker.DefineFunction("env", "callback", (Caller c) =>
{
var mem = c.GetMemory("memory");
var mem = c.GetMemory("memory")!.Value;

mem.ReadByte(10).Should().Be(20);
mem.WriteByte(10, 21);
Expand All @@ -90,7 +90,7 @@ public void ItCanReadAndWriteMemoryFromCaller()
var callback = instance.GetFunction("call_callback")!;

// Write a value into memory
var memory = instance.GetMemory("memory")!;
var memory = instance.GetMemory("memory")!.Value;
memory.WriteByte(10, 20);

// Callback checks that value and writes another
Expand All @@ -116,7 +116,7 @@ public void ItCanReadAndWriteMemorySpanFromCaller()
var callback = instance.GetFunction("call_callback")!;

// Write a value into memory
var memory = instance.GetMemory("memory")!;
var memory = instance.GetMemory("memory")!.Value;
memory.WriteByte(10, 20);

// Callback checks that value and writes another
Expand Down
2 changes: 1 addition & 1 deletion tests/FunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public FunctionTests(FunctionsFixture fixture)
Linker.Define("env", "do_throw", Function.FromCallback(Store, () => throw new Exception(THROW_MESSAGE)));
Linker.Define("env", "check_string", Function.FromCallback(Store, (Caller caller, int address, int length) =>
{
caller.GetMemory("mem").ReadString(address, length).Should().Be("Hello World");
caller.GetMemory("mem")!.Value.ReadString(address, length).Should().Be("Hello World");
}));

Linker.Define("env", "return_i32", Function.FromCallback(Store, GetBoundFuncIntDelegate()));
Expand Down
2 changes: 1 addition & 1 deletion tests/LinkerFunctionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public LinkerFunctionTests(LinkerFunctionsFixture fixture)
Linker.DefineFunction("env", "do_throw", () => throw new Exception(THROW_MESSAGE));
Linker.DefineFunction("env", "check_string", (Caller caller, int address, int length) =>
{
caller.GetMemory("mem").ReadString(address, length).Should().Be("Hello World");
caller.GetMemory("mem")!.Value.ReadString(address, length).Should().Be("Hello World");
});

Linker.DefineFunction("env", "return_i32", GetBoundFuncIntDelegate());
Expand Down
5 changes: 3 additions & 2 deletions tests/Memory64AccessTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using FluentAssertions;
using FluentAssertions.Execution;
using Xunit;

namespace Wasmtime.Tests
Expand Down Expand Up @@ -34,7 +35,7 @@ public unsafe void ItCanAccessMemoryWith65537Pages()
memoryExport.Is64Bit.Should().BeTrue();

var instance = Linker.Instantiate(Store, Fixture.Module);
var memory = instance.GetMemory("mem");
var memory = instance.GetMemory("mem")!.Value;

memory.Minimum.Should().Be(0x10001);
memory.Maximum.Should().Be(0x1000000000000);
Expand Down Expand Up @@ -75,7 +76,7 @@ public unsafe void ItCanAccessMemoryWith65537Pages()
public void ItThrowsForOutOfBoundsAccess()
{
var instance = Linker.Instantiate(Store, Fixture.Module);
var memory = instance.GetMemory("mem");
var memory = instance.GetMemory("mem")!.Value;

#pragma warning disable CS0618 // Type or member is obsolete
Action action = () => memory.GetSpan();
Expand Down
12 changes: 10 additions & 2 deletions tests/MemoryAccessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public MemoryAccessTests(MemoryAccessFixture fixture)
Linker = new Linker(Fixture.Engine);
}

[Fact]
public void AccessDefaultThrows()
{
var memory = default(Memory);

Assert.Throws<NullReferenceException>(() => memory.GetLength());
}

[Fact]
public void ItGrows()
{
Expand Down Expand Up @@ -67,7 +75,7 @@ public unsafe void ItCanAccessMemoryWith65536Pages()
memoryExport.Is64Bit.Should().BeFalse();

var instance = Linker.Instantiate(Store, Fixture.Module);
var memory = instance.GetMemory("mem");
var memory = instance.GetMemory("mem")!.Value;

memory.Minimum.Should().Be(0x10000);
memory.Maximum.Should().BeNull();
Expand Down Expand Up @@ -108,7 +116,7 @@ public unsafe void ItCanAccessMemoryWith65536Pages()
public void ItThrowsForOutOfBoundsAccess()
{
var instance = Linker.Instantiate(Store, Fixture.Module);
var memory = instance.GetMemory("mem");
var memory = instance.GetMemory("mem")!.Value;

#pragma warning disable CS0618 // Type or member is obsolete
Action action = () => memory.GetSpan();
Expand Down
8 changes: 2 additions & 6 deletions tests/MemoryExportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public void ItHasTheExpectedNumberOfExportedTables()
public void ItReadsAndWritesGenericTypes()
{
var instance = Linker.Instantiate(Store, Fixture.Module);
var memory = instance.GetMemory("mem");

memory.Should().NotBeNull();
var memory = instance.GetMemory("mem")!.Value;

memory.Write(11, new TestStruct { A = 17, B = -34346 });
var result = memory.Read<TestStruct>(11);
Expand All @@ -78,9 +76,7 @@ struct TestStruct
public void ItCreatesExternsForTheMemories()
{
var instance = Linker.Instantiate(Store, Fixture.Module);
var memory = instance.GetMemory("mem");

memory.Should().NotBeNull();
var memory = instance.GetMemory("mem")!.Value;

memory.ReadString(0, 11).Should().Be("Hello World");
int written = memory.WriteString(0, "WebAssembly Rocks!");
Expand Down
18 changes: 9 additions & 9 deletions tests/WasiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void ItHasNoEnvironmentByDefault(string path)
store.SetWasiConfiguration(new WasiConfiguration());
var instance = linker.Instantiate(store, module);

var memory = instance.GetMemory("memory");
var memory = instance.GetMemory("memory")!.Value;
memory.Should().NotBeNull();
var call_environ_sizes_get = instance.GetFunction("call_environ_sizes_get");
call_environ_sizes_get.Should().NotBeNull();
Expand Down Expand Up @@ -58,7 +58,7 @@ public void ItHasSpecifiedEnvironment(string path)
store.SetWasiConfiguration(config);
var instance = linker.Instantiate(store, module);

var memory = instance.GetMemory("memory");
var memory = instance.GetMemory("memory")!.Value;
memory.Should().NotBeNull();
var call_environ_sizes_get = instance.GetFunction("call_environ_sizes_get");
call_environ_sizes_get.Should().NotBeNull();
Expand Down Expand Up @@ -95,7 +95,7 @@ public void ItInheritsEnvironment(string path)
store.SetWasiConfiguration(config);
var instance = linker.Instantiate(store, module);

var memory = instance.GetMemory("memory");
var memory = instance.GetMemory("memory")!.Value;
memory.Should().NotBeNull();
var call_environ_sizes_get = instance.GetFunction("call_environ_sizes_get");
call_environ_sizes_get.Should().NotBeNull();
Expand All @@ -119,7 +119,7 @@ public void ItHasNoArgumentsByDefault(string path)
store.SetWasiConfiguration(new WasiConfiguration());
var instance = linker.Instantiate(store, module);

var memory = instance.GetMemory("memory");
var memory = instance.GetMemory("memory")!.Value;
memory.Should().NotBeNull();
var call_args_sizes_get = instance.GetFunction("call_args_sizes_get");
call_args_sizes_get.Should().NotBeNull();
Expand Down Expand Up @@ -154,7 +154,7 @@ public void ItHasSpecifiedArguments(string path)
store.SetWasiConfiguration(config);
var instance = linker.Instantiate(store, module);

var memory = instance.GetMemory("memory");
var memory = instance.GetMemory("memory")!.Value;
memory.Should().NotBeNull();
var call_args_sizes_get = instance.GetFunction("call_args_sizes_get");
call_args_sizes_get.Should().NotBeNull();
Expand Down Expand Up @@ -191,7 +191,7 @@ public void ItInheritsArguments(string path)
store.SetWasiConfiguration(config);
var instance = linker.Instantiate(store, module);

var memory = instance.GetMemory("memory");
var memory = instance.GetMemory("memory")!.Value;
memory.Should().NotBeNull();
var call_args_sizes_get = instance.GetFunction("call_args_sizes_get");
call_args_sizes_get.Should().NotBeNull();
Expand Down Expand Up @@ -223,7 +223,7 @@ public void ItSetsStdIn(string path)
store.SetWasiConfiguration(config);
var instance = linker.Instantiate(store, module);

var memory = instance.GetMemory("memory");
var memory = instance.GetMemory("memory")!.Value;
memory.Should().NotBeNull();
var call_fd_read = instance.GetFunction("call_fd_read");
call_fd_read.Should().NotBeNull();
Expand Down Expand Up @@ -267,7 +267,7 @@ public void ItSetsStdOutAndStdErr(string path, int fd)
store.SetWasiConfiguration(config);
var instance = linker.Instantiate(store, module);

var memory = instance.GetMemory("memory");
var memory = instance.GetMemory("memory")!.Value;
memory.Should().NotBeNull();
var call_fd_write = instance.GetFunction("call_fd_write");
call_fd_write.Should().NotBeNull();
Expand Down Expand Up @@ -306,7 +306,7 @@ public void ItSetsPreopenDirectories(string path)
store.SetWasiConfiguration(config);
var instance = linker.Instantiate(store, module);

var memory = instance.GetMemory("memory");
var memory = instance.GetMemory("memory")!.Value;
memory.Should().NotBeNull();
var call_path_open = instance.GetFunction("call_path_open");
call_path_open.Should().NotBeNull();
Expand Down

0 comments on commit 617be6f

Please sign in to comment.