neige_lua/state/
state.rs

1use std::{
2    cell::{Ref, RefCell, RefMut},
3    rc::Rc,
4};
5
6use crate::value::value::LuaValue;
7
8use super::{node::LuaNode, stack::LuaStack};
9
10#[derive(Clone, Debug)]
11pub struct LuaState {
12    pub node: Rc<RefCell<LuaNode>>,
13}
14
15impl LuaState {
16    pub fn new() -> Self {
17        let node = LuaNode::new();
18        Self { node }
19    }
20
21    pub(crate) fn pop_lua_stack(&self) {
22        let mut node = self.get_node_mut();
23        if let Some(stack) = &node.stack.clone() {
24            node.stack = stack.borrow().prev.clone();
25            stack.borrow_mut().prev = None;
26        }
27    }
28
29    pub(crate) fn push_lua_stack(&self, lua_stack: Rc<RefCell<LuaStack>>) {
30        let mut node = self.get_node_mut();
31        if let Some(prev_stack) = &node.stack {
32            lua_stack.borrow_mut().prev = Some(prev_stack.clone())
33        }
34        node.stack = Some(lua_stack);
35    }
36
37    pub(crate) fn get_node(&self) -> Ref<LuaNode> {
38        self.node.borrow()
39    }
40
41    /// 获取node信息
42    /// ### 返回值
43    /// * `RefMut<LuaNode>` lua node的引用
44    pub(crate) fn get_node_mut(&self) -> RefMut<LuaNode> {
45        self.node.borrow_mut()
46    }
47
48    pub(crate) fn registry_get(&self, key: &LuaValue) -> LuaValue {
49        let node = self.get_node();
50        node.registry.get(key)
51    }
52
53    pub(crate) fn registry_set(&self, key: LuaValue, val: LuaValue) {
54        let node = self.get_node();
55        node.registry.put(key, val)
56    }
57}