Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Linker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public Linker(Engine engine)
handle = new Handle(Native.wasmtime_linker_new(engine.NativeHandle));
}

private Linker(IntPtr handle)
{
this.handle = new Handle(handle);
}

/// <summary>
/// Configures whether or not the linker allows later definitions to shadow previous definitions.
/// </summary>
Expand All @@ -38,6 +43,14 @@ public bool AllowShadowing
}
}

/// <summary>
/// Clones existing linker.
/// </summary>
public Linker Clone()
{
return new Linker(Native.wasmtime_linker_clone(handle));
}

private void Define<T>(string module, string name, T item)
where T : IExternal
{
Expand Down Expand Up @@ -511,6 +524,9 @@ internal static class Native
[DllImport(Engine.LibraryName)]
public static extern void wasmtime_linker_allow_shadowing(Handle linker, [MarshalAs(UnmanagedType.I1)] bool allow);

[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_linker_clone(Handle linker);

[DllImport(Engine.LibraryName)]
public static unsafe extern IntPtr wasmtime_linker_define(Handle linker, IntPtr context, byte* module, nuint moduleLen, byte* name, nuint nameLen, in Extern item);

Expand Down
89 changes: 89 additions & 0 deletions tests/LinkerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,95 @@ public void ItThrowsWithNullEngine()
Assert.Throws<ArgumentNullException>(() => new Linker(null!));
}

[Fact]
public void CloneCarriesAllowShadowing()
{
Linker.AllowShadowing = true;

using var clone = Linker.Clone();

clone.DefineFunction("", "hello", () => { });
clone.DefineFunction("", "hello", () => { });
}

[Fact]
public void CloneCarriesDefinitions()
{
var called = 0;
Linker.DefineFunction("", "hello", () => { called++; });

using var clone = Linker.Clone();
clone.GetFunction(Store, "", "hello")!.Invoke();

Assert.Equal(1, called);
}

[Fact]
public void CloneCarriesWasi()
{
Linker.DefineWasi();

using var clone = Linker.Clone();

Assert.Throws<WasmtimeException>(() => clone.DefineWasi());
}

[Fact]
public void CloneDisallowsShadowingByDefault()
{
using var clone = Linker.Clone();

clone.DefineFunction("", "hello", () => { });
Assert.Throws<WasmtimeException>(() => clone.DefineFunction("", "hello", () => { }));
}

[Fact]
public void CloneDoesNotShareDefinitionsAddedToTheClone()
{
using var clone = Linker.Clone();
clone.DefineFunction("", "hello", () => { });

Assert.NotNull(clone.GetFunction(Store, "", "hello"));
Assert.Null(Linker.GetFunction(Store, "", "hello"));
}

[Fact]
public void CloneDoesNotShareDefinitionsAddedToTheOriginal()
{
using var clone = Linker.Clone();
Linker.DefineFunction("", "hello", () => { });

Assert.NotNull(Linker.GetFunction(Store, "", "hello"));
Assert.Null(clone.GetFunction(Store, "", "hello"));
}

[Fact]
public void CloneOutlivesTheOriginal()
{
var called = 0;
var linker = new Linker(Fixture.Engine);
linker.DefineFunction("", "hello", () => { called++; });

using var clone = linker.Clone();
linker.Dispose();

GC.Collect();
GC.WaitForPendingFinalizers();

clone.GetFunction(Store, "", "hello")!.Invoke();

Assert.Equal(1, called);
}

[Fact]
public void CloneThrowsWhenDisposed()
{
var linker = new Linker(Fixture.Engine);
linker.Dispose();

Assert.Throws<ObjectDisposedException>(() => linker.Clone());
}

[Fact]
public void DefineThrowsWithNullStore()
{
Expand Down
Loading