Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Fix CI.

This commit:

* Updates target framework to `net8.0`.
* Fixes the `externref` API to match recent changes in upstream Wasmtime.
* Remove WASI "snapshot 0" from the tests (so old!).

* Bump .NET version in CI.

* Attempt to fix Windows CI.
  • Loading branch information
peterhuene authored Apr 16, 2024
1 parent 76b39fa commit b2b0480
Show file tree
Hide file tree
Showing 26 changed files with 141 additions and 213 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.0.x'
dotnet-version: '8.0.x'
# workaround for actions/setup-dotnet#155
- name: Clear package cache
run: dotnet clean Wasmtime.sln && dotnet nuget locals all --clear
Expand Down
26 changes: 13 additions & 13 deletions benchmarks/simple/simple.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<ProjectReference Include="..\..\src\Wasmtime.csproj" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<ProjectReference Include="..\..\src\Wasmtime.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion examples/consumefuel/consumefuel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/externref/externref.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/funcref/funcref.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/global/global.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/hello.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion examples/memory/memory.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/storedata/storedata.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion examples/table/table.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions src/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public bool CheckTypeSignature(Type? returnType = null, params Type[] parameters
}

// Validate the return type(s)
if(returnType != null)
if (returnType != null)
{
// Multiple return types are represented by a tuple.
if (returnType.IsTupleType())
Expand Down Expand Up @@ -295,7 +295,7 @@ private unsafe void InvokeWithoutReturn(Span<ValueRaw> arguments, StoreContext s
Span<Value> args = stackalloc Value[Parameters.Count];
for (var i = 0; i < arguments.Length; ++i)
{
args[i] = arguments[i].ToValue(Parameters[i]);
args[i] = arguments[i].ToValue(store, Parameters[i]);
}

// Make some space to store the return results
Expand Down Expand Up @@ -641,14 +641,14 @@ internal static unsafe IntPtr InvokeUntypedCallback(UntypedCallbackDelegate call

for (int i = 0; i < argumentsSpan.Length; i++)
{
argumentsSpan[i] = args[i].ToValueBox();
argumentsSpan[i] = args[i].ToValueBox(caller.Store);
}

callback(caller, argumentsSpan, resultsSpan);

for (int i = 0; i < resultsSpan.Length; i++)
{
results[i] = resultsSpan[i].ToValue(resultKinds[i]);
results[i] = resultsSpan[i].ToValue(caller.Store, resultKinds[i]);
}
}
finally
Expand Down
8 changes: 4 additions & 4 deletions src/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Global(Store store, ValueKind kind, object? initialValue, Mutability muta
throw new InvalidOperationException("Failed to create global type, invalid ValueKind or Mutability");
}

var value = Value.FromObject(initialValue, Kind);
var value = Value.FromObject(store, initialValue, Kind);
var error = Native.wasmtime_global_new(store.Context.handle, globalType, in value, out this.global);
GC.KeepAlive(store);

Expand Down Expand Up @@ -155,7 +155,7 @@ public void SetValue(object? value)
throw new InvalidOperationException("The global is immutable and cannot be changed.");
}

var v = Value.FromObject(value, Kind);
var v = Value.FromObject(store, value, Kind);
Native.wasmtime_global_set(store.Context.handle, this.global, in v);
GC.KeepAlive(store);

Expand Down Expand Up @@ -300,7 +300,7 @@ public T GetValue()
Native.wasmtime_global_get(context.handle, _global.global, out var v);
GC.KeepAlive(_store);

var result = _converter.Unbox(_store, v.ToValueBox());
var result = _converter.Unbox(_store, v.ToValueBox(_store));
v.Dispose();

return result;
Expand All @@ -317,7 +317,7 @@ public void SetValue(T value)
throw new InvalidOperationException("The global is immutable and cannot be changed.");
}

using (var v = _converter.Box(value).ToValue(_global.Kind))
using (var v = _converter.Box(value).ToValue(_store, _global.Kind))
{
var context = _store.Context;
Native.wasmtime_global_set(context.handle, _global.global, in v);
Expand Down
2 changes: 1 addition & 1 deletion src/Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ public Instance(Store store, Module module, params object[] imports)

if (type != null && type.Value != @extern.kind)
{
return null;
return null;
}

var name = Encoding.UTF8.GetString(namePtr, checked((int)nameLen));
Expand Down
2 changes: 1 addition & 1 deletion src/Linker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void Define<T>(string module, string name, T item)
}

var ext = item.AsExtern();

using var nameBytes = name.ToUTF8(stackalloc byte[Math.Min(64, name.Length * 2)]);
using var moduleBytes = module.ToUTF8(stackalloc byte[Math.Min(64, module.Length * 2)]);

Expand Down
6 changes: 3 additions & 3 deletions src/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Table(Store store, TableKind kind, object? initialValue, uint initial, ui
limits
));

var value = Value.FromObject(initialValue, Kind);
var value = Value.FromObject(store, initialValue, Kind);
var error = Native.wasmtime_table_new(store.Context.handle, tableType, in value, out this.table);
GC.KeepAlive(store);
value.Dispose();
Expand Down Expand Up @@ -132,7 +132,7 @@ public Table(Store store, TableKind kind, object? initialValue, uint initial, ui
/// <param name="value">The value to set.</param>
public void SetElement(uint index, object? value)
{
var v = Value.FromObject(value, Kind);
var v = Value.FromObject(store, value, Kind);
var error = Native.wasmtime_table_set(store.Context.handle, this.table, index, v);
GC.KeepAlive(store);
v.Dispose();
Expand Down Expand Up @@ -162,7 +162,7 @@ public uint GetSize()
/// <returns>Returns the previous number of elements in the table.</returns>
public uint Grow(uint delta, object? initialValue)
{
var v = Value.FromObject(initialValue, Kind);
var v = Value.FromObject(store, initialValue, Kind);

var error = Native.wasmtime_table_grow(store.Context.handle, this.table, delta, v, out var prev);
GC.KeepAlive(store);
Expand Down
3 changes: 0 additions & 3 deletions src/TrapException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,6 @@ public TrapException(string message, Exception? inner) : base(message, inner) {
/// </summary>
public TrapCode Type { get; private set; }

/// <inheritdoc/>
protected TrapException(SerializationInfo info, StreamingContext context) : base(info, context) { }

internal TrapException(string message, IReadOnlyList<TrapFrame>? frames, TrapCode type, Exception? innerException = null)
: base(message, innerException)
{
Expand Down
30 changes: 18 additions & 12 deletions src/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public static bool TryGetKind(Type type, out ValueKind kind)
return false;
}

public static Value FromValueBox(ValueBox box)
public static Value FromValueBox(Store store, ValueBox box)
{
var value = new Value();
value.kind = box.Kind;
Expand All @@ -235,33 +235,36 @@ public static Value FromValueBox(ValueBox box)
if (box.ExternRefObject is not null)
{
value.of.externref = Native.wasmtime_externref_new(
store.Context.handle,
GCHandle.ToIntPtr(GCHandle.Alloc(box.ExternRefObject)),
Finalizer
);

GC.KeepAlive(store);
}
}

return value;
}

public ValueBox ToValueBox()
public ValueBox ToValueBox(Store store)
{
if (kind != ValueKind.ExternRef)
{
return new ValueBox(kind, of);
}
else
{
return new ValueBox(ResolveExternRef());
return new ValueBox(ResolveExternRef(store));
}
}

public static Value FromObject(object? o, TableKind kind)
public static Value FromObject(Store store, object? o, TableKind kind)
{
return FromObject(o, (ValueKind)kind);
return FromObject(store, o, (ValueKind)kind);
}

public static Value FromObject(object? o, ValueKind kind)
public static Value FromObject(Store store, object? o, ValueKind kind)
{
var value = new Value();
value.kind = kind;
Expand Down Expand Up @@ -306,9 +309,12 @@ public static Value FromObject(object? o, ValueKind kind)
if (!(o is null))
{
value.of.externref = Native.wasmtime_externref_new(
store.Context.handle,
GCHandle.ToIntPtr(GCHandle.Alloc(o)),
Value.Finalizer
);

GC.KeepAlive(store);
}
break;

Expand Down Expand Up @@ -360,7 +366,7 @@ public static Value FromObject(object? o, ValueKind kind)
return of.v128;

case ValueKind.ExternRef:
return ResolveExternRef();
return ResolveExternRef(store);

case ValueKind.FuncRef:
return store.GetCachedExtern(of.funcref);
Expand All @@ -370,13 +376,13 @@ public static Value FromObject(object? o, ValueKind kind)
}
}

private object? ResolveExternRef()
private object? ResolveExternRef(Store store)
{
if (of.externref == IntPtr.Zero)
{
return null;
}
var data = Native.wasmtime_externref_data(of.externref);
var data = Native.wasmtime_externref_data(store.Context.handle, of.externref);
if (data == IntPtr.Zero)
{
return null;
Expand All @@ -392,13 +398,13 @@ public static class Native
public static extern void wasmtime_val_delete(in Value val);

[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_externref_new(IntPtr data, Finalizer? finalizer);
public static extern IntPtr wasmtime_externref_new(IntPtr context, IntPtr data, Finalizer? finalizer);

[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_externref_data(IntPtr externref);
public static extern IntPtr wasmtime_externref_data(IntPtr context, IntPtr externref);

[DllImport(Engine.LibraryName)]
public static extern void wasmtime_externref_delete(IntPtr externref);
public static extern void wasmtime_externref_delete(IntPtr context, IntPtr externref);

[DllImport(Engine.LibraryName)]
public static extern IntPtr wasmtime_externref_from_raw(IntPtr context, IntPtr raw);
Expand Down
8 changes: 4 additions & 4 deletions src/ValueBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public V128 AsV128()
public Function AsFunction(Store store)
{
ThrowIfNotOfCorrectKind(ValueKind.FuncRef);

return store.GetCachedExtern(Union.funcref);
}

Expand All @@ -100,12 +100,12 @@ public Function AsFunction(Store store)
return (T?)ExternRefObject;
}

internal Value ToValue(ValueKind convertTo)
internal Value ToValue(Store store, ValueKind convertTo)
{
if (convertTo != Kind)
return Value.FromValueBox(ConvertTo(convertTo));
return Value.FromValueBox(store, ConvertTo(convertTo));

return Value.FromValueBox(this);
return Value.FromValueBox(store, this);
}

private ValueBox ConvertTo(ValueKind convertTo)
Expand Down
7 changes: 4 additions & 3 deletions src/ValueRaw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ private GenericValueRawConverter()

try
{
var data = Value.Native.wasmtime_externref_data(externref);
var data = Value.Native.wasmtime_externref_data(storeContext.handle, externref);
if (data != IntPtr.Zero)
{
o = GCHandle.FromIntPtr(data).Target!;
}
}
finally
{
Value.Native.wasmtime_externref_delete(externref);
Value.Native.wasmtime_externref_delete(storeContext.handle, externref);
}
}

Expand All @@ -285,6 +285,7 @@ public void Box(StoreContext storeContext, Store store, ref ValueRaw valueRaw, T
if (value is not null)
{
var externref = Value.Native.wasmtime_externref_new(
storeContext.handle,
GCHandle.ToIntPtr(GCHandle.Alloc(value)),
Value.Finalizer);

Expand All @@ -300,7 +301,7 @@ public void Box(StoreContext storeContext, Store store, ref ValueRaw valueRaw, T
{
// We still must delete the old externref afterwards because
// wasmtime_externref_to_raw doesn't transfer ownership.
Value.Native.wasmtime_externref_delete(externref);
Value.Native.wasmtime_externref_delete(storeContext.handle, externref);
}
}

Expand Down
Loading

0 comments on commit b2b0480

Please sign in to comment.