From 8c2b76ad2341814ada57c43b1497ab2a2150aeb8 Mon Sep 17 00:00:00 2001 From: Lisandro Crespo Date: Thu, 9 Jul 2026 11:33:38 -0500 Subject: [PATCH 1/3] Bring #2149 to post .NET 10 --- .../diag/snapshots/Module#FFI.verified.cs | 10 +-- .../snapshots/Module#FFI.verified.cs | 10 +-- .../server/snapshots/Module#FFI.verified.cs | 10 +-- crates/bindings-csharp/Codegen/Module.cs | 10 +-- crates/bindings-csharp/Runtime/Http.cs | 4 +- .../bindings-csharp/Runtime/Internal/FFI.cs | 66 +++++++++---------- .../Runtime/Internal/IIndex.cs | 32 ++++----- .../Runtime/Internal/IReducer.cs | 4 +- .../Runtime/Internal/ITable.cs | 33 +++++----- .../Runtime/Internal/Module.cs | 52 +++++++-------- .../Runtime/Internal/Procedure.cs | 4 +- crates/bindings-csharp/Runtime/Log.cs | 6 +- .../bindings-csharp/Runtime/LogStopwatch.cs | 2 +- 13 files changed, 122 insertions(+), 121 deletions(-) diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs index e6df8c9e52a..07bb8a6483e 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs @@ -3745,7 +3745,7 @@ public static void __describe_module__(SpacetimeDB.Internal.BytesSink d) => [UnmanagedCallersOnly(EntryPoint = "__call_reducer__")] public static SpacetimeDB.Internal.Errno __call_reducer__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -3771,7 +3771,7 @@ SpacetimeDB.Internal.BytesSink error [UnmanagedCallersOnly(EntryPoint = "__call_procedure__")] public static SpacetimeDB.Internal.Errno __call_procedure__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -3797,7 +3797,7 @@ SpacetimeDB.Internal.BytesSink result_sink [UnmanagedCallersOnly(EntryPoint = "__call_http_handler__")] public static SpacetimeDB.Internal.Errno __call_http_handler__( - uint id, + int id, SpacetimeDB.Timestamp timestamp, SpacetimeDB.Internal.BytesSource request, SpacetimeDB.Internal.BytesSource request_body, @@ -3815,7 +3815,7 @@ SpacetimeDB.Internal.BytesSink response_body_sink [UnmanagedCallersOnly(EntryPoint = "__call_view__")] public static SpacetimeDB.Internal.Errno __call_view__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -3835,7 +3835,7 @@ SpacetimeDB.Internal.BytesSink sink [UnmanagedCallersOnly(EntryPoint = "__call_view_anon__")] public static SpacetimeDB.Internal.Errno __call_view_anon__( - uint id, + int id, SpacetimeDB.Internal.BytesSource args, SpacetimeDB.Internal.BytesSink sink ) => SpacetimeDB.Internal.Module.__call_view_anon__(id, args, sink); diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/explicitnames/snapshots/Module#FFI.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/explicitnames/snapshots/Module#FFI.verified.cs index 1602df16086..e0124d76783 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/explicitnames/snapshots/Module#FFI.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/explicitnames/snapshots/Module#FFI.verified.cs @@ -635,7 +635,7 @@ public static void __describe_module__(SpacetimeDB.Internal.BytesSink d) => [UnmanagedCallersOnly(EntryPoint = "__call_reducer__")] public static SpacetimeDB.Internal.Errno __call_reducer__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -661,7 +661,7 @@ SpacetimeDB.Internal.BytesSink error [UnmanagedCallersOnly(EntryPoint = "__call_procedure__")] public static SpacetimeDB.Internal.Errno __call_procedure__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -687,7 +687,7 @@ SpacetimeDB.Internal.BytesSink result_sink [UnmanagedCallersOnly(EntryPoint = "__call_http_handler__")] public static SpacetimeDB.Internal.Errno __call_http_handler__( - uint id, + int id, SpacetimeDB.Timestamp timestamp, SpacetimeDB.Internal.BytesSource request, SpacetimeDB.Internal.BytesSource request_body, @@ -705,7 +705,7 @@ SpacetimeDB.Internal.BytesSink response_body_sink [UnmanagedCallersOnly(EntryPoint = "__call_view__")] public static SpacetimeDB.Internal.Errno __call_view__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -725,7 +725,7 @@ SpacetimeDB.Internal.BytesSink sink [UnmanagedCallersOnly(EntryPoint = "__call_view_anon__")] public static SpacetimeDB.Internal.Errno __call_view_anon__( - uint id, + int id, SpacetimeDB.Internal.BytesSource args, SpacetimeDB.Internal.BytesSink sink ) => SpacetimeDB.Internal.Module.__call_view_anon__(id, args, sink); diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs index 1ce8d795d15..234e43c9c50 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs @@ -2563,7 +2563,7 @@ public static void __describe_module__(SpacetimeDB.Internal.BytesSink d) => [UnmanagedCallersOnly(EntryPoint = "__call_reducer__")] public static SpacetimeDB.Internal.Errno __call_reducer__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -2589,7 +2589,7 @@ SpacetimeDB.Internal.BytesSink error [UnmanagedCallersOnly(EntryPoint = "__call_procedure__")] public static SpacetimeDB.Internal.Errno __call_procedure__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -2615,7 +2615,7 @@ SpacetimeDB.Internal.BytesSink result_sink [UnmanagedCallersOnly(EntryPoint = "__call_http_handler__")] public static SpacetimeDB.Internal.Errno __call_http_handler__( - uint id, + int id, SpacetimeDB.Timestamp timestamp, SpacetimeDB.Internal.BytesSource request, SpacetimeDB.Internal.BytesSource request_body, @@ -2633,7 +2633,7 @@ SpacetimeDB.Internal.BytesSink response_body_sink [UnmanagedCallersOnly(EntryPoint = "__call_view__")] public static SpacetimeDB.Internal.Errno __call_view__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -2653,7 +2653,7 @@ SpacetimeDB.Internal.BytesSink sink [UnmanagedCallersOnly(EntryPoint = "__call_view_anon__")] public static SpacetimeDB.Internal.Errno __call_view_anon__( - uint id, + int id, SpacetimeDB.Internal.BytesSource args, SpacetimeDB.Internal.BytesSink sink ) => SpacetimeDB.Internal.Module.__call_view_anon__(id, args, sink); diff --git a/crates/bindings-csharp/Codegen/Module.cs b/crates/bindings-csharp/Codegen/Module.cs index 34040525b94..43bbc702687 100644 --- a/crates/bindings-csharp/Codegen/Module.cs +++ b/crates/bindings-csharp/Codegen/Module.cs @@ -2896,7 +2896,7 @@ public static void Main() { [UnmanagedCallersOnly(EntryPoint = "__call_reducer__")] public static SpacetimeDB.Internal.Errno __call_reducer__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -2921,7 +2921,7 @@ SpacetimeDB.Internal.BytesSink error [UnmanagedCallersOnly(EntryPoint = "__call_procedure__")] public static SpacetimeDB.Internal.Errno __call_procedure__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -2946,7 +2946,7 @@ SpacetimeDB.Internal.BytesSink result_sink [UnmanagedCallersOnly(EntryPoint = "__call_http_handler__")] public static SpacetimeDB.Internal.Errno __call_http_handler__( - uint id, + int id, SpacetimeDB.Timestamp timestamp, SpacetimeDB.Internal.BytesSource request, SpacetimeDB.Internal.BytesSource request_body, @@ -2963,7 +2963,7 @@ SpacetimeDB.Internal.BytesSink response_body_sink [UnmanagedCallersOnly(EntryPoint = "__call_view__")] public static SpacetimeDB.Internal.Errno __call_view__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -2982,7 +2982,7 @@ SpacetimeDB.Internal.BytesSink sink [UnmanagedCallersOnly(EntryPoint = "__call_view_anon__")] public static SpacetimeDB.Internal.Errno __call_view_anon__( - uint id, + int id, SpacetimeDB.Internal.BytesSource args, SpacetimeDB.Internal.BytesSink sink ) => SpacetimeDB.Internal.Module.__call_view_anon__( diff --git a/crates/bindings-csharp/Runtime/Http.cs b/crates/bindings-csharp/Runtime/Http.cs index 2567f4a11e5..0a6bd712de0 100644 --- a/crates/bindings-csharp/Runtime/Http.cs +++ b/crates/bindings-csharp/Runtime/Http.cs @@ -331,9 +331,9 @@ public Result Send(HttpRequest request) var status = FFI.procedure_http_request( requestBytes, - (uint)requestBytes.Length, + requestBytes.Length, bodyBytes, - (uint)bodyBytes.Length, + bodyBytes.Length, out var out_ ); diff --git a/crates/bindings-csharp/Runtime/Internal/FFI.cs b/crates/bindings-csharp/Runtime/Internal/FFI.cs index c9f07994f1d..380f10ba5f0 100644 --- a/crates/bindings-csharp/Runtime/Internal/FFI.cs +++ b/crates/bindings-csharp/Runtime/Internal/FFI.cs @@ -199,16 +199,16 @@ public readonly record struct RowIter(uint Handle) [WasmImportLinkage] [LibraryImport(StdbNamespace10_0)] public static partial CheckedStatus table_id_from_name( - [In] byte[] name, - uint name_len, + ReadOnlySpan name, + int name_len, out TableId out_ ); [WasmImportLinkage] [LibraryImport(StdbNamespace10_0)] public static partial CheckedStatus index_id_from_name( - [In] byte[] name, - uint name_len, + ReadOnlySpan name, + int name_len, out IndexId out_ ); @@ -228,7 +228,7 @@ out RowIter out_ public static partial CheckedStatus datastore_index_scan_point_bsatn( IndexId index_id, ReadOnlySpan point, - uint point_len, + int point_len, out RowIter out_ ); @@ -237,7 +237,7 @@ out RowIter out_ public static partial CheckedStatus datastore_delete_by_index_scan_point_bsatn( IndexId index_id, ReadOnlySpan point, - uint point_len, + int point_len, out uint out_ ); @@ -246,12 +246,12 @@ out uint out_ public static partial CheckedStatus datastore_index_scan_range_bsatn( IndexId index_id, ReadOnlySpan prefix, - uint prefix_len, + int prefix_len, ColId prefix_elems, ReadOnlySpan rstart, - uint rstart_len, + int rstart_len, ReadOnlySpan rend, - uint rend_len, + int rend_len, out RowIter out_ ); @@ -259,8 +259,8 @@ out RowIter out_ [LibraryImport(StdbNamespace10_0)] public static partial Errno row_iter_bsatn_advance( RowIter iter_handle, - [MarshalUsing(CountElementName = nameof(buffer_len))] [Out] byte[] buffer, - ref uint buffer_len + Span buffer, + ref int buffer_len ); [WasmImportLinkage] @@ -272,7 +272,7 @@ ref uint buffer_len public static partial CheckedStatus datastore_insert_bsatn( TableId table_id, Span row, - ref uint row_len + ref int row_len ); [WasmImportLinkage] @@ -281,7 +281,7 @@ public static partial CheckedStatus datastore_update_bsatn( TableId table_id, IndexId index_id, Span row, - ref uint row_len + ref int row_len ); [WasmImportLinkage] @@ -289,12 +289,12 @@ ref uint row_len public static partial CheckedStatus datastore_delete_by_index_scan_range_bsatn( IndexId index_id, ReadOnlySpan prefix, - uint prefix_len, + int prefix_len, ColId prefix_elems, ReadOnlySpan rstart, - uint rstart_len, + int rstart_len, ReadOnlySpan rend, - uint rend_len, + int rend_len, out uint out_ ); @@ -302,8 +302,8 @@ out uint out_ [LibraryImport(StdbNamespace10_0)] public static partial CheckedStatus datastore_delete_all_by_eq_bsatn( TableId table_id, - [In] byte[] relation, - uint relation_len, + ReadOnlySpan relation, + int relation_len, out uint out_ ); @@ -316,7 +316,7 @@ out uint out_ public static partial Errno bytes_source_read( BytesSource source, Span buffer, - ref uint buffer_len + ref int buffer_len ); [WasmImportLinkage] @@ -324,7 +324,7 @@ ref uint buffer_len public static partial CheckedStatus bytes_sink_write( BytesSink sink, ReadOnlySpan buffer, - ref uint buffer_len + ref int buffer_len ); public enum LogLevel : byte @@ -341,13 +341,13 @@ public enum LogLevel : byte [LibraryImport(StdbNamespace10_0)] public static partial void console_log( LogLevel level, - [In] byte[] target, - uint target_len, - [In] byte[] filename, - uint filename_len, + ReadOnlySpan target, + int target_len, + ReadOnlySpan filename, + int filename_len, uint line_number, - [In] byte[] message, - uint message_len + ReadOnlySpan message, + int message_len ); [NativeMarshalling(typeof(ConsoleTimerIdMarshaller))] @@ -377,7 +377,7 @@ internal static class ConsoleTimerIdMarshaller [WasmImportLinkage] [LibraryImport(StdbNamespace10_0)] - public static partial ConsoleTimerId console_timer_start([In] byte[] name, uint name_len); + public static partial ConsoleTimerId console_timer_start(ReadOnlySpan name, int name_len); [WasmImportLinkage] [LibraryImport(StdbNamespace10_0)] @@ -386,10 +386,10 @@ internal static class ConsoleTimerIdMarshaller [WasmImportLinkage] [LibraryImport(StdbNamespace10_0)] public static partial void volatile_nonatomic_schedule_immediate( - [In] byte[] name, - uint name_len, - [In] byte[] args, - uint args_len + ReadOnlySpan name, + int name_len, + ReadOnlySpan args, + int args_len ); // Note #1: our Identity type has the same layout as a fixed-size 32-byte little-endian buffer, @@ -436,9 +436,9 @@ public readonly struct BytesSourcePair [LibraryImport(StdbNamespace10_3, EntryPoint = "procedure_http_request")] public static partial Errno procedure_http_request( ReadOnlySpan request, - uint request_len, + int request_len, ReadOnlySpan body, - uint body_len, + int body_len, out BytesSourcePair out_ ); } diff --git a/crates/bindings-csharp/Runtime/Internal/IIndex.cs b/crates/bindings-csharp/Runtime/Internal/IIndex.cs index 33525924f2d..1bfcc05cd38 100644 --- a/crates/bindings-csharp/Runtime/Internal/IIndex.cs +++ b/crates/bindings-csharp/Runtime/Internal/IIndex.cs @@ -14,7 +14,7 @@ public abstract class IndexBase public IndexBase(string name) { var name_bytes = System.Text.Encoding.UTF8.GetBytes(name); - FFI.index_id_from_name(name_bytes, (uint)name_bytes.Length, out indexId); + FFI.index_id_from_name(name_bytes, name_bytes.Length, out indexId); } private static void ToParams( @@ -53,12 +53,12 @@ protected uint DoDelete(Bounds bounds) FFI.datastore_delete_by_index_scan_range_bsatn( indexId, prefix, - (uint)prefix.Length, + prefix.Length, prefixElems, rstart, - (uint)rstart.Length, + rstart.Length, rend, - (uint)rend.Length, + rend.Length, out var out_ ); return out_; @@ -73,12 +73,12 @@ protected override void IterStart(out FFI.RowIter handle) FFI.datastore_index_scan_range_bsatn( indexId, prefix, - (uint)prefix.Length, + prefix.Length, prefixElems, rstart, - (uint)rstart.Length, + rstart.Length, rend, - (uint)rend.Length, + rend.Length, out handle ); } @@ -102,7 +102,7 @@ public abstract class UniqueIndex(string name) : IndexBase { protected override void IterStart(out FFI.RowIter handle) => - FFI.datastore_index_scan_point_bsatn(indexId, point, (uint)point.Length, out handle); + FFI.datastore_index_scan_point_bsatn(indexId, point, point.Length, out handle); } protected IEnumerable DoFilter(T key) => DoFilter(ToBounds(key)); @@ -116,7 +116,7 @@ public bool Delete(T key) FFI.datastore_delete_by_index_scan_point_bsatn( indexId, point, - (uint)point.Length, + point.Length, out var numDeleted ); return numDeleted > 0; @@ -148,7 +148,7 @@ protected Row DoUpdate(Row row) { // Insert the row. var bytes = IStructuralReadWrite.ToBytes(row); - var bytes_len = (uint)bytes.Length; + var bytes_len = bytes.Length; FFI.datastore_update_bsatn(ITableView.tableId, indexId, bytes, ref bytes_len); return ITableView.IntegrateGeneratedColumns(row, bytes, bytes_len); @@ -165,7 +165,7 @@ public abstract class RefUniqueIndex(string name) : IndexBas private sealed class RawPointIter(FFI.IndexId indexId, byte[] point) : RawTableIterBase { protected override void IterStart(out FFI.RowIter handle) => - FFI.datastore_index_scan_point_bsatn(indexId, point, (uint)point.Length, out handle); + FFI.datastore_index_scan_point_bsatn(indexId, point, point.Length, out handle); } protected IEnumerable DoFilter(T key) => DoFilter(ToBounds(key)); @@ -179,7 +179,7 @@ public bool Delete(T key) FFI.datastore_delete_by_index_scan_point_bsatn( indexId, point, - (uint)point.Length, + point.Length, out var numDeleted ); return numDeleted > 0; @@ -211,7 +211,7 @@ protected Row DoUpdate(Row row) { // Insert the row. var bytes = IStructuralReadWrite.ToBytes(row); - var bytes_len = (uint)bytes.Length; + var bytes_len = bytes.Length; FFI.datastore_update_bsatn(ITableView.tableId, indexId, bytes, ref bytes_len); return ITableView.IntegrateGeneratedColumns(row, bytes, bytes_len); @@ -229,7 +229,7 @@ public abstract class ReadOnlyUniqueIndex(string name) private sealed class RawPointIter(FFI.IndexId indexId, byte[] point) : RawTableIterBase { protected override void IterStart(out FFI.RowIter handle) => - FFI.datastore_index_scan_point_bsatn(indexId, point, (uint)point.Length, out handle); + FFI.datastore_index_scan_point_bsatn(indexId, point, point.Length, out handle); } protected IEnumerable Filter(T key) => Filter(ToBounds(key)); @@ -268,7 +268,7 @@ public abstract class ReadOnlyRefUniqueIndex(string name) private sealed class RawPointIter(FFI.IndexId indexId, byte[] point) : RawTableIterBase { protected override void IterStart(out FFI.RowIter handle) => - FFI.datastore_index_scan_point_bsatn(indexId, point, (uint)point.Length, out handle); + FFI.datastore_index_scan_point_bsatn(indexId, point, point.Length, out handle); } protected IEnumerable Filter(T key) => Filter(ToBounds(key)); @@ -310,7 +310,7 @@ protected override void IterStart(out FFI.RowIter handle) => protected ReadOnlyTableView(string tableName) { var nameBytes = Encoding.UTF8.GetBytes(tableName); - FFI.table_id_from_name(nameBytes, (uint)nameBytes.Length, out tableId); + FFI.table_id_from_name(nameBytes, nameBytes.Length, out tableId); } protected ulong DoCount() diff --git a/crates/bindings-csharp/Runtime/Internal/IReducer.cs b/crates/bindings-csharp/Runtime/Internal/IReducer.cs index 878c98a2a2e..d51b5a03206 100644 --- a/crates/bindings-csharp/Runtime/Internal/IReducer.cs +++ b/crates/bindings-csharp/Runtime/Internal/IReducer.cs @@ -32,9 +32,9 @@ public static void VolatileNonatomicScheduleImmediate(string name, MemoryStream FFI.volatile_nonatomic_schedule_immediate( name_bytes, - (uint)name_bytes.Length, + name_bytes.Length, args_bytes, - (uint)args_bytes.Length + args_bytes.Length ); } } diff --git a/crates/bindings-csharp/Runtime/Internal/ITable.cs b/crates/bindings-csharp/Runtime/Internal/ITable.cs index a322216a1d3..9455c801473 100644 --- a/crates/bindings-csharp/Runtime/Internal/ITable.cs +++ b/crates/bindings-csharp/Runtime/Internal/ITable.cs @@ -24,23 +24,26 @@ public bool MoveNext() return false; } - uint buffer_len; + int bufferLen; + // This loop makes no sense and should be unnecessary. + // I (Lisandro 2026-07-09) am leaving the loop here because it's probably a similar case to the one in + // `Runtime/Internal/Module.cs#Consume` and in the future might become useful again. while (true) { - var requested_len = (uint)buffer.Length; - buffer_len = requested_len; - var ret = FFI.row_iter_bsatn_advance(handle, buffer, ref buffer_len); + var requestedLen = buffer.Length; + bufferLen = requestedLen; + var ret = FFI.row_iter_bsatn_advance(handle, buffer, ref bufferLen); if (ret == Errno.EXHAUSTED) { handle = FFI.RowIter.INVALID; - if (buffer_len == requested_len) + if (bufferLen == requestedLen) { - buffer_len = 0; + bufferLen = 0; } } // On success, the only way `buffer_len == 0` is for the iterator to be exhausted. // This happens when the host iterator was empty from the start. - System.Diagnostics.Debug.Assert(!(ret == Errno.OK && buffer_len == 0)); + System.Diagnostics.Debug.Assert(!(ret == Errno.OK && bufferLen == 0)); switch (ret) { // Iterator advanced and may also be `EXHAUSTED`. @@ -48,8 +51,8 @@ public bool MoveNext() // In both cases, update `Current` to point at the valid range in the scratch `buffer`. case Errno.EXHAUSTED or Errno.OK: - Current = new ArraySegment(buffer, 0, (int)buffer_len); - return buffer_len != 0; + Current = new ArraySegment(buffer, 0, bufferLen); + return bufferLen != 0; // Couldn't find the iterator, error! case Errno.NO_SUCH_ITER: throw new NoSuchIterException(); @@ -58,7 +61,7 @@ public bool MoveNext() // The `buffer_len` will have been updated with the necessary size. case Errno.BUFFER_TOO_SMALL: ArrayPool.Shared.Return(buffer); - buffer = ArrayPool.Shared.Rent((int)buffer_len); + buffer = ArrayPool.Shared.Rent(bufferLen); continue; default: throw new UnknownException(ret); @@ -142,7 +145,7 @@ protected override void IterStart(out FFI.RowIter handle) => new(() => { var name_bytes = System.Text.Encoding.UTF8.GetBytes(tableName); - FFI.table_id_from_name(name_bytes, (uint)name_bytes.Length, out var out_); + FFI.table_id_from_name(name_bytes, name_bytes.Length, out var out_); return out_; }); @@ -172,16 +175,16 @@ protected static T DoInsert(T row) { // Insert the row. var bytes = IStructuralReadWrite.ToBytes(row); - var bytes_len = (uint)bytes.Length; + var bytes_len = bytes.Length; FFI.datastore_insert_bsatn(tableId, bytes, ref bytes_len); return IntegrateGeneratedColumns(row, bytes, bytes_len); } // Writes back any generated column values. - static T IntegrateGeneratedColumns(T row, byte[] bytes, uint gen_len) + static T IntegrateGeneratedColumns(T row, byte[] bytes, int gen_len) { - using var stream = new MemoryStream(bytes, 0, (int)gen_len); + using var stream = new MemoryStream(bytes, 0, gen_len); using var reader = new BinaryReader(stream); return View.ReadGenFields(reader, row); } @@ -198,7 +201,7 @@ protected static bool DoDelete(T row) FFI.datastore_delete_all_by_eq_bsatn( tableId, stream.GetBuffer(), - (uint)stream.Length, + checked((int)stream.Length), out var out_ ); return out_ > 0; diff --git a/crates/bindings-csharp/Runtime/Internal/Module.cs b/crates/bindings-csharp/Runtime/Internal/Module.cs index eebd573cfc5..d6a635a8a98 100644 --- a/crates/bindings-csharp/Runtime/Internal/Module.cs +++ b/crates/bindings-csharp/Runtime/Internal/Module.cs @@ -458,8 +458,8 @@ public static byte[] Consume(this BytesSource source) throw new UnknownException(ret); } - var buffer = new byte[len]; - var written = 0U; + var buffer = new byte[checked((int)len)]; + var written = 0; // Because we've reserved space in our buffer already, this loop should be unnecessary. // We expect the first call to `bytes_source_read` to always return `-1`. // I (pgoldman 2025-09-26) am leaving the loop here because there's no downside to it, @@ -469,15 +469,15 @@ public static byte[] Consume(this BytesSource source) while (true) { // Write into the spare capacity of the buffer. - var spare = buffer.AsSpan((int)written); - var buf_len = (uint)spare.Length; + var spare = buffer.AsSpan(written); + var buf_len = spare.Length; ret = FFI.bytes_source_read(source, spare, ref buf_len); written += buf_len; switch (ret) { // Host side source exhausted, we're done. case Errno.EXHAUSTED: - Array.Resize(ref buffer, (int)written); + Array.Resize(ref buffer, written); return buffer; // Wrote the entire spare capacity. // Need to reserve more space in the buffer. @@ -498,15 +498,13 @@ public static byte[] Consume(this BytesSource source) } } - private static void Write(this BytesSink sink, byte[] bytes) + private static void Write(this BytesSink sink, ReadOnlySpan bytes) { - var start = 0U; - while (start != bytes.Length) + while (!bytes.IsEmpty) { - var written = (uint)bytes.Length; - var buffer = bytes.AsSpan((int)start); - FFI.bytes_sink_write(sink, buffer, ref written); - start += written; + var written = bytes.Length; + FFI.bytes_sink_write(sink, bytes, ref written); + bytes = bytes[written..]; } } @@ -529,7 +527,7 @@ public static void __describe_module__(BytesSink description) } public static Errno __call_reducer__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -544,10 +542,10 @@ BytesSink error try { var senderIdentity = Identity.From( - MemoryMarshal.AsBytes([sender_0, sender_1, sender_2, sender_3]).ToArray() + MemoryMarshal.AsBytes([sender_0, sender_1, sender_2, sender_3]) ); var connectionId = ConnectionId.From( - MemoryMarshal.AsBytes([conn_id_0, conn_id_1]).ToArray() + MemoryMarshal.AsBytes([conn_id_0, conn_id_1]) ); var random = new Random((int)timestamp.MicrosecondsSinceUnixEpoch); var time = timestamp.ToStd(); @@ -556,7 +554,7 @@ BytesSink error using var stream = new MemoryStream(args.Consume()); using var reader = new BinaryReader(stream); - reducers[(int)id].Invoke(reader, ctx); + reducers[id].Invoke(reader, ctx); if (stream.Position != stream.Length) { throw new Exception("Unrecognised extra bytes in the reducer arguments"); @@ -573,7 +571,7 @@ BytesSink error } public static Errno __call_procedure__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -588,10 +586,10 @@ BytesSink resultSink try { var sender = Identity.From( - MemoryMarshal.AsBytes([sender_0, sender_1, sender_2, sender_3]).ToArray() + MemoryMarshal.AsBytes([sender_0, sender_1, sender_2, sender_3]) ); var connectionId = ConnectionId.From( - MemoryMarshal.AsBytes([conn_id_0, conn_id_1]).ToArray() + MemoryMarshal.AsBytes([conn_id_0, conn_id_1]) ); var random = new Random((int)timestamp.MicrosecondsSinceUnixEpoch); var time = timestamp.ToStd(); @@ -600,7 +598,7 @@ BytesSink resultSink using var stream = new MemoryStream(args.Consume()); using var reader = new BinaryReader(stream); - var bytes = procedures[(int)id].Invoke(reader, ctx); + var bytes = procedures[id].Invoke(reader, ctx); if (stream.Position != stream.Length) { throw new Exception("Unrecognised extra bytes in the procedure arguments"); @@ -620,7 +618,7 @@ BytesSink resultSink } public static Errno __call_http_handler__( - uint id, + int id, Timestamp timestamp, BytesSource request, BytesSource requestBody, @@ -643,7 +641,7 @@ BytesSink responseBodySink throw new Exception("Unrecognised extra bytes in the HTTP handler request"); } - var response = httpHandlers[(int)id] + var response = httpHandlers[id] .Invoke(ctx, SpacetimeDB.HttpClient.FromWire(requestWire, requestBody.Consume())); var (responseWire, responseBody) = SpacetimeDB.HttpClient.ToWire(response); responseSink.Write( @@ -685,7 +683,7 @@ BytesSink responseBodySink /// /// public static Errno __call_view__( - uint id, + int id, ulong sender_0, ulong sender_1, ulong sender_2, @@ -697,12 +695,12 @@ BytesSink rows try { var sender = Identity.From( - MemoryMarshal.AsBytes([sender_0, sender_1, sender_2, sender_3]).ToArray() + MemoryMarshal.AsBytes([sender_0, sender_1, sender_2, sender_3]) ); var ctx = newViewContext!(sender); using var stream = new MemoryStream(args.Consume()); using var reader = new BinaryReader(stream); - var bytes = viewDispatchers[(int)id].Invoke(reader, ctx); + var bytes = viewDispatchers[id].Invoke(reader, ctx); rows.Write(bytes); return (Errno)2; } @@ -733,14 +731,14 @@ BytesSink rows /// The current ABI is identified by returning error code 2. /// /// - public static Errno __call_view_anon__(uint id, BytesSource args, BytesSink rows) + public static Errno __call_view_anon__(int id, BytesSource args, BytesSink rows) { try { var ctx = newAnonymousViewContext!(); using var stream = new MemoryStream(args.Consume()); using var reader = new BinaryReader(stream); - var bytes = anonymousViewDispatchers[(int)id].Invoke(reader, ctx); + var bytes = anonymousViewDispatchers[id].Invoke(reader, ctx); rows.Write(bytes); return (Errno)2; } diff --git a/crates/bindings-csharp/Runtime/Internal/Procedure.cs b/crates/bindings-csharp/Runtime/Internal/Procedure.cs index 81d2be9a749..becd184d13b 100644 --- a/crates/bindings-csharp/Runtime/Internal/Procedure.cs +++ b/crates/bindings-csharp/Runtime/Internal/Procedure.cs @@ -63,9 +63,9 @@ public static void VolatileNonatomicScheduleImmediate(string name, MemoryStream { FFI.volatile_nonatomic_schedule_immediate( name_bytes, - (uint)name_bytes.Length, + name_bytes.Length, args_bytes, - (uint)args_bytes.Length + args_bytes.Length ); } catch (Exception ex) diff --git a/crates/bindings-csharp/Runtime/Log.cs b/crates/bindings-csharp/Runtime/Log.cs index 55a78afde85..1526d32caf0 100644 --- a/crates/bindings-csharp/Runtime/Log.cs +++ b/crates/bindings-csharp/Runtime/Log.cs @@ -168,12 +168,12 @@ int lineNumber FFI.console_log( level, target_bytes, - (uint)target_bytes.Length, + target_bytes.Length, filename_bytes, - (uint)filename_bytes.Length, + filename_bytes.Length, (uint)lineNumber, text_bytes, - (uint)text_bytes.Length + text_bytes.Length ); } } diff --git a/crates/bindings-csharp/Runtime/LogStopwatch.cs b/crates/bindings-csharp/Runtime/LogStopwatch.cs index 4804e1e77ad..5ce00519d98 100644 --- a/crates/bindings-csharp/Runtime/LogStopwatch.cs +++ b/crates/bindings-csharp/Runtime/LogStopwatch.cs @@ -11,7 +11,7 @@ public sealed class LogStopwatch : IDisposable public LogStopwatch(string name) { var name_bytes = Encoding.UTF8.GetBytes(name); - StopwatchId = FFI.console_timer_start(name_bytes, (uint)name_bytes.Length); + StopwatchId = FFI.console_timer_start(name_bytes, name_bytes.Length); } void IDisposable.Dispose() From 0c14b12fd39c18a086fe406a4aa21b37c600b90f Mon Sep 17 00:00:00 2001 From: Lisandro Crespo Date: Thu, 9 Jul 2026 11:40:17 -0500 Subject: [PATCH 2/3] Remove incorrect comment --- crates/bindings-csharp/Runtime/Internal/ITable.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/crates/bindings-csharp/Runtime/Internal/ITable.cs b/crates/bindings-csharp/Runtime/Internal/ITable.cs index 9455c801473..382fbc94ae7 100644 --- a/crates/bindings-csharp/Runtime/Internal/ITable.cs +++ b/crates/bindings-csharp/Runtime/Internal/ITable.cs @@ -23,15 +23,11 @@ public bool MoveNext() { return false; } - - int bufferLen; - // This loop makes no sense and should be unnecessary. - // I (Lisandro 2026-07-09) am leaving the loop here because it's probably a similar case to the one in - // `Runtime/Internal/Module.cs#Consume` and in the future might become useful again. + while (true) { var requestedLen = buffer.Length; - bufferLen = requestedLen; + var bufferLen = requestedLen; var ret = FFI.row_iter_bsatn_advance(handle, buffer, ref bufferLen); if (ret == Errno.EXHAUSTED) { From c8b4a8bb728cad2e16908dd89e9c17bb4c633518 Mon Sep 17 00:00:00 2001 From: Lisandro Crespo Date: Tue, 28 Jul 2026 14:42:13 -0500 Subject: [PATCH 3/3] Fix lint --- crates/bindings-csharp/Runtime/Internal/ITable.cs | 2 +- crates/bindings-csharp/Runtime/Internal/Module.cs | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/bindings-csharp/Runtime/Internal/ITable.cs b/crates/bindings-csharp/Runtime/Internal/ITable.cs index 382fbc94ae7..e7acaa0b216 100644 --- a/crates/bindings-csharp/Runtime/Internal/ITable.cs +++ b/crates/bindings-csharp/Runtime/Internal/ITable.cs @@ -23,7 +23,7 @@ public bool MoveNext() { return false; } - + while (true) { var requestedLen = buffer.Length; diff --git a/crates/bindings-csharp/Runtime/Internal/Module.cs b/crates/bindings-csharp/Runtime/Internal/Module.cs index d6a635a8a98..d850b930c77 100644 --- a/crates/bindings-csharp/Runtime/Internal/Module.cs +++ b/crates/bindings-csharp/Runtime/Internal/Module.cs @@ -544,9 +544,7 @@ BytesSink error var senderIdentity = Identity.From( MemoryMarshal.AsBytes([sender_0, sender_1, sender_2, sender_3]) ); - var connectionId = ConnectionId.From( - MemoryMarshal.AsBytes([conn_id_0, conn_id_1]) - ); + var connectionId = ConnectionId.From(MemoryMarshal.AsBytes([conn_id_0, conn_id_1])); var random = new Random((int)timestamp.MicrosecondsSinceUnixEpoch); var time = timestamp.ToStd(); @@ -588,9 +586,7 @@ BytesSink resultSink var sender = Identity.From( MemoryMarshal.AsBytes([sender_0, sender_1, sender_2, sender_3]) ); - var connectionId = ConnectionId.From( - MemoryMarshal.AsBytes([conn_id_0, conn_id_1]) - ); + var connectionId = ConnectionId.From(MemoryMarshal.AsBytes([conn_id_0, conn_id_1])); var random = new Random((int)timestamp.MicrosecondsSinceUnixEpoch); var time = timestamp.ToStd();