diff --git a/crates/dojo/core-cairo-test/src/tests/meta/introspect.cairo b/crates/dojo/core-cairo-test/src/tests/meta/introspect.cairo index 743798e0e3..42ee23da31 100644 --- a/crates/dojo/core-cairo-test/src/tests/meta/introspect.cairo +++ b/crates/dojo/core-cairo-test/src/tests/meta/introspect.cairo @@ -12,12 +12,11 @@ struct WithArray { arr: Array } -#[derive(Drop, Introspect)] -struct WithFixedArray { - value: u32, - arr: [u8; 3] -} - +// #[derive(Drop, Introspect)] +// struct WithFixedArray { +// value: u32, +// arr: [u8; 3] +// } #[derive(Drop, Introspect)] struct WithByteArray { @@ -173,11 +172,11 @@ fn test_size_with_array() { assert!(Introspect::::size().is_none()); } -fn test_size_with_fixed_array() { - let size = Introspect::::size(); - assert!(size.is_some()); - assert!(size.unwrap() == 4); -} +// fn test_size_with_fixed_array() { +// let size = Introspect::::size(); +// assert!(size.is_some()); +// assert!(size.unwrap() == 4); +// } #[test] fn test_size_with_byte_array() { diff --git a/crates/dojo/core/src/meta/introspect.cairo b/crates/dojo/core/src/meta/introspect.cairo index c96993e648..be5dc40b8c 100644 --- a/crates/dojo/core/src/meta/introspect.cairo +++ b/crates/dojo/core/src/meta/introspect.cairo @@ -11,7 +11,7 @@ pub enum Ty { // And `Box` is not serializable. So using a Span, even if it's to have // one element, does the trick. Array: Span, - FixedArray: Span<(Ty, u32)>, + // FixedArray: Span<(Ty, u32)>, ByteArray, } @@ -91,20 +91,20 @@ pub impl Introspect_bool of Introspect { } } -pub impl Introspect_FixedArray> of Introspect<[T; N]> { - fn size() -> Option { - match Introspect::::size() { - Option::Some(size) => Option::Some(size * N), - Option::None => Option::None - } - } - fn layout() -> Layout { - Layout::FixedArray([(Introspect::::layout(), N)].span()) - } - fn ty() -> Ty { - Ty::FixedArray([(Introspect::::ty(), N)].span()) - } -} +// pub impl Introspect_FixedArray> of Introspect<[T; N]> { +// fn size() -> Option { +// match Introspect::::size() { +// Option::Some(size) => Option::Some(size * N), +// Option::None => Option::None +// } +// } +// fn layout() -> Layout { +// Layout::FixedArray([(Introspect::::layout(), N)].span()) +// } +// fn ty() -> Ty { +// Ty::FixedArray([(Introspect::::ty(), N)].span()) +// } +// } pub impl Introspect_u8 of Introspect { fn size() -> Option { diff --git a/crates/dojo/core/src/meta/layout.cairo b/crates/dojo/core/src/meta/layout.cairo index 8e8d7ef9cd..25d0d284be 100644 --- a/crates/dojo/core/src/meta/layout.cairo +++ b/crates/dojo/core/src/meta/layout.cairo @@ -15,7 +15,7 @@ pub enum Layout { // And `Box` is not serializable. So using a Span, even if it's to have // one element, does the trick. Array: Span, - FixedArray: Span<(Layout, u32)>, + // FixedArray: Span<(Layout, u32)>, ByteArray, // there is one layout per variant. // the `selector` field identifies the variant @@ -31,7 +31,7 @@ pub impl LayoutCompareImpl of LayoutCompareTrait { (Layout::Struct(_), Layout::Struct(_)) => true, (Layout::Tuple(_), Layout::Tuple(_)) => true, (Layout::Array(_), Layout::Array(_)) => true, - (Layout::FixedArray(_), Layout::FixedArray(_)) => true, + // (Layout::FixedArray(_), Layout::FixedArray(_)) => true, (Layout::ByteArray, Layout::ByteArray) => true, (Layout::Enum(_), Layout::Enum(_)) => true, _ => false diff --git a/crates/dojo/core/src/storage/layout.cairo b/crates/dojo/core/src/storage/layout.cairo index 548ad24038..1818351182 100644 --- a/crates/dojo/core/src/storage/layout.cairo +++ b/crates/dojo/core/src/storage/layout.cairo @@ -22,9 +22,9 @@ pub fn write_layout( Layout::Fixed(layout) => { write_fixed_layout(model, key, values, ref offset, layout); }, Layout::Struct(layout) => { write_struct_layout(model, key, values, ref offset, layout); }, Layout::Array(layout) => { write_array_layout(model, key, values, ref offset, layout); }, - Layout::FixedArray(layout) => { - write_fixed_array_layout(model, key, values, ref offset, layout); - }, + // Layout::FixedArray(layout) => { + // write_fixed_array_layout(model, key, values, ref offset, layout); + // }, Layout::Tuple(layout) => { write_tuple_layout(model, key, values, ref offset, layout); }, Layout::ByteArray => { write_byte_array_layout(model, key, values, ref offset); }, Layout::Enum(layout) => { write_enum_layout(model, key, values, ref offset, layout); } @@ -92,29 +92,29 @@ pub fn write_array_layout( /// * `values` - the model record values. /// * `offset` - the start of model record values in the `values` parameter. /// * `item_layout` - the model record layout (temporary a Span because of type recursion issue). -pub fn write_fixed_array_layout( - model: felt252, - key: felt252, - values: Span, - ref offset: u32, - mut item_layout: Span<(Layout, u32)> -) { - let (item_layout, array_len): (Layout, u32) = *item_layout.pop_front().unwrap(); - assert((values.len() - offset) >= array_len, 'Invalid values length'); - - // first, read array size which is the first felt252 from values - assert(array_len.into() <= database::MAX_ARRAY_LENGTH, 'invalid array length'); - - // then, write the array size - database::set(model, key, values, offset, [packing::PACKING_MAX_BITS].span()); - offset += 1; - - // and then, write array items - for i in 0 - ..array_len { - write_layout(model, combine_key(key, i.into()), values, ref offset, item_layout); - }; -} +// pub fn write_fixed_array_layout( +// model: felt252, +// key: felt252, +// values: Span, +// ref offset: u32, +// mut item_layout: Span<(Layout, u32)> +// ) { +// let (item_layout, array_len): (Layout, u32) = *item_layout.pop_front().unwrap(); +// assert((values.len() - offset) >= array_len, 'Invalid values length'); + +// // first, read array size which is the first felt252 from values +// assert(array_len.into() <= database::MAX_ARRAY_LENGTH, 'invalid array length'); + +// // then, write the array size +// database::set(model, key, values, offset, [packing::PACKING_MAX_BITS].span()); +// offset += 1; + +// // and then, write array items +// for i in 0 +// ..array_len { +// write_layout(model, combine_key(key, i.into()), values, ref offset, item_layout); +// }; +// } /// pub fn write_byte_array_layout( @@ -247,13 +247,13 @@ pub fn delete_array_layout(model: felt252, key: felt252) { database::delete(model, key, [packing::PACKING_MAX_BITS].span()); } -pub fn delete_fixed_array_layout(model: felt252, key: felt252, mut layout: Span<(Layout, u32)>) { - let (item_layout, array_len): (Layout, u32) = *layout.pop_front().unwrap(); - database::delete(model, key, [packing::PACKING_MAX_BITS].span()); - for i in 0..array_len { - delete_layout(model, combine_key(key, i.into()), item_layout); - } -} +// pub fn delete_fixed_array_layout(model: felt252, key: felt252, mut layout: Span<(Layout, u32)>) { +// let (item_layout, array_len): (Layout, u32) = *layout.pop_front().unwrap(); +// database::delete(model, key, [packing::PACKING_MAX_BITS].span()); +// for i in 0..array_len { +// delete_layout(model, combine_key(key, i.into()), item_layout); +// } +// } /// pub fn delete_byte_array_layout(model: felt252, key: felt252) { @@ -284,7 +284,7 @@ pub fn delete_layout(model: felt252, key: felt252, layout: Layout) { Layout::Fixed(layout) => { delete_fixed_layout(model, key, layout); }, Layout::Struct(layout) => { delete_struct_layout(model, key, layout); }, Layout::Array(_) => { delete_array_layout(model, key); }, - Layout::FixedArray(layout) => { delete_fixed_array_layout(model, key, layout); }, + // Layout::FixedArray(layout) => { delete_fixed_array_layout(model, key, layout); }, Layout::Tuple(layout) => { delete_tuple_layout(model, key, layout); }, Layout::ByteArray => { delete_byte_array_layout(model, key); }, Layout::Enum(layout) => { delete_enum_layout(model, key, layout); } @@ -367,7 +367,7 @@ pub fn read_layout(model: felt252, key: felt252, ref read_data: Array, Layout::Fixed(layout) => read_fixed_layout(model, key, ref read_data, layout), Layout::Struct(layout) => read_struct_layout(model, key, ref read_data, layout), Layout::Array(layout) => read_array_layout(model, key, ref read_data, layout), - Layout::FixedArray(layout) => read_fixed_array_layout(model, key, ref read_data, layout), + // Layout::FixedArray(layout) => read_fixed_array_layout(model, key, ref read_data, layout), Layout::Tuple(layout) => read_tuple_layout(model, key, ref read_data, layout), Layout::ByteArray => read_byte_array_layout(model, key, ref read_data), Layout::Enum(layout) => read_enum_layout(model, key, ref read_data, layout), @@ -423,34 +423,23 @@ pub fn read_array_layout( }; } -pub fn read_fixed_array_layout( - model: felt252, key: felt252, ref read_data: Array, mut layout: Span<(Layout, u32)> -) { - // read number of array items - let (item_layout, array_len): (Layout, u32) = *layout.pop_front().unwrap(); - let res = database::get(model, key, [packing::PACKING_MAX_BITS].span()); - assert(res.len() == 1, 'internal database error'); +// pub fn read_fixed_array_layout( +// model: felt252, key: felt252, ref read_data: Array, mut layout: Span<(Layout, u32)> +// ) { +// // read number of array items +// let (item_layout, array_len): (Layout, u32) = *layout.pop_front().unwrap(); +// let res = database::get(model, key, [packing::PACKING_MAX_BITS].span()); +// assert(res.len() == 1, 'internal database error'); - assert(array_len.into() <= database::MAX_ARRAY_LENGTH, 'invalid array length'); - - read_data.append(array_len.into()); - - let mut i = 0; - loop { - if i >= array_len { - break; - } +// assert(array_len.into() <= database::MAX_ARRAY_LENGTH, 'invalid array length'); - let field_key = combine_key(key, i.into()); - read_layout(model, field_key, ref read_data, item_layout); +// read_data.append(array_len.into()); - i += 1; - }; - for i in 0 - ..array_len { - read_layout(model, combine_key(key, i.into()), ref read_data, item_layout); - }; -} +// for i in 0 +// ..array_len { +// read_layout(model, combine_key(key, i.into()), ref read_data, item_layout); +// }; +// } /// pub fn read_byte_array_layout(model: felt252, key: felt252, ref read_data: Array) { diff --git a/crates/dojo/lang/src/derive_macros/introspect/layout.rs b/crates/dojo/lang/src/derive_macros/introspect/layout.rs index 05828f939b..e3e2a398fd 100644 --- a/crates/dojo/lang/src/derive_macros/introspect/layout.rs +++ b/crates/dojo/lang/src/derive_macros/introspect/layout.rs @@ -7,9 +7,8 @@ use cairo_lang_syntax::node::{ids, Terminal, TypedSyntaxNode}; use starknet::core::utils::get_selector_from_name; use super::utils::{ - get_array_item_type, get_fixed_array_type_and_size, get_tuple_item_types, is_array, - is_byte_array, is_fixed_array, is_tuple, is_unsupported_option_type, - primitive_type_introspection, + get_array_item_type, get_tuple_item_types, is_array, is_byte_array, is_tuple, + is_unsupported_option_type, primitive_type_introspection, }; pub enum Wrapper { @@ -96,6 +95,14 @@ pub fn get_layout_from_type_clause( let tuple_type = expr.as_syntax_node().get_text(db); build_tuple_layout_from_type(diagnostics, type_clause.stable_ptr().0, &tuple_type) } + // Expr::FixedSizeArray(fixed_size_array) => { + // let fixed_array_type = fixed_size_array.as_syntax_node().get_text(db); + // build_fixed_array_layout_from_type( + // diagnostics, + // type_clause.stable_ptr().0, + // &fixed_array_type, + // ) + // } _ => { diagnostics.push(PluginDiagnostic { stable_ptr: type_clause.stable_ptr().0, @@ -133,47 +140,47 @@ pub fn build_array_layout_from_type( } } -pub fn build_member_layout_from_type( - diagnostics: &mut Vec, - diagnostic_item: ids::SyntaxStablePtrId, - item_type: &str, -) -> Wrapper { - if is_array(item_type) { - Wrapper::Array(build_array_layout_from_type(diagnostics, diagnostic_item, item_type)) - } else if is_fixed_array(item_type) { - Wrapper::Array(build_fixed_array_layout_from_type(diagnostics, diagnostic_item, item_type)) - } else if is_tuple(item_type) { - Wrapper::Array(build_tuple_layout_from_type(diagnostics, diagnostic_item, item_type)) - } else { - Wrapper::Introspect - } -} +// pub fn build_member_layout_from_type( +// diagnostics: &mut Vec, +// diagnostic_item: ids::SyntaxStablePtrId, +// item_type: &str, +// ) -> Wrapper { +// if is_array(item_type) { +// Wrapper::Array(build_array_layout_from_type(diagnostics, diagnostic_item, item_type)) +// // } else if is_fixed_array(item_type) { +// // Wrapper::Array(build_fixed_array_layout_from_type(diagnostics, diagnostic_item, item_type)) +// } else if is_tuple(item_type) { +// Wrapper::Array(build_tuple_layout_from_type(diagnostics, diagnostic_item, item_type)) +// } else { +// Wrapper::Introspect +// } +// } -pub fn build_fixed_array_layout_from_type( - diagnostics: &mut Vec, - diagnostic_item: ids::SyntaxStablePtrId, - item_type: &str, -) -> String { - let (array_item_type, array_size) = get_fixed_array_type_and_size(item_type); - match build_member_layout_from_type(diagnostics, diagnostic_item, &array_item_type) { - Wrapper::Introspect => { - format!( - "dojo::meta::introspect::Introspect::<({}, {})>::layout()", - array_item_type, array_size - ) - } - Wrapper::Array(layout) => { - format!( - "dojo::meta::Layout::FixedArray( - array![ - ({}, {}) - ].span(), - )", - layout, array_size - ) - } - } -} +// pub fn build_fixed_array_layout_from_type( +// diagnostics: &mut Vec, +// diagnostic_item: ids::SyntaxStablePtrId, +// item_type: &str, +// ) -> String { +// let (array_item_type, array_size) = get_fixed_array_type_and_size(item_type); +// match build_member_layout_from_type(diagnostics, diagnostic_item, &array_item_type) { +// Wrapper::Introspect => { +// format!( +// "dojo::meta::introspect::Introspect::<({}, {})>::layout()", +// array_item_type, array_size +// ) +// } +// Wrapper::Array(layout) => { +// format!( +// "dojo::meta::Layout::FixedArray( +// array![ +// ({}, {}) +// ].span(), +// )", +// layout, array_size +// ) +// } +// } +// } /// Build the tuple layout describing the provided tuple type. /// item_type could be something like (u8, u32, u128) for example. @@ -206,8 +213,8 @@ pub fn build_item_layout_from_type( ) -> String { if is_array(item_type) { build_array_layout_from_type(diagnostics, diagnostic_item, item_type) - } else if is_fixed_array(item_type) { - build_fixed_array_layout_from_type(diagnostics, diagnostic_item, item_type) + // } else if is_fixed_array(item_type) { + // build_fixed_array_layout_from_type(diagnostics, diagnostic_item, item_type) } else if is_tuple(item_type) { build_tuple_layout_from_type(diagnostics, diagnostic_item, item_type) } else { @@ -387,8 +394,6 @@ pub fn get_packed_item_layout_from_type( vec!["ERROR".to_string()] } else if is_tuple(item_type) { get_packed_tuple_layout_from_type(diagnostics, diagnostic_item, item_type) - } else if is_fixed_array(item_type) { - // TODO: Implement fixed array packed layout } else { let primitives = primitive_type_introspection(); diff --git a/crates/dojo/lang/src/derive_macros/introspect/ty.rs b/crates/dojo/lang/src/derive_macros/introspect/ty.rs index 209c121e9d..177a22ef5f 100644 --- a/crates/dojo/lang/src/derive_macros/introspect/ty.rs +++ b/crates/dojo/lang/src/derive_macros/introspect/ty.rs @@ -5,10 +5,7 @@ use cairo_lang_syntax::node::db::SyntaxGroup; use cairo_lang_syntax::node::helpers::QueryAttrs; use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode}; -use super::utils::{ - get_array_item_type, get_fixed_array_type_and_size, get_tuple_item_types, is_array, - is_byte_array, is_fixed_array, is_tuple, -}; +use super::utils::{get_array_item_type, get_tuple_item_types, is_array, is_byte_array, is_tuple}; pub fn build_struct_ty(db: &dyn SyntaxGroup, name: &String, struct_ast: &ItemStruct) -> String { let members_ty = struct_ast @@ -110,17 +107,17 @@ pub fn build_item_ty_from_type(item_type: &String) -> String { )", build_item_ty_from_type(&array_item_type) ) - } else if is_fixed_array(item_type) { - let (array_item_type, size) = get_fixed_array_type_and_size(&item_type); - format!( - "dojo::meta::introspect::Ty::FixedArray( - array![ - ({}, {}) - ].span() - )", - build_item_ty_from_type(&array_item_type), - size - ) + // } else if is_fixed_array(item_type) { + // let (array_item_type, size) = get_fixed_array_type_and_size(&item_type); + // format!( + // "dojo::meta::introspect::Ty::FixedArray( + // array![ + // ({}, {}) + // ].span() + // )", + // build_item_ty_from_type(&array_item_type), + // size + // ) } else if is_byte_array(item_type) { "dojo::meta::introspect::Ty::ByteArray".to_string() } else if is_tuple(item_type) { diff --git a/crates/dojo/lang/src/derive_macros/introspect/utils.rs b/crates/dojo/lang/src/derive_macros/introspect/utils.rs index eac7efacfe..af9c7d08e8 100644 --- a/crates/dojo/lang/src/derive_macros/introspect/utils.rs +++ b/crates/dojo/lang/src/derive_macros/introspect/utils.rs @@ -1,7 +1,7 @@ -use regex::Regex; +// use regex::Regex; use std::collections::HashMap; -const FIXED_ARRAY_REGEX: &str = r"\[[^;]+;\s*\d{1,10}\s*\]"; +// const FIXED_ARRAY_REGEX: &str = r"\[[^;]+;\s*\d{1,10}\s*\]"; #[derive(Clone, Default, Debug)] pub struct TypeIntrospection(pub usize, pub Vec); @@ -30,10 +30,10 @@ pub fn is_unsupported_option_type(ty: &str) -> bool { ty.starts_with("Option<(") } -pub fn is_fixed_array(ty: &str) -> bool { - let re = Regex::new(FIXED_ARRAY_REGEX).unwrap(); - re.is_match(ty) -} +// pub fn is_fixed_array(ty: &str) -> bool { +// let re = Regex::new(FIXED_ARRAY_REGEX).unwrap(); +// re.is_match(ty) +// } pub fn is_byte_array(ty: &str) -> bool { ty.eq("ByteArray") @@ -55,10 +55,10 @@ pub fn get_array_item_type(ty: &str) -> String { } } -pub fn get_fixed_array_type_and_size(ty: &str) -> (String, usize) { - let mut parts = ty.trim().strip_prefix('[').unwrap().strip_suffix(']').unwrap().split(';'); - (parts.next().unwrap().trim().to_string(), parts.last().unwrap().trim().parse().unwrap()) -} +// pub fn get_fixed_array_type_and_size(ty: &str) -> (String, usize) { +// let mut parts = ty.trim().strip_prefix('[').unwrap().strip_suffix(']').unwrap().split(';'); +// (parts.next().unwrap().trim().to_string(), parts.last().unwrap().trim().parse().unwrap()) +// } /// split a tuple in array of items (nested tuples are not splitted). /// example (u8, (u16, u32), u128) -> ["u8", "(u16, u32)", "u128"] pub fn get_tuple_item_types(ty: &str) -> Vec {