Skip to content

Commit

Permalink
merge last modifications from storage-layout
Browse files Browse the repository at this point in the history
  • Loading branch information
remybar committed May 10, 2024
2 parents f0a7909 + 9fe788a commit 3043460
Show file tree
Hide file tree
Showing 18 changed files with 1,604 additions and 941 deletions.
42 changes: 42 additions & 0 deletions crates/dojo-core/src/database/introspect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,48 @@ impl Introspect_option<T, +Introspect<T>> of Introspect<Option<T>> {
)
}

fn ty() -> Ty {
// TODO: Not used anymore => to remove
Ty::Primitive('u8')
}
}

impl Introspect_array<T, +Introspect<T>> of Introspect<Array<T>> {
fn size() -> Option<usize> {
Option::None
}
fn layout() -> Layout {
Layout::Array(
array![
FieldLayout {
selector: '',
layout: Introspect::<T>::layout()
}
].span()
)
}

fn ty() -> Ty {
// TODO: Not used anymore => to remove
Ty::Primitive('u8')
}
}

impl Introspect_span<T, +Introspect<T>> of Introspect<Span<T>> {
fn size() -> Option<usize> {
Option::None
}
fn layout() -> Layout {
Layout::Array(
array![
FieldLayout {
selector: '',
layout: Introspect::<T>::layout()
}
].span()
)
}

fn ty() -> Ty {
// TODO: Not used anymore => to remove
Ty::Primitive('u8')
Expand Down
4 changes: 3 additions & 1 deletion crates/dojo-core/src/world_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn create_struct_with_enum_second_variant() -> Span<felt252> {
array![1].span()
}

#[derive(Introspect, Drop, Serde)]
#[derive(Introspect, Copy, Drop, Serde)]
#[dojo::model]
struct StructSimpleArrayModel {
#[key]
Expand All @@ -104,6 +104,8 @@ struct StructSimpleArrayModel {
c: u128,
}

impl ArrayU64Copy of core::traits::Copy::<Array<u64>>;

fn create_struct_simple_array_model() -> Span<felt252> {
array![1, 4, 10, 20, 30, 40, 2].span()
}
Expand Down
40 changes: 25 additions & 15 deletions crates/dojo-lang/src/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use cairo_lang_syntax::node::{Terminal, TypedSyntaxNode};
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use dojo_world::manifest::Member;
use itertools::Itertools;
use starknet::core::utils::get_selector_from_name;

#[derive(PartialEq)]
enum CompositeType {
Expand Down Expand Up @@ -131,18 +132,23 @@ fn compute_tuple_size(
}

fn build_array_layout_from_type(item_type: &String) -> String {
let item_type = get_array_item_type(item_type);
format!(
"dojo::database::introspect::Layout::Array(
let array_item_type = get_array_item_type(item_type);

if is_tuple(&array_item_type) {
format!(
"dojo::database::introspect::Layout::Array(
array![
dojo::database::introspect::FieldLayout {{
selector: '',
layout: {}
}}
].span()
)",
build_item_layout_from_type(&item_type)
)
build_item_layout_from_type(&array_item_type)
)
} else {
format!("dojo::database::introspect::Introspect::<{}>::layout()", item_type)
}
}

fn build_tuple_layout_from_type(item_type: &String) -> String {
Expand Down Expand Up @@ -668,11 +674,12 @@ fn handle_introspect_internal(

layout.push(format!(
"dojo::database::introspect::FieldLayout {{
selector: selector!(\"{}\"),
selector: {},
layout: \
dojo::database::introspect::Layout::Fixed(array![{}].span())
}}",
m.name, values
get_selector_from_name(m.name.as_str()).unwrap(),
values
))
}
};
Expand All @@ -693,10 +700,10 @@ fn handle_introspect_internal(
CompositeType::Struct => {
layout.push(format!(
"dojo::database::introspect::FieldLayout {{
selector: selector!(\"{}\"),
selector: {},
layout: dojo::database::introspect::Layout::ByteArray
}}",
m.name
get_selector_from_name(m.name.as_str()).unwrap()
));
}
}
Expand All @@ -723,10 +730,11 @@ fn handle_introspect_internal(
CompositeType::Struct => {
layout.push(format!(
"dojo::database::introspect::FieldLayout {{
selector: selector!(\"{}\"),
selector: {},
layout: {}
}}",
m.name, array_layout
get_selector_from_name(m.name.as_str()).unwrap(),
array_layout
));
}
}
Expand All @@ -750,10 +758,11 @@ fn handle_introspect_internal(
CompositeType::Struct => {
layout.push(format!(
"dojo::database::introspect::FieldLayout {{
selector: selector!(\"{}\"),
selector: {},
layout: {}
}}",
m.name, tuple_layout
get_selector_from_name(m.name.as_str()).unwrap(),
tuple_layout
));
}
}
Expand All @@ -778,10 +787,11 @@ fn handle_introspect_internal(
CompositeType::Struct => {
layout.push(format!(
"dojo::database::introspect::FieldLayout {{
selector: selector!(\"{}\"),
selector: {},
layout: dojo::database::introspect::Introspect::<{}>::layout()
}}",
m.name, m.ty
get_selector_from_name(m.name.as_str()).unwrap(),
m.ty
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
},
{
"type": "struct",
"name": "core::array::Span::<core::felt252>",
"name": "core::byte_array::ByteArray",
"members": [
{
"name": "snapshot",
"type": "@core::array::Array::<core::felt252>"
"name": "data",
"type": "core::array::Array::<core::bytes_31::bytes31>"
},
{
"name": "pending_word",
"type": "core::felt252"
},
{
"name": "pending_word_len",
"type": "core::integer::u32"
}
]
},
Expand All @@ -24,7 +32,17 @@
},
{
"name": "metadata_uri",
"type": "core::array::Span::<core::felt252>"
"type": "core::byte_array::ByteArray"
}
]
},
{
"type": "struct",
"name": "core::array::Span::<core::felt252>",
"members": [
{
"name": "snapshot",
"type": "@core::array::Array::<core::felt252>"
}
]
},
Expand All @@ -38,6 +56,60 @@
}
]
},
{
"type": "struct",
"name": "dojo::database::introspect::FieldLayout",
"members": [
{
"name": "selector",
"type": "core::felt252"
},
{
"name": "layout",
"type": "dojo::database::introspect::Layout"
}
]
},
{
"type": "struct",
"name": "core::array::Span::<dojo::database::introspect::FieldLayout>",
"members": [
{
"name": "snapshot",
"type": "@core::array::Array::<dojo::database::introspect::FieldLayout>"
}
]
},
{
"type": "enum",
"name": "dojo::database::introspect::Layout",
"variants": [
{
"name": "Fixed",
"type": "core::array::Span::<core::integer::u8>"
},
{
"name": "Struct",
"type": "core::array::Span::<dojo::database::introspect::FieldLayout>"
},
{
"name": "Tuple",
"type": "core::array::Span::<dojo::database::introspect::FieldLayout>"
},
{
"name": "Array",
"type": "core::array::Span::<dojo::database::introspect::FieldLayout>"
},
{
"name": "ByteArray",
"type": "()"
},
{
"name": "Enum",
"type": "core::array::Span::<dojo::database::introspect::FieldLayout>"
}
]
},
{
"type": "enum",
"name": "core::bool",
Expand Down Expand Up @@ -89,7 +161,7 @@
"name": "model",
"inputs": [
{
"name": "name",
"name": "selector",
"type": "core::felt252"
}
],
Expand Down Expand Up @@ -193,7 +265,7 @@
},
{
"name": "layout",
"type": "core::array::Span::<core::integer::u8>"
"type": "dojo::database::introspect::Layout"
}
],
"outputs": [
Expand Down Expand Up @@ -221,23 +293,12 @@
},
{
"name": "layout",
"type": "core::array::Span::<core::integer::u8>"
"type": "dojo::database::introspect::Layout"
}
],
"outputs": [],
"state_mutability": "external"
},
{
"type": "function",
"name": "base",
"inputs": [],
"outputs": [
{
"type": "core::starknet::class_hash::ClassHash"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "delete_entity",
Expand All @@ -252,12 +313,23 @@
},
{
"name": "layout",
"type": "core::array::Span::<core::integer::u8>"
"type": "dojo::database::introspect::Layout"
}
],
"outputs": [],
"state_mutability": "external"
},
{
"type": "function",
"name": "base",
"inputs": [],
"outputs": [
{
"type": "core::starknet::class_hash::ClassHash"
}
],
"state_mutability": "view"
},
{
"type": "function",
"name": "is_owner",
Expand Down Expand Up @@ -319,7 +391,7 @@
"type": "core::felt252"
},
{
"name": "system",
"name": "contract",
"type": "core::starknet::contract_address::ContractAddress"
}
],
Expand All @@ -339,7 +411,7 @@
"type": "core::felt252"
},
{
"name": "system",
"name": "contract",
"type": "core::starknet::contract_address::ContractAddress"
}
],
Expand All @@ -355,7 +427,7 @@
"type": "core::felt252"
},
{
"name": "system",
"name": "contract",
"type": "core::starknet::contract_address::ContractAddress"
}
],
Expand Down Expand Up @@ -623,7 +695,7 @@
},
{
"name": "uri",
"type": "core::array::Span::<core::felt252>",
"type": "core::byte_array::ByteArray",
"kind": "data"
}
]
Expand All @@ -635,7 +707,7 @@
"members": [
{
"name": "name",
"type": "core::felt252",
"type": "core::byte_array::ByteArray",
"kind": "data"
},
{
Expand Down Expand Up @@ -710,7 +782,7 @@
"kind": "data"
},
{
"name": "system",
"name": "contract",
"type": "core::starknet::contract_address::ContractAddress",
"kind": "data"
},
Expand Down
Loading

0 comments on commit 3043460

Please sign in to comment.