[−][src]Struct lox_lang::VM
Implementations
impl VM
[src]
pub fn add_std_globals(&mut self)
[src]
pub fn add_arguments_instance(&mut self)
[src]
impl VM
[src]
pub fn alloc<T: Any>(&mut self, obj: T) -> Gc<T>
[src]
Allocate a garbage-collected value on the heap.
This method is how to obtain a Gc
pointer (not exported from this crate and has no public
constructor). Values allocated with this method will be owned (and eventually freed) by the
VM. If the value lives until the VM goes out of scope, it will be freed in the VM's Drop
implementation.
For a usage example, see NativeFun
.
impl VM
[src]
pub fn buffer_output(&mut self, should_buffer: bool)
[src]
Print directly to stdout, or buffer output internally.
Pass true
to this method to buffer print output internally. This output can then be
accessed through this struct's Read
implementation.
Pass false
(the default mode) to sink all program output directly to Stdout
.
Example
use std::io::Read; let mut vm = lox_lang::VM::default(); vm.buffer_output(true); vm.interpret("print nil == true;"); let mut buffer = String::new(); vm.read_to_string(&mut buffer).unwrap(); assert_eq!(buffer, "false\n"); vm.interpret("for (var a = 3; a < 12; a = a + 3) print a;"); buffer.clear(); vm.read_to_string(&mut buffer).unwrap(); assert_eq!(buffer, "3\n6\n9\n");
impl<'source> VM
[src]
pub fn interpret<T: AsRef<str> + 'source>(
&mut self,
source: T
) -> Result<(), Vec<Error>>
[src]
&mut self,
source: T
) -> Result<(), Vec<Error>>
Compile and run Lox code from a source string.
Errors
Errors are returned in a Vec
. This is because compilation (not runtime) is able to
continue after error(s) are encountered.
pub fn define_global<T: ?Sized + ToString>(&mut self, name: &T, value: Value)
[src]
Define a global variable inside the runtime.
Since globals are late bound in Lox, functions that reference the provided name will see the provided value, even if they were declared in the runtime before calling this method.
Example
use std::io::Read; let mut vm = lox_lang::VM::default(); vm.buffer_output(true); vm.interpret("fun hello() { print world; }"); vm.define_global("world", "greetings, Earthling.".into()); vm.interpret("hello();"); let mut buffer = String::new(); vm.read_to_string(&mut buffer).unwrap(); assert_eq!(buffer, "greetings, Earthling.\n");
Trait Implementations
impl Default for VM
[src]
fn default() -> Self
[src]
The VM
constructor.
Program output defaults to Stdout
. To customize
this behavior, see the buffer_output
method.
impl Drop for VM
[src]
impl Read for VM
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
[src]
fn read_vectored(&mut self, bufs: &mut [IoSliceMut]) -> Result<usize, Error>
1.36.0[src]
fn is_read_vectored(&self) -> bool
[src]
unsafe fn initializer(&self) -> Initializer
[src]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn bytes(self) -> Bytes<Self>
1.0.0[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
R: Read,
fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
Auto Trait Implementations
impl !RefUnwindSafe for VM
impl !Send for VM
impl !Sync for VM
impl Unpin for VM
impl !UnwindSafe for VM
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,