pub struct VM { /* private fields */ }
Expand description
Implementations§
Source§impl VM
impl VM
pub fn add_std_globals(&mut self)
pub fn add_arguments_instance(&mut self)
Source§impl VM
impl VM
Sourcepub fn alloc<T: Any>(&mut self, obj: T) -> Gc<T>
pub fn alloc<T: Any>(&mut self, obj: T) -> Gc<T>
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
.
Source§impl VM
impl VM
Sourcepub fn buffer_output(&mut self, should_buffer: bool)
pub fn buffer_output(&mut self, should_buffer: bool)
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");
Source§impl<'source> VM
impl<'source> VM
Sourcepub fn interpret<T: AsRef<str> + 'source>(
&mut self,
source: T,
) -> Result<(), Vec<Error>>
pub fn interpret<T: AsRef<str> + 'source>( &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.
Sourcepub fn define_global<T: ?Sized + ToString>(&mut self, name: &T, value: Value)
pub fn define_global<T: ?Sized + ToString>(&mut self, name: &T, value: Value)
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§
Source§impl Default for VM
impl Default for VM
Source§fn default() -> Self
fn default() -> Self
The VM
constructor.
Program output defaults to Stdout
. To customize
this behavior, see the buffer_output
method.
Source§impl Read for VM
impl Read for VM
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
buf
. Read more1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read more