Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Sep 19, 2023
1 parent 34ea459 commit b9ec70e
Show file tree
Hide file tree
Showing 21 changed files with 154 additions and 102 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/dojo-core/src/database/schema.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ enum Ty {
#[derive(Copy, Drop, Serde)]
struct Struct {
name: felt252,
// attrs: Span<felt252>,
attrs: Span<felt252>,
children: Span<Span<felt252>>
}

#[derive(Copy, Drop, Serde)]
struct EnumMember {
name: felt252,
attrs: Span<felt252>,
values: Span<Span<felt252>>
}

Expand Down
1 change: 1 addition & 0 deletions crates/dojo-lang/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ pub fn handle_component_struct(
fn ty() -> dojo::database::schema::Ty {
dojo::database::schema::Ty::Struct(dojo::database::schema::Struct {
name: '$type_name$',
attrs: array![].span(),
children: array![$member_types$].span()
})
}
Expand Down
3 changes: 2 additions & 1 deletion crates/dojo-lang/src/inline_macros/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ impl InlineMacroExprPlugin for GetMacro {

builder.add_str(&format!(
"\n let mut __{component}_layout__ = array::ArrayTrait::new();
dojo::database::schema::SchemaIntrospection::<{component}>::layout(ref __{component}_layout__);
dojo::database::schema::SchemaIntrospection::<{component}>::layout(ref \
__{component}_layout__);
let __{component}_layout_span__ = \
array::ArrayTrait::span(@__{component}_layout__);
let mut __{component}_values__ = {}.entity('{component}', __get_macro_keys__, \
Expand Down
20 changes: 18 additions & 2 deletions crates/dojo-lang/src/manifest_test_data/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ test_manifest_file
"key": false
}
],
"class_hash": "0x3b9351ee7af2238ea0b154383b5d07c6052af18ca9776e8d8d6ff0bd535afb4",
"class_hash": "0x3682ef3ef44d8db8c8bfe72533e2e3b17e7b80efd6550b365deed7b4b3f8597",
"abi": [
{
"type": "function",
Expand Down Expand Up @@ -1058,6 +1058,10 @@ test_manifest_file
"name": "name",
"type": "core::felt252"
},
{
"name": "attrs",
"type": "core::array::Span::<core::felt252>"
},
{
"name": "children",
"type": "core::array::Span::<core::array::Span::<core::felt252>>"
Expand All @@ -1072,6 +1076,10 @@ test_manifest_file
"name": "name",
"type": "core::felt252"
},
{
"name": "attrs",
"type": "core::array::Span::<core::felt252>"
},
{
"name": "values",
"type": "core::array::Span::<core::array::Span::<core::felt252>>"
Expand Down Expand Up @@ -1129,7 +1137,7 @@ test_manifest_file
"key": false
}
],
"class_hash": "0x24e7831b9ebd7622e87f41d7663a85661085590221cb42a76514ad0931f52d1",
"class_hash": "0x71a5cb83c2d96f4b97a015d36f936de6f93166a1434bbacd29147933c4ac314",
"abi": [
{
"type": "function",
Expand Down Expand Up @@ -1202,6 +1210,10 @@ test_manifest_file
"name": "name",
"type": "core::felt252"
},
{
"name": "attrs",
"type": "core::array::Span::<core::felt252>"
},
{
"name": "children",
"type": "core::array::Span::<core::array::Span::<core::felt252>>"
Expand All @@ -1216,6 +1228,10 @@ test_manifest_file
"name": "name",
"type": "core::felt252"
},
{
"name": "attrs",
"type": "core::array::Span::<core::felt252>"
},
{
"name": "values",
"type": "core::array::Span::<core::array::Span::<core::felt252>>"
Expand Down
6 changes: 1 addition & 5 deletions crates/dojo-lang/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ impl GeneratedFileAuxData for DojoAuxData {
self
}
fn eq(&self, other: &dyn GeneratedFileAuxData) -> bool {
if let Some(other) = other.as_any().downcast_ref::<Self>() {
self == other
} else {
false
}
if let Some(other) = other.as_any().downcast_ref::<Self>() { self == other } else { false }
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/dojo-lang/src/plugin_test_data/component
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl PositionSchemaIntrospection of dojo::database::schema::SchemaIntrospection<
dojo::database::schema::Ty::Struct(
dojo::database::schema::Struct {
name: 'Position',
attrs: array![].span(),
children: array![
dojo::database::schema::serialize_member(
@dojo::database::schema::Member {
Expand Down Expand Up @@ -252,6 +253,7 @@ impl RolesSchemaIntrospection of dojo::database::schema::SchemaIntrospection<Rol
dojo::database::schema::Ty::Struct(
dojo::database::schema::Struct {
name: 'Roles',
attrs: array![].span(),
children: array![
dojo::database::schema::serialize_member(
@dojo::database::schema::Member {
Expand Down Expand Up @@ -367,6 +369,7 @@ impl PlayerSchemaIntrospection of dojo::database::schema::SchemaIntrospection<Pl
dojo::database::schema::Ty::Struct(
dojo::database::schema::Struct {
name: 'Player',
attrs: array![].span(),
children: array![
dojo::database::schema::serialize_member(
@dojo::database::schema::Member {
Expand Down
51 changes: 37 additions & 14 deletions crates/dojo-types/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ pub struct Member {

impl std::fmt::Display for Member {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Member {{ name: {}, type: {}, key: {} }}", self.name, self.ty, self.key)
if self.key {
write!(f, "#[key]\n{}: {}", self.name, self.ty)
} else {
write!(f, "{}: {}", self.name, self.ty)
}
}
}

Expand All @@ -21,32 +25,51 @@ pub enum Ty {
Enum(Enum),
}

impl std::fmt::Display for Ty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Ty::Simple(s) => write!(f, "{}", s),
Ty::Struct(structure) => {
let members =
structure.children.iter().map(|m| m.to_string()).collect::<Vec<_>>().join(", ");
write!(f, "{}", members)
}
Ty::Enum(e) => {
let values = e.values.iter().map(|v| v.to_string()).collect::<Vec<_>>().join(", ");
write!(f, "{}", values)
}
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Struct {
pub name: String,
pub children: Vec<Member>,
}

impl std::fmt::Display for Struct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)?;
for child in &self.children {
write!(f, "\n{}", child)?;
}
Ok(())
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct Enum {
pub name: String,
pub values: Vec<Ty>,
}

impl std::fmt::Display for Ty {
impl std::fmt::Display for Enum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Ty::Simple(s) => write!(f, "{}", s),
Ty::Struct(members) => {
let members =
members.children.iter().map(|m| m.to_string()).collect::<Vec<_>>().join(", ");
write!(f, "[{}]", members)
}
Ty::Enum(e) => {
// let values = e.values.join(", ");
let values = "";
write!(f, "Enum({})", values)
}
write!(f, "{}", self.name)?;
for value in &self.values {
write!(f, "\n{}", value)?;
}
Ok(())
}
}
20 changes: 12 additions & 8 deletions crates/dojo-world/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ impl Manifest {
.call(
FunctionCall {
contract_address: world_address,
calldata: vec![cairo_short_string_to_felt(&component.name)
.map_err(ManifestError::InvalidNameError)?],
calldata: vec![
cairo_short_string_to_felt(&component.name)
.map_err(ManifestError::InvalidNameError)?,
],
entry_point_selector: get_selector_from_name("component").unwrap(),
},
BlockId::Tag(BlockTag::Pending),
Expand All @@ -190,12 +192,14 @@ impl Manifest {
.call(
FunctionCall {
contract_address: world_address,
calldata: vec![cairo_short_string_to_felt(
// because the name returns by the `name` method of
// a system contract is without the 'System' suffix
system.name.strip_suffix("System").unwrap_or(&system.name),
)
.map_err(ManifestError::InvalidNameError)?],
calldata: vec![
cairo_short_string_to_felt(
// because the name returns by the `name` method of
// a system contract is without the 'System' suffix
system.name.strip_suffix("System").unwrap_or(&system.name),
)
.map_err(ManifestError::InvalidNameError)?,
],
entry_point_selector: get_selector_from_name("system").unwrap(),
},
BlockId::Tag(BlockTag::Pending),
Expand Down
32 changes: 16 additions & 16 deletions crates/sozo/src/ops/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ pub async fn execute(command: ComponentCommands, env_metadata: Option<Environmen
if to_json {
println!("{}", serde_json::to_string_pretty(&schema)?)
} else {
// let output = format!(
// r"struct {name} {{
// {}
// }}",
// schema
// .iter()
// .map(|s| match s {
// Ty::Struct(_s) => "".to_string(),
// // format!(r" {}: {}", s.children[0].key, s.children[0].ty),
// Ty::Simple(s) => s.to_string(),
// Ty::Enum(_e) => "".to_string(),
// })
// .collect::<Vec<String>>()
// .join("\n")
// );
// let output = format!(
// r"struct {name} {{
// {}
// }}",
// schema
// .iter()
// .map(|s| match s {
// Ty::Struct(_s) => "".to_string(),
// // format!(r" {}: {}", s.children[0].key,
// s.children[0].ty), Ty::Simple(s) =>
// s.to_string(), Ty::Enum(_e) =>
// "".to_string(), })
// .collect::<Vec<String>>()
// .join("\n")
// );

// println!("{output}")
// println!("{output}")
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/torii/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version = "0.2.1"
[dependencies]
async-std = { version = "1.12.0", default-features = false, features = [ "std" ] }
async-trait.workspace = true
crypto-bigint = "0.5.1"
crypto-bigint = "0.5.3"
dojo-types = { path = "../../dojo-types" }
parking_lot = "0.12.1"
starknet-crypto.workspace = true
Expand Down
34 changes: 21 additions & 13 deletions crates/torii/client/src/contract/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<'a, P: Provider + Sync> ComponentReader<'a, P> {
}

let unpacked = unpack::<P>(packed, layout)?;
println!("{:?}", unpacked);

Ok(unpacked)
}
}
Expand Down Expand Up @@ -182,8 +182,11 @@ pub fn unpack<P: Provider>(
offset = 0;
}

// Calculate the result and push it to the unpacked values.
let mask = U256::from(((1 << size) - 1) as u8);
let mut mask = U256::from(0_u8);
for _ in 0..size {
mask = (mask << 1) | U256::from(1_u8);
}

let result = mask & (unpacking >> offset);
let result_fe = FieldElement::from_hex_be(&result.to_string())
.map_err(|_| ComponentError::ConvertingFelt)?;
Expand All @@ -202,7 +205,7 @@ fn parse_ty<P: Provider>(data: &[FieldElement]) -> Result<Ty, ComponentError<P::
0 => parse_simple::<P>(&data[1..]),
1 => parse_struct::<P>(&data[1..]),
2 => parse_enum::<P>(&data[1..]),
_ => return Err(ComponentError::InvalidSchema),
_ => Err(ComponentError::InvalidSchema),
}
}

Expand All @@ -216,11 +219,16 @@ fn parse_struct<P: Provider>(data: &[FieldElement]) -> Result<Ty, ComponentError
let name =
parse_cairo_short_string(&data[0]).map_err(ComponentError::ParseCairoShortStringError)?;

let children_len: u32 = data[1].try_into().unwrap();
let attrs_len: u32 = data[1].try_into().unwrap();
let attrs_slice_start = 2;
let attrs_slice_end = attrs_slice_start + attrs_len as usize;
let _attrs = &data[attrs_slice_start..attrs_slice_end];

let children_len: u32 = data[attrs_slice_end].try_into().unwrap();
let children_len = children_len as usize;

let mut children = vec![];
let mut offset = 2;
let mut offset = attrs_slice_end + 1;

for i in 0..children_len {
let start = i + offset;
Expand All @@ -231,10 +239,6 @@ fn parse_struct<P: Provider>(data: &[FieldElement]) -> Result<Ty, ComponentError
offset += len as usize;
}

let attrs = vec![];

let key = attrs.contains(&cairo_short_string_to_felt("key").unwrap());

Ok(Ty::Struct(Struct { name, children }))
}

Expand All @@ -243,7 +247,6 @@ fn parse_member<P: Provider>(data: &[FieldElement]) -> Result<Member, ComponentE
parse_cairo_short_string(&data[0]).map_err(ComponentError::ParseCairoShortStringError)?;

let attributes_len: u32 = data[1].try_into().unwrap();

let slice_start = 2;
let slice_end = slice_start + attributes_len as usize;
let attributes = &data[slice_start..slice_end];
Expand All @@ -259,11 +262,16 @@ fn parse_enum<P: Provider>(data: &[FieldElement]) -> Result<Ty, ComponentError<P
let name =
parse_cairo_short_string(&data[0]).map_err(ComponentError::ParseCairoShortStringError)?;

let values_len: u32 = data[1].try_into().unwrap();
let attrs_len: u32 = data[1].try_into().unwrap();
let attrs_slice_start = 2;
let attrs_slice_end = attrs_slice_start + attrs_len as usize;
let _attrs = &data[attrs_slice_start..attrs_slice_end];

let values_len: u32 = data[attrs_slice_end].try_into().unwrap();
let values_len = values_len as usize;

let mut values = vec![];
let mut offset = 2;
let mut offset = attrs_slice_end + 1;

for i in 0..values_len {
let start = i + offset;
Expand Down
Loading

0 comments on commit b9ec70e

Please sign in to comment.