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
sourceimpl Env
impl Env
pub fn new(global: GlobalEnvRef) -> Env
pub fn new_with_user(global: GlobalEnvRef, user: Rc<RefCell<dyn Any>>) -> Env
sourcepub fn set_stdio(&mut self, stdio: Stdio)
pub fn set_stdio(&mut self, stdio: Stdio)
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\"]");
sourcepub fn with_user_do<T: 'static, F, X>(&mut self, f: F) -> X where
F: Fn(&mut T) -> X,
pub fn with_user_do<T: 'static, F, X>(&mut self, f: F) -> X where
F: Fn(&mut T) -> X,
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 export_name(&mut self, name: &str, value: &VVal)
pub fn set_bp(&mut self, env_size: usize) -> usize
pub fn reset_bp(&mut self, env_size: usize, oldbp: usize)
pub fn self_object(&self) -> VVal
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>,
pub fn push_sp(&mut self, n: usize)
pub fn push(&mut self, v: VVal) -> usize
pub fn stk(&self, offs: usize) -> &VVal
pub fn stk_i(&self, offs: usize) -> i64
pub fn inc_local(&mut self, idx: usize, inc: i16) -> i64
pub fn pop(&mut self) -> VVal
pub fn null_locals(&mut self, from: usize, to: usize)
pub fn popn(&mut self, n: usize)
sourcepub fn dump_stack(&self)
pub fn dump_stack(&self)
Prints a dump of the stack state.
pub fn stk2vec(&self, count: usize) -> VVal
pub fn argv(&self) -> VVal
pub fn argv_ref(&self) -> &[VVal]
pub fn get_up_raw(&mut self, i: usize) -> VVal
pub fn get_up_captured_ref(&self, i: usize) -> VVal
pub fn get_up(&self, i: usize) -> VVal
pub fn arg_ref(&self, i: usize) -> Option<&VVal>
pub fn arg_err_internal(&self, i: usize) -> Option<VVal>
pub fn arg(&self, i: usize) -> VVal
pub fn get_local_up_promotion(&mut self, i: usize) -> VVal
pub fn get_local_captured_ref(&self, i: usize) -> VVal
pub fn reg(&self, i: i32) -> VVal
pub fn get_local(&self, i: usize) -> VVal
pub fn assign_ref_up(&mut self, i: usize, value: VVal)
pub fn assign_ref_local(&mut self, i: usize, value: VVal)
pub fn set_up(&mut self, index: usize, value: VVal)
pub fn set_consume(&mut self, i: usize, value: VVal)
pub fn unwind_depth(&self) -> usize
pub fn push_unwind(&mut self, uwa: UnwindAction)
pub fn unwind_to_depth(&mut self, depth: usize)
pub fn push_fun_call(&mut self, fu: Rc<VValFun>, argc: usize)
pub fn push_clear_locals(&mut self, from: usize, to: usize)
pub fn push_unwind_self(&mut self, new_self: VVal)
pub fn cleanup_loop(&mut self)
pub fn push_loop_info(
&mut self,
current_pc: usize,
break_pc: usize,
uw_depth_offs: usize
)
pub fn push_iter(&mut self, iter: Rc<RefCell<VValIter>>)
pub fn dump_unwind_stack(&self) -> String
pub fn unwind(&mut self, ua: UnwindAction)
pub fn unwind_one(&mut self)
pub fn setup_accumulator(&mut self, v: VVal)
pub fn with_accum<T>(&mut self, v: VVal, acfun: T) -> Result<VVal, StackAction> where
T: Fn(&mut Env) -> Result<VVal, StackAction>,
pub fn get_accum_value(&self) -> VVal
pub fn get_accum_function(&self) -> VVal
sourcepub fn new_err(&self, s: String) -> VVal
pub fn new_err(&self, s: String) -> VVal
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));
pub fn new_panic(&self, val: VVal) -> StackAction
pub fn new_panic_argv(&self, val: VVal, args: VVal) -> StackAction
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Env
impl !Send for Env
impl !Sync for Env
impl Unpin for Env
impl !UnwindSafe for Env
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more