From 5acc25200a0c29145ae44bd241064e34863fdf2b Mon Sep 17 00:00:00 2001 From: Nasr Date: Thu, 4 Jul 2024 11:28:45 -0400 Subject: [PATCH 1/3] feat: update c bindings to new 0.7.3 --- Cargo.lock | 32 ++++++++++---------- Cargo.toml | 8 ++--- dojo.h | 57 ++++++++++++------------------------ dojo.hpp | 45 ++++------------------------ src/c/mod.rs | 8 ++--- src/c/types.rs | 79 ++++++++++++++------------------------------------ 6 files changed, 69 insertions(+), 160 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 97d191d..9677b58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2347,8 +2347,8 @@ dependencies = [ [[package]] name = "dojo-lang" -version = "0.7.3-alpha.1" -source = "git+https://github.com/dojoengine/dojo?rev=2ea37a177fcfeedfebf6be8a55a462a752bbe4ec#2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" +version = "0.7.3" +source = "git+https://github.com/dojoengine/dojo?rev=e8539d5b199efbd51c89c75da32f6592de5d9262#e8539d5b199efbd51c89c75da32f6592de5d9262" dependencies = [ "anyhow", "cairo-lang-compiler", @@ -2394,8 +2394,8 @@ dependencies = [ [[package]] name = "dojo-types" -version = "0.7.3-alpha.1" -source = "git+https://github.com/dojoengine/dojo?rev=2ea37a177fcfeedfebf6be8a55a462a752bbe4ec#2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" +version = "0.7.3" +source = "git+https://github.com/dojoengine/dojo?rev=e8539d5b199efbd51c89c75da32f6592de5d9262#e8539d5b199efbd51c89c75da32f6592de5d9262" dependencies = [ "cainome 0.2.3 (git+https://github.com/cartridge-gg/cainome?rev=ec18eea5)", "crypto-bigint", @@ -2411,8 +2411,8 @@ dependencies = [ [[package]] name = "dojo-world" -version = "0.7.3-alpha.1" -source = "git+https://github.com/dojoengine/dojo?rev=2ea37a177fcfeedfebf6be8a55a462a752bbe4ec#2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" +version = "0.7.3" +source = "git+https://github.com/dojoengine/dojo?rev=e8539d5b199efbd51c89c75da32f6592de5d9262#e8539d5b199efbd51c89c75da32f6592de5d9262" dependencies = [ "anyhow", "async-trait", @@ -8125,8 +8125,8 @@ dependencies = [ [[package]] name = "sozo-ops" -version = "0.7.3-alpha.1" -source = "git+https://github.com/dojoengine/dojo?rev=2ea37a177fcfeedfebf6be8a55a462a752bbe4ec#2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" +version = "0.7.3" +source = "git+https://github.com/dojoengine/dojo?rev=e8539d5b199efbd51c89c75da32f6592de5d9262#e8539d5b199efbd51c89c75da32f6592de5d9262" dependencies = [ "anyhow", "async-trait", @@ -9233,8 +9233,8 @@ checksum = "ea68304e134ecd095ac6c3574494fc62b909f416c4fca77e440530221e549d3d" [[package]] name = "torii-client" -version = "0.7.3-alpha.1" -source = "git+https://github.com/dojoengine/dojo?rev=2ea37a177fcfeedfebf6be8a55a462a752bbe4ec#2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" +version = "0.7.3" +source = "git+https://github.com/dojoengine/dojo?rev=e8539d5b199efbd51c89c75da32f6592de5d9262#e8539d5b199efbd51c89c75da32f6592de5d9262" dependencies = [ "async-trait", "crypto-bigint", @@ -9261,8 +9261,8 @@ dependencies = [ [[package]] name = "torii-core" -version = "0.7.3-alpha.1" -source = "git+https://github.com/dojoengine/dojo?rev=2ea37a177fcfeedfebf6be8a55a462a752bbe4ec#2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" +version = "0.7.3" +source = "git+https://github.com/dojoengine/dojo?rev=e8539d5b199efbd51c89c75da32f6592de5d9262#e8539d5b199efbd51c89c75da32f6592de5d9262" dependencies = [ "anyhow", "async-trait", @@ -9296,8 +9296,8 @@ dependencies = [ [[package]] name = "torii-grpc" -version = "0.7.3-alpha.1" -source = "git+https://github.com/dojoengine/dojo?rev=2ea37a177fcfeedfebf6be8a55a462a752bbe4ec#2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" +version = "0.7.3" +source = "git+https://github.com/dojoengine/dojo?rev=e8539d5b199efbd51c89c75da32f6592de5d9262#e8539d5b199efbd51c89c75da32f6592de5d9262" dependencies = [ "bytes", "crypto-bigint", @@ -9335,8 +9335,8 @@ dependencies = [ [[package]] name = "torii-relay" -version = "0.7.3-alpha.1" -source = "git+https://github.com/dojoengine/dojo?rev=2ea37a177fcfeedfebf6be8a55a462a752bbe4ec#2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" +version = "0.7.3" +source = "git+https://github.com/dojoengine/dojo?rev=e8539d5b199efbd51c89c75da32f6592de5d9262#e8539d5b199efbd51c89c75da32f6592de5d9262" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 2902a76..2f940c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,12 +7,12 @@ version = "0.7.0" crate-type = ["cdylib", "rlib", "staticlib"] [dependencies] -dojo-types = { git = "https://github.com/dojoengine/dojo", rev = "2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" } -torii-client = { git = "https://github.com/dojoengine/dojo", rev = "2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" } +dojo-types = { git = "https://github.com/dojoengine/dojo", rev = "e8539d5b199efbd51c89c75da32f6592de5d9262" } +torii-client = { git = "https://github.com/dojoengine/dojo", rev = "e8539d5b199efbd51c89c75da32f6592de5d9262" } torii-grpc = { git = "https://github.com/dojoengine/dojo", features = [ "client", -], rev = "2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" } -torii-relay = { git = "https://github.com/dojoengine/dojo", rev = "2ea37a177fcfeedfebf6be8a55a462a752bbe4ec" } +], rev = "e8539d5b199efbd51c89c75da32f6592de5d9262" } +torii-relay = { git = "https://github.com/dojoengine/dojo", rev = "e8539d5b199efbd51c89c75da32f6592de5d9262" } parking_lot = "0.12.1" starknet = "0.10.0" diff --git a/dojo.h b/dojo.h index b0659a9..692af43 100644 --- a/dojo.h +++ b/dojo.h @@ -228,36 +228,22 @@ typedef struct Ty { }; } Ty; -typedef enum COptionTy_Tag { - SomeTy, - NoneTy, -} COptionTy_Tag; +typedef enum ResultTy_Tag { + OkTy, + ErrTy, +} ResultTy_Tag; -typedef struct COptionTy { - COptionTy_Tag tag; +typedef struct ResultTy { + ResultTy_Tag tag; union { struct { - struct Ty *some; - }; - }; -} COptionTy; - -typedef enum ResultCOptionTy_Tag { - OkCOptionTy, - ErrCOptionTy, -} ResultCOptionTy_Tag; - -typedef struct ResultCOptionTy { - ResultCOptionTy_Tag tag; - union { - struct { - struct COptionTy ok; + struct Ty *ok; }; struct { struct Error err; }; }; -} ResultCOptionTy; +} ResultTy; typedef struct CArrayFieldElement { struct FieldElement *data; @@ -306,13 +292,20 @@ typedef struct ResultCArrayEntity { }; } ResultCArrayEntity; +typedef const struct FieldElement *COptionFieldElement; + +typedef struct CArrayCOptionFieldElement { + COptionFieldElement *data; + uintptr_t data_len; +} CArrayCOptionFieldElement; + typedef struct CArrayc_char { const char **data; uintptr_t data_len; } CArrayc_char; typedef struct KeysClause { - struct CArrayFieldElement keys; + struct CArrayCOptionFieldElement keys; enum PatternMatching pattern_matching; struct CArrayc_char models; } KeysClause; @@ -390,24 +383,12 @@ typedef struct Clause { }; } Clause; -typedef enum COptionClause_Tag { - SomeClause, - NoneClause, -} COptionClause_Tag; - -typedef struct COptionClause { - COptionClause_Tag tag; - union { - struct { - struct Clause some; - }; - }; -} COptionClause; +typedef const struct Clause *COptionClause; typedef struct Query { uint32_t limit; uint32_t offset; - struct COptionClause clause; + COptionClause clause; } Query; typedef struct CArrayModelKeysClause { @@ -637,7 +618,7 @@ struct ResultCArrayu8 client_publish_message(struct ToriiClient *client, const char *message, struct Signature signature); -struct ResultCOptionTy client_model(struct ToriiClient *client, const struct ModelKeysClause *keys); +struct ResultTy client_model(struct ToriiClient *client, const struct ModelKeysClause *keys); struct ResultCArrayEntity client_entities(struct ToriiClient *client, const struct Query *query); diff --git a/dojo.hpp b/dojo.hpp index a03f811..b16608c 100644 --- a/dojo.hpp +++ b/dojo.hpp @@ -471,44 +471,6 @@ struct Ty { } }; -template -struct COption { - enum class Tag { - Some, - None, - }; - - struct Some_Body { - T _0; - }; - - Tag tag; - union { - Some_Body some; - }; - - static COption Some(const T &_0) { - COption result; - ::new (&result.some._0) (T)(_0); - result.tag = Tag::Some; - return result; - } - - bool IsSome() const { - return tag == Tag::Some; - } - - static COption None() { - COption result; - result.tag = Tag::None; - return result; - } - - bool IsNone() const { - return tag == Tag::None; - } -}; - struct ModelKeysClause { CArray keys; const char *model; @@ -524,8 +486,11 @@ struct Entity { CArray models; }; +template +using COption = const T*; + struct KeysClause { - CArray keys; + CArray> keys; PatternMatching pattern_matching; CArray models; }; @@ -851,7 +816,7 @@ Result> client_publish_message(ToriiClient *client, const char *message, Signature signature); -Result> client_model(ToriiClient *client, const ModelKeysClause *keys); +Result client_model(ToriiClient *client, const ModelKeysClause *keys); Result> client_entities(ToriiClient *client, const Query *query); diff --git a/src/c/mod.rs b/src/c/mod.rs index 363fa1b..46e324f 100644 --- a/src/c/mod.rs +++ b/src/c/mod.rs @@ -1,7 +1,7 @@ mod types; use self::types::{ - BlockId, CArray, COption, Call, Entity, Error, Model, Query, Result, Signature, ToriiClient, + BlockId, CArray, Call, Entity, Error, Model, Query, Result, Signature, ToriiClient, Ty, WorldMetadata, }; use crate::constants; @@ -105,16 +105,16 @@ pub unsafe extern "C" fn client_publish_message( pub unsafe extern "C" fn client_model( client: *mut ToriiClient, keys: &ModelKeysClause, -) -> Result> { +) -> Result<*mut Ty> { let keys = (&keys.clone()).into(); let entity_future = unsafe { (*client).inner.model(&keys) }; match (*client).runtime.block_on(entity_future) { Ok(ty) => { if let Some(ty) = ty { - Result::Ok(COption::Some(Box::into_raw(Box::new((&ty).into())))) + Result::Ok(Box::into_raw(Box::new((&ty).into()))) } else { - Result::Ok(COption::None) + Result::Ok(std::ptr::null_mut()) } } Err(e) => Result::Err(e.into()), diff --git a/src/c/types.rs b/src/c/types.rs index 3ca234e..525b05f 100644 --- a/src/c/types.rs +++ b/src/c/types.rs @@ -8,30 +8,8 @@ pub enum Result { Ok(T), Err(Error), } -#[derive(Debug, Clone)] -#[repr(C)] -pub enum COption { - Some(T), - None, -} -impl From> for COption { - fn from(val: Option) -> Self { - match val { - Some(v) => COption::Some(v), - None => COption::None, - } - } -} - -impl From> for Option { - fn from(val: COption) -> Self { - match val { - COption::Some(v) => Some(v), - COption::None => None, - } - } -} +type COption = *const T; #[derive(Debug, Clone)] #[repr(C)] @@ -243,7 +221,7 @@ pub enum PatternMatching { #[derive(Clone, Debug)] #[repr(C)] pub struct KeysClause { - pub keys: CArray, + pub keys: CArray>, pub pattern_matching: PatternMatching, pub models: CArray<*const c_char>, } @@ -737,40 +715,25 @@ impl From<&dojo_types::primitive::Primitive> for Primitive { impl From<&Query> for torii_grpc::types::Query { fn from(val: &Query) -> Self { - match &val.clause { - COption::Some(clause) => { - let clause = (&clause.clone()).into(); - torii_grpc::types::Query { - limit: val.limit, - offset: val.offset, - clause: Option::Some(clause), - } - } - COption::None => torii_grpc::types::Query { - limit: val.limit, - offset: val.offset, - clause: Option::None, - }, + Self { + limit: val.limit, + offset: val.offset, + clause: unsafe { val.clause.as_ref().map(|c| c.into()) }, } } } impl From<&torii_grpc::types::Query> for Query { fn from(val: &torii_grpc::types::Query) -> Self { - match &val.clause { - Option::Some(clause) => { - let clause = (&clause.clone()).into(); - Query { - limit: val.limit, - offset: val.offset, - clause: COption::Some(clause), - } - } - Option::None => Query { - limit: val.limit, - offset: val.offset, - clause: COption::None, - }, + Query { + limit: val.limit, + offset: val.offset, + // convert option to ptr + clause: val + .clause + .as_ref() + .map(|c| Box::into_raw(Box::new(c.into())) as *const _) + .unwrap_or(std::ptr::null()), } } } @@ -849,7 +812,7 @@ impl From<&torii_grpc::types::EntityKeysClause> for EntityKeysClause { impl From<&KeysClause> for torii_grpc::types::KeysClause { fn from(val: &KeysClause) -> Self { - let keys: Vec = (&val.keys).into(); + let keys: Vec<_> = (&val.keys).into(); let keys = std::mem::ManuallyDrop::new(keys); let models: Vec<*const c_char> = (&val.models).into(); @@ -858,8 +821,8 @@ impl From<&KeysClause> for torii_grpc::types::KeysClause { torii_grpc::types::KeysClause { keys: keys .iter() - .map(Into::into) - .collect::>(), + .map(|o| unsafe { o.as_ref().map(|k| k.into()) }) + .collect::>(), pattern_matching: (&val.pattern_matching).into(), models: models .iter() @@ -874,13 +837,13 @@ impl From<&torii_grpc::types::KeysClause> for KeysClause { let keys = val .keys .iter() - .map(|k| k.into()) - .collect::>(); + .map(|k| k.as_ref().map(|k| Box::into_raw(Box::new(k.clone())) as *const _).unwrap_or(std::ptr::null())) + .collect::>(); let models = val .models .iter() .map(|m| CString::new(m.clone()).unwrap().into_raw() as *const c_char) - .collect::>(); + .collect::>(); KeysClause { models: models.into(), From 6716e71cd783aaedb10fd96c77e091982d66a2b6 Mon Sep 17 00:00:00 2001 From: Nasr Date: Thu, 4 Jul 2024 11:55:46 -0400 Subject: [PATCH 2/3] refactor; using option non null --- src/c/types.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/c/types.rs b/src/c/types.rs index 525b05f..e22c94e 100644 --- a/src/c/types.rs +++ b/src/c/types.rs @@ -1,5 +1,8 @@ use starknet::core::utils::get_selector_from_name; -use std::ffi::{c_char, CStr, CString}; +use std::{ + ffi::{c_char, CStr, CString}, + ptr::NonNull, +}; use torii_client::client::Client; #[derive(Debug, Clone)] @@ -9,8 +12,6 @@ pub enum Result { Err(Error), } -type COption = *const T; - #[derive(Debug, Clone)] #[repr(C)] pub struct Signature { @@ -193,7 +194,8 @@ impl From<&starknet::core::types::FieldElement> for FieldElement { pub struct Query { pub limit: u32, pub offset: u32, - pub clause: COption, + // nullable + pub clause: Option>, } #[derive(Clone, Debug)] @@ -221,7 +223,7 @@ pub enum PatternMatching { #[derive(Clone, Debug)] #[repr(C)] pub struct KeysClause { - pub keys: CArray>, + pub keys: CArray<*mut FieldElement>, pub pattern_matching: PatternMatching, pub models: CArray<*const c_char>, } @@ -718,22 +720,24 @@ impl From<&Query> for torii_grpc::types::Query { Self { limit: val.limit, offset: val.offset, - clause: unsafe { val.clause.as_ref().map(|c| c.into()) }, + clause: unsafe { val.clause.map(|c| c.as_ref().into()) }, } } } impl From<&torii_grpc::types::Query> for Query { fn from(val: &torii_grpc::types::Query) -> Self { + let clause_ptr = val + .clause + .as_ref() + .map(|c| Box::into_raw(Box::new(c.into()))) + .unwrap_or(std::ptr::null_mut()); + Query { limit: val.limit, offset: val.offset, // convert option to ptr - clause: val - .clause - .as_ref() - .map(|c| Box::into_raw(Box::new(c.into())) as *const _) - .unwrap_or(std::ptr::null()), + clause: NonNull::new(clause_ptr), } } } @@ -837,7 +841,11 @@ impl From<&torii_grpc::types::KeysClause> for KeysClause { let keys = val .keys .iter() - .map(|k| k.as_ref().map(|k| Box::into_raw(Box::new(k.clone())) as *const _).unwrap_or(std::ptr::null())) + .map(|k| { + k.as_ref() + .map(|k| Box::into_raw(Box::new(k.into()))) + .unwrap_or(std::ptr::null_mut()) + }) .collect::>(); let models = val .models From 9dbce5d1ac83228fde212cfc5efa75a4b586b6c7 Mon Sep 17 00:00:00 2001 From: Nasr Date: Thu, 4 Jul 2024 12:02:35 -0400 Subject: [PATCH 3/3] feat: update wasm --- dojo.h | 18 +++++++----------- dojo.hpp | 7 ++----- src/c/mod.rs | 4 ++-- src/wasm/mod.rs | 7 +++++-- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/dojo.h b/dojo.h index 692af43..f4c45a3 100644 --- a/dojo.h +++ b/dojo.h @@ -250,6 +250,11 @@ typedef struct CArrayFieldElement { uintptr_t data_len; } CArrayFieldElement; +typedef struct CArrayFieldElement { + struct FieldElement **data; + uintptr_t data_len; +} CArrayFieldElement; + typedef struct ModelKeysClause { struct CArrayFieldElement keys; const char *model; @@ -292,20 +297,13 @@ typedef struct ResultCArrayEntity { }; } ResultCArrayEntity; -typedef const struct FieldElement *COptionFieldElement; - -typedef struct CArrayCOptionFieldElement { - COptionFieldElement *data; - uintptr_t data_len; -} CArrayCOptionFieldElement; - typedef struct CArrayc_char { const char **data; uintptr_t data_len; } CArrayc_char; typedef struct KeysClause { - struct CArrayCOptionFieldElement keys; + struct CArrayFieldElement keys; enum PatternMatching pattern_matching; struct CArrayc_char models; } KeysClause; @@ -383,12 +381,10 @@ typedef struct Clause { }; } Clause; -typedef const struct Clause *COptionClause; - typedef struct Query { uint32_t limit; uint32_t offset; - COptionClause clause; + struct Clause *clause; } Query; typedef struct CArrayModelKeysClause { diff --git a/dojo.hpp b/dojo.hpp index b16608c..72a356e 100644 --- a/dojo.hpp +++ b/dojo.hpp @@ -486,11 +486,8 @@ struct Entity { CArray models; }; -template -using COption = const T*; - struct KeysClause { - CArray> keys; + CArray keys; PatternMatching pattern_matching; CArray models; }; @@ -670,7 +667,7 @@ struct Clause { struct Query { uint32_t limit; uint32_t offset; - COption clause; + Clause *clause; }; struct ModelMetadata { diff --git a/src/c/mod.rs b/src/c/mod.rs index 46e324f..e532b25 100644 --- a/src/c/mod.rs +++ b/src/c/mod.rs @@ -1,8 +1,8 @@ mod types; use self::types::{ - BlockId, CArray, Call, Entity, Error, Model, Query, Result, Signature, ToriiClient, - Ty, WorldMetadata, + BlockId, CArray, Call, Entity, Error, Model, Query, Result, Signature, ToriiClient, Ty, + WorldMetadata, }; use crate::constants; use crate::types::{Account, Provider, Subscription}; diff --git a/src/wasm/mod.rs b/src/wasm/mod.rs index fcb37fb..80e1739 100644 --- a/src/wasm/mod.rs +++ b/src/wasm/mod.rs @@ -233,7 +233,7 @@ impl From<&EntityKeysClause> for torii_grpc::types::EntityKeysClause { #[derive(Tsify, Serialize, Deserialize, Debug)] #[tsify(into_wasm_abi, from_wasm_abi)] pub struct KeysClause { - pub keys: Vec, + pub keys: Vec>, pub pattern_matching: PatternMatching, pub models: Vec, } @@ -274,7 +274,10 @@ impl From<&KeysClause> for torii_grpc::types::KeysClause { keys: value .keys .iter() - .map(|k: &String| FieldElement::from_str(k.as_str()).unwrap()) + .map(|o| { + o.as_ref() + .map(|k| FieldElement::from_str(k.as_str()).unwrap()) + }) .collect(), models: value.models.iter().map(|m| m.to_string()).collect(), pattern_matching: (&value.pattern_matching).into(),