Skip to content

Commit 2b69583

Browse files
authored
WASM ABI: get_table_id -> table_id_from_name (#1634)
1 parent 1dd1e63 commit 2b69583

File tree

14 files changed

+56
-44
lines changed

14 files changed

+56
-44
lines changed

crates/bindings-csharp/Runtime/Exceptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ public abstract class StdbException : Exception
77
public abstract override string Message { get; }
88
}
99

10-
public class NoSuchTableException : StdbException
10+
public class NotInTransactionException : StdbException
1111
{
12-
public override string Message => "No such table";
12+
public override string Message => "ABI call can only be made while in a transaction";
1313
}
1414

15-
public class LookupNotFoundException : StdbException
15+
public class NoSuchTableException : StdbException
1616
{
17-
public override string Message => "Value or range provided not found in table";
17+
public override string Message => "No such table";
1818
}
1919

2020
public class UniqueAlreadyExistsException : StdbException

crates/bindings-csharp/Runtime/Internal/FFI.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public enum Errno : short
2323
EXHAUSTED = -1,
2424
OK = 0,
2525
HOST_CALL_FAILURE = 1,
26+
NOT_IN_TRANSACTION = 2,
2627
NO_SUCH_TABLE = 4,
27-
LOOKUP_NOT_FOUND = 2,
2828
NO_SUCH_ITER = 6,
2929
NO_SUCH_BYTES = 8,
3030
NO_SPACE = 9,
@@ -69,8 +69,8 @@ public static CheckedStatus ConvertToManaged(Errno status)
6969
}
7070
throw status switch
7171
{
72+
Errno.NOT_IN_TRANSACTION => new NotInTransactionException(),
7273
Errno.NO_SUCH_TABLE => new NoSuchTableException(),
73-
Errno.LOOKUP_NOT_FOUND => new LookupNotFoundException(),
7474
Errno.UNIQUE_ALREADY_EXISTS => new UniqueAlreadyExistsException(),
7575
Errno.BUFFER_TOO_SMALL => new BufferTooSmallException(),
7676
Errno.NO_SUCH_BYTES => new NoSuchBytesException(),
@@ -114,7 +114,7 @@ public readonly record struct RowIter(uint Handle)
114114
}
115115

116116
[LibraryImport(StdbNamespace)]
117-
public static partial CheckedStatus _get_table_id(
117+
public static partial CheckedStatus _table_id_from_name(
118118
[In] byte[] name,
119119
uint name_len,
120120
out TableId out_

crates/bindings-csharp/Runtime/Internal/ITable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected override void IterStart(out FFI.RowIter handle) =>
135135
new(() =>
136136
{
137137
var name_bytes = System.Text.Encoding.UTF8.GetBytes(typeof(T).Name);
138-
FFI._get_table_id(name_bytes, (uint)name_bytes.Length, out var out_);
138+
FFI._table_id_from_name(name_bytes, (uint)name_bytes.Length, out var out_);
139139
return out_;
140140
});
141141

crates/bindings-csharp/Runtime/bindings.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ IMPORT(void, _console_log,
4242
(level, target, target_len, filename, filename_len, line_number, message,
4343
message_len));
4444

45-
IMPORT(Status, _get_table_id,
45+
IMPORT(Status, _table_id_from_name,
4646
(const uint8_t* name, uint32_t name_len, TableId* id),
4747
(name, name_len, id));
4848
IMPORT(Status, _iter_by_col_eq,

crates/bindings-csharp/Runtime/build/SpacetimeDB.Runtime.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<NativeLibrary Include="$(MSBuildThisFileDirectory)..\bindings.c" />
55
<UnmanagedEntryPointsAssembly Include="SpacetimeDB.Runtime" />
66
<WasmImport Include="$(SpacetimeNamespace)!_console_log" />
7-
<WasmImport Include="$(SpacetimeNamespace)!_get_table_id" />
87
<WasmImport Include="$(SpacetimeNamespace)!_iter_by_col_eq" />
98
<WasmImport Include="$(SpacetimeNamespace)!_insert" />
109
<WasmImport Include="$(SpacetimeNamespace)!_delete_by_col_eq" />
1110
<WasmImport Include="$(SpacetimeNamespace)!_delete_by_rel" />
1211
<WasmImport Include="$(SpacetimeNamespace)!_iter_start" />
1312
<WasmImport Include="$(SpacetimeNamespace)!_iter_start_filtered" />
13+
<WasmImport Include="$(SpacetimeNamespace)!_table_id_from_name" />
1414
<WasmImport Include="$(SpacetimeNamespace)!_row_iter_bsatn_advance" />
1515
<WasmImport Include="$(SpacetimeNamespace)!_row_iter_bsatn_close" />
1616
<WasmImport Include="$(SpacetimeNamespace)!_bytes_source_read" />

crates/bindings-macro/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,11 @@ fn spacetimedb_tabletype_impl(item: syn::DeriveInput) -> syn::Result<TokenStream
567567

568568
let mut columns = Vec::<Column>::new();
569569

570-
let get_table_id_func = quote! {
570+
let table_id_from_name_func = quote! {
571571
fn table_id() -> spacetimedb::TableId {
572572
static TABLE_ID: std::sync::OnceLock<spacetimedb::TableId> = std::sync::OnceLock::new();
573573
*TABLE_ID.get_or_init(|| {
574-
spacetimedb::get_table_id(<Self as spacetimedb::TableType>::TABLE_NAME)
574+
spacetimedb::table_id_from_name(<Self as spacetimedb::TableType>::TABLE_NAME)
575575
})
576576
}
577577
};
@@ -774,7 +774,7 @@ fn spacetimedb_tabletype_impl(item: syn::DeriveInput) -> syn::Result<TokenStream
774774
];
775775
const INDEXES: &'static [spacetimedb::IndexDesc<'static>] = &[#(#indexes),*];
776776
type InsertResult = #insert_result;
777-
#get_table_id_func
777+
#table_id_from_name_func
778778
}
779779
};
780780

crates/bindings-sys/src/lib.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,24 @@ pub mod raw {
2121
#[link(wasm_import_module = "spacetime_10.0")]
2222
extern "C" {
2323
/// Queries the `table_id` associated with the given (table) `name`
24-
/// where `name` points to a UTF-8 slice in WASM memory of `name_len` bytes.
24+
/// where `name` is the UTF-8 slice in WASM memory at `name_ptr[..name_len]`.
2525
///
2626
/// The table id is written into the `out` pointer.
2727
///
28-
/// Returns an error if
29-
/// - a table with the provided `table_id` doesn't exist
30-
/// - the slice `(name, name_len)` is not valid UTF-8
31-
/// - `name + name_len` overflows a 64-bit address.
32-
/// - writing to `out` overflows a 32-bit integer
33-
pub fn _get_table_id(name: *const u8, name_len: usize, out: *mut TableId) -> u16;
28+
/// # Traps
29+
///
30+
/// Traps if:
31+
/// - `name_ptr` is NULL or `name` is not in bounds of WASM memory.
32+
/// - `name` is not valid UTF-8.
33+
/// - `out` is NULL or `out[..size_of::<TableId>()]` is not in bounds of WASM memory.
34+
///
35+
/// # Errors
36+
///
37+
/// Returns an error:
38+
///
39+
/// - `NOT_IN_TRANSACTION`, when called outside of a transaction.
40+
/// - `NO_SUCH_TABLE`, when `name` is not the name of a table.
41+
pub fn _table_id_from_name(name: *const u8, name_len: usize, out: *mut TableId) -> u16;
3442

3543
/// Finds all rows in the table identified by `table_id`,
3644
/// where the row has a column, identified by `col_id`,
@@ -511,8 +519,8 @@ unsafe fn call<T: Copy>(f: impl FnOnce(*mut T) -> u16) -> Result<T, Errno> {
511519
///
512520
/// Returns an error if the table does not exist.
513521
#[inline]
514-
pub fn get_table_id(name: &str) -> Result<TableId, Errno> {
515-
unsafe { call(|out| raw::_get_table_id(name.as_ptr(), name.len(), out)) }
522+
pub fn table_id_from_name(name: &str) -> Result<TableId, Errno> {
523+
unsafe { call(|out| raw::_table_id_from_name(name.as_ptr(), name.len(), out)) }
516524
}
517525

518526
/// Finds all rows in the table identified by `table_id`,

crates/bindings/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ pub fn decode_schema<'a>(bytes: &mut impl BufReader<'a>) -> Result<ProductType,
116116
/// Queries and returns the `table_id` associated with the given (table) `name`.
117117
///
118118
/// Panics if the table does not exist.
119-
pub fn get_table_id(table_name: &str) -> TableId {
120-
sys::get_table_id(table_name).unwrap_or_else(|_| {
119+
pub fn table_id_from_name(table_name: &str) -> TableId {
120+
sys::table_id_from_name(table_name).unwrap_or_else(|_| {
121121
panic!("Failed to get table with name: {}", table_name);
122122
})
123123
}

crates/core/src/error.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,6 @@ pub enum NodesError {
322322
DecodeFilter(#[source] DecodeError),
323323
#[error("table with provided name or id doesn't exist")]
324324
TableNotFound,
325-
#[error("row with column of given value not found")]
326-
ColumnValueNotFound,
327-
#[error("range of rows not found")]
328-
RangeNotFound,
329325
#[error("column is out of bounds")]
330326
BadColumn,
331327
#[error("can't perform operation; not inside transaction")]

crates/core/src/host/instance_env.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ impl InstanceEnv {
187187

188188
/// Returns the `table_id` associated with the given `table_name`.
189189
///
190-
/// Errors with `TableNotFound` if the table does not exist.
190+
/// Errors with `GetTxError` if not in a transaction
191+
/// and `TableNotFound` if the table does not exist.
191192
#[tracing::instrument(skip_all)]
192-
pub fn get_table_id(&self, table_name: &str) -> Result<TableId, NodesError> {
193+
pub fn table_id_from_name(&self, table_name: &str) -> Result<TableId, NodesError> {
193194
let stdb = &*self.dbic.relational_db;
194195
let tx = &mut *self.get_tx()?;
195196

0 commit comments

Comments
 (0)