Chunk

Struct Chunk 

Source
pub struct Chunk {
    pub module_name: Rc<str>,
    pub codegen_location: Location,
    pub preallocate_stack_slots: u32,
    /* private fields */
}
Expand description

A chunk of bytecode.

Fields§

§module_name: Rc<str>

The name of the module where the chunk is located.

§codegen_location: Location

The location emitted for each quad-byte on calls to push.

§preallocate_stack_slots: u32

How many stack slots to preallocate with nil values for variable lookups.

Implementations§

Source§

impl Chunk

Source

pub fn new(module_name: Rc<str>) -> Self

Constructs an empty chunk.

Source

pub fn emit(&mut self, what: impl EncodeInstruction) -> usize

Pushes an encodable piece of data into the chunk. Returns where it’s located.

Source

pub fn emit_number(&mut self, number: f64)

Pushes a number into the chunk.

Source

pub fn emit_string(&mut self, string: &str)

Pushes a string into the chunk.

The string is padded with zeroes such that opcodes are aligned to four bytes.

Source

pub fn patch(&mut self, position: usize, instruction: impl EncodeInstruction)

Patches the instruction at the given position.

Source

pub unsafe fn read_instruction(&self, pc: &mut usize) -> (Opcode, Opr24)

Reads an instruction.

§Safety

Assumes that pc is within the chunk’s bounds and that the opcode at pc is valid.

Source

pub unsafe fn read_number(&self, pc: &mut usize) -> f64

Reads a number.

§Safety

Assumes that pc is within the chunk’s bounds, skipping any checks.

Source

pub unsafe fn read_string(&self, pc: &mut usize) -> &str

Reads a string.

§Safety

This assumes the original string was encoded as proper UTF-8 (which it should have been considering the only way to write a string is to use a &str in the first place).

Source

pub fn len(&self) -> usize

Returns the length of the chunk (in bytes).

Source

pub fn is_empty(&self) -> bool

Returns whether the chunk is empty.

Source

pub fn location(&self, pc: usize) -> Location

Returns the location (in file) of the program counter.

Source

pub fn at_end(&self, pc: usize) -> bool

Returns whether the given program counter is at the end of the chunk.

Trait Implementations§

Source§

impl Debug for Chunk

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Chunk

§

impl RefUnwindSafe for Chunk

§

impl !Send for Chunk

§

impl !Sync for Chunk

§

impl Unpin for Chunk

§

impl UnwindSafe for Chunk

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> 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, 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.