Struct wlambda::vval::Env [−][src]
pub struct Env {}Show 16 fields
pub args: Vec<VVal>, pub call_stack: Vec<Rc<VValFun>>, pub unwind_stack: Vec<UnwindAction>, pub current_self: VVal, pub bp: usize, pub sp: usize, pub argc: usize, pub user: Rc<RefCell<dyn Any>>, pub exports: FnvHashMap<Symbol, VVal>, pub stdio: Stdio, pub accum_val: VVal, pub accum_fun: VVal, pub global: GlobalEnvRef, pub vm_nest: usize, pub loop_info: LoopInfo, pub iter: Option<Rc<RefCell<VValIter>>>,
Expand description
The runtime environment of the evaluator.
The easiest way to get an instance of this, is to create an EvalContext:
use wlambda::{VVal, EvalContext}; let mut ctx = EvalContext::new_default(); let res = VVal::new_str("a") .call(&mut *ctx.local.borrow_mut(), &[VVal::new_str("b")]) .unwrap(); assert_eq!(res.s(), "\"ab\"");
Fields
args: Vec<VVal>
The argument stack, initialized to a size of START_STACK_SIZE
.
call_stack: Vec<Rc<VValFun>>
A stack of the currently called functions.
Used for accessing the up values, backtrace and other details about the function.
unwind_stack: Vec<UnwindAction>
A stack that holds cleanup routines that need to be handled:
current_self: VVal
Holds the object of the currently called method:
bp: usize
The basepointer to reference arguments and local variables.
bp + n (n >= 0)
references a local variablebp - n (n > 0)
references an argument
sp: usize
The current stack pointer.
argc: usize
The argument count to the current call.
user: Rc<RefCell<dyn Any>>
A user defined variable that holds user context information. See also the with_user_do function.
exports: FnvHashMap<Symbol, VVal>
The exported names of this module.
stdio: Stdio
This is the standard output used for any functions in
WLambda that print something. Such as std:displayln
or std:writeln
.
accum_val: VVal
Current accumulator value:
accum_fun: VVal
Current accumulator function:
global: GlobalEnvRef
A pointer to the global environment, holding stuff like the module loader and thread creator.
vm_nest: usize
A counter that counts the nesting depth in vm() calls
loop_info: LoopInfo
Holds information to process ‘next’ and ‘break’ for loop constructs:
iter: Option<Rc<RefCell<VValIter>>>
Holds the current iterator for the ‘iter’ construct.
Implementations
Sets a custom stdio procedure. This can be used to redirect the standard input/output operations of WLambda to other sinks and sources. For instance writing and reading from a Buffer when using WASM or some other embedded application:
use wlambda::*; use std::rc::Rc; use std::cell::RefCell; let new_output : Rc<RefCell<Vec<u8>>> = Rc::new(RefCell::new(vec![])); let new_input = Rc::new(RefCell::new(std::io::Cursor::new("abc\ndef\n1 2 3 4\n" .to_string().as_bytes().to_vec()))); let memory_stdio = vval::Stdio::new_from_mem(new_input, new_output.clone()); let mut ctx = EvalContext::new_default(); ctx.local.borrow_mut().set_stdio(memory_stdio); ctx.eval("std:displayln :TEST 123").unwrap(); let output = String::from_utf8(new_output.borrow().clone()).unwrap(); assert_eq!(output, "TEST 123\n"); let out_lines = ctx.eval("!l = $[]; std:io:lines \\std:push l _; l").unwrap().s(); assert_eq!(out_lines, "$[\"abc\\n\",\"def\\n\",\"1 2 3 4\\n\"]");
Easier access to the user data field in Env
In the following example the user supplies a registry vector for storing VVals. A callback is stored, which is then later executed.
use wlambda::{VVal, EvalContext, GlobalEnv}; use wlambda::vval::Env; use std::rc::Rc; use std::cell::RefCell; let global = GlobalEnv::new_default(); global.borrow_mut().add_func("reg", |env: &mut Env, _argc: usize| { let fun = env.arg(0); env.with_user_do(|v: &mut Vec<VVal>| v.push(fun.clone())); Ok(VVal::None) }, Some(1), Some(1)); let reg : Rc<RefCell<Vec<VVal>>> = Rc::new(RefCell::new(Vec::new())); let mut ctx = EvalContext::new_with_user(global, reg.clone()); ctx.eval("reg { _ + 10 }").unwrap(); let n = reg.borrow_mut()[0].clone(); let ret = ctx.call(&n, &vec![VVal::Int(11)]).unwrap(); assert_eq!(ret.i(), 21);
pub fn with_object<T>(
&mut self,
object: VVal,
f: T
) -> Result<VVal, StackAction> where
T: Fn(&mut Env) -> Result<VVal, StackAction>,
pub fn with_local_call_info<T>(
&mut self,
argc: usize,
f: T
) -> Result<VVal, StackAction> where
T: Fn(&mut Env) -> Result<VVal, StackAction>,
pub fn with_restore_sp<T>(&mut self, f: T) -> Result<VVal, StackAction> where
T: Fn(&mut Env) -> Result<VVal, StackAction>,
Prints a dump of the stack state.
pub fn with_accum<T>(&mut self, v: VVal, acfun: T) -> Result<VVal, StackAction> where
T: Fn(&mut Env) -> Result<VVal, StackAction>,
Creates a new error VVal with the given string as error message. Takes care to annotate the error value with the current function information.
use wlambda::compiler::EvalContext; use wlambda::vval::{VVal, VValFun, Env}; let mut ctx = EvalContext::new_default(); ctx.set_global_var("xyz", &VValFun::new_fun( move |env: &mut Env, argc: usize| { let ok = false; if !ok { Ok(env.new_err( format!("Something was not ok!"))) } else { Ok(VVal::Bol(true)) } }, None, None, false));