pub struct ArenaIndex<'a> { /* private fields */ }
Expand description
Interpreter guard that acts as a GC arena savepoint.
Arena savepoints ensure mruby objects are reaped even when allocated with the C API.
mruby manages objects created via the C API in a memory construct called the arena. The arena is a stack and objects stored there are permanently alive to avoid having to track lifetimes externally to the interpreter.
An ArenaIndex
is an index to some position of the stack. When restoring
an ArenaIndex
, the stack pointer is moved. All objects beyond the pointer
are no longer live and are eligible to be collected at the next GC.
ArenaIndex
implements Drop
, so letting it go out of scope is
sufficient to ensure objects get collected eventually.
Implementations§
Source§impl<'a> ArenaIndex<'a>
impl<'a> ArenaIndex<'a>
Sourcepub fn new(interp: &'a mut Artichoke) -> Result<Self, ArenaSavepointError>
pub fn new(interp: &'a mut Artichoke) -> Result<Self, ArenaSavepointError>
Create a new arena savepoint.
Methods from Deref<Target = Artichoke>§
Sourcepub fn protect(&mut self, value: Value) -> Value
pub fn protect(&mut self, value: Value) -> Value
Prevent the given value from being garbage collected.
Calls sys::mrb_gc_protect
on this value which adds it to the GC
arena. This object will remain in the arena until ArenaIndex::restore
restores the arena to an index before this call to protect.
Sourcepub unsafe fn with_ffi_boundary<F, T>(
&mut self,
func: F,
) -> Result<T, InterpreterExtractError>
pub unsafe fn with_ffi_boundary<F, T>( &mut self, func: F, ) -> Result<T, InterpreterExtractError>
Execute a a closure by moving the State
into the mrb
instance.
This method prepares this interpreter to cross an FFI boundary. When the
Artichoke implementation calls mruby FFI functions, the State
must be
moved into the sys::mrb_state
userdata pointer.
§Safety
This method moves the State
out of this instance into the mrb
instance. During this function’s execution, this instance may be
partially initialized.
This function is only safe to call if the closure only calls FFI
functions that use a raw *mut sys::mrb_state
.