Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

feat: add ModelDefinition to Dojo Model #10

Merged
merged 3 commits into from
Oct 11, 2024
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
41 changes: 40 additions & 1 deletion crates/compiler/src/plugin/attribute_macros/patches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,25 @@ pub impl $type_name$ModelImpl of dojo::model::Model<$type_name$> {
fn packed_size() -> Option<usize> {
dojo::meta::layout::compute_packed_size(Self::layout())
}

#[inline(always)]
fn schema() -> dojo::meta::introspect::Ty {
dojo::meta::introspect::Introspect::<$type_name$>::ty()
}

#[inline(always)]
fn definition() -> dojo::model::ModelDefinition {
dojo::model::ModelDefinition {
name: Self::name(),
namespace: Self::namespace(),
namespace_selector: Self::namespace_hash(),
version: Self::version(),
layout: Self::layout(),
schema: Self::schema(),
packed_size: Self::packed_size(),
unpacked_size: dojo::meta::introspect::Introspect::<$type_name$>::size(),
}
}
}

#[cfg(target: \"test\")]
Expand Down Expand Up @@ -506,6 +525,10 @@ pub mod $contract_name$ {
fn schema(self: @ContractState) -> dojo::meta::introspect::Ty {
dojo::meta::introspect::Introspect::<$type_name$>::ty()
}

fn definition(self: @ContractState) -> dojo::model::ModelDefinition {
dojo::model::Model::<$type_name$>::definition()
}
}

#[abi(embed_v0)]
Expand Down Expand Up @@ -576,13 +599,25 @@ pub impl $type_name$EventImpl of dojo::event::Event<$type_name$> {
$event_namespace_hash$
}

#[inline(always)]
fn definition() -> dojo::event::EventDefinition {
dojo::event::EventDefinition {
name: Self::name(),
namespace: Self::namespace(),
namespace_selector: Self::namespace_hash(),
version: Self::version(),
layout: Self::layout(),
schema: Self::schema()
}
}

#[inline(always)]
fn layout() -> dojo::meta::Layout {
dojo::meta::introspect::Introspect::<$type_name$>::layout()
}

#[inline(always)]
fn schema(self: @$type_name$) -> dojo::meta::introspect::Ty {
fn schema() -> dojo::meta::introspect::Ty {
dojo::meta::introspect::Introspect::<$type_name$>::ty()
}

Expand Down Expand Up @@ -659,6 +694,10 @@ pub mod $contract_name$ {
$event_namespace_hash$
}

fn definition(self: @ContractState) -> dojo::event::EventDefinition {
dojo::event::Event::<$type_name$>::definition()
}

fn layout(self: @ContractState) -> dojo::meta::Layout {
dojo::event::Event::<$type_name$>::layout()
}
Expand Down
114 changes: 111 additions & 3 deletions crates/compiler/src/plugin/plugin_test_data/event
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,25 @@ pub impl MessageEventImpl of dojo::event::Event<Message> {
1452123528942907587532668415362544424816022573043154497385993678618948064048
}

#[inline(always)]
fn definition() -> dojo::event::EventDefinition {
dojo::event::EventDefinition {
name: Self::name(),
namespace: Self::namespace(),
namespace_selector: Self::namespace_hash(),
version: Self::version(),
layout: Self::layout(),
schema: Self::schema()
}
}

#[inline(always)]
fn layout() -> dojo::meta::Layout {
dojo::meta::introspect::Introspect::<Message>::layout()
}

#[inline(always)]
fn schema(self: @Message) -> dojo::meta::introspect::Ty {
fn schema() -> dojo::meta::introspect::Ty {
dojo::meta::introspect::Introspect::<Message>::ty()
}

Expand Down Expand Up @@ -254,6 +266,10 @@ pub mod message {
1452123528942907587532668415362544424816022573043154497385993678618948064048
}

fn definition(self: @ContractState) -> dojo::event::EventDefinition {
dojo::event::Event::<Message>::definition()
}

fn layout(self: @ContractState) -> dojo::meta::Layout {
dojo::event::Event::<Message>::layout()
}
Expand Down Expand Up @@ -454,6 +470,25 @@ fn __wrapper__DojoEventImpl__namespace_hash(mut data: Span::<felt252>) -> Span::
core::array::ArrayTrait::span(@arr)
}

#[implicit_precedence(core::pedersen::Pedersen, core::RangeCheck, core::integer::Bitwise, core::ec::EcOp, core::poseidon::Poseidon, core::SegmentArena, core::circuit::RangeCheck96, core::circuit::AddMod, core::circuit::MulMod, core::gas::GasBuiltin, System)]
fn __wrapper__DojoEventImpl__definition(mut data: Span::<felt252>) -> Span::<felt252> {
core::internal::require_implicit::<System>();
core::internal::revoke_ap_tracking();
core::option::OptionTraitImpl::expect(core::gas::withdraw_gas(), 'Out of gas');

assert(core::array::SpanTrait::is_empty(data), 'Input too long for arguments');
core::option::OptionTraitImpl::expect(
core::gas::withdraw_gas_all(core::gas::get_builtin_costs()), 'Out of gas',
);
let mut contract_state = unsafe_new_contract_state();
let res = DojoEventImpl::definition(@contract_state, );
let mut arr = ArrayTrait::new();
// References.
// Result.
core::serde::Serde::<dojo::event::EventDefinition>::serialize(@res, ref arr);
core::array::ArrayTrait::span(@arr)
}

#[implicit_precedence(core::pedersen::Pedersen, core::RangeCheck, core::integer::Bitwise, core::ec::EcOp, core::poseidon::Poseidon, core::SegmentArena, core::circuit::RangeCheck96, core::circuit::AddMod, core::circuit::MulMod, core::gas::GasBuiltin, System)]
fn __wrapper__DojoEventImpl__layout(mut data: Span::<felt252>) -> Span::<felt252> {
core::internal::require_implicit::<System>();
Expand Down Expand Up @@ -501,6 +536,7 @@ pub mod __external {
pub use super::__wrapper__DojoEventImpl__selector as selector;
pub use super::__wrapper__DojoEventImpl__name_hash as name_hash;
pub use super::__wrapper__DojoEventImpl__namespace_hash as namespace_hash;
pub use super::__wrapper__DojoEventImpl__definition as definition;
pub use super::__wrapper__DojoEventImpl__layout as layout;
pub use super::__wrapper__DojoEventImpl__schema as schema;
}
Expand Down Expand Up @@ -647,13 +683,25 @@ pub impl MyEventWithNamespaceEventImpl of dojo::event::Event<MyEventWithNamespac
1685136890688416384941629523783652800960468745356230625531475538826800548713
}

#[inline(always)]
fn definition() -> dojo::event::EventDefinition {
dojo::event::EventDefinition {
name: Self::name(),
namespace: Self::namespace(),
namespace_selector: Self::namespace_hash(),
version: Self::version(),
layout: Self::layout(),
schema: Self::schema()
}
}

#[inline(always)]
fn layout() -> dojo::meta::Layout {
dojo::meta::introspect::Introspect::<MyEventWithNamespace>::layout()
}

#[inline(always)]
fn schema(self: @MyEventWithNamespace) -> dojo::meta::introspect::Ty {
fn schema() -> dojo::meta::introspect::Ty {
dojo::meta::introspect::Introspect::<MyEventWithNamespace>::ty()
}

Expand Down Expand Up @@ -713,6 +761,10 @@ pub mod my_event_with_namespace {
1685136890688416384941629523783652800960468745356230625531475538826800548713
}

fn definition(self: @ContractState) -> dojo::event::EventDefinition {
dojo::event::Event::<MyEventWithNamespace>::definition()
}

fn layout(self: @ContractState) -> dojo::meta::Layout {
dojo::event::Event::<MyEventWithNamespace>::layout()
}
Expand Down Expand Up @@ -913,6 +965,25 @@ fn __wrapper__DojoEventImpl__namespace_hash(mut data: Span::<felt252>) -> Span::
core::array::ArrayTrait::span(@arr)
}

#[implicit_precedence(core::pedersen::Pedersen, core::RangeCheck, core::integer::Bitwise, core::ec::EcOp, core::poseidon::Poseidon, core::SegmentArena, core::circuit::RangeCheck96, core::circuit::AddMod, core::circuit::MulMod, core::gas::GasBuiltin, System)]
fn __wrapper__DojoEventImpl__definition(mut data: Span::<felt252>) -> Span::<felt252> {
core::internal::require_implicit::<System>();
core::internal::revoke_ap_tracking();
core::option::OptionTraitImpl::expect(core::gas::withdraw_gas(), 'Out of gas');

assert(core::array::SpanTrait::is_empty(data), 'Input too long for arguments');
core::option::OptionTraitImpl::expect(
core::gas::withdraw_gas_all(core::gas::get_builtin_costs()), 'Out of gas',
);
let mut contract_state = unsafe_new_contract_state();
let res = DojoEventImpl::definition(@contract_state, );
let mut arr = ArrayTrait::new();
// References.
// Result.
core::serde::Serde::<dojo::event::EventDefinition>::serialize(@res, ref arr);
core::array::ArrayTrait::span(@arr)
}

#[implicit_precedence(core::pedersen::Pedersen, core::RangeCheck, core::integer::Bitwise, core::ec::EcOp, core::poseidon::Poseidon, core::SegmentArena, core::circuit::RangeCheck96, core::circuit::AddMod, core::circuit::MulMod, core::gas::GasBuiltin, System)]
fn __wrapper__DojoEventImpl__layout(mut data: Span::<felt252>) -> Span::<felt252> {
core::internal::require_implicit::<System>();
Expand Down Expand Up @@ -960,6 +1031,7 @@ pub mod __external {
pub use super::__wrapper__DojoEventImpl__selector as selector;
pub use super::__wrapper__DojoEventImpl__name_hash as name_hash;
pub use super::__wrapper__DojoEventImpl__namespace_hash as namespace_hash;
pub use super::__wrapper__DojoEventImpl__definition as definition;
pub use super::__wrapper__DojoEventImpl__layout as layout;
pub use super::__wrapper__DojoEventImpl__schema as schema;
}
Expand Down Expand Up @@ -1106,13 +1178,25 @@ pub impl MyEventNoHistoricalEventImpl of dojo::event::Event<MyEventNoHistorical>
1452123528942907587532668415362544424816022573043154497385993678618948064048
}

#[inline(always)]
fn definition() -> dojo::event::EventDefinition {
dojo::event::EventDefinition {
name: Self::name(),
namespace: Self::namespace(),
namespace_selector: Self::namespace_hash(),
version: Self::version(),
layout: Self::layout(),
schema: Self::schema()
}
}

#[inline(always)]
fn layout() -> dojo::meta::Layout {
dojo::meta::introspect::Introspect::<MyEventNoHistorical>::layout()
}

#[inline(always)]
fn schema(self: @MyEventNoHistorical) -> dojo::meta::introspect::Ty {
fn schema() -> dojo::meta::introspect::Ty {
dojo::meta::introspect::Introspect::<MyEventNoHistorical>::ty()
}

Expand Down Expand Up @@ -1172,6 +1256,10 @@ pub mod my_event_no_historical {
1452123528942907587532668415362544424816022573043154497385993678618948064048
}

fn definition(self: @ContractState) -> dojo::event::EventDefinition {
dojo::event::Event::<MyEventNoHistorical>::definition()
}

fn layout(self: @ContractState) -> dojo::meta::Layout {
dojo::event::Event::<MyEventNoHistorical>::layout()
}
Expand Down Expand Up @@ -1372,6 +1460,25 @@ fn __wrapper__DojoEventImpl__namespace_hash(mut data: Span::<felt252>) -> Span::
core::array::ArrayTrait::span(@arr)
}

#[implicit_precedence(core::pedersen::Pedersen, core::RangeCheck, core::integer::Bitwise, core::ec::EcOp, core::poseidon::Poseidon, core::SegmentArena, core::circuit::RangeCheck96, core::circuit::AddMod, core::circuit::MulMod, core::gas::GasBuiltin, System)]
fn __wrapper__DojoEventImpl__definition(mut data: Span::<felt252>) -> Span::<felt252> {
core::internal::require_implicit::<System>();
core::internal::revoke_ap_tracking();
core::option::OptionTraitImpl::expect(core::gas::withdraw_gas(), 'Out of gas');

assert(core::array::SpanTrait::is_empty(data), 'Input too long for arguments');
core::option::OptionTraitImpl::expect(
core::gas::withdraw_gas_all(core::gas::get_builtin_costs()), 'Out of gas',
);
let mut contract_state = unsafe_new_contract_state();
let res = DojoEventImpl::definition(@contract_state, );
let mut arr = ArrayTrait::new();
// References.
// Result.
core::serde::Serde::<dojo::event::EventDefinition>::serialize(@res, ref arr);
core::array::ArrayTrait::span(@arr)
}

#[implicit_precedence(core::pedersen::Pedersen, core::RangeCheck, core::integer::Bitwise, core::ec::EcOp, core::poseidon::Poseidon, core::SegmentArena, core::circuit::RangeCheck96, core::circuit::AddMod, core::circuit::MulMod, core::gas::GasBuiltin, System)]
fn __wrapper__DojoEventImpl__layout(mut data: Span::<felt252>) -> Span::<felt252> {
core::internal::require_implicit::<System>();
Expand Down Expand Up @@ -1419,6 +1526,7 @@ pub mod __external {
pub use super::__wrapper__DojoEventImpl__selector as selector;
pub use super::__wrapper__DojoEventImpl__name_hash as name_hash;
pub use super::__wrapper__DojoEventImpl__namespace_hash as namespace_hash;
pub use super::__wrapper__DojoEventImpl__definition as definition;
pub use super::__wrapper__DojoEventImpl__layout as layout;
pub use super::__wrapper__DojoEventImpl__schema as schema;
}
Expand Down
Loading
Loading