use std::ops::{BitOr, BitOrAssign};
use super::{IntoLua, Result, Table, Value, ValueRef};
use crate::Runtime;
pub(crate) type HookCallback = dyn Fn(&mut HookContext<'_>) -> Result<HookAction>;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct HookMask(pub(crate) u8);
impl HookMask {
pub const NONE: Self = Self(0);
pub const CALL: Self = Self(1);
pub const RETURN: Self = Self(2);
pub const LINE: Self = Self(4);
pub fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
}
impl BitOr for HookMask {
type Output = Self;
fn bitor(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
impl BitOrAssign for HookMask {
fn bitor_assign(&mut self, other: Self) {
*self = *self | other;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HookEvent {
Call,
Return,
Line,
Count,
TailCall,
}
impl HookEvent {
pub fn name(self) -> &'static str {
match self {
Self::Call => "call",
Self::Return => "return",
Self::Line => "line",
Self::Count => "count",
Self::TailCall => "tail call",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HookAction {
Continue,
Yield,
}
#[derive(Clone, Debug)]
pub enum HookFunction {
Host(String),
Lua(Value),
InheritedLua,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct HookSettings {
pub function: HookFunction,
pub mask: HookMask,
pub count: i32,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct HookInfo {
pub name: Option<Vec<u8>>,
pub namewhat: &'static str,
pub what: &'static str,
pub source: Vec<u8>,
pub short_src: Vec<u8>,
pub currentline: i64,
pub linedefined: i64,
pub lastlinedefined: i64,
pub istailcall: bool,
pub ftransfer: u32,
pub ntransfer: u32,
pub nparams: i64,
pub nups: i64,
pub isvararg: bool,
}
pub struct HookContext<'cx> {
pub(crate) runtime: &'cx mut Runtime,
pub(crate) thread: crate::id::Handle<crate::heap::ThreadObj>,
pub(crate) event: HookEvent,
pub(crate) line: Option<u32>,
}
impl HookContext<'_> {
pub fn event(&self) -> HookEvent {
self.event
}
pub fn line(&self) -> Option<u32> {
self.line
}
pub fn info(&self, level: usize) -> Result<Option<HookInfo>> {
self.runtime.api_hook_info(self.thread, level)
}
pub fn depth(&self) -> Result<usize> {
self.runtime.api_hook_depth(self.thread)
}
pub fn local(&self, level: usize, index: i32) -> Result<Option<(Vec<u8>, ValueRef<'_>)>> {
self.runtime.api_hook_local(self.thread, level, index)
}
pub fn set_local(
&mut self,
level: usize,
index: i32,
value: impl IntoLua,
) -> Result<Option<Vec<u8>>> {
let value = value.into_lua(self.runtime)?;
self.runtime
.api_hook_set_local(self.thread, level, index, &value)
}
pub fn upvalue(&self, level: usize, index: usize) -> Result<Option<(Vec<u8>, ValueRef<'_>)>> {
self.runtime.api_hook_upvalue(self.thread, level, index)
}
pub fn thread(&mut self) -> Result<Value> {
self.runtime
.api_owned(crate::value::Value::Thread(self.thread))
}
pub fn globals(&mut self) -> Table {
self.runtime.globals()
}
pub fn create_table(&mut self) -> Result<Table> {
self.runtime.create_table()
}
pub fn raw_get<K: IntoLua, V: super::FromLua>(&mut self, table: &Table, key: K) -> Result<V> {
table.raw_get(self.runtime, key)
}
pub fn raw_set<K: IntoLua, V: IntoLua>(
&mut self,
table: &Table,
key: K,
value: V,
) -> Result<()> {
table.raw_set(self.runtime, key, value)
}
pub fn raw_len(&self, table: &Table) -> Result<i64> {
table.raw_len(self.runtime)
}
pub fn create_string(&mut self, bytes: impl AsRef<[u8]>) -> Result<super::LuaString> {
self.runtime.create_string(bytes)
}
pub fn error(&mut self, value: impl IntoLua) -> Result<super::LuaError> {
let value = value.into_lua(self.runtime)?;
super::LuaError::new(value, crate::LuaFault::Error, self.runtime)
}
pub fn set_hook(&mut self, symbol: &str, mask: HookMask, count: i32) -> Result<()> {
self.runtime.api_set_hook(self.thread, symbol, mask, count)
}
pub fn clear_hook(&mut self) -> Result<()> {
self.runtime.api_clear_hook(self.thread)
}
}