Struct red4ext_rs::types::StackFrame

source ·
pub struct StackFrame(/* private fields */);
Expand description

A script stack frame.

Implementations§

source§

impl StackFrame

source

pub fn func(&self) -> &Function

Returns the current function of the stack frame.

source

pub fn parent(&self) -> Option<&StackFrame>

Returns the parent stack frame.

source

pub fn parent_iter(&self) -> impl Iterator<Item = &StackFrame>

Returns an iterator over all parent stack frames.

source

pub fn context(&self) -> Option<&IScriptable>

Returns the context of the stack frame, the this pointer.

source

pub fn has_code(&self) -> bool

Returns true if the stack frame has a code block.

source

pub fn locals(&self) -> ValueContainer

Returns the memory address where local variables are stored.

source

pub fn params(&self) -> ValueContainer

Returns the memory address where parameters are stored.

source

pub unsafe fn instr_at<I: Instr>(&self, offset: isize) -> Option<&I>

Interprets the code at specified offset as an instruction of type I.

source

pub unsafe fn step(&mut self)

Steps over a single opcode (1 byte).

source

pub unsafe fn get_arg<T>(&mut self) -> T
where T: FromRepr, T::Repr: Default,

Retrieves the next argument from the stack frame.

§Safety

The type T must be the correct type of the next argument.

source

pub fn args_state(&self) -> StackArgsState

Captures the state of stack arguments.

Use its returned value with restore_args to restore the state of arguments.

source

pub unsafe fn restore_args(&mut self, state: StackArgsState)

Allows to reset the state of function arguments.

§Safety

The state must be saved before reading arguments.

The state must be restored before passing it back to game code.

Stack arguments should neither be partially read, nor partially restored.

§Example

unsafe extern "C" fn detour(
    i: *mut IScriptable,
    f: *mut StackFrame,
    a3: VoidPtr,
    a4: VoidPtr,
    cb: unsafe extern "C" fn(i: *mut IScriptable, f: *mut StackFrame, a3: VoidPtr, a4: VoidPtr),
) {
    let frame = &mut *f;

    // stack must be saved before reading stack function parameters
    let state = frame.args_state();

    // assuming our function accepts these 3 parameters
    let event_name: CName = StackFrame::get_arg(frame);
    let entity_id: EntityId = StackFrame::get_arg(frame);
    let emitter_name: CName = StackFrame::get_arg(frame);

    if should_detour(event_name) {
        // do something else...
    } else {
        // since we've read stack function arguments,
        // stack arguments must be restored before callback.
        frame.restore_args(state);
        cb(i, f, a3, a4);
    }
}

Trait Implementations§

source§

impl Debug for StackFrame

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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

§

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

§

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.