Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to .NET 8 WASI support #587

Merged
merged 16 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
run: docker compose up -d
- name: Run smoketests
run: test/run-smoke-tests.sh --parallel -x bitcraftmini-pretest
- name: Stop containers
- name: Stop containers
if: always()
run: docker compose down

Expand All @@ -30,6 +30,10 @@ jobs:

- uses: dsherret/rust-toolchain-file@v1

- uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.x'

- name: Create /stdb dir
run: |
sudo mkdir /stdb
Expand Down
2 changes: 1 addition & 1 deletion crates/bindings-csharp/Codegen/Codegen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AssemblyName>SpacetimeDB.Codegen</AssemblyName>
<AssemblyVersion>0.7.3</AssemblyVersion>
<AssemblyVersion>0.8.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
23 changes: 11 additions & 12 deletions crates/bindings-csharp/Codegen/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
a.AttributeClass?.ToDisplayString()
== "SpacetimeDB.ColumnAttribute"
)
.Select(
a =>
(ColumnAttrs)a.ConstructorArguments[0].Value!
)
.Select(a => (ColumnAttrs)a.ConstructorArguments[0].Value!)
.SingleOrDefault();

if (indexKind.HasFlag(ColumnAttrs.AutoInc))
Expand Down Expand Up @@ -112,7 +109,7 @@ or SpecialType.System_UInt64

var extensions =
$@"
private static readonly Lazy<uint> tableId = new (() => SpacetimeDB.Runtime.GetTableId(nameof({t.Name})));
private static readonly Lazy<SpacetimeDB.RawBindings.TableId> tableId = new (() => SpacetimeDB.Runtime.GetTableId(nameof({t.Name})));

public static IEnumerable<{t.Name}> Iter() =>
new SpacetimeDB.Runtime.RawTableIter(tableId.Value)
Expand All @@ -139,7 +136,11 @@ public void Insert() {{
}}
";

foreach (var (f, index) in t.Fields.Select((f, i) => (f, i)))
foreach (
var (f, index) in t.Fields.Select(
(f, i) => (f, $"new SpacetimeDB.RawBindings.ColId({i})")
)
)
{
if (f.IndexKind.HasFlag(ColumnAttrs.Unique))
{
Expand Down Expand Up @@ -273,6 +274,7 @@ void IReducer.Invoke(BinaryReader reader, SpacetimeDB.Runtime.DbEventArgs dbEven
using SpacetimeDB.Module;
using System.Runtime.CompilerServices;
using static SpacetimeDB.Runtime;
using System.Diagnostics.CodeAnalysis;

static class ModuleRegistration {{
{string.Join("\n", addReducers.Select(r => r.Class))}
Expand All @@ -281,13 +283,10 @@ static class ModuleRegistration {{
// [ModuleInitializer] - doesn't work because assemblies are loaded lazily;
// might make use of it later down the line, but for now assume there is only one
// module so we can use `Main` instead.
public static void Main() {{
// incredibly weird bugfix for incredibly weird bug
// see https://github.com/dotnet/dotnet-wasi-sdk/issues/24
// - looks like it has to be stringified at least once in Main or it will fail everywhere
// - looks like ToString() will crash with stack overflow, but interpolation works
var _bugFix = $""{{DateTimeOffset.UnixEpoch}}"";

// Prevent trimming of FFI exports that are invoked from C and not visible to C# trimmer.
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(SpacetimeDB.Module.FFI))]
public static void Main() {{
{string.Join("\n", addReducers.Select(r => $"FFI.RegisterReducer(new {r.Name}());"))}
{string.Join("\n", addTables)}
}}
Expand Down
58 changes: 42 additions & 16 deletions crates/bindings-csharp/Runtime/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace SpacetimeDB.Module;
using System.IO;
using System.Linq;
using SpacetimeDB.SATS;
using System.Runtime.InteropServices;

[SpacetimeDB.Type]
public partial struct IndexDef
Expand All @@ -13,11 +14,11 @@ public partial struct IndexDef
Runtime.IndexType Type;
byte[] ColumnIds;

public IndexDef(string name, Runtime.IndexType type, byte[] columnIds)
public IndexDef(string name, Runtime.IndexType type, RawBindings.ColId[] columnIds)
{
Name = name;
Type = type;
ColumnIds = columnIds;
ColumnIds = columnIds.Select(id => (byte)id).ToArray();
}
}

Expand All @@ -26,6 +27,7 @@ public partial struct TableDef
{
string Name;
AlgebraicTypeRef Data;

// bitflags should be serialized as bytes rather than sum types
byte[] ColumnAttrs;
IndexDef[] Indices;
Expand Down Expand Up @@ -167,49 +169,73 @@ public static void RegisterReducer(IReducer reducer)

public static AlgebraicTypeRef AllocTypeRef() => module.AllocTypeRef();

public static void SetTypeRef<T>(AlgebraicTypeRef typeRef, AlgebraicType type, bool anonymous = false) =>
module.SetTypeRef<T>(typeRef, type, anonymous);
public static void SetTypeRef<T>(
AlgebraicTypeRef typeRef,
AlgebraicType type,
bool anonymous = false
) => module.SetTypeRef<T>(typeRef, type, anonymous);

// Note: this is accessed by C bindings.
private static byte[] DescribeModule()
// [UnmanagedCallersOnly(EntryPoint = "__describe_module__")]
public static RawBindings.Buffer __describe_module__()
{
// replace `module` with a temporary internal module that will register ModuleDef, AlgebraicType and other internal types
// during the ModuleDef.GetSatsTypeInfo() instead of exposing them via user's module.
var userModule = module;
try
{
module = new();
return ModuleDef.GetSatsTypeInfo().ToBytes(userModule);
var moduleBytes = ModuleDef.GetSatsTypeInfo().ToBytes(userModule);
var res = RawBindings._buffer_alloc(moduleBytes, (uint)moduleBytes.Length);
return res;
}
catch (Exception e)
{
Runtime.Log($"Error while describing the module: {e}", Runtime.LogLevel.Error);
return RawBindings.Buffer.INVALID;
}
finally
{
module = userModule;
}
}

// Note: this is accessed by C bindings.
private static string? CallReducer(
private static byte[] Consume(this RawBindings.Buffer buffer)
{
var len = RawBindings._buffer_len(buffer);
var result = new byte[len];
RawBindings._buffer_consume(buffer, result, len);
return result;
}

// [UnmanagedCallersOnly(EntryPoint = "__call_reducer__")]
public static RawBindings.Buffer __call_reducer__(
uint id,
byte[] caller_identity,
byte[] caller_address,
RawBindings.Buffer caller_identity,
RawBindings.Buffer caller_address,
ulong timestamp,
byte[] args
RawBindings.Buffer args
)
{
try
{
using var stream = new MemoryStream(args);
using var stream = new MemoryStream(args.Consume());
using var reader = new BinaryReader(stream);
reducers[(int)id].Invoke(reader, new(caller_identity, caller_address, timestamp));
reducers[(int)id].Invoke(
reader,
new(caller_identity.Consume(), caller_address.Consume(), timestamp)
);
if (stream.Position != stream.Length)
{
throw new Exception("Unrecognised extra bytes in the reducer arguments");
}
return null;
return /* no exception */
RawBindings.Buffer.INVALID;
}
catch (Exception e)
{
return e.ToString();
var error_str = e.ToString();
var error_bytes = System.Text.Encoding.UTF8.GetBytes(error_str);
return RawBindings._buffer_alloc(error_bytes, (uint)error_bytes.Length);
}
}
}
Loading