multiversx_sc_scenario/debug_executor/
static_var_stack.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::{cell::RefCell, rc::Rc};

use multiversx_sc::types::LockableStaticBuffer;

use super::TxStaticVars;

#[derive(Debug, Default)]
pub struct StaticVarData {
    pub lockable_static_buffer_cell: RefCell<LockableStaticBuffer>,
    pub static_vars_cell: RefCell<TxStaticVars>,
}

#[derive(Debug, Default)]
pub struct StaticVarStack(Vec<Rc<StaticVarData>>);

thread_local!(
    static STATIC_STACK: RefCell<StaticVarStack> = RefCell::new(StaticVarStack::default())
);

impl StaticVarStack {
    pub fn static_peek() -> Rc<StaticVarData> {
        STATIC_STACK.with(|cell| {
            let stack = cell.borrow();
            stack.0.last().unwrap().clone()
        })
    }

    pub fn static_push() {
        STATIC_STACK.with(|cell| {
            let mut stack = cell.borrow_mut();
            stack.0.push(Rc::default());
        })
    }

    pub fn static_pop() -> Rc<StaticVarData> {
        STATIC_STACK.with(|cell| {
            let mut stack = cell.borrow_mut();
            stack.0.pop().unwrap()
        })
    }
}