pub struct Context {
pub module_loader: Rc<RefCell<dyn ModuleLoader>>,
pub cwd: PathBuf,
}Expand description
Struct to interact with the runtime.
§Example
let ctx = Context::new();
let src_code = r#"
use { println, eprintln } from "std::io";
fn add(a: float, b: float) -> float {
return a + b;
}
fn main() -> int {
let i = 3.14;
let j = true;
if j {
i = add(i, 2.0);
} else {
eprintln("Goodbye, world!");
}
return 0;
}
main();
"#;
let source = Source::from_string(src_code);
let module = Module::from_source(source, ctx)?;
let result = ctx.eval(module);
assert_eq!(result, Ok(Value::Int(3)));Fields§
§module_loader: Rc<RefCell<dyn ModuleLoader>>§cwd: PathBufImplementations§
Source§impl Context
impl Context
Sourcepub fn insert_module(&mut self, name: String, module: Module)
pub fn insert_module(&mut self, name: String, module: Module)
Insert a module into the context.
§Arguments
name- The name of the module.module- The module to insert.
Sourcepub fn query_module(&self, name: &str) -> Option<Module>
pub fn query_module(&self, name: &str) -> Option<Module>
Sourcepub fn load_module(&mut self, referrer: &Module, spec: &str) -> Result<Module>
pub fn load_module(&mut self, referrer: &Module, spec: &str) -> Result<Module>
Load a module from the context.
This function is different from query_module in that it will attempt to load the module from the cache
if it is not found it will try to resolve the path and load the module.
§Arguments
referrer- The module that is requesting the module.spec- The name of the module to load.
pub fn module_keys(&self) -> Vec<String>
Sourcepub fn upsert_module(&mut self, name: String, module: Module)
pub fn upsert_module(&mut self, name: String, module: Module)
Inserts or updates a module in the context.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Context
impl !RefUnwindSafe for Context
impl !Send for Context
impl !Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more