Struct GlobalEnv

Source
pub struct GlobalEnv {
    pub resolver: Option<Rc<RefCell<dyn ModuleResolver>>>,
    pub thread_creator: Option<Arc<Mutex<dyn ThreadCreator>>>,
    /* private fields */
}
Expand description

Holds global environment variables.

This data structure is part of the API. It’s there to make functions or values available to a WLambda program.

This environment structure is usually wrapped inside an EvalContext which augments it for calling the compiler and allows evaluation of the code.

See also:

Fields§

§resolver: Option<Rc<RefCell<dyn ModuleResolver>>>

Holds the default module resolver for this global environment. If some code executed with this global environment uses !@import this resolver will be used to resolve the given module name.

§thread_creator: Option<Arc<Mutex<dyn ThreadCreator>>>

Holds the default thread creator for this global environment. If some code executed with this global environment spawns a thread, then this thread creator will create the thread handle, which is passed back to WLambda.

Implementations§

Source§

impl GlobalEnv

Source

pub fn add_func<T>( &mut self, fnname: &str, fun: T, min_args: Option<usize>, max_args: Option<usize>, )
where T: 'static + Fn(&mut Env, usize) -> Result<VVal, StackAction>,

Adds a function to a GlobalEnv.

This is an example of how to add a function:

use wlambda::compiler::GlobalEnv;
use wlambda::vval::{Env, VVal};

let g = GlobalEnv::new();
g.borrow_mut().add_func(
    "push",
    |env: &mut Env, argc: usize| {
        if argc < 2 { return Ok(VVal::None); }
        let v = env.arg(0);
        v.push(env.arg(1).clone());
        Ok(v.clone())
    }, Some(2), Some(2));
Source

pub fn set_var(&mut self, var: &str, val: &VVal)

Sets a global variable to a value.

See also EvalContext::set_global_var()

Source

pub fn get_var(&self, var: &str) -> Option<VVal>

Returns the value of a global variable.

See also EvalContext::get_global_var()

Source

pub fn get_var_ref(&self, var: &str) -> Option<VVal>

Returns the reference to the value of a global variable. This does not dereference the VVal::Ref that usually holds the global variable.

See also EvalContext::get_global_var()

Source

pub fn set_module(&mut self, mod_name: &str, symtbl: SymbolTable)

Sets a symbol table for a module before a module asks for it. Modules set via this function have precedence over resolved modules via set_resolver().

Here is an example how to setup your own module:

 use wlambda::{VVal, EvalContext, GlobalEnv, SymbolTable};

 let my_mod = SymbolTable::new();

 let genv = GlobalEnv::new_default();
 genv.borrow_mut().set_module("test", my_mod);
Source

pub fn import_module_as(&mut self, mod_name: &str, prefix: &str) -> bool

Imports all symbols from the designated module with the specified prefix applied. This does not call out to the resolver and only works on previously set_module modules. Returns true if the module was found.

Source

pub fn set_resolver(&mut self, res: Rc<RefCell<dyn ModuleResolver>>)

Sets the module resolver. There is a LocalFileModuleResolver available which loads the modules relative to the current working directory.

Please note that modules made available using set_module have priority over modules that are provided by the resolver.

 use std::rc::Rc;
 use std::cell::RefCell;

 let global = wlambda::compiler::GlobalEnv::new_default();

 let lfmr = Rc::new(RefCell::new(
     wlambda::compiler::LocalFileModuleResolver::new()));

 global.borrow_mut().set_resolver(lfmr);
Source

pub fn new() -> GlobalEnvRef

Creates a new completely empty GlobalEnv.

There is no core language, no std lib. You have to add all that on your own via set_var and set_module.

Source

pub fn new_default() -> GlobalEnvRef

Returns a default global environment. Where default means what the author of WLambda decided what is default at the moment. For more precise global environment, that is not a completely moving target consider the alternate constructor variants.

Global environments constructed with this typically contain:

  • set_module("wlambda", wlambda::prelude::core_symbol_table())
  • set_module("std", wlambda::prelude::std_symbol_table())
  • set_resolver(Rc::new(RefCell::new(wlambda::compiler::LocalFileModuleResolver::new()))
  • set_thread_creator(Some(Arc::new(Mutex::new(DefaultThreadCreator::new()))))
  • import_module_as("wlambda", "")
  • import_module_as("std", "std")

On top of that, the WLambda module has been imported without a prefix and the std module has been loaded with an std: prefix. This means you can load and eval scripts you see all over this documentation.

Source

pub fn import_modules_from(&mut self, parent_global_env: &GlobalEnv)

This function adds the modules that were loaded into memory from the given parent_global_env to the current environment.

Source

pub fn import_from_symtbl(&mut self, prefix: &str, symtbl: SymbolTable)

Imports all functions from the given SymbolTable with the given prefix to the GlobalEnv.

Source

pub fn new_empty_default() -> GlobalEnvRef

This is like new_default but does not import anything, neither the core language nor the std module.

Source

pub fn set_thread_creator(&mut self, tc: Option<Arc<Mutex<dyn ThreadCreator>>>)

Assigns a new thread creator to this GlobalEnv. It will be used to spawn new threads if std:thread:spawn from WLambda’s standard library is called.

Source

pub fn get_thread_creator(&self) -> Option<Arc<Mutex<dyn ThreadCreator>>>

Returns the thread creator for this GlobalEnv if one is set.

Trait Implementations§

Source§

impl Clone for GlobalEnv

Source§

fn clone(&self) -> GlobalEnv

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GlobalEnv

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.