Trait HeapOperations

Source
pub trait HeapOperations {
    // Required methods
    fn alloc_object(&mut self) -> HeapHandleId;
    fn alloc_array(&mut self) -> HeapHandleId;
    fn alloc_function(
        &mut self,
        bytecode: Bytecode,
        arg_count: ArgIndex,
        local_count: LocalIndex,
    ) -> HeapHandleId;
    fn get_object_property(
        &self,
        handle: HeapHandleId,
        key: &str,
    ) -> Option<&Value>;
    fn set_object_property(
        &mut self,
        handle: HeapHandleId,
        key: String,
        value: Value,
    );
    fn set_array_element(
        &mut self,
        handle: HeapHandleId,
        index: ArraySize,
        value: Value,
    );
    fn get_heap(&self) -> &Heap;

    // Provided methods
    fn get_array_element(
        &self,
        _handle: HeapHandleId,
        _index: ArraySize,
    ) -> Option<&Value> { ... }
    fn has_object_property(&self, handle: HeapHandleId, key: &str) -> bool { ... }
    fn get_array_length(&self, _handle: HeapHandleId) -> usize { ... }
}
Expand description

Defines operations for managing the VM’s heap memory

This trait provides the interface for all heap operations including object allocation, property access, and memory management.

Required Methods§

Source

fn alloc_object(&mut self) -> HeapHandleId

Allocates a new object on the heap

§Returns
  • A handle to the newly allocated object
Source

fn alloc_array(&mut self) -> HeapHandleId

Allocates a new array on the heap

§Returns
  • A handle to the newly allocated array
Source

fn alloc_function( &mut self, bytecode: Bytecode, arg_count: ArgIndex, local_count: LocalIndex, ) -> HeapHandleId

Allocates a new function on the heap

§Arguments
  • bytecode - The function’s bytecode
  • arg_count - The number of arguments the function accepts
  • local_count - The number of local variables the function uses
§Returns
  • A handle to the newly allocated function
Source

fn get_object_property(&self, handle: HeapHandleId, key: &str) -> Option<&Value>

Gets a property value from an object

§Arguments
  • handle - The handle to the object
  • key - The name of the property to retrieve
§Returns
  • Some(&value) if the property exists
  • None if the property does not exist
Source

fn set_object_property( &mut self, handle: HeapHandleId, key: String, value: Value, )

Sets a property value on an object

§Arguments
  • handle - The handle to the object
  • key - The name of the property to set
  • value - The value to assign to the property
Source

fn set_array_element( &mut self, handle: HeapHandleId, index: ArraySize, value: Value, )

Sets an element in an array

§Arguments
  • handle - The handle to the array
  • index - The index where to set the element
  • value - The value to assign to the array element
Source

fn get_heap(&self) -> &Heap

Gets a reference to the underlying heap

This allows direct access to the heap for advanced operations that cannot be expressed through the trait interface.

§Returns
  • A reference to the underlying heap

Provided Methods§

Source

fn get_array_element( &self, _handle: HeapHandleId, _index: ArraySize, ) -> Option<&Value>

Gets an element from an array

§Arguments
  • handle - The handle to the array
  • index - The index of the element to retrieve
§Returns
  • Some(&value) if the element exists at the given index
  • None if the index is out of bounds
Source

fn has_object_property(&self, handle: HeapHandleId, key: &str) -> bool

Checks if an object has a specific property

§Arguments
  • handle - The handle to the object
  • key - The name of the property to check
§Returns
  • true if the property exists
  • false if the property does not exist
Source

fn get_array_length(&self, _handle: HeapHandleId) -> usize

Gets the length of an array

§Arguments
  • handle - The handle to the array
§Returns
  • The number of elements in the array

Implementors§