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 variable
  • bp - 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\"]");

Returns the passed in user context value.

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);

Prints a dump of the stack state.

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));

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.