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

chore: c header #97

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 2 additions & 11 deletions dojo.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,7 @@ typedef enum Primitive_Tag {
U32,
U64,
U128,
U256,
#if defined(TARGET_POINTER_WIDTH_32)
U256,
#endif
U256_,
Bool,
Felt252,
ClassHash,
Expand Down Expand Up @@ -548,13 +545,8 @@ typedef struct Primitive {
uint8_t u128[16];
};
struct {
uint64_t u256[4];
struct U256 u256;
};
#if defined(TARGET_POINTER_WIDTH_32)
struct {
uint32_t u256[8];
};
#endif
struct {
bool bool_;
};
Expand Down Expand Up @@ -798,7 +790,6 @@ extern "C" {
*
* # Parameters
* * `torii_url` - URL of the Torii server
* * `rpc_url` - URL of the Starknet RPC endpoint
* * `libp2p_relay_url` - URL of the libp2p relay server
* * `world` - World address as a FieldElement
*
Expand Down
56 changes: 13 additions & 43 deletions dojo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ struct Entity {
CArray<Struct> models;
};

struct U256 {
uint8_t data[32];
};

struct Primitive {
enum class Tag {
I8,
Expand All @@ -140,10 +144,7 @@ struct Primitive {
U32,
U64,
U128,
U256,
#if defined(TARGET_POINTER_WIDTH_32)
U256,
#endif
U256_,
Bool,
Felt252,
ClassHash,
Expand Down Expand Up @@ -191,16 +192,10 @@ struct Primitive {
uint8_t _0[16];
};

struct U256_Body {
uint64_t _0[4];
struct U256__Body {
U256 _0;
};

#if defined(TARGET_POINTER_WIDTH_32)
struct U256_Body {
uint32_t _0[8];
};
#endif

struct Bool_Body {
bool _0;
};
Expand Down Expand Up @@ -233,10 +228,7 @@ struct Primitive {
U32_Body u32;
U64_Body u64;
U128_Body u128;
U256_Body u256;
#if defined(TARGET_POINTER_WIDTH_32)
U256_Body u256;
#endif
U256__Body u256;
Bool_Body bool_;
Felt252_Body felt252;
ClassHash_Body class_hash;
Expand Down Expand Up @@ -358,33 +350,16 @@ struct Primitive {
return tag == Tag::U128;
}

static Primitive U256(const uint64_t (&_0)[4]) {
Primitive result;
for (int i = 0; i < 4; i++) {
::new (&result.u256._0[i]) (uint64_t)(_0[i]);
}
result.tag = Tag::U256;
return result;
}

bool IsU256() const {
return tag == Tag::U256;
}

#if defined(TARGET_POINTER_WIDTH_32)
static Primitive U256(const uint32_t (&_0)[8]) {
static Primitive U256_(const U256 &_0) {
Primitive result;
for (int i = 0; i < 8; i++) {
::new (&result.u256._0[i]) (uint32_t)(_0[i]);
}
result.tag = Tag::U256;
::new (&result.u256._0) (U256)(_0);
result.tag = Tag::U256_;
return result;
}

bool IsU256() const {
return tag == Tag::U256;
bool IsU256_() const {
return tag == Tag::U256_;
}
#endif

static Primitive Bool(const bool &_0) {
Primitive result;
Expand Down Expand Up @@ -592,10 +567,6 @@ struct Event {
FieldElement transaction_hash;
};

struct U256 {
uint8_t data[32];
};

struct Token {
FieldElement contract_address;
U256 token_id;
Expand Down Expand Up @@ -940,7 +911,6 @@ extern "C" {
///
/// # Parameters
/// * `torii_url` - URL of the Torii server
/// * `rpc_url` - URL of the Starknet RPC endpoint
/// * `libp2p_relay_url` - URL of the libp2p relay server
/// * `world` - World address as a FieldElement
///
Expand Down
7 changes: 2 additions & 5 deletions dojo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ cdef extern from *:
U32,
U64,
U128,
U256,
U256,
U256_,
Bool,
Felt252,
ClassHash,
Expand All @@ -342,8 +341,7 @@ cdef extern from *:
uint32_t u32;
uint64_t u64;
uint8_t u128[16];
uint64_t u256[4];
uint32_t u256[8];
U256 u256;
bool bool_;
FieldElement felt252;
FieldElement class_hash;
Expand Down Expand Up @@ -498,7 +496,6 @@ cdef extern from *:
#
# # Parameters
# * `torii_url` - URL of the Torii server
# * `rpc_url` - URL of the Starknet RPC endpoint
# * `libp2p_relay_url` - URL of the libp2p relay server
# * `world` - World address as a FieldElement
#
Expand Down
1 change: 0 additions & 1 deletion src/c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ lazy_static! {
///
/// # Parameters
/// * `torii_url` - URL of the Torii server
/// * `rpc_url` - URL of the Starknet RPC endpoint
/// * `libp2p_relay_url` - URL of the libp2p relay server
/// * `world` - World address as a FieldElement
///
Expand Down
14 changes: 4 additions & 10 deletions src/c/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,10 +854,7 @@ pub enum Primitive {
U64(u64),
// TODO: better way?
U128([u8; 16]),
#[cfg(target_pointer_width = "64")]
U256([u64; 4]),
#[cfg(target_pointer_width = "32")]
U256([u32; 8]),
U256_(U256),
Bool(bool),
Felt252(FieldElement),
ClassHash(FieldElement),
Expand All @@ -882,7 +879,7 @@ impl From<&Primitive> for dojo_types::primitive::Primitive {
Primitive::U128(v) => {
dojo_types::primitive::Primitive::U128(Some(u128::from_be_bytes(*v)))
}
Primitive::U256(v) => dojo_types::primitive::Primitive::U256(Some((*v).into())),
Primitive::U256_(v) => dojo_types::primitive::Primitive::U256(Some(v.into())),
Primitive::Bool(v) => dojo_types::primitive::Primitive::Bool(Some(*v)),
Primitive::Felt252(v) => {
dojo_types::primitive::Primitive::Felt252(Some((&v.clone()).into()))
Expand Down Expand Up @@ -927,12 +924,9 @@ impl From<&dojo_types::primitive::Primitive> for Primitive {
}
dojo_types::primitive::Primitive::U256(v) => {
if let Some(v) = v {
Primitive::U256(v.to_words())
Primitive::U256_(v.into())
} else {
#[cfg(target_pointer_width = "64")]
return Primitive::U256([0; 4]);
#[cfg(target_pointer_width = "32")]
return Primitive::U256([0; 8]);
Primitive::U256_(U256 { data: [0; 32] })
}
}
dojo_types::primitive::Primitive::Bool(v) => Primitive::Bool(v.unwrap_or(false)),
Expand Down