Skip to content

Commit

Permalink
wip: handle Ty for Array and ByteArray + fix sozo model commands
Browse files Browse the repository at this point in the history
  • Loading branch information
remybar committed May 2, 2024
1 parent 0ed6c63 commit 822e3b4
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 126 deletions.
14 changes: 4 additions & 10 deletions crates/dojo-core/src/database/introspect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ struct FieldLayout {
layout: Layout
}

// #[derive(Copy, Drop, Serde, Debug)]
// struct ItemLayout {
// layout: Layout // booo bad compiler ! bad !
// }

#[derive(Copy, Drop, Serde, Debug)]
enum Layout {
Fixed: Span<u8>,
Expand Down Expand Up @@ -36,9 +31,8 @@ enum Ty {
Struct: Struct,
Enum: Enum,
Tuple: Span<Span<felt252>>,
// Store the capacity of the array.
FixedSizeArray: u32,
DynamicSizeArray,
Array: Span<felt252>,
ByteArray
}

#[derive(Copy, Drop, Serde)]
Expand Down Expand Up @@ -188,7 +182,7 @@ impl Introspect_u256 of Introspect<u256> {
Layout::Fixed(array![128, 128].span())
}
fn ty() -> Ty {
Ty::FixedSizeArray(2)
Ty::Primitive('u256')
}
}

Expand Down Expand Up @@ -225,6 +219,6 @@ impl Introspect_bytearray of Introspect<ByteArray> {
Layout::ByteArray
}
fn ty() -> Ty {
Ty::DynamicSizeArray
Ty::ByteArray
}
}
6 changes: 3 additions & 3 deletions crates/dojo-core/src/resource_metadata.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ impl ResourceMetadataIntrospect<> of dojo::database::introspect::Introspect<Reso
),
dojo::database::introspect::serialize_member(
@dojo::database::introspect::Member {
name: 'metadata_uri',
ty: dojo::database::introspect::Ty::DynamicSizeArray,
attrs: array![].span()
name: 'metadata_uri',
ty: dojo::database::introspect::Ty::ByteArray,
attrs: array![].span()
}
)
].span()
Expand Down
45 changes: 22 additions & 23 deletions crates/dojo-lang/src/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ fn get_array_item_type(ty: String) -> String {
/// * struct_ast: The AST of the struct.
/// Returns:
/// * A RewriteNode containing the generated code.
pub fn handle_introspect_struct(
db: &dyn SyntaxGroup,
diagnostics: &mut Vec<PluginDiagnostic>,
struct_ast: ItemStruct,
) -> RewriteNode {
pub fn handle_introspect_struct(db: &dyn SyntaxGroup, struct_ast: ItemStruct) -> RewriteNode {
let name = struct_ast.name(db).text(db).into();

let mut member_types: Vec<String> = vec![];
Expand Down Expand Up @@ -95,15 +91,28 @@ pub fn handle_introspect_struct(
attrs.join(","),
));
} else if is_byte_array(ty.clone()) {
// TODO for Ty
member_types.push(format!(
"dojo::database::introspect::serialize_member(
@dojo::database::introspect::Member {{
name: '{member_name}',
ty: dojo::database::introspect::Ty::ByteArray,
attrs: array![{}].span()
}}
)",
attrs.join(","),
));
} else if is_array_type(ty.clone()) {
let item_type = get_array_item_type(ty);

member_types.push(format!(
"dojo::database::introspect::serialize_member(
@dojo::database::introspect::Member {{
name: '{member_name}',
ty: dojo::database::introspect::Ty::DynamicSizeArray,
ty: dojo::database::introspect::Ty::Array(
dojo::database::introspect::serialize_member_type(
@dojo::database::introspect::Introspect::<{item_type}>::ty()
)
),
attrs: array![{}].span()
}})",
attrs.join(","),
Expand All @@ -114,23 +123,13 @@ pub fn handle_introspect_struct(
let tuple_items = (*tuple.expressions(db))
.elements(db)
.iter()
.filter_map(|e| {
.map(|e| {
let e_ty = e.as_syntax_node().get_text(db).trim().to_string();
if primitive_sizes.get(&e_ty).is_some() {
Some(format!(
"dojo::database::introspect::serialize_member_type(
@dojo::database::introspect::Ty::Primitive('{e_ty}')
)"
))
} else {
diagnostics.push(PluginDiagnostic {
stable_ptr: member.stable_ptr().0,
message: "Only primitive types are supported inside tuples."
.to_string(),
severity: Severity::Error,
});
None
}
format!(
"dojo::database::introspect::serialize_member_type(
@dojo::database::introspect::Introspect::<{e_ty}>::ty()
)"
)
})
.collect::<Vec<_>>();

Expand Down
7 changes: 2 additions & 5 deletions crates/dojo-lang/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,8 @@ impl MacroPlugin for BuiltinDojoPlugin {
rewrite_nodes.push(handle_print_struct(db, struct_ast.clone()));
}
"Introspect" => {
rewrite_nodes.push(handle_introspect_struct(
db,
&mut diagnostics,
struct_ast.clone(),
));
rewrite_nodes
.push(handle_introspect_struct(db, struct_ast.clone()));
}
_ => continue,
}
Expand Down
49 changes: 30 additions & 19 deletions crates/dojo-lang/src/plugin_test_data/introspect
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,11 @@ dojo::database::introspect::serialize_member(
dojo::database::introspect::serialize_member(
@dojo::database::introspect::Member {
name: 'after',
ty: dojo::database::introspect::Ty::DynamicSizeArray,
ty: dojo::database::introspect::Ty::Array(
dojo::database::introspect::serialize_member_type(
@dojo::database::introspect::Introspect::<u32>::ty()
)
),
attrs: array![].span()
})

Expand Down Expand Up @@ -1356,7 +1360,14 @@ dojo::database::introspect::serialize_member(
name: 'before',
ty: dojo::database::introspect::Ty::Primitive('u8'),
attrs: array![].span()
})
}),
dojo::database::introspect::serialize_member(
@dojo::database::introspect::Member {
name: 'after',
ty: dojo::database::introspect::Ty::ByteArray,
attrs: array![].span()
}
)

].span()
})
Expand Down Expand Up @@ -1417,7 +1428,11 @@ dojo::database::introspect::serialize_member(
dojo::database::introspect::serialize_member(
@dojo::database::introspect::Member {
name: 'after',
ty: dojo::database::introspect::Ty::DynamicSizeArray,
ty: dojo::database::introspect::Ty::Array(
dojo::database::introspect::serialize_member_type(
@dojo::database::introspect::Introspect::<Vec2>::ty()
)
),
attrs: array![].span()
})

Expand Down Expand Up @@ -1489,11 +1504,11 @@ dojo::database::introspect::serialize_member(
ty: dojo::database::introspect::Ty::Tuple(
array![
dojo::database::introspect::serialize_member_type(
@dojo::database::introspect::Ty::Primitive('u8')
),
@dojo::database::introspect::Introspect<u8>::ty()
),
dojo::database::introspect::serialize_member_type(
@dojo::database::introspect::Ty::Primitive('u256')
)
@dojo::database::introspect::Introspect<u256>::ty()
)
].span()
),
attrs: array![].span()
Expand Down Expand Up @@ -1582,8 +1597,14 @@ dojo::database::introspect::serialize_member(
ty: dojo::database::introspect::Ty::Tuple(
array![
dojo::database::introspect::serialize_member_type(
@dojo::database::introspect::Ty::Primitive('u8')
)
@dojo::database::introspect::Introspect<u8>::ty()
),
dojo::database::introspect::serialize_member_type(
@dojo::database::introspect::Introspect<Vec2>::ty()
),
dojo::database::introspect::serialize_member_type(
@dojo::database::introspect::Introspect<EnumCustom>::ty()
)
].span()
),
attrs: array![].span()
Expand Down Expand Up @@ -1649,13 +1670,3 @@ error: Dynamic arrays are not supported.
--> test_src/lib.cairo:29:9
Left: Array<u32>,
^**********^

error: Only primitive types are supported inside tuples.
--> test_src/lib.cairo:122:5
after: (u8, Vec2, EnumCustom),
^***************************^

error: Only primitive types are supported inside tuples.
--> test_src/lib.cairo:122:5
after: (u8, Vec2, EnumCustom),
^***************************^
16 changes: 8 additions & 8 deletions crates/dojo-lang/src/plugin_test_data/system
Original file line number Diff line number Diff line change
Expand Up @@ -619,22 +619,22 @@ error: Unsupported attribute.
^*****^

error: Unknown inline item macro: 'component'.
--> test_src/lib.cairo[ContractAllowedRefSelf]:10:21
--> test_src/lib.cairo[ContractAllowedRefSelf]:11:21
component!(path: dojo::components::upgradeable::upgradeable, storage: upgradeable, event: UpgradeableEvent);
^**********************************************************************************************************^

error: Unsupported attribute.
--> test_src/lib.cairo[ContractAllowedRefSelf]:12:21
--> test_src/lib.cairo[ContractAllowedRefSelf]:13:21
#[abi(embed_v0)]
^**************^

error: Unsupported attribute.
--> test_src/lib.cairo[ContractAllowedRefSelf]:19:21
--> test_src/lib.cairo[ContractAllowedRefSelf]:20:21
#[abi(embed_v0)]
^**************^

error: Unsupported attribute.
--> test_src/lib.cairo[ContractAllowedRefSelf]:26:21
--> test_src/lib.cairo[ContractAllowedRefSelf]:27:21
#[abi(embed_v0)]
^**************^

Expand All @@ -644,22 +644,22 @@ error: Unsupported attribute.
^**************^

error: Unsupported attribute.
--> test_src/lib.cairo[ContractAllowedRefSelf]:35:13
--> test_src/lib.cairo[ContractAllowedRefSelf]:36:13
#[event]
^******^

error: Unsupported attribute.
--> test_src/lib.cairo[ContractAllowedRefSelf]:41:13
--> test_src/lib.cairo[ContractAllowedRefSelf]:42:13
#[storage]
^********^

error: Unsupported attribute.
--> test_src/lib.cairo[ContractAllowedRefSelf]:44:17
--> test_src/lib.cairo[ContractAllowedRefSelf]:45:17
#[substorage(v0)]
^***************^

error: Unknown inline item macro: 'component'.
--> test_src/lib.cairo[MyFaultyContract]:10:21
--> test_src/lib.cairo[MyFaultyContract]:11:21
component!(path: dojo::components::upgradeable::upgradeable, storage: upgradeable, event: UpgradeableEvent);
^**********************************************************************************************************^

Expand Down
15 changes: 15 additions & 0 deletions crates/dojo-types/src/packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ pub fn unpack(
/// Parse a raw schema of a model into a Cairo type, [Ty]
pub fn parse_ty(data: &[FieldElement]) -> Result<Ty, ParseError> {
let member_type: u8 = data[0].try_into()?;

match member_type {
0 => parse_simple(&data[1..]),
1 => parse_struct(&data[1..]),
2 => parse_enum(&data[1..]),
3 => parse_tuple(&data[1..]),
4 => parse_array(&data[1..]),
5 => parse_byte_array(),
_ => Err(ParseError::InvalidSchema),
}
}
Expand Down Expand Up @@ -197,6 +200,18 @@ fn parse_tuple(data: &[FieldElement]) -> Result<Ty, ParseError> {
Ok(Ty::Tuple(children))
}

fn parse_array(data: &[FieldElement]) -> Result<Ty, ParseError> {
let mut v = data.to_vec();
let _ = v.remove(0);

let item_ty = parse_ty(v.as_slice())?;
Ok(Ty::Array(vec![item_ty]))
}

fn parse_byte_array() -> Result<Ty, ParseError> {
Ok(Ty::ByteArray("".to_string()))
}

#[cfg(test)]
mod tests {
use starknet::core::types::FieldElement;
Expand Down
Loading

0 comments on commit 822e3b4

Please sign in to comment.