Skip to content

Commit

Permalink
piecrust: expose CallTree and CallTreeElem
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Leegwater Simões committed Sep 26, 2023
1 parent b7b04e7 commit 00b62d5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
1 change: 1 addition & 0 deletions piecrust/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Expose `CallTree` and `CallTreeElem` in the public API [#206]
- Add `CallTreeIter` to improve iteration over call tree [#206]
- Add `panic` import implementation [#271]
- Add `Error::ContractPanic` variant [#271]
Expand Down
17 changes: 9 additions & 8 deletions piecrust/src/session/call_stack.rs → piecrust/src/call_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::mem;

use piecrust_uplink::ContractId;

/// An element of the call tree.
#[derive(Debug, Clone, Copy)]
pub struct CallTreeElem {
pub contract_id: ContractId,
Expand All @@ -22,14 +23,14 @@ pub struct CallTree(Option<*mut CallTreeInner>);

impl CallTree {
/// Creates a new empty call tree, starting with at the given contract.
pub const fn new() -> Self {
pub(crate) const fn new() -> Self {
Self(None)
}

/// Push an element to the call tree.
///
/// This pushes a new child to the current node, and advances to it.
pub fn push(&mut self, elem: CallTreeElem) {
pub(crate) fn push(&mut self, elem: CallTreeElem) {
match self.0 {
None => self.0 = Some(CallTreeInner::new(elem)),
Some(inner) => unsafe {
Expand All @@ -41,7 +42,7 @@ impl CallTree {
}

/// Moves to the previous node, returning the current element.
pub fn move_up(&mut self) -> Option<CallTreeElem> {
pub(crate) fn move_up(&mut self) -> Option<CallTreeElem> {
self.0.map(|inner| unsafe {
let elem = (*inner).elem;

Expand All @@ -57,7 +58,7 @@ impl CallTree {

/// Moves to the previous node, returning the current element, while
/// clearing the tree under it.
pub fn move_up_prune(&mut self) -> Option<CallTreeElem> {
pub(crate) fn move_up_prune(&mut self) -> Option<CallTreeElem> {
self.0.map(|inner| unsafe {
let elem = (*inner).elem;

Expand All @@ -76,7 +77,7 @@ impl CallTree {
/// node.
///
/// The zeroth previous element is the current node.
pub fn nth_up(&self, n: usize) -> Option<CallTreeElem> {
pub(crate) fn nth_up(&self, n: usize) -> Option<CallTreeElem> {
let mut current = self.0;

let mut i = 0;
Expand All @@ -89,15 +90,15 @@ impl CallTree {
}

/// Clears the call tree of all elements.
pub fn clear(&mut self) {
pub(crate) fn clear(&mut self) {
while self.0.is_some() {
self.move_up();
}
}

/// Returns an iterator over the call tree, starting from the rightmost
/// leaf, and proceeding to the top of the current position of the tree.
pub fn iter(&self) -> CallTreeIter {
pub fn iter(&self) -> impl Iterator<Item = &CallTreeElem> {
CallTreeIter {
node: self.0,
_marker: PhantomData,
Expand Down Expand Up @@ -150,7 +151,7 @@ impl CallTreeInner {
///
/// It starts at the righmost node and proceeds leftward towards its siblings,
/// up toward the root.
pub struct CallTreeIter<'a> {
struct CallTreeIter<'a> {
node: Option<*mut CallTreeInner>,
_marker: PhantomData<&'a ()>,
}
Expand Down
2 changes: 2 additions & 0 deletions piecrust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
#[macro_use]
mod bytecode_macro;
mod call_tree;
mod contract;
mod error;
mod imports;
Expand All @@ -114,6 +115,7 @@ mod store;
mod types;
mod vm;

pub use call_tree::{CallTree, CallTreeElem};
pub use contract::{ContractData, ContractDataBuilder};
pub use error::Error;
pub use session::{CallReceipt, Session, SessionData};
Expand Down
8 changes: 2 additions & 6 deletions piecrust/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

pub mod call_stack;

use std::borrow::Cow;
use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
Expand All @@ -24,15 +22,13 @@ use rkyv::{
};
use wasmer_types::WASM_PAGE_SIZE;

use crate::call_tree::{CallTree, CallTreeElem};
use crate::contract::{ContractData, ContractMetadata, WrappedContract};
use crate::error::Error::{self, InitalizationError, PersistenceError};
use crate::instance::WrappedInstance;
use crate::store::{ContractSession, Objectcode};
use crate::types::StandardBufSerializer;
use crate::vm::HostQueries;
use crate::Error;
use crate::Error::{InitalizationError, PersistenceError};

use call_stack::{CallTree, CallTreeElem};

const MAX_META_SIZE: usize = ARGBUF_LEN;
pub const INIT_METHOD: &str = "init";
Expand Down

0 comments on commit 00b62d5

Please sign in to comment.