diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index c3db7cdffb..5af9475250 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -29,7 +29,8 @@ pub fn eval_operator(op: &Expression) -> Result { } fn eval_call(state: &State, call: &Call) -> Result { - let decl = state.parser_state.get_decl(call.decl_id); + let parser_state = state.parser_state.borrow(); + let decl = parser_state.get_decl(call.decl_id); if let Some(block_id) = decl.body { let state = state.enter_scope(); for (arg, param) in call @@ -44,7 +45,8 @@ fn eval_call(state: &State, call: &Call) -> Result { state.add_var(var_id, result); } - let block = state.parser_state.get_block(block_id); + let parser_state = state.parser_state.borrow(); + let block = parser_state.get_block(block_id); eval_block(&state, block) } else if decl.signature.name == "let" { let var_id = call.positional[0] @@ -91,14 +93,15 @@ fn eval_call(state: &State, call: &Call) -> Result { let result = eval_expression(state, cond)?; match result { Value::Bool { val, span } => { + let parser_state = state.parser_state.borrow(); if val { - let block = state.parser_state.get_block(then_block); + let block = parser_state.get_block(then_block); let state = state.enter_scope(); eval_block(&state, block) } else if let Some(else_case) = else_case { if let Some(else_expr) = else_case.as_keyword() { if let Some(block_id) = else_expr.as_block() { - let block = state.parser_state.get_block(block_id); + let block = parser_state.get_block(block_id); let state = state.enter_scope(); eval_block(&state, block) } else { @@ -129,7 +132,8 @@ fn eval_call(state: &State, call: &Call) -> Result { let block = call.positional[0] .as_block() .expect("internal error: expected block"); - let block = state.parser_state.get_block(block); + let parser_state = state.parser_state.borrow(); + let block = parser_state.get_block(block); let state = state.enter_scope(); let start_time = Instant::now(); @@ -152,7 +156,8 @@ fn eval_call(state: &State, call: &Call) -> Result { let block = call.positional[2] .as_block() .expect("internal error: expected block"); - let block = state.parser_state.get_block(block); + let parser_state = state.parser_state.borrow(); + let block = parser_state.get_block(block); let state = state.enter_scope(); @@ -176,15 +181,15 @@ fn eval_call(state: &State, call: &Call) -> Result { span: call.positional[0].span, }) } else if decl.signature.name == "vars" { - state.parser_state.print_vars(); + state.parser_state.borrow().print_vars(); Ok(Value::Nothing { span: call.positional[0].span, }) } else if decl.signature.name == "decls" { - state.parser_state.print_decls(); + state.parser_state.borrow().print_decls(); Ok(Value::Nothing { span: call.head }) } else if decl.signature.name == "blocks" { - state.parser_state.print_blocks(); + state.parser_state.borrow().print_blocks(); Ok(Value::Nothing { span: call.head }) } else if decl.signature.name == "stack" { state.print_stack(); @@ -229,7 +234,8 @@ pub fn eval_expression(state: &State, expr: &Expression) -> Result { - let block = state.parser_state.get_block(*block_id); + let parser_state = state.parser_state.borrow(); + let block = parser_state.get_block(*block_id); let state = state.enter_scope(); eval_block(&state, block) diff --git a/crates/nu-engine/src/state.rs b/crates/nu-engine/src/state.rs index fdbe38e7c3..e1a8305229 100644 --- a/crates/nu-engine/src/state.rs +++ b/crates/nu-engine/src/state.rs @@ -3,19 +3,19 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc}; use crate::{value::Value, ShellError}; -pub struct State<'a> { - pub parser_state: &'a ParserState, +pub struct State { + pub parser_state: Rc>, pub stack: Stack, } -impl<'a> State<'a> { +impl State { pub fn get_var(&self, var_id: VarId) -> Result { self.stack.get_var(var_id) } - pub fn enter_scope(&self) -> State<'a> { + pub fn enter_scope(&self) -> State { Self { - parser_state: self.parser_state, + parser_state: self.parser_state.clone(), stack: self.stack.clone().enter_scope(), } } diff --git a/src/main.rs b/src/main.rs index c22aa1227e..789963ef3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,7 @@ fn main() -> std::io::Result<()> { ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta); let state = nu_engine::State { - parser_state: &*parser_state.borrow(), + parser_state: parser_state.clone(), stack: nu_engine::Stack::new(), }; @@ -88,7 +88,7 @@ fn main() -> std::io::Result<()> { ParserState::merge_delta(&mut *parser_state.borrow_mut(), delta); let state = nu_engine::State { - parser_state: &*parser_state.borrow(), + parser_state: parser_state.clone(), stack: stack.clone(), };