use std::cell::RefCell;
use std::rc::Rc;
use crate::string::StringPool;
pub use lua_types::error::LuaError;
pub use lua_types::{CallInfoIdx, StackIdx};
pub struct StackIdxConv(pub StackIdx);
#[inline(always)]
pub fn stack_idx_to_i32(i: StackIdx) -> i32 { i.0 as i32 }
impl From<u32> for StackIdxConv {
#[inline(always)]
fn from(v: u32) -> Self { StackIdxConv(StackIdx(v)) }
}
impl From<i32> for StackIdxConv {
#[inline(always)]
fn from(v: i32) -> Self { StackIdxConv(StackIdx(v.max(0) as u32)) }
}
impl From<usize> for StackIdxConv {
#[inline(always)]
fn from(v: usize) -> Self { StackIdxConv(StackIdx(v as u32)) }
}
impl From<StackIdx> for StackIdxConv {
#[inline(always)]
fn from(v: StackIdx) -> Self { StackIdxConv(v) }
}
pub use lua_types::value::{LuaTable, LuaValue, F2Imod};
pub use lua_types::string::LuaString;
pub use lua_types::userdata::LuaUserData;
pub use lua_types::closure::{LuaCFnPtr, LuaClosure, LuaLClosure as LuaClosureLua, LuaCClosure as LuaClosureC};
pub use lua_types::proto::LuaProto;
pub use lua_types::upval::{UpVal, UpValState};
pub use lua_types::gc::GcRef;
pub type LuaCFunction = fn(&mut LuaState) -> Result<usize, LuaError>;
pub(crate) const EXTRA_STACK: usize = 5;
pub(crate) const LUA_MINSTACK: usize = 20;
pub(crate) const BASIC_STACK_SIZE: usize = 2 * LUA_MINSTACK;
pub(crate) const LUAI_MAXCCALLS: u32 = 200;
pub(crate) const CIST_C: u16 = 1 << 1;
pub(crate) const CIST_OAH: u16 = 1 << 0;
pub(crate) const CIST_FRESH: u16 = 1 << 2;
pub(crate) const CIST_HOOKED: u16 = 1 << 3;
pub(crate) const CIST_YPCALL: u16 = 1 << 4;
pub(crate) const CIST_TAIL: u16 = 1 << 5;
pub(crate) const CIST_HOOKYIELD: u16 = 1 << 6;
pub(crate) const CIST_FIN: u16 = 1 << 7;
pub(crate) const CIST_TRAN: u16 = 1 << 8;
pub(crate) const CIST_CLSRET: u16 = 1 << 9;
pub(crate) const CIST_RECST: u32 = 10;
pub(crate) const LUA_RIDX_MAINTHREAD: i64 = 1;
pub(crate) const LUA_RIDX_GLOBALS: i64 = 2;
pub(crate) const LUA_RIDX_LAST: usize = 2;
const LUA_NUMTYPES: usize = 9;
const LUA_EXTRASPACE: usize = std::mem::size_of::<*mut ()>();
const GCSTPUSR: u8 = 1;
const GCSTPGC: u8 = 2;
const GCS_PAUSE: u8 = 0;
const LUAI_GCPAUSE: u32 = 200;
const LUAI_GCMUL: u32 = 100;
const LUAI_GCSTEPSIZE: u8 = 13;
const LUAI_GENMAJORMUL: u32 = 100;
const LUAI_GENMINORMUL: u8 = 20;
const WHITE0BIT: u8 = 0;
const STRCACHE_N: usize = 53;
const STRCACHE_M: usize = 2;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GcKind {
Incremental = 0,
Generational = 1,
}
pub use lua_types::status::LuaStatus;
#[derive(Clone)]
pub struct StackValue {
pub val: LuaValue,
pub tbc_delta: u16,
}
impl Default for StackValue {
fn default() -> Self {
StackValue {
val: LuaValue::Nil,
tbc_delta: 0,
}
}
}
#[derive(Clone)]
pub struct CallInfo {
pub func: StackIdx,
pub top: StackIdx,
pub previous: Option<CallInfoIdx>,
pub next: Option<CallInfoIdx>,
pub u: CallInfoFrame,
pub u2: CallInfoExtra,
pub nresults: i16,
pub callstatus: u16,
}
#[derive(Clone, Copy)]
pub enum CallInfoFrame {
Lua {
savedpc: u32,
trap: bool,
nextraargs: i32,
},
C {
k: Option<LuaKFunction>,
old_errfunc: isize,
ctx: isize,
},
}
pub type LuaKFunction = fn(&mut LuaState, status: i32, ctx: isize) -> Result<usize, LuaError>;
#[derive(Default, Clone, Copy)]
pub struct CallInfoExtra {
pub value: i32,
pub ftransfer: u16,
pub ntransfer: u16,
}
impl CallInfoFrame {
pub fn c_default() -> Self {
CallInfoFrame::C {
k: None,
old_errfunc: 0,
ctx: 0,
}
}
pub fn lua_default() -> Self {
CallInfoFrame::Lua {
savedpc: 0,
trap: false,
nextraargs: 0,
}
}
}
impl Default for CallInfo {
fn default() -> Self {
CallInfo {
func: StackIdx(0),
top: StackIdx(0),
previous: None,
next: None,
u: CallInfoFrame::c_default(),
u2: CallInfoExtra::default(),
nresults: 0,
callstatus: 0,
}
}
}
impl CallInfo {
pub fn is_lua(&self) -> bool { (self.callstatus & CIST_C) == 0 }
pub fn is_lua_code(&self) -> bool { self.is_lua() }
pub fn is_vararg_func(&self) -> bool { false }
pub fn saved_pc(&self) -> u32 {
if let CallInfoFrame::Lua { savedpc, .. } = self.u { savedpc } else { 0 }
}
pub fn set_saved_pc(&mut self, pc: u32) {
if let CallInfoFrame::Lua { ref mut savedpc, .. } = self.u { *savedpc = pc; }
}
pub fn nextra_args(&self) -> i32 {
if let CallInfoFrame::Lua { nextraargs, .. } = self.u { nextraargs } else { 0 }
}
pub fn transfer_ftransfer(&self) -> u16 { self.u2.ftransfer }
pub fn transfer_ntransfer(&self) -> u16 { self.u2.ntransfer }
pub fn set_trap(&mut self, t: bool) {
if let CallInfoFrame::Lua { ref mut trap, .. } = self.u { *trap = t; }
}
pub fn recover_status(&self) -> i32 {
((self.callstatus >> CIST_RECST) & 7) as i32
}
pub fn set_recover_status<T: Into<i32>>(&mut self, status: T) {
let st = (status.into() & 7) as u16;
self.callstatus = (self.callstatus & !(7u16 << CIST_RECST)) | (st << CIST_RECST);
}
pub fn get_oah(&self) -> bool { (self.callstatus & CIST_OAH) != 0 }
pub fn set_oah(&mut self, allow: bool) {
self.callstatus = (self.callstatus & !CIST_OAH) | (if allow { CIST_OAH } else { 0 });
}
pub fn u_c_old_errfunc(&self) -> isize {
if let CallInfoFrame::C { old_errfunc, .. } = self.u { old_errfunc } else { 0 }
}
pub fn u_c_ctx(&self) -> isize {
if let CallInfoFrame::C { ctx, .. } = self.u { ctx } else { 0 }
}
pub fn u_c_k(&self) -> Option<LuaKFunction> {
if let CallInfoFrame::C { k, .. } = self.u { k } else { None }
}
pub fn set_u_c_k(&mut self, k: Option<LuaKFunction>) {
if let CallInfoFrame::C { k: ref mut slot, .. } = self.u {
*slot = k;
}
}
pub fn set_u_c_ctx(&mut self, ctx: isize) {
if let CallInfoFrame::C { ctx: ref mut slot, .. } = self.u {
*slot = ctx;
}
}
pub fn set_u_c_old_errfunc(&mut self, old_errfunc: isize) {
if let CallInfoFrame::C { old_errfunc: ref mut slot, .. } = self.u {
*slot = old_errfunc;
}
}
pub fn set_u2_funcidx(&mut self, idx: i32) {
self.u2.value = idx;
}
}
pub trait LuaValueExt {
fn base_type(&self) -> lua_types::LuaType;
fn to_number_no_strconv(&self) -> Option<f64>;
fn to_number_with_strconv(&self) -> Option<f64>;
fn to_integer_no_strconv(&self) -> Option<i64>;
fn to_integer_with_strconv(&self) -> Option<i64>;
fn full_type_tag(&self) -> u8;
}
impl LuaValueExt for LuaValue {
fn base_type(&self) -> lua_types::LuaType { self.type_tag() }
fn to_number_no_strconv(&self) -> Option<f64> {
match self {
LuaValue::Float(f) => Some(*f),
LuaValue::Int(i) => Some(*i as f64),
_ => None,
}
}
fn to_number_with_strconv(&self) -> Option<f64> {
if let Some(n) = self.to_number_no_strconv() { return Some(n); }
if let LuaValue::Str(s) = self {
let mut tmp = LuaValue::Nil;
let sz = crate::object::str2num(s.as_bytes(), &mut tmp);
if sz == 0 { return None; }
return match tmp {
LuaValue::Int(i) => Some(i as f64),
LuaValue::Float(f) => Some(f),
_ => None,
};
}
None
}
fn to_integer_no_strconv(&self) -> Option<i64> {
match self {
LuaValue::Int(i) => Some(*i),
LuaValue::Float(f) if f.fract() == 0.0 && f.is_finite() => {
let min_f = i64::MIN as f64;
let max_plus1_f = -(i64::MIN as f64);
if *f >= min_f && *f < max_plus1_f {
Some(*f as i64)
} else {
None
}
}
_ => None,
}
}
fn to_integer_with_strconv(&self) -> Option<i64> {
if let Some(i) = self.to_integer_no_strconv() { return Some(i); }
if let LuaValue::Str(s) = self {
let mut tmp = LuaValue::Nil;
let sz = crate::object::str2num(s.as_bytes(), &mut tmp);
if sz == 0 { return None; }
return tmp.to_integer_no_strconv();
}
None
}
fn full_type_tag(&self) -> u8 {
match self {
LuaValue::Nil => 0x00,
LuaValue::Bool(false) => 0x01,
LuaValue::Bool(true) => 0x11,
LuaValue::Int(_) => 0x03,
LuaValue::Float(_) => 0x13,
LuaValue::Str(s) if s.is_short() => 0x04,
LuaValue::Str(_) => 0x14,
LuaValue::LightUserData(_) => 0x02,
LuaValue::Table(_) => 0x05,
LuaValue::Function(LuaClosure::Lua(_)) => 0x06,
LuaValue::Function(LuaClosure::LightC(_)) => 0x16,
LuaValue::Function(LuaClosure::C(_)) => 0x26,
LuaValue::UserData(_) => 0x07,
LuaValue::Thread(_) => 0x08,
}
}
}
pub trait LuaTypeExt {
fn type_name(&self) -> &'static [u8];
}
impl LuaTypeExt for lua_types::LuaType {
fn type_name(&self) -> &'static [u8] {
use lua_types::LuaType::*;
match self {
None => b"no value",
Nil => b"nil",
Boolean => b"boolean",
LightUserData => b"userdata",
Number => b"number",
String => b"string",
Table => b"table",
Function => b"function",
UserData => b"userdata",
Thread => b"thread",
}
}
}
pub trait StackIdxExt {
fn saturating_sub(self, n: impl Into<StackIdxConv>) -> u32;
fn wrapping_sub(self, n: impl Into<StackIdxConv>) -> u32;
fn raw(self) -> u32;
}
impl StackIdxExt for StackIdx {
#[inline(always)]
fn saturating_sub(self, n: impl Into<StackIdxConv>) -> u32 { self.0.saturating_sub(n.into().0.0) }
#[inline(always)]
fn wrapping_sub(self, n: impl Into<StackIdxConv>) -> u32 { self.0.wrapping_sub(n.into().0.0) }
#[inline(always)]
fn raw(self) -> u32 { self.0 }
}
pub trait LuaTableRefExt {
fn metatable(&self) -> Option<GcRef<LuaTable>>;
fn as_ptr(&self) -> *const ();
fn get(&self, _k: &LuaValue) -> LuaValue;
fn get_int(&self, _k: i64) -> LuaValue;
fn get_short_str(&self, _k: &GcRef<LuaString>) -> LuaValue;
fn raw_set(&self, _state: &mut LuaState, _k: LuaValue, _v: LuaValue) -> Result<(), LuaError>;
fn raw_set_int(&self, _state: &mut LuaState, _k: i64, _v: LuaValue) -> Result<(), LuaError>;
fn invalidate_tm_cache(&self);
fn resize(&self, _state: &mut LuaState, _na: usize, _nh: usize) -> Result<(), LuaError>;
fn next(&self, _k: LuaValue) -> Result<Option<(LuaValue, LuaValue)>, LuaError>;
}
impl LuaTableRefExt for GcRef<LuaTable> {
#[inline]
fn metatable(&self) -> Option<GcRef<LuaTable>> { (**self).metatable() }
#[inline]
fn as_ptr(&self) -> *const () { GcRef::identity(self) as *const () }
#[inline]
fn get(&self, k: &LuaValue) -> LuaValue { (**self).get(k) }
#[inline]
fn get_int(&self, k: i64) -> LuaValue { (**self).get_int(k) }
#[inline]
fn get_short_str(&self, k: &GcRef<LuaString>) -> LuaValue { (**self).get_short_str(k) }
#[inline]
fn raw_set(&self, _state: &mut LuaState, k: LuaValue, v: LuaValue) -> Result<(), LuaError> {
(**self).try_raw_set(k, v)
}
#[inline]
fn raw_set_int(&self, _state: &mut LuaState, k: i64, v: LuaValue) -> Result<(), LuaError> {
(**self).try_raw_set_int(k, v)
}
fn invalidate_tm_cache(&self) {}
fn resize(&self, _state: &mut LuaState, na: usize, nh: usize) -> Result<(), LuaError> {
let na32 = na.min(u32::MAX as usize) as u32;
let nh32 = nh.min(u32::MAX as usize) as u32;
(**self).resize(na32, nh32)
}
fn next(&self, k: LuaValue) -> Result<Option<(LuaValue, LuaValue)>, LuaError> {
(**self).try_next_pair(&k)
}
}
pub trait LuaUserDataRefExt {
fn metatable(&self) -> Option<GcRef<LuaTable>>;
fn set_metatable(&self, mt: Option<GcRef<LuaTable>>);
fn as_ptr(&self) -> *const ();
fn len(&self) -> usize;
}
impl LuaUserDataRefExt for GcRef<LuaUserData> {
fn metatable(&self) -> Option<GcRef<LuaTable>> { (**self).metatable() }
fn set_metatable(&self, mt: Option<GcRef<LuaTable>>) { (**self).set_metatable(mt); }
fn as_ptr(&self) -> *const () { GcRef::identity(self) as *const () }
fn len(&self) -> usize { self.0.data.len() }
}
pub trait LuaStringRefExt {
fn is_white(&self) -> bool;
fn hash(&self) -> u32;
fn as_gc_ref(&self) -> GcRef<LuaString>;
}
impl LuaStringRefExt for GcRef<LuaString> {
fn is_white(&self) -> bool { false }
fn hash(&self) -> u32 { self.0.hash() }
fn as_gc_ref(&self) -> GcRef<LuaString> { self.clone() }
}
pub trait LuaLClosureRefExt {
fn proto(&self) -> &GcRef<LuaProto>;
fn nupvalues(&self) -> usize;
}
impl LuaLClosureRefExt for GcRef<lua_types::closure::LuaLClosure> {
fn proto(&self) -> &GcRef<LuaProto> { &self.0.proto }
fn nupvalues(&self) -> usize { self.0.upvals.len() }
}
pub trait LuaClosureExt {
fn nupvalues(&self) -> usize;
}
impl LuaClosureExt for LuaClosure {
fn nupvalues(&self) -> usize {
match self {
LuaClosure::Lua(l) => l.0.upvals.len(),
LuaClosure::C(c) => c.0.upvalues.len(),
LuaClosure::LightC(_) => 0,
}
}
}
pub trait LuaProtoExt {
fn source_bytes(&self) -> &[u8];
fn source_string(&self) -> Option<&GcRef<LuaString>>;
}
impl LuaProtoExt for LuaProto {
fn source_bytes(&self) -> &[u8] {
match &self.source { Some(s) => s.0.as_bytes(), None => &[] }
}
fn source_string(&self) -> Option<&GcRef<LuaString>> { self.source.as_ref() }
}
pub trait Collectable: std::fmt::Debug {}
impl std::fmt::Debug for LuaState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "LuaState")
}
}
impl Collectable for LuaState {}
pub type ParserHook = fn(
state: &mut LuaState,
source: &[u8],
name: &[u8],
firstchar: i32,
) -> Result<GcRef<lua_types::closure::LuaLClosure>, LuaError>;
pub type FileLoaderHook = fn(filename: &[u8]) -> Result<Vec<u8>, LuaError>;
pub type FileOpenHook =
fn(filename: &[u8], mode: &[u8]) -> Result<Box<dyn lua_types::LuaFileHandle>, LuaError>;
pub type PopenHook =
fn(cmd: &[u8], mode: &[u8]) -> Result<Box<dyn lua_types::LuaFileHandle>, LuaError>;
pub type FileRemoveHook = fn(filename: &[u8]) -> Result<(), LuaError>;
pub type FileRenameHook = fn(from: &[u8], to: &[u8]) -> Result<(), LuaError>;
#[derive(Clone, Copy, Debug)]
pub enum OsExecuteReason {
Exit,
Signal,
}
#[derive(Debug)]
pub struct OsExecuteResult {
pub success: bool,
pub reason: OsExecuteReason,
pub code: i32,
}
pub type OsExecuteHook = fn(cmd: &[u8]) -> Result<OsExecuteResult, LuaError>;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DynLibId(pub u64);
pub enum DynamicSymbol {
RustNative(LuaCFunction),
LuaCAbi(*const ()),
Unsupported { reason: Vec<u8> },
}
pub type DynLibLoadHook =
fn(state: &mut LuaState, path: &[u8], see_global: bool) -> Result<DynLibId, LuaError>;
pub type DynLibSymbolHook =
fn(state: &mut LuaState, handle: DynLibId, symbol: &[u8]) -> Result<DynamicSymbol, LuaError>;
pub type DynLibUnloadHook = fn(handle: DynLibId);
pub struct ThreadRegistryEntry {
pub state: Rc<RefCell<LuaState>>,
pub value: GcRef<lua_types::value::LuaThread>,
}
pub struct GlobalState {
pub parser_hook: Option<ParserHook>,
pub file_loader_hook: Option<FileLoaderHook>,
pub file_open_hook: Option<FileOpenHook>,
pub popen_hook: Option<PopenHook>,
pub file_remove_hook: Option<FileRemoveHook>,
pub file_rename_hook: Option<FileRenameHook>,
pub os_execute_hook: Option<OsExecuteHook>,
pub dynlib_load_hook: Option<DynLibLoadHook>,
pub dynlib_symbol_hook: Option<DynLibSymbolHook>,
pub dynlib_unload_hook: Option<DynLibUnloadHook>,
pub totalbytes: isize,
pub gc_debt: isize,
pub gc_estimate: usize,
pub lastatomic: usize,
pub strt: StringPool,
pub l_registry: LuaValue,
pub globals: LuaValue,
pub loaded: LuaValue,
pub nilvalue: LuaValue,
pub seed: u32,
pub currentwhite: u8,
pub gcstate: u8,
pub gckind: u8,
pub gcstopem: bool,
pub genminormul: u8,
pub genmajormul: u8,
pub gcstp: u8,
pub gcemergency: bool,
pub gcpause: u8,
pub gcstepmul: u8,
pub gcstepsize: u8,
pub sweepgc_cursor: usize,
pub weak_tables_registry: Vec<lua_types::gc::GcWeak<lua_types::value::LuaTable>>,
pub gc_tracked_long_strings: Vec<(lua_types::gc::GcWeak<lua_types::string::LuaString>, usize)>,
pub pending_finalizers: Vec<GcRef<lua_types::value::LuaTable>>,
pub to_be_finalized: Vec<GcRef<lua_types::value::LuaTable>>,
pub twups: Vec<GcRef<LuaState>>,
pub panic: Option<LuaCFunction>,
pub mainthread: Option<GcRef<LuaState>>,
pub threads: std::collections::HashMap<u64, ThreadRegistryEntry>,
pub main_thread_value: GcRef<lua_types::value::LuaThread>,
pub current_thread_id: u64,
pub main_thread_id: u64,
pub next_thread_id: u64,
pub memerrmsg: GcRef<LuaString>,
pub tmname: Vec<GcRef<LuaString>>,
pub mt: [Option<GcRef<LuaTable>>; LUA_NUMTYPES],
pub strcache: [[GcRef<LuaString>; STRCACHE_M]; STRCACHE_N],
pub interned_lt: std::collections::HashMap<Box<[u8]>, GcRef<LuaString>>,
pub warnf: Option<Box<dyn FnMut(&[u8], bool)>>,
pub c_functions: Vec<LuaCFunction>,
pub heap: lua_gc::Heap,
pub cross_thread_upvals: std::collections::HashMap<(u64, StackIdx), LuaValue>,
pub suspended_parent_stacks: Vec<Vec<LuaValue>>,
pub suspended_parent_open_upvals: Vec<Vec<GcRef<UpVal>>>,
}
impl GlobalState {
pub fn total_bytes(&self) -> usize {
(self.totalbytes + self.gc_debt) as usize
}
pub fn get_thread(&self, id: u64) -> Option<&ThreadRegistryEntry> {
self.threads.get(&id)
}
pub fn thread_value_for(&self, id: u64) -> Option<GcRef<lua_types::value::LuaThread>> {
if id == self.main_thread_id {
Some(self.main_thread_value.clone())
} else {
self.threads.get(&id).map(|e| e.value.clone())
}
}
pub fn is_complete(&self) -> bool {
matches!(self.nilvalue, LuaValue::Nil)
}
pub fn current_white(&self) -> u8 {
self.currentwhite
}
pub fn other_white(&self) -> u8 {
self.currentwhite ^ 0x03
}
pub fn is_gen_mode(&self) -> bool {
self.gckind == GcKind::Generational as u8
}
pub fn gc_running(&self) -> bool {
self.gcstp == 0
}
pub fn keep_invariant(&self) -> bool {
false
}
pub fn is_sweep_phase(&self) -> bool {
false
}
pub fn gc_debt(&self) -> isize { self.gc_debt }
pub fn set_gc_debt(&mut self, d: isize) { self.gc_debt = d; }
pub fn gc_at_pause(&self) -> bool { self.gcstate == 0 }
pub fn gc_pause_param(&self) -> u8 { self.gcpause }
pub fn set_gc_pause_param(&mut self, p: u8) { self.gcpause = p; }
pub fn gc_stepmul_param(&self) -> u8 { self.gcstepmul }
pub fn set_gc_stepmul_param(&mut self, p: u8) { self.gcstepmul = p; }
pub fn set_gc_genmajormul(&mut self, p: u8) { self.genmajormul = p; }
pub fn gc_stop_flags(&self) -> u8 { self.gcstp }
pub fn set_gc_stop_flags(&mut self, f: u8) { self.gcstp = f; }
pub fn stop_gc_internal(&mut self) -> u8 {
let old = self.gcstp;
self.gcstp |= GCSTPGC;
old
}
pub fn set_gc_stop_user(&mut self) {
self.gcstp = GCSTPUSR;
}
pub fn clear_gc_stop(&mut self) { self.gcstp = 0; }
pub fn is_gc_running(&self) -> bool { self.gcstp == 0 }
pub fn is_gc_stopped_internally(&self) -> bool { (self.gcstp & GCSTPGC) != 0 }
pub fn tm_name<T: TmIndex>(&self, tm: T) -> Option<GcRef<LuaString>> {
self.tmname.get(tm.tm_index()).cloned()
}
}
pub trait TmIndex: Copy {
fn tm_index(self) -> usize;
}
impl TmIndex for lua_types::tagmethod::TagMethod {
fn tm_index(self) -> usize { self as u8 as usize }
}
impl TmIndex for crate::tagmethods::TagMethod {
fn tm_index(self) -> usize { self as u8 as usize }
}
impl TmIndex for usize {
fn tm_index(self) -> usize { self }
}
impl TmIndex for u8 {
fn tm_index(self) -> usize { self as usize }
}
use lua_types::tagmethod::TagMethod;
pub struct LuaState {
pub status: u8,
pub allowhook: bool,
pub nci: u32,
pub top: StackIdx,
pub stack_last: StackIdx,
pub stack: Vec<StackValue>,
pub ci: CallInfoIdx,
pub call_info: Vec<CallInfo>,
pub openupval: Vec<GcRef<UpVal>>,
pub tbclist: Vec<StackIdx>,
pub(crate) global: Rc<RefCell<GlobalState>>,
pub hook: Option<Box<dyn FnMut(&mut LuaState, &crate::debug::LuaDebug)>>,
pub hookmask: u8,
pub basehookcount: i32,
pub hookcount: i32,
pub errfunc: isize,
pub nCcalls: u32,
pub oldpc: u32,
pub marked: u8,
pub cached_thread_id: u64,
}
impl LuaState {
pub fn global(&self) -> std::cell::Ref<'_, GlobalState> {
self.global.borrow()
}
pub fn global_mut(&self) -> std::cell::RefMut<'_, GlobalState> {
self.global.borrow_mut()
}
pub fn global_rc(&self) -> Rc<RefCell<GlobalState>> {
Rc::clone(&self.global)
}
pub fn c_calls(&self) -> u32 {
self.nCcalls & 0xffff
}
pub fn inc_nny(&mut self) {
self.nCcalls += 0x10000;
}
pub fn dec_nny(&mut self) {
self.nCcalls -= 0x10000;
}
pub fn is_yieldable(&self) -> bool {
(self.nCcalls & 0xffff0000) == 0
}
pub fn reset_hook_count(&mut self) {
self.hookcount = self.basehookcount;
}
pub fn stack_size(&self) -> usize {
self.stack_last.0 as usize
}
#[inline(always)]
pub fn push(&mut self, val: LuaValue) {
let top = self.top.0 as usize;
if top < self.stack.len() {
self.stack[top] = StackValue { val, tbc_delta: 0 };
} else {
self.stack.push(StackValue { val, tbc_delta: 0 });
}
self.top = StackIdx(self.top.0 + 1);
}
#[inline(always)]
pub fn pop(&mut self) -> LuaValue {
if self.top.0 == 0 {
return LuaValue::Nil;
}
self.top = StackIdx(self.top.0 - 1);
self.stack[self.top.0 as usize].val.clone()
}
#[inline(always)]
pub fn stack_val(&self, idx: StackIdx) -> &LuaValue {
&self.stack[idx.0 as usize].val
}
#[inline(always)]
pub fn set_stack_val(&mut self, idx: StackIdx, val: LuaValue) {
self.stack[idx.0 as usize].val = val;
}
pub fn gc(&mut self) -> GcHandle<'_> {
GcHandle { _state: self }
}
pub fn new_table(&mut self) -> GcRef<LuaTable> {
GcRef::new(LuaTable::placeholder())
}
pub fn intern_str(&mut self, bytes: &[u8]) -> Result<GcRef<LuaString>, LuaError> {
if bytes.len() <= crate::string::MAX_SHORT_LEN {
if let Some(existing) = self.global().interned_lt.get(bytes) {
return Ok(existing.clone());
}
let _local = crate::string::new(self, bytes)?;
let new_ref = GcRef::new(LuaString::from_bytes(bytes.to_vec()));
self.global_mut()
.interned_lt
.insert(bytes.to_vec().into_boxed_slice(), new_ref.clone());
Ok(new_ref)
} else {
let new_ref = GcRef::new(LuaString::from_bytes(bytes.to_vec()));
let size = bytes.len()
+ std::mem::size_of::<LuaString>()
+ std::mem::size_of::<usize>();
let mut g = self.global_mut();
g.gc_debt += size as isize;
g.gc_tracked_long_strings
.push((new_ref.downgrade(), size));
Ok(new_ref)
}
}
#[inline(always)]
pub fn top_idx(&self) -> StackIdx {
self.top
}
}
impl LuaState {
#[inline(always)]
pub fn get_at(&self, idx: impl Into<StackIdxConv>) -> LuaValue {
let i: StackIdx = idx.into().0;
match self.stack.get(i.0 as usize) {
Some(slot) => slot.val.clone(),
None => LuaValue::Nil,
}
}
#[inline(always)]
pub fn set_at(&mut self, idx: impl Into<StackIdxConv>, v: LuaValue) {
let i: StackIdx = idx.into().0;
self.stack[i.0 as usize].val = v;
}
pub fn clear_stack_range(&mut self, start: StackIdx, end: StackIdx) {
if end.0 <= start.0 {
return;
}
let end_u = end.0 as usize;
if end_u > self.stack.len() {
self.stack.resize_with(end_u, StackValue::default);
}
for i in start.0..end.0 {
self.stack[i as usize].val = LuaValue::Nil;
self.stack[i as usize].tbc_delta = 0;
}
}
#[inline]
pub fn get_int_at(&self, idx: impl Into<StackIdxConv>) -> Option<i64> {
let i: StackIdx = idx.into().0;
match self.stack.get(i.0 as usize) {
Some(slot) => match &slot.val {
LuaValue::Int(v) => Some(*v),
_ => None,
},
None => None,
}
}
#[inline]
pub fn get_int_pair_at(
&self,
rb: impl Into<StackIdxConv>,
rc: impl Into<StackIdxConv>,
) -> Option<(i64, i64)> {
let ib = self.get_int_at(rb)?;
let ic = self.get_int_at(rc)?;
Some((ib, ic))
}
#[inline]
pub fn get_num_at(&self, idx: impl Into<StackIdxConv>) -> Option<f64> {
let i: StackIdx = idx.into().0;
match self.stack.get(i.0 as usize) {
Some(slot) => match &slot.val {
LuaValue::Float(f) => Some(*f),
LuaValue::Int(v) => Some(*v as f64),
_ => None,
},
None => None,
}
}
#[inline]
pub fn get_float_at(&self, idx: impl Into<StackIdxConv>) -> Option<f64> {
let i: StackIdx = idx.into().0;
match self.stack.get(i.0 as usize) {
Some(slot) => match &slot.val {
LuaValue::Float(f) => Some(*f),
_ => None,
},
None => None,
}
}
#[inline]
pub fn get_num_pair_at(
&self,
rb: impl Into<StackIdxConv>,
rc: impl Into<StackIdxConv>,
) -> Option<(f64, f64)> {
let nb = self.get_num_at(rb)?;
let nc = self.get_num_at(rc)?;
Some((nb, nc))
}
#[inline(always)]
pub fn set_top(&mut self, idx: impl Into<StackIdxConv>) {
let new_top: StackIdx = idx.into().0;
let new_top_u = new_top.0 as usize;
if new_top_u > self.stack.len() {
self.stack.resize_with(new_top_u, StackValue::default);
}
self.top = new_top;
}
#[inline(always)]
pub fn set_top_idx(&mut self, idx: impl Into<StackIdxConv>) {
let new_top: StackIdx = idx.into().0;
self.top = new_top;
}
#[inline(always)]
pub fn dec_top(&mut self) {
if self.top.0 > 0 {
self.top = StackIdx(self.top.0 - 1);
}
}
#[inline(always)]
pub fn pop_n(&mut self, n: usize) {
let cur = self.top.0 as usize;
let new = cur.saturating_sub(n);
self.top = StackIdx(new as u32);
}
#[inline(always)]
pub fn peek_at(&mut self, idx: impl Into<StackIdxConv>) -> LuaValue {
let i: StackIdx = idx.into().0;
match self.stack.get(i.0 as usize) {
Some(slot) => slot.val.clone(),
None => LuaValue::Nil,
}
}
#[inline(always)]
pub fn peek_top(&mut self) -> LuaValue {
if self.top.0 == 0 {
return LuaValue::Nil;
}
self.stack[(self.top.0 - 1) as usize].val.clone()
}
pub fn peek_string_at_top(&mut self) -> GcRef<LuaString> {
match self.peek_top() {
LuaValue::Str(s) => s,
_ => panic!("peek_string_at_top: top of stack is not a string"),
}
}
pub fn stack_at(&mut self, idx: impl Into<StackIdxConv>) -> &mut LuaValue {
let i: StackIdx = idx.into().0;
&mut self.stack[i.0 as usize].val
}
pub fn stack_set_nil(&mut self, idx: impl Into<StackIdxConv>) {
let i: StackIdx = idx.into().0;
let slot = i.0 as usize;
if slot < self.stack.len() {
self.stack[slot].val = LuaValue::Nil;
}
}
pub fn stack_resize(&mut self, size: usize) -> Result<(), LuaError> {
self.stack.resize_with(size, StackValue::default);
Ok(())
}
pub fn stack_available(&mut self) -> usize {
(self.stack_last.0 as usize).saturating_sub(self.top.0 as usize)
}
pub fn check_stack(&mut self, n: i32) -> Result<(), LuaError> {
let free = (self.stack_last.0 as i32) - (self.top.0 as i32);
if free <= n {
self.grow_stack(n, true)?;
}
Ok(())
}
pub fn grow_stack(&mut self, n: i32, raise_error: bool) -> Result<(), LuaError> {
crate::do_::grow_stack(self, n, raise_error).map(|_| ())
}
#[inline(always)]
pub fn get_ci(&self, idx: CallInfoIdx) -> &CallInfo { &self.call_info[idx.as_usize()] }
#[inline(always)]
pub fn get_ci_mut(&mut self, idx: CallInfoIdx) -> &mut CallInfo { &mut self.call_info[idx.as_usize()] }
#[inline(always)]
pub fn current_call_info(&self) -> &CallInfo { &self.call_info[self.ci.as_usize()] }
#[inline(always)]
pub fn current_call_info_mut(&mut self) -> &mut CallInfo { let i = self.ci.as_usize(); &mut self.call_info[i] }
#[inline(always)]
pub fn current_ci_idx(&self) -> CallInfoIdx { self.ci }
pub fn call_stack_mut(&mut self) -> &mut Vec<CallInfo> { &mut self.call_info }
#[inline(always)]
pub fn next_ci(&mut self) -> Result<CallInfoIdx, LuaError> {
match self.call_info[self.ci.as_usize()].next {
Some(idx) => Ok(idx),
None => Ok(extend_ci(self)),
}
}
#[inline(always)]
pub fn prev_ci(&self, idx: CallInfoIdx) -> Option<CallInfoIdx> { self.call_info[idx.as_usize()].previous }
pub fn get_prev_ci(&self, idx: CallInfoIdx) -> Option<&CallInfo> {
self.call_info[idx.as_usize()]
.previous
.map(|p| &self.call_info[p.as_usize()])
}
#[inline(always)]
pub fn is_base_ci(&self, idx: CallInfoIdx) -> bool { idx.as_usize() == 0 }
#[inline(always)]
pub fn is_current_ci(&self, idx: CallInfoIdx) -> bool { idx == self.ci }
pub fn ci_next_func(&self, idx: CallInfoIdx) -> StackIdx {
let next = self.call_info[idx.as_usize()]
.next
.expect("ci_next_func: no next CallInfo");
self.call_info[next.as_usize()].func
}
#[inline(always)]
pub fn ci_top(&self, idx: CallInfoIdx) -> StackIdx { self.call_info[idx.as_usize()].top }
#[inline(always)]
pub fn ci_trap(&mut self, idx: CallInfoIdx) -> bool {
if let CallInfoFrame::Lua { trap, .. } = self.call_info[idx.as_usize()].u {
trap
} else {
false
}
}
#[inline(always)]
pub fn ci_savedpc(&self, idx: CallInfoIdx) -> u32 { self.call_info[idx.as_usize()].saved_pc() }
#[inline(always)]
pub fn set_ci_savedpc(&mut self, idx: CallInfoIdx, pc: u32) {
self.call_info[idx.as_usize()].set_saved_pc(pc);
}
#[inline(always)]
pub fn set_ci_previous(&mut self, idx: CallInfoIdx) {
self.ci = self.call_info[idx.as_usize()]
.previous
.expect("set_ci_previous: returning frame has no previous CallInfo");
}
#[inline(always)]
pub fn ci_previous(&self, idx: CallInfoIdx) -> Option<CallInfoIdx> { self.call_info[idx.as_usize()].previous }
#[inline(always)]
pub fn ci_adjust_func(&mut self, idx: CallInfoIdx, delta: i32) {
let ci = &mut self.call_info[idx.as_usize()];
ci.func = StackIdx((ci.func.0 as i32 - delta) as u32);
}
#[inline(always)]
pub fn ci_base(&self, idx: CallInfoIdx) -> StackIdx { self.call_info[idx.as_usize()].func + 1 }
#[inline(always)]
pub fn ci_is_fresh(&self, idx: CallInfoIdx) -> bool {
(self.call_info[idx.as_usize()].callstatus & CIST_FRESH) != 0
}
#[inline(always)]
pub fn ci_lua_closure(&self, idx: CallInfoIdx) -> Option<GcRef<lua_types::closure::LuaLClosure>> {
let func_idx = self.call_info[idx.as_usize()].func;
match self.get_at(func_idx) {
LuaValue::Function(lua_types::closure::LuaClosure::Lua(cl)) => Some(cl),
_ => None,
}
}
#[inline(always)]
pub fn ci_nextraargs(&self, idx: CallInfoIdx) -> i32 {
self.call_info[idx.as_usize()].nextra_args()
}
#[inline(always)]
pub fn ci_nres(&self, idx: CallInfoIdx) -> i32 {
self.call_info[idx.as_usize()].u2.value
}
#[inline(always)]
pub fn ci_nres_set(&mut self, idx: CallInfoIdx, n: i32) {
self.call_info[idx.as_usize()].u2.value = n;
}
#[inline(always)]
pub fn ci_nresults(&self, idx: CallInfoIdx) -> i32 { self.call_info[idx.as_usize()].nresults as i32 }
pub fn ci_prev_instruction(&self, idx: CallInfoIdx) -> lua_types::opcode::Instruction {
let pc = self.call_info[idx.as_usize()].saved_pc();
let cl = self.ci_lua_closure(idx)
.expect("ci_prev_instruction: CallInfo does not hold a Lua closure");
cl.proto.code[(pc - 1) as usize]
}
pub fn ci_prev2_instruction(&self, idx: CallInfoIdx) -> lua_types::opcode::Instruction {
let pc = self.call_info[idx.as_usize()].saved_pc();
let cl = self.ci_lua_closure(idx)
.expect("ci_prev2_instruction: CallInfo does not hold a Lua closure");
cl.proto.code[(pc - 2) as usize]
}
pub fn ci_skip_next_instruction(&mut self, idx: CallInfoIdx) {
let pc = self.call_info[idx.as_usize()].saved_pc();
self.call_info[idx.as_usize()].set_saved_pc(pc + 1);
}
pub fn ci_step_pc_back(&mut self, idx: CallInfoIdx) {
let pc = self.call_info[idx.as_usize()].saved_pc();
self.call_info[idx.as_usize()].set_saved_pc(pc - 1);
}
pub fn get_ci_pcrel(&mut self, idx: CallInfoIdx) -> u32 {
self.call_info[idx.as_usize()].saved_pc().saturating_sub(1)
}
pub fn get_ci_u2_funcidx(&mut self, idx: CallInfoIdx) -> i32 {
self.call_info[idx.as_usize()].u2.value
}
pub fn get_ci_u2_nres(&mut self, idx: CallInfoIdx) -> i32 {
self.call_info[idx.as_usize()].u2.value
}
pub fn get_ci_u2_nyield(&mut self, idx: CallInfoIdx) -> i32 {
self.call_info[idx.as_usize()].u2.value
}
pub fn get_ci_vararg_info(&mut self, idx: CallInfoIdx) -> (bool, i32, i32) {
let nextraargs = self.call_info[idx.as_usize()].nextra_args();
match self.ci_lua_closure(idx) {
Some(cl) => (cl.proto.is_vararg, nextraargs, cl.proto.numparams as i32),
None => (false, nextraargs, 0),
}
}
pub fn get_ci_lua_proto_numparams(&mut self, idx: CallInfoIdx) -> u8 {
self.ci_lua_closure(idx)
.map(|cl| cl.proto.numparams)
.unwrap_or(0)
}
pub fn set_ci_u2_nres(&mut self, idx: CallInfoIdx, n: i32) {
self.call_info[idx.as_usize()].u2.value = n;
}
pub fn set_ci_u2_nyield(&mut self, idx: CallInfoIdx, n: i32) {
self.call_info[idx.as_usize()].u2.value = n;
}
pub fn set_ci_transfer_info(&mut self, idx: CallInfoIdx, ftransfer: u16, ntransfer: u16) {
let ci = &mut self.call_info[idx.as_usize()];
ci.u2.ftransfer = ftransfer;
ci.u2.ntransfer = ntransfer;
}
pub fn shrink_ci(&mut self) { shrink_ci(self) }
pub fn check_c_stack(&mut self) -> Result<(), LuaError> { check_c_stack(self) }
pub fn status(&mut self) -> LuaStatus { LuaStatus::from_raw(self.status as i32) }
pub fn errfunc(&mut self) -> isize { self.errfunc }
pub fn old_pc(&mut self) -> u32 { self.oldpc }
pub fn set_old_pc(&mut self, pc: u32) { self.oldpc = pc; }
pub fn set_oldpc(&mut self, pc: u32) { self.oldpc = pc; }
pub fn _hook_call_noargs(&mut self) {}
pub fn hook(&self) -> Option<&Box<dyn FnMut(&mut LuaState, &crate::debug::LuaDebug)>> {
self.hook.as_ref()
}
pub fn has_hook(&mut self) -> bool { self.hook.is_some() }
pub fn hook_count(&mut self) -> i32 { self.hookcount }
pub fn set_hook_count(&mut self, n: i32) { self.hookcount = n; }
pub fn hook_mask(&self) -> u8 { self.hookmask }
pub fn set_hook_mask(&mut self, m: u8) { self.hookmask = m; }
pub fn base_hook_count(&self) -> i32 { self.basehookcount }
pub fn set_base_hook_count(&mut self, n: i32) { self.basehookcount = n; }
pub fn set_hook(&mut self, h: Option<Box<dyn FnMut(&mut LuaState, &crate::debug::LuaDebug)>>) {
self.hook = h;
}
pub fn call_hook_event(&mut self, event: i32, line: i32) -> Result<(), LuaError> {
crate::do_::hook(self, event, line, 0, 0)
}
pub fn registry_value(&self) -> LuaValue { self.global().l_registry.clone() }
pub fn registry_get(&self, key: usize) -> LuaValue {
let reg = self.global().l_registry.clone();
match reg {
LuaValue::Table(t) => t.get(&LuaValue::Int(key as i64)),
_ => LuaValue::Nil,
}
}
pub fn new_string(&mut self, bytes: &[u8]) -> Result<GcRef<LuaString>, LuaError> { self.intern_or_create_str(bytes) }
pub fn new_proto(&mut self) -> GcRef<LuaProto> {
GcRef::new(LuaProto::placeholder())
}
pub fn new_lclosure(&mut self, proto: GcRef<LuaProto>, nupvals: usize) -> GcRef<LuaClosureLua> {
let mut upvals = Vec::with_capacity(nupvals);
for _ in 0..nupvals {
upvals.push(std::cell::Cell::new(self.new_upval_closed(LuaValue::Nil)));
}
GcRef::new(LuaClosureLua { proto, upvals })
}
pub fn new_upval_closed(&mut self, v: LuaValue) -> GcRef<UpVal> {
GcRef::new(UpVal::closed(v))
}
pub fn new_upval_open(&mut self, thread_id: usize, level: StackIdx) -> GcRef<UpVal> {
GcRef::new(UpVal::open(thread_id, level))
}
pub fn intern_or_create_str(&mut self, bytes: &[u8]) -> Result<GcRef<LuaString>, LuaError> {
self.intern_str(bytes)
}
pub fn new_userdata(&mut self, _size: usize, _nuvalue: usize) -> Result<GcRef<LuaUserData>, LuaError> {
Err(LuaError::runtime(format_args!("new_userdata not implemented in this Phase-B build; use new_userdata_typed instead")))
}
pub fn new_c_closure(&mut self, _f: LuaCFunction, _n: i32) -> Result<LuaClosure, LuaError> {
Err(LuaError::runtime(format_args!("new_c_closure not implemented in this Phase-B build; use push_cclosure in lua_vm::api instead")))
}
pub fn push_closure(
&mut self,
proto_idx: usize,
ci: CallInfoIdx,
base: StackIdx,
ra: StackIdx,
) -> Result<(), LuaError> {
let parent_cl = self.ci_lua_closure(ci).expect(
"push_closure: current frame is not a Lua closure",
);
let child_proto = parent_cl.proto.p[proto_idx].clone();
let nup = child_proto.upvalues.len();
let mut upvals: Vec<std::cell::Cell<GcRef<UpVal>>> = Vec::with_capacity(nup);
for i in 0..nup {
let desc = &child_proto.upvalues[i];
let uv = if desc.instack {
let level = base + desc.idx as i32;
crate::func::find_upval(self, level)
} else {
parent_cl.upval(desc.idx as usize)
};
upvals.push(std::cell::Cell::new(uv));
}
let new_cl = GcRef::new(LuaClosureLua {
proto: child_proto,
upvals,
});
self.set_at(ra, LuaValue::Function(LuaClosure::Lua(new_cl)));
Ok(())
}
pub fn new_tbc_upval(&mut self, idx: StackIdx) -> Result<(), LuaError> {
crate::func::new_tbc_upval(self, idx)
}
#[inline(always)]
pub fn upvalue_get(&self, cl: &GcRef<LuaClosureLua>, n: usize) -> LuaValue {
let uv = cl.upval(n);
let (thread_id, idx) = match uv.try_open_payload() {
Some(p) => p,
None => return *uv.closed_value(),
};
let current = self.cached_thread_id;
let tid = thread_id as u64;
if tid == current {
return self.stack[idx.0 as usize].val;
}
self.upvalue_get_cross_thread(tid, idx)
}
#[cold]
#[inline(never)]
fn upvalue_get_cross_thread(&self, tid: u64, idx: StackIdx) -> LuaValue {
let entry_rc = {
let g = self.global();
g.threads.get(&tid).map(|e| e.state.clone())
};
if let Some(rc) = entry_rc {
if let Ok(home_state) = rc.try_borrow() {
return home_state.get_at(idx);
}
}
let g = self.global();
match g.cross_thread_upvals.get(&(tid, idx)) {
Some(v) => *v,
None => LuaValue::Nil,
}
}
#[inline(always)]
pub fn upvalue_set(&mut self, cl: &GcRef<LuaClosureLua>, n: usize, val: LuaValue) -> Result<(), LuaError> {
let uv = cl.upval(n);
match uv.try_open_payload() {
Some((thread_id, idx)) => {
let tid = thread_id as u64;
let current = self.cached_thread_id;
if tid == current {
self.stack[idx.0 as usize].val = val;
return Ok(());
}
return self.upvalue_set_cross_thread(tid, idx, val);
}
None => {
uv.set_closed_value(val);
}
}
Ok(())
}
#[cold]
#[inline(never)]
fn upvalue_set_cross_thread(
&mut self,
tid: u64,
idx: StackIdx,
val: LuaValue,
) -> Result<(), LuaError> {
let entry_rc = {
let g = self.global();
g.threads.get(&tid).map(|e| e.state.clone())
};
if let Some(rc) = entry_rc {
if let Ok(mut home_state) = rc.try_borrow_mut() {
home_state.set_at(idx, val);
return Ok(());
}
}
let mut g = self.global_mut();
g.cross_thread_upvals.insert((tid, idx), val);
Ok(())
}
pub fn protected_call_raw(&mut self, func: StackIdx, nresults: i32, errfunc: StackIdx) -> Result<(), LuaError> {
let ef = errfunc.0 as isize;
let status = crate::do_::pcall(
self,
|s| s.call_no_yield(func, nresults),
func,
ef,
);
match status {
LuaStatus::Ok => Ok(()),
LuaStatus::ErrSyntax => {
let err_val = self.get_at(func);
self.set_top(func);
Err(LuaError::Syntax(err_val))
}
LuaStatus::Yield => {
self.set_top(func);
Err(LuaError::Yield)
}
_ => {
let err_val = self.get_at(func);
self.set_top(func);
Err(LuaError::Runtime(err_val))
}
}
}
pub fn protected_parser(&mut self, z: crate::zio::ZIO, name: &[u8], mode: Option<&[u8]>) -> LuaStatus {
crate::do_::protected_parser(self, z, name, mode)
}
pub fn do_call(&mut self, func: StackIdx, nresults: i32) -> Result<(), LuaError> {
crate::do_::call(self, func, nresults)
}
pub fn do_call_no_yield(&mut self, func: StackIdx, nresults: i32) -> Result<(), LuaError> {
crate::do_::callnoyield(self, func, nresults)
}
pub fn call_no_yield(&mut self, func: StackIdx, nresults: i32) -> Result<(), LuaError> {
crate::do_::callnoyield(self, func, nresults)
}
pub fn call_at(&mut self, func: StackIdx, nresults: i32) -> Result<(), LuaError> {
crate::do_::call(self, func, nresults)
}
#[inline(always)]
pub fn precall(&mut self, func: StackIdx, nresults: i32) -> Result<Option<CallInfoIdx>, LuaError> {
crate::do_::precall(self, func, nresults)
}
#[inline(always)]
pub fn pretailcall(
&mut self,
ci: CallInfoIdx,
func: StackIdx,
narg1: i32,
delta: i32,
) -> Result<i32, LuaError> {
crate::do_::pretailcall(self, ci, func, narg1, delta)
}
#[inline(always)]
pub fn poscall<N: TryInto<i32>>(&mut self, ci: CallInfoIdx, nres: N) -> Result<(), LuaError>
where
<N as TryInto<i32>>::Error: std::fmt::Debug,
{
let n = nres.try_into().expect("poscall: nres out of i32 range");
crate::do_::poscall(self, ci, n)
}
pub fn adjust_results(&mut self, nresults: i32) {
const LUA_MULTRET: i32 = -1;
if nresults <= LUA_MULTRET {
let ci_idx = self.ci.as_usize();
if self.call_info[ci_idx].top.0 < self.top.0 {
self.call_info[ci_idx].top = self.top;
}
}
}
pub fn adjust_varargs(
&mut self,
ci: CallInfoIdx,
nfixparams: i32,
cl: &GcRef<lua_types::closure::LuaLClosure>,
) -> Result<(), LuaError> {
crate::tagmethods::adjust_varargs(self, nfixparams, ci, &cl.0.proto)
}
pub fn get_varargs(
&mut self,
ci: CallInfoIdx,
ra: StackIdx,
n: i32,
) -> Result<i32, LuaError> {
crate::tagmethods::get_varargs(self, ci, ra, n)?;
Ok(0)
}
pub fn close_upvals(&mut self, level: StackIdx) -> Result<(), LuaError> {
crate::func::close_upval(self, level);
Ok(())
}
pub fn close_upvals_status(&mut self, level: StackIdx, _status: i32) -> Result<(), LuaError> {
crate::func::close_upval(self, level);
Ok(())
}
pub fn close_upvals_from_base(&mut self, ci: CallInfoIdx) -> Result<(), LuaError> {
let base = self.ci_base(ci);
crate::func::close_upval(self, base);
Ok(())
}
pub fn arith_op(&mut self, op: i32, p1: &LuaValue, p2: &LuaValue) -> Result<LuaValue, LuaError> {
let arith_op = match op {
0 => lua_types::arith::ArithOp::Add,
1 => lua_types::arith::ArithOp::Sub,
2 => lua_types::arith::ArithOp::Mul,
3 => lua_types::arith::ArithOp::Mod,
4 => lua_types::arith::ArithOp::Pow,
5 => lua_types::arith::ArithOp::Div,
6 => lua_types::arith::ArithOp::Idiv,
7 => lua_types::arith::ArithOp::Band,
8 => lua_types::arith::ArithOp::Bor,
9 => lua_types::arith::ArithOp::Bxor,
10 => lua_types::arith::ArithOp::Shl,
11 => lua_types::arith::ArithOp::Shr,
12 => lua_types::arith::ArithOp::Unm,
13 => lua_types::arith::ArithOp::Bnot,
_ => return Err(LuaError::runtime(format_args!("invalid arith op {}", op))),
};
let mut res = LuaValue::Nil;
if crate::object::raw_arith(self, arith_op, p1, p2, &mut res)? {
Ok(res)
} else {
Err(LuaError::arith_error(p1, p2, "perform arithmetic on"))
}
}
pub fn concat(&mut self, n: i32) -> Result<(), LuaError> {
crate::vm::concat(self, n)
}
pub fn less_than(&mut self, l: &LuaValue, r: &LuaValue) -> Result<bool, LuaError> {
crate::vm::less_than(self, l, r)
}
pub fn less_equal(&mut self, l: &LuaValue, r: &LuaValue) -> Result<bool, LuaError> {
crate::vm::less_equal(self, l, r)
}
pub fn equal_obj(&self, _ctx: Option<&LuaValue>, l: &LuaValue, r: &LuaValue) -> bool {
crate::vm::equal_obj(None, l, r).unwrap_or(false)
}
pub fn equal_obj_with_tm(&mut self, l: &LuaValue, r: &LuaValue) -> Result<bool, LuaError> {
crate::vm::equal_obj(Some(self), l, r)
}
pub fn obj_len(&mut self, v: &LuaValue) -> Result<LuaValue, LuaError> {
match v {
LuaValue::Table(_) => {
let mt = self.table_metatable(v);
let tm = self.fast_tm_table(mt.as_ref(), TagMethod::Len);
if matches!(tm, LuaValue::Nil) {
let n = self.table_length(v)?;
return Ok(LuaValue::Int(n));
}
self.push(LuaValue::Nil);
let slot = StackIdx(self.top.0 - 1);
crate::tagmethods::call_tm_res(self, tm, v.clone(), v.clone(), slot)?;
Ok(self.pop())
}
LuaValue::Str(s) => Ok(LuaValue::Int(s.len() as i64)),
other => {
let tm = crate::tagmethods::get_tm_by_obj(self, other, crate::tagmethods::TagMethod::Len);
if matches!(tm, LuaValue::Nil) {
return Err(LuaError::type_error(other, "get length of"));
}
self.push(LuaValue::Nil);
let slot = StackIdx(self.top.0 - 1);
crate::tagmethods::call_tm_res(self, tm, v.clone(), v.clone(), slot)?;
Ok(self.pop())
}
}
}
pub fn obj_to_string(&mut self, idx: i32) -> Result<GcRef<LuaString>, LuaError> {
let slot: StackIdx = if idx > 0 {
let ci_func = self.current_call_info().func;
ci_func + idx
} else {
debug_assert!(idx != 0, "invalid index");
StackIdx((self.top_idx().0 as i32 + idx) as u32)
};
let val = self.get_at(slot);
match val {
LuaValue::Str(s) => Ok(s),
LuaValue::Int(_) | LuaValue::Float(_) => {
let s = crate::object::num_to_string(self, &val)?;
self.set_at(slot, LuaValue::Str(s.clone()));
Ok(s)
}
_ => Err(LuaError::type_error(&val, "convert to string")),
}
}
pub fn coerce_to_string(&mut self, idx: StackIdx) -> Result<GcRef<LuaString>, LuaError> {
let val = self.get_at(idx);
match val {
LuaValue::Str(s) => Ok(s),
LuaValue::Int(_) | LuaValue::Float(_) => {
let s = crate::object::num_to_string(self, &val)?;
self.set_at(idx, LuaValue::Str(s.clone()));
Ok(s)
}
_ => Err(LuaError::type_error(&val, "convert to string")),
}
}
pub fn str_to_num(&mut self, s: &[u8]) -> Option<(LuaValue, usize)> {
let mut out = LuaValue::Nil;
let sz = crate::object::str2num(s, &mut out);
if sz == 0 { None } else { Some((out, sz)) }
}
pub fn fast_get(&mut self, t: &LuaValue, k: &LuaValue) -> Result<Option<LuaValue>, LuaError> {
let LuaValue::Table(tbl) = t else { return Ok(None); };
let v = tbl.get(k);
if matches!(v, LuaValue::Nil) { Ok(None) } else { Ok(Some(v)) }
}
pub fn fast_get_int(&mut self, t: &LuaValue, k: i64) -> Result<Option<LuaValue>, LuaError> {
let LuaValue::Table(tbl) = t else { return Ok(None); };
let v = tbl.get_int(k);
if matches!(v, LuaValue::Nil) { Ok(None) } else { Ok(Some(v)) }
}
pub fn fast_get_short_str(&mut self, t: &LuaValue, k: &LuaValue) -> Result<Option<LuaValue>, LuaError> {
let LuaValue::Table(tbl) = t else { return Ok(None); };
let LuaValue::Str(s) = k else { return Ok(None); };
let v = tbl.get_short_str(s);
if matches!(v, LuaValue::Nil) { Ok(None) } else { Ok(Some(v)) }
}
pub fn fast_tm_table(&mut self, t: Option<&GcRef<LuaTable>>, tm: TagMethod) -> LuaValue {
let Some(mt) = t else { return LuaValue::Nil; };
debug_assert!((tm as u8) <= TagMethod::Eq as u8);
let ename = self.global().tmname[tm as usize].clone();
mt.get_short_str(&ename)
}
pub fn fast_tm_ud(&mut self, u: &GcRef<LuaUserData>, tm: TagMethod) -> LuaValue {
let mt = u.metatable();
self.fast_tm_table(mt.as_ref(), tm)
}
pub fn table_get_with_tm(&mut self, t: &LuaValue, k: &LuaValue) -> Result<LuaValue, LuaError> {
if let LuaValue::Table(tbl) = t {
if tbl.metatable().is_none() {
return Ok(tbl.get(k));
}
}
if let Some(v) = self.fast_get(t, k)? {
return Ok(v);
}
let res = self.top_idx();
self.push(LuaValue::Nil);
crate::vm::finish_get(self, t.clone(), k.clone(), res, true, None)?;
let value = self.get_at(res);
self.pop();
Ok(value)
}
#[inline]
pub fn table_set_with_tm(&mut self, t: &LuaValue, k: LuaValue, v: LuaValue) -> Result<(), LuaError> {
if let LuaValue::Table(tbl) = t {
if tbl.metatable().is_none() {
self.gc_barrier_back(t, &v);
return self.table_raw_set(t, k, v);
}
}
if self.fast_get(t, &k)?.is_some() {
self.gc_barrier_back(t, &v);
return self.table_raw_set(t, k, v);
}
crate::vm::finish_set(self, t.clone(), k, v, true, None, None)
}
#[inline]
pub fn table_raw_set(&mut self, t: &LuaValue, k: LuaValue, v: LuaValue) -> Result<(), LuaError> {
let LuaValue::Table(tbl) = t else {
return Err(LuaError::type_error(t, "index"));
};
let tbl = tbl.clone();
tbl.raw_set(self, k, v)
}
#[inline]
pub fn table_array_set(&mut self, t: &LuaValue, idx: usize, v: LuaValue) -> Result<(), LuaError> {
let LuaValue::Table(tbl) = t else {
return Err(LuaError::type_error(t, "index"));
};
let tbl = tbl.clone();
tbl.raw_set_int(self, idx as i64 + 1, v)
}
pub fn table_ensure_array(&mut self, t: &LuaValue, n: usize) -> Result<(), LuaError> {
let LuaValue::Table(tbl) = t else {
return Err(LuaError::type_error(t, "index"));
};
if n > tbl.array_len() {
tbl.resize(self, n, 0)?;
}
Ok(())
}
pub fn table_length(&mut self, t: &LuaValue) -> Result<i64, LuaError> {
let LuaValue::Table(tbl) = t else {
return Err(LuaError::type_error(t, "get length of"));
};
Ok(tbl.getn() as i64)
}
pub fn table_metatable(&mut self, v: &LuaValue) -> Option<GcRef<LuaTable>> {
match v {
LuaValue::Table(t) => t.metatable(),
LuaValue::UserData(u) => u.metatable(),
other => {
let idx = other.base_type() as usize;
self.global().mt[idx].clone()
}
}
}
pub fn table_resize(&mut self, t: &GcRef<LuaTable>, na: usize, nh: usize) -> Result<(), LuaError> {
t.resize(self, na, nh)
}
pub fn table_getn(&self, t: &GcRef<LuaTable>) -> i64 {
let mut i: i64 = 1;
loop {
let v = t.get_int(i);
if matches!(v, LuaValue::Nil) {
return i - 1;
}
i += 1;
}
}
pub fn try_bin_tm(&mut self, p1: &LuaValue, p1_idx: Option<StackIdx>, p2: &LuaValue, p2_idx: Option<StackIdx>, res: StackIdx, tm: lua_types::tagmethod::TagMethod) -> Result<(), LuaError> {
let event = crate::tagmethods::TagMethod::from_u8(tm as u8);
crate::tagmethods::try_bin_tm(self, p1, p1_idx, p2, p2_idx, res, event)
}
pub fn try_bin_i_tm(&mut self, p1: &LuaValue, p1_idx: Option<StackIdx>, imm: i64, flip: bool, res: StackIdx, tm: lua_types::tagmethod::TagMethod) -> Result<(), LuaError> {
let event = crate::tagmethods::TagMethod::from_u8(tm as u8);
crate::tagmethods::try_bini_tm(self, p1, p1_idx, imm, flip, res, event)
}
pub fn try_bin_assoc_tm(&mut self, p1: &LuaValue, p1_idx: Option<StackIdx>, p2: &LuaValue, p2_idx: Option<StackIdx>, flip: bool, res: StackIdx, tm: lua_types::tagmethod::TagMethod) -> Result<(), LuaError> {
let event = crate::tagmethods::TagMethod::from_u8(tm as u8);
crate::tagmethods::try_bin_assoc_tm(self, p1, p1_idx, p2, p2_idx, flip, res, event)
}
pub fn try_concat_tm(&mut self, _p1: &LuaValue, _p2: &LuaValue) -> Result<(), LuaError> {
crate::tagmethods::try_concat_tm(self)
}
pub fn call_tm(&mut self, f: LuaValue, p1: &LuaValue, p2: &LuaValue, p3: &LuaValue) -> Result<(), LuaError> {
crate::tagmethods::call_tm(self, f, p1.clone(), p2.clone(), p3.clone())
}
pub fn call_tm_res(&mut self, f: LuaValue, p1: &LuaValue, p2: &LuaValue, res: StackIdx) -> Result<(), LuaError> {
crate::tagmethods::call_tm_res(self, f, p1.clone(), p2.clone(), res)
}
pub fn call_tm_res_bool(&mut self, f: LuaValue, p1: &LuaValue, p2: &LuaValue) -> Result<bool, LuaError> {
let res = self.top_idx();
self.push(LuaValue::Nil);
crate::tagmethods::call_tm_res(self, f, p1.clone(), p2.clone(), res)?;
let result = self.get_at(res).clone();
self.pop();
Ok(!matches!(result, LuaValue::Nil | LuaValue::Bool(false)))
}
pub fn call_order_tm(&mut self, p1: &LuaValue, p2: &LuaValue, tm: lua_types::tagmethod::TagMethod) -> Result<bool, LuaError> {
let event = crate::tagmethods::TagMethod::from_u8(tm as u8);
crate::tagmethods::call_order_tm(self, p1, p2, event)
}
pub fn call_order_i_tm(&mut self, p1: &LuaValue, v2: i64, flip: bool, isfloat: bool, tm: lua_types::tagmethod::TagMethod) -> Result<bool, LuaError> {
let event = crate::tagmethods::TagMethod::from_u8(tm as u8);
crate::tagmethods::call_orderi_tm(self, p1, v2 as i32, flip, isfloat, event)
}
#[inline(always)]
pub fn proto_code(&self, cl: &GcRef<lua_types::closure::LuaLClosure>, pc: u32) -> lua_types::opcode::Instruction {
cl.proto.code[pc as usize]
}
#[inline(always)]
pub fn proto_const(&self, cl: &GcRef<lua_types::closure::LuaLClosure>, idx: usize) -> LuaValue {
cl.proto.k[idx].clone()
}
#[inline(always)]
pub fn proto_const_int(&self, cl: &GcRef<lua_types::closure::LuaLClosure>, idx: usize) -> Option<i64> {
match &cl.proto.k[idx] {
LuaValue::Int(v) => Some(*v),
_ => None,
}
}
#[inline(always)]
pub fn proto_const_num(&self, cl: &GcRef<lua_types::closure::LuaLClosure>, idx: usize) -> Option<f64> {
match &cl.proto.k[idx] {
LuaValue::Float(f) => Some(*f),
LuaValue::Int(v) => Some(*v as f64),
_ => None,
}
}
pub fn get_proto_instr(&self, ci: CallInfoIdx, pc: u32) -> lua_types::opcode::Instruction {
let cl = self.ci_lua_closure(ci)
.expect("get_proto_instr: CallInfo does not hold a Lua closure");
cl.proto.code[pc as usize]
}
pub fn trace_call(&mut self, _idx: CallInfoIdx) -> Result<bool, LuaError> {
Ok(crate::debug::trace_call(self)? != 0)
}
pub fn trace_exec(&mut self, _idx: CallInfoIdx, pc: u32) -> Result<bool, LuaError> {
Ok(crate::debug::trace_exec(self, pc)? != 0)
}
pub fn hook_call(&mut self, idx: CallInfoIdx) -> Result<(), LuaError> {
crate::do_::hookcall(self, idx)
}
#[inline(always)]
fn gc_step_flags(&self) -> Option<(bool, bool)> {
let g = self.global();
if !g.is_gc_running() {
return None;
}
let should_collect = g.heap.would_collect();
let has_finalizers = !g.to_be_finalized.is_empty();
if should_collect || has_finalizers {
Some((should_collect, has_finalizers))
} else {
None
}
}
#[inline(always)]
pub fn gc_check_step(&mut self) {
if !self.allowhook {
return;
}
let Some((should_collect, has_finalizers)) = self.gc_step_flags() else {
return;
};
if should_collect {
self.gc().check_step();
}
if has_finalizers || !self.global().to_be_finalized.is_empty() {
crate::api::run_pending_finalizers(self);
}
}
#[inline(always)]
pub fn gc_cond_step(&mut self) {
if !self.allowhook {
return;
}
let Some((should_collect, has_finalizers)) = self.gc_step_flags() else {
return;
};
if should_collect {
self.gc().check_step();
}
if has_finalizers || !self.global().to_be_finalized.is_empty() {
crate::api::run_pending_finalizers(self);
}
}
pub fn gc_barrier_back<T, U>(&mut self, _t: T, _v: U) { }
pub fn gc_barrier_upval<T, U, V>(&mut self, _cl: T, _uv: U, _v: V) { }
pub fn is_main_thread(&mut self) -> bool {
let g = self.global();
g.current_thread_id == g.main_thread_id
}
pub fn obj_type_name<'v>(&self, v: &'v LuaValue) -> std::borrow::Cow<'static, [u8]> {
match v {
LuaValue::LightUserData(_) => std::borrow::Cow::Borrowed(b"light userdata"),
LuaValue::Table(t) => {
if let Some(mt) = t.metatable() {
if let LuaValue::Str(s) = mt.get_str_bytes(b"__name") {
return std::borrow::Cow::Owned(s.as_bytes().to_vec());
}
}
std::borrow::Cow::Borrowed(crate::tagmethods::type_name(v.base_type()))
}
LuaValue::UserData(u) => {
if let Some(mt) = u.metatable() {
if let LuaValue::Str(s) = mt.get_str_bytes(b"__name") {
return std::borrow::Cow::Owned(s.as_bytes().to_vec());
}
}
std::borrow::Cow::Borrowed(crate::tagmethods::type_name(v.base_type()))
}
_ => std::borrow::Cow::Borrowed(crate::tagmethods::type_name(v.base_type())),
}
}
pub fn full_type_name(&mut self, v: &LuaValue) -> Result<Vec<u8>, LuaError> {
crate::tagmethods::obj_type_name(self, v)
}
pub fn emit_warning(&mut self, _msg: &[u8], _to_cont: bool) { warning(self, _msg, _to_cont) }
}
pub struct GcHandle<'a> {
_state: &'a mut LuaState,
}
struct CollectRoots<'a> {
global: &'a GlobalState,
thread: &'a LuaState,
}
impl<'a> lua_gc::Trace for CollectRoots<'a> {
fn trace(&self, m: &mut lua_gc::Marker) {
self.global.trace(m);
self.thread.trace(m);
}
}
fn trace_reachable_threads(
global: &GlobalState,
_current_thread_id: u64,
marker: &mut lua_gc::Marker,
) {
use lua_gc::Trace;
loop {
let visited_before = marker.visited_count();
for (id, entry) in global.threads.iter() {
if thread_entry_marked_alive(marker, *id, entry) {
if let Ok(thread) = entry.state.try_borrow() {
thread.trace(marker);
}
}
}
marker.drain_gray_queue();
if marker.visited_count() == visited_before {
break;
}
}
}
fn thread_entry_marked_alive(
marker: &lua_gc::Marker,
id: u64,
entry: &ThreadRegistryEntry,
) -> bool {
marker.is_visited(entry.value.identity()) && entry.value.id == id
}
fn close_open_upvalues_for_unreachable_threads(
global: &GlobalState,
marker: &mut lua_gc::Marker,
) {
use lua_gc::Trace;
let mut closed_values = Vec::<LuaValue>::new();
for (id, entry) in global.threads.iter() {
if entry.value.id != *id {
continue;
}
if thread_entry_marked_alive(marker, *id, entry) {
continue;
}
let Ok(thread) = entry.state.try_borrow() else {
continue;
};
for uv in thread.openupval.iter() {
if !marker.is_visited(uv.identity()) {
continue;
}
let Some((thread_id, idx)) = uv.try_open_payload() else {
continue;
};
if thread_id as u64 != *id {
continue;
}
let value = thread.get_at(idx);
uv.close_with(value.clone());
closed_values.push(value);
}
}
for value in closed_values {
value.trace(marker);
}
marker.drain_gray_queue();
}
impl<'a> GcHandle<'a> {
pub fn check_step(&self) {
if !self._state.global().is_gc_running() {
return;
}
self.collect_via_heap( false);
}
pub fn full_collect(&self) {
self.collect_via_heap( true);
}
fn collect_via_heap(&self, force: bool) {
use lua_gc::Trace;
let state_ref: &LuaState = &*self._state;
if !force {
let g = state_ref.global.borrow();
if !g.heap.would_collect() {
return;
}
}
let weak_tables_snapshot: Vec<lua_types::gc::GcRef<lua_types::value::LuaTable>> = {
let g = state_ref.global.borrow();
let mut seen = std::collections::HashSet::<usize>::new();
g.weak_tables_registry
.iter()
.filter_map(|w| w.upgrade())
.filter(|t| seen.insert(t.identity()))
.collect()
};
let pending_snapshot: Vec<lua_types::gc::GcRef<lua_types::value::LuaTable>> = {
let g = state_ref.global.borrow();
g.pending_finalizers.clone()
};
let long_string_snapshot: Vec<(usize, usize)> = {
let g = state_ref.global.borrow();
g.gc_tracked_long_strings
.iter()
.map(|(w, sz)| (w.0.identity(), *sz))
.collect()
};
let alive_ids: std::cell::RefCell<std::collections::HashSet<usize>> =
std::cell::RefCell::new(std::collections::HashSet::new());
let newly_unreachable: std::cell::RefCell<Vec<lua_types::gc::GcRef<lua_types::value::LuaTable>>> =
std::cell::RefCell::new(Vec::new());
let dead_long_strings: std::cell::RefCell<std::collections::HashSet<usize>> =
std::cell::RefCell::new(std::collections::HashSet::new());
let alive_thread_ids: std::cell::RefCell<std::collections::HashSet<u64>> =
std::cell::RefCell::new(std::collections::HashSet::new());
let collect_ran = std::cell::Cell::new(false);
{
let global = state_ref.global.borrow();
global.heap.unpause();
let roots = CollectRoots { global: &*global, thread: state_ref };
let hook = |marker: &mut lua_gc::Marker| {
collect_ran.set(true);
trace_reachable_threads(&*global, global.current_thread_id, marker);
close_open_upvalues_for_unreachable_threads(&*global, marker);
loop {
let visited_before = marker.visited_count();
for t in &weak_tables_snapshot {
let t_id = t.identity();
if !marker.is_visited(t_id) {
continue;
}
let to_mark = t.ephemeron_values_to_mark(
&|id| marker.is_visited(id),
);
for v in &to_mark {
v.trace(marker);
}
}
marker.drain_gray_queue();
if marker.visited_count() == visited_before {
break;
}
}
for pf in &pending_snapshot {
if !marker.is_visited(pf.identity()) {
marker.mark(pf.0);
newly_unreachable.borrow_mut().push(pf.clone());
}
}
marker.drain_gray_queue();
loop {
let visited_before = marker.visited_count();
for t in &weak_tables_snapshot {
let t_id = t.identity();
if !marker.is_visited(t_id) {
continue;
}
let to_mark = t.ephemeron_values_to_mark(
&|id| marker.is_visited(id),
);
for v in &to_mark {
v.trace(marker);
}
}
marker.drain_gray_queue();
if marker.visited_count() == visited_before {
break;
}
}
for t in &weak_tables_snapshot {
let id = t.identity();
if marker.is_visited(id) {
let to_mark = t.prune_weak_dead(&|id| marker.is_visited(id));
for v in &to_mark {
v.trace(marker);
}
alive_ids.borrow_mut().insert(id);
}
}
marker.drain_gray_queue();
{
let mut dead = dead_long_strings.borrow_mut();
for (id, _sz) in &long_string_snapshot {
if !marker.is_visited(*id) {
dead.insert(*id);
}
}
}
{
let mut alive = alive_thread_ids.borrow_mut();
for (id, entry) in global.threads.iter() {
if thread_entry_marked_alive(marker, *id, entry) {
alive.insert(*id);
}
}
}
};
if force {
global.heap.full_collect_with_post_mark(&roots, hook);
} else {
global.heap.step_with_post_mark(&roots, hook);
}
}
if !collect_ran.get() {
return;
}
let alive_set = alive_ids.into_inner();
let promote: Vec<lua_types::gc::GcRef<lua_types::value::LuaTable>> =
newly_unreachable.into_inner();
let promote_ids: std::collections::HashSet<usize> =
promote.iter().map(|t| t.identity()).collect();
let dead_ls_ids = dead_long_strings.into_inner();
let alive_thread_ids = alive_thread_ids.into_inner();
let mut g = state_ref.global.borrow_mut();
g.weak_tables_registry
.retain(|w| alive_set.contains(&w.0.identity()));
let main_thread_id = g.main_thread_id;
g.threads.retain(|id, _| alive_thread_ids.contains(id));
g.cross_thread_upvals
.retain(|(id, _), _| *id == main_thread_id || alive_thread_ids.contains(id));
g.pending_finalizers
.retain(|t| !promote_ids.contains(&t.identity()));
g.to_be_finalized.extend(promote);
if !dead_ls_ids.is_empty() {
let mut freed: isize = 0;
g.gc_tracked_long_strings.retain(|(w, sz)| {
if dead_ls_ids.contains(&w.0.identity()) {
freed += *sz as isize;
false
} else {
true
}
});
g.gc_debt -= freed;
}
}
pub fn step(&self) { }
pub fn incremental_step(&self, work_units: isize) -> bool {
use lua_gc::{StepBudget, StepOutcome, Trace};
let state_ref: &LuaState = &*self._state;
let weak_tables_snapshot: Vec<lua_types::gc::GcRef<lua_types::value::LuaTable>> = {
let g = state_ref.global.borrow();
let mut seen = std::collections::HashSet::<usize>::new();
g.weak_tables_registry
.iter()
.filter_map(|w| w.upgrade())
.filter(|t| seen.insert(t.identity()))
.collect()
};
let pending_snapshot: Vec<lua_types::gc::GcRef<lua_types::value::LuaTable>> = {
let g = state_ref.global.borrow();
g.pending_finalizers.clone()
};
let long_string_snapshot: Vec<(usize, usize)> = {
let g = state_ref.global.borrow();
g.gc_tracked_long_strings
.iter()
.map(|(w, sz)| (w.0.identity(), *sz))
.collect()
};
let alive_ids: std::cell::RefCell<std::collections::HashSet<usize>> =
std::cell::RefCell::new(std::collections::HashSet::new());
let newly_unreachable: std::cell::RefCell<Vec<lua_types::gc::GcRef<lua_types::value::LuaTable>>> =
std::cell::RefCell::new(Vec::new());
let dead_long_strings: std::cell::RefCell<std::collections::HashSet<usize>> =
std::cell::RefCell::new(std::collections::HashSet::new());
let alive_thread_ids: std::cell::RefCell<std::collections::HashSet<u64>> =
std::cell::RefCell::new(std::collections::HashSet::new());
let atomic_ran = std::cell::Cell::new(false);
let outcome = {
let global = state_ref.global.borrow();
global.heap.unpause();
let roots = CollectRoots { global: &*global, thread: state_ref };
let hook = |marker: &mut lua_gc::Marker| {
atomic_ran.set(true);
trace_reachable_threads(&*global, global.current_thread_id, marker);
close_open_upvalues_for_unreachable_threads(&*global, marker);
loop {
let visited_before = marker.visited_count();
for t in &weak_tables_snapshot {
let t_id = t.identity();
if !marker.is_visited(t_id) {
continue;
}
let to_mark = t.ephemeron_values_to_mark(
&|id| marker.is_visited(id),
);
for v in &to_mark {
v.trace(marker);
}
}
marker.drain_gray_queue();
if marker.visited_count() == visited_before {
break;
}
}
for pf in &pending_snapshot {
if !marker.is_visited(pf.identity()) {
marker.mark(pf.0);
newly_unreachable.borrow_mut().push(pf.clone());
}
}
marker.drain_gray_queue();
loop {
let visited_before = marker.visited_count();
for t in &weak_tables_snapshot {
let t_id = t.identity();
if !marker.is_visited(t_id) {
continue;
}
let to_mark = t.ephemeron_values_to_mark(
&|id| marker.is_visited(id),
);
for v in &to_mark {
v.trace(marker);
}
}
marker.drain_gray_queue();
if marker.visited_count() == visited_before {
break;
}
}
for t in &weak_tables_snapshot {
let id = t.identity();
if marker.is_visited(id) {
let to_mark = t.prune_weak_dead(&|id| marker.is_visited(id));
for v in &to_mark {
v.trace(marker);
}
alive_ids.borrow_mut().insert(id);
}
}
marker.drain_gray_queue();
{
let mut dead = dead_long_strings.borrow_mut();
for (id, _sz) in &long_string_snapshot {
if !marker.is_visited(*id) {
dead.insert(*id);
}
}
}
{
let mut alive = alive_thread_ids.borrow_mut();
for (id, entry) in global.threads.iter() {
if thread_entry_marked_alive(marker, *id, entry) {
alive.insert(*id);
}
}
}
};
let budget = StepBudget::from_work(work_units);
global.heap.incremental_step_with_post_mark(&roots, budget, hook)
};
if atomic_ran.get() {
let alive_set = alive_ids.into_inner();
let promote: Vec<lua_types::gc::GcRef<lua_types::value::LuaTable>> =
newly_unreachable.into_inner();
let promote_ids: std::collections::HashSet<usize> =
promote.iter().map(|t| t.identity()).collect();
let dead_ls_ids = dead_long_strings.into_inner();
let alive_thread_ids = alive_thread_ids.into_inner();
let mut g = state_ref.global.borrow_mut();
g.weak_tables_registry
.retain(|w| alive_set.contains(&w.0.identity()));
let main_thread_id = g.main_thread_id;
g.threads.retain(|id, _| alive_thread_ids.contains(id));
g.cross_thread_upvals
.retain(|(id, _), _| *id == main_thread_id || alive_thread_ids.contains(id));
g.pending_finalizers
.retain(|t| !promote_ids.contains(&t.identity()));
g.to_be_finalized.extend(promote);
if !dead_ls_ids.is_empty() {
let mut freed: isize = 0;
g.gc_tracked_long_strings.retain(|(w, sz)| {
if dead_ls_ids.contains(&w.0.identity()) {
freed += *sz as isize;
false
} else {
true
}
});
g.gc_debt -= freed;
}
}
matches!(outcome, StepOutcome::Paused)
}
pub fn prune_weak_tables_mark_only(&self) {
use lua_gc::Trace;
let state_ref: &LuaState = &*self._state;
let weak_tables_snapshot: Vec<lua_types::gc::GcRef<lua_types::value::LuaTable>> = {
let g = state_ref.global.borrow();
let mut seen = std::collections::HashSet::<usize>::new();
g.weak_tables_registry
.iter()
.filter_map(|w| w.upgrade())
.filter(|t| seen.insert(t.identity()))
.collect()
};
let global = state_ref.global.borrow();
global.heap.unpause();
let roots = CollectRoots { global: &*global, thread: state_ref };
let hook = |marker: &mut lua_gc::Marker| {
trace_reachable_threads(&*global, global.current_thread_id, marker);
loop {
let visited_before = marker.visited_count();
for t in &weak_tables_snapshot {
let t_id = t.identity();
if !marker.is_visited(t_id) {
continue;
}
let to_mark = t.ephemeron_values_to_mark(
&|id| marker.is_visited(id),
);
for v in &to_mark {
v.trace(marker);
}
}
marker.drain_gray_queue();
if marker.visited_count() == visited_before {
break;
}
}
for t in &weak_tables_snapshot {
if marker.is_visited(t.identity()) {
let to_mark = t.prune_weak_dead(&|id| marker.is_visited(id));
for v in &to_mark {
v.trace(marker);
}
}
}
};
global.heap.mark_only_with_post_mark(&roots, hook);
}
pub fn change_mode(&self, mode: GcKind) {
self._state.global_mut().gckind = mode as u8;
}
pub fn fix_object<T: lua_gc::Trace + 'static>(&self, _o: &GcRef<T>) { }
pub fn free_all_objects(&self) {
}
pub fn barrier(&self, _p: &dyn std::any::Any, _v: &LuaValue) {}
pub fn barrier_back(&self, _p: &dyn std::any::Any, _v: &LuaValue) {}
pub fn obj_barrier(&self, _p: &dyn std::any::Any, _o: &dyn std::any::Any) {}
pub fn obj_barrier_back(&self, _p: &dyn std::any::Any, _o: &dyn std::any::Any) {}
}
fn make_seed() -> u32 {
use std::time::{SystemTime, UNIX_EPOCH};
let t = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs() as u32)
.unwrap_or(0);
crate::string::hash_bytes(&t.to_le_bytes(), t)
}
pub(crate) fn set_debt(g: &mut GlobalState, mut debt: isize) {
let tb = g.total_bytes() as isize;
debug_assert!(tb > 0);
if debt < tb.saturating_sub(isize::MAX) {
debt = tb - isize::MAX;
}
g.totalbytes = tb - debt;
g.gc_debt = debt;
}
pub(crate) fn reclaim_dead_long_strings(g: &mut GlobalState) {
let mut freed: isize = 0;
g.gc_tracked_long_strings.retain(|(w, sz)| {
if w.strong_count() == 0 {
freed += *sz as isize;
false
} else {
true
}
});
g.gc_debt -= freed;
}
pub fn set_c_stack_limit(_state: &mut LuaState, _limit: u32) -> i32 {
let _ = (_state, _limit);
LUAI_MAXCCALLS as i32
}
pub(crate) fn extend_ci(state: &mut LuaState) -> CallInfoIdx {
debug_assert!(
state.call_info[state.ci.0 as usize].next.is_none(),
"extend_ci: current ci already has a cached next frame"
);
let current_idx = state.ci;
let new_idx = CallInfoIdx(state.call_info.len() as u32);
state.call_info.push(CallInfo {
previous: Some(current_idx),
next: None,
u: CallInfoFrame::lua_default(),
..CallInfo::default()
});
state.call_info[current_idx.0 as usize].next = Some(new_idx);
state.nci += 1;
new_idx
}
fn free_ci(state: &mut LuaState) {
let ci_idx = state.ci.0 as usize;
let mut next_opt = state.call_info[ci_idx].next.take();
while let Some(idx) = next_opt {
next_opt = state.call_info[idx.0 as usize].next;
state.nci = state.nci.saturating_sub(1);
}
state.call_info.truncate(ci_idx + 1);
}
pub(crate) fn shrink_ci(state: &mut LuaState) {
let ci_idx = state.ci.0 as usize;
if state.call_info[ci_idx].next.is_none() {
return;
}
let free_count = state.call_info.len().saturating_sub(ci_idx + 1);
if free_count <= 1 {
return;
}
let keep = free_count / 2;
let removed = free_count - keep;
let new_len = ci_idx + 1 + keep;
state.call_info.truncate(new_len);
state.nci = state.nci.saturating_sub(removed as u32);
if let Some(last) = state.call_info.last_mut() {
last.next = None;
}
}
pub(crate) fn check_c_stack(state: &mut LuaState) -> Result<(), LuaError> {
if state.c_calls() == LUAI_MAXCCALLS {
return Err(LuaError::runtime(format_args!("C stack overflow")));
}
if state.c_calls() >= (LUAI_MAXCCALLS / 10 * 11) {
return Err(LuaError::runtime(format_args!(
"error while handling stack overflow (C stack overflow)"
)));
}
Ok(())
}
pub fn inc_c_stack(state: &mut LuaState) -> Result<(), LuaError> {
state.nCcalls += 1;
if state.c_calls() >= LUAI_MAXCCALLS {
check_c_stack(state)?;
}
Ok(())
}
fn stack_init(thread: &mut LuaState) {
let total_slots = BASIC_STACK_SIZE + EXTRA_STACK;
thread.stack = vec![StackValue::default(); total_slots];
thread.tbclist = Vec::new();
thread.top = StackIdx(0);
thread.stack_last = StackIdx(BASIC_STACK_SIZE as u32);
let base_ci = CallInfo {
func: StackIdx(0),
top: StackIdx(1 + LUA_MINSTACK as u32),
previous: None,
next: None,
callstatus: CIST_C,
nresults: 0,
u: CallInfoFrame::c_default(),
u2: CallInfoExtra::default(),
};
if thread.call_info.is_empty() {
thread.call_info.push(base_ci);
} else {
thread.call_info[0] = base_ci;
thread.call_info.truncate(1);
}
thread.stack[0] = StackValue { val: LuaValue::Nil, tbc_delta: 0 };
thread.top = StackIdx(1);
thread.ci = CallInfoIdx(0);
}
fn free_stack(state: &mut LuaState) {
if state.stack.is_empty() {
return;
}
state.ci = CallInfoIdx(0);
free_ci(state);
debug_assert_eq!(state.nci, 0, "nci should be 0 after free_ci");
state.stack.clear();
state.stack.shrink_to_fit();
}
fn init_registry(state: &mut LuaState) -> Result<(), LuaError> {
let registry = state.new_table();
state.global_mut().l_registry = LuaValue::Table(registry.clone());
let globals = state.new_table();
state.global_mut().globals = LuaValue::Table(globals);
let loaded = state.new_table();
state.global_mut().loaded = LuaValue::Table(loaded);
Ok(())
}
fn lua_open(state: &mut LuaState) -> Result<(), LuaError> {
stack_init(state);
init_registry(state)?;
crate::string::init(state)?;
crate::tagmethods::init(state)?;
state.global_mut().gcstp = 0;
state.global().heap.unpause();
state.global_mut().nilvalue = LuaValue::Nil;
Ok(())
}
fn preinit_thread(thread: &mut LuaState, global: Rc<RefCell<GlobalState>>) {
thread.global = global;
thread.stack = Vec::new();
thread.call_info = Vec::new();
thread.ci = CallInfoIdx(0);
thread.nci = 0;
thread.nCcalls = 0;
thread.hook = None;
thread.hookmask = 0;
thread.basehookcount = 0;
thread.allowhook = true;
thread.hookcount = thread.basehookcount;
thread.openupval = Vec::new();
thread.status = LuaStatus::Ok as u8;
thread.errfunc = 0;
thread.oldpc = 0;
}
fn close_state(state: &mut LuaState) {
let is_complete = state.global().is_complete();
if !is_complete {
state.gc().free_all_objects();
} else {
state.ci = CallInfoIdx(0);
state.gc().free_all_objects();
}
state.global_mut().strt = StringPool::default();
free_stack(state);
}
pub fn new_thread(state: &mut LuaState, initial_body: Option<LuaValue>) -> Result<(), LuaError> {
state.gc().check_step();
let global_rc = state.global_rc();
let hookmask = state.hookmask;
let basehookcount = state.basehookcount;
let reserved_id = {
let mut g = state.global_mut();
let id = g.next_thread_id;
g.next_thread_id += 1;
id
};
let mut new_thread = LuaState {
status: LuaStatus::Ok as u8,
allowhook: true,
nci: 0,
top: StackIdx(0),
stack_last: StackIdx(0),
stack: Vec::new(),
ci: CallInfoIdx(0),
call_info: Vec::new(),
openupval: Vec::new(),
tbclist: Vec::new(),
global: global_rc.clone(),
hook: None,
hookmask: 0,
basehookcount: 0,
hookcount: 0,
errfunc: 0,
nCcalls: 0,
oldpc: 0,
marked: 0,
cached_thread_id: reserved_id,
};
preinit_thread(&mut new_thread, global_rc);
new_thread.hookmask = hookmask;
new_thread.basehookcount = basehookcount;
new_thread.reset_hook_count();
stack_init(&mut new_thread);
if let Some(body) = initial_body {
new_thread.push(body);
}
let thread_ref: Rc<RefCell<LuaState>> = Rc::new(RefCell::new(new_thread));
let value = {
let mut g = state.global_mut();
let id = reserved_id;
let value = GcRef::new(lua_types::value::LuaThread::new(id));
g.threads.insert(
id,
ThreadRegistryEntry { state: thread_ref, value: value.clone() },
);
value
};
state.push(LuaValue::Thread(value));
Ok(())
}
pub(crate) fn free_thread(caller: &mut LuaState, thread: &mut LuaState) {
let _ = caller;
debug_assert!(
thread.openupval.is_empty(),
"free_thread: open upvalues remain after close_upval"
);
free_stack(thread);
}
pub fn reset_thread(state: &mut LuaState, status: i32) -> i32 {
state.ci = CallInfoIdx(0);
let ci_idx = 0usize;
if !state.stack.is_empty() {
state.stack[0].val = LuaValue::Nil;
}
state.call_info[ci_idx].func = StackIdx(0);
state.call_info[ci_idx].callstatus = CIST_C;
let mut status = if status == LuaStatus::Yield as i32 {
LuaStatus::Ok as i32
} else {
status
};
state.status = LuaStatus::Ok as u8;
let close_status = crate::do_::close_protected(
state,
StackIdx(1),
LuaStatus::from_raw(status),
);
status = close_status as i32;
if status != LuaStatus::Ok as i32 {
crate::do_::set_error_obj(state, LuaStatus::from_raw(status), StackIdx(1));
} else {
state.top = StackIdx(1);
}
let new_ci_top = StackIdx(state.top.0 + LUA_MINSTACK as u32);
state.call_info[ci_idx].top = new_ci_top;
let needed = new_ci_top.0 as usize;
if state.stack.len() < needed {
state.stack.resize(needed, StackValue::default());
}
status
}
pub fn close_thread(state: &mut LuaState, from: Option<&LuaState>) -> i32 {
state.nCcalls = match from {
Some(f) => f.c_calls(),
None => 0,
};
let current_status = state.status as i32;
let result = reset_thread(state, current_status);
result
}
pub fn reset_thread_api(state: &mut LuaState) -> i32 {
close_thread(state, None)
}
pub fn new_state() -> Option<LuaState> {
let placeholder_str = GcRef::new(LuaString::placeholder());
let initial_white = 1u8 << WHITE0BIT;
let global = GlobalState {
parser_hook: None,
file_loader_hook: None,
file_open_hook: None,
popen_hook: None,
file_remove_hook: None,
file_rename_hook: None,
os_execute_hook: None,
dynlib_load_hook: None,
dynlib_symbol_hook: None,
dynlib_unload_hook: None,
totalbytes: std::mem::size_of::<GlobalState>() as isize,
gc_debt: 0,
gc_estimate: 0,
lastatomic: 0,
strt: StringPool::default(),
l_registry: LuaValue::Nil,
globals: LuaValue::Nil,
loaded: LuaValue::Nil,
nilvalue: LuaValue::Int(0),
seed: make_seed(),
currentwhite: initial_white,
gcstate: GCS_PAUSE,
gckind: GcKind::Incremental as u8,
gcstopem: false,
genminormul: LUAI_GENMINORMUL,
genmajormul: (LUAI_GENMAJORMUL / 4) as u8,
gcstp: GCSTPGC,
gcemergency: false,
gcpause: (LUAI_GCPAUSE / 4) as u8,
gcstepmul: (LUAI_GCMUL / 4) as u8,
gcstepsize: LUAI_GCSTEPSIZE,
sweepgc_cursor: 0,
weak_tables_registry: Vec::new(),
gc_tracked_long_strings: Vec::new(),
pending_finalizers: Vec::new(),
to_be_finalized: Vec::new(),
twups: Vec::new(),
panic: None,
mainthread: None,
threads: std::collections::HashMap::new(),
main_thread_value: GcRef::new(lua_types::value::LuaThread::new(0)),
current_thread_id: 0,
main_thread_id: 0,
next_thread_id: 1,
memerrmsg: placeholder_str.clone(),
tmname: Vec::new(),
mt: std::array::from_fn(|_| None),
strcache: std::array::from_fn(|_| {
std::array::from_fn(|_| placeholder_str.clone())
}),
interned_lt: std::collections::HashMap::new(),
warnf: None,
c_functions: Vec::new(),
heap: lua_gc::Heap::new(),
cross_thread_upvals: std::collections::HashMap::new(),
suspended_parent_stacks: Vec::new(),
suspended_parent_open_upvals: Vec::new(),
};
let global_rc = Rc::new(RefCell::new(global));
let initial_marked = initial_white;
let mut main_thread = LuaState {
status: LuaStatus::Ok as u8,
allowhook: true,
nci: 0,
top: StackIdx(0),
stack_last: StackIdx(0),
stack: Vec::new(),
ci: CallInfoIdx(0),
call_info: Vec::new(),
openupval: Vec::new(),
tbclist: Vec::new(),
global: global_rc.clone(),
hook: None,
hookmask: 0,
basehookcount: 0,
hookcount: 0,
errfunc: 0,
nCcalls: 0,
oldpc: 0,
marked: initial_marked,
cached_thread_id: 0,
};
preinit_thread(&mut main_thread, global_rc.clone());
main_thread.inc_nny();
match lua_open(&mut main_thread) {
Ok(()) => {}
Err(_) => {
close_state(&mut main_thread);
return None;
}
}
Some(main_thread)
}
pub fn close(mut state: LuaState) {
close_state(&mut state);
}
pub(crate) fn warning(state: &mut LuaState, msg: &[u8], to_cont: bool) {
let has_warnf = state.global().warnf.is_some();
if has_warnf {
let mut warnf = state.global_mut().warnf.take();
if let Some(ref mut f) = warnf {
f(msg, to_cont);
}
state.global_mut().warnf = warnf;
}
}
pub(crate) fn warn_error(state: &mut LuaState, where_: &[u8]) {
let top_idx = state.top.0.saturating_sub(1) as usize;
let errobj = state.stack.get(top_idx).map(|sv| sv.val.clone()).unwrap_or(LuaValue::Nil);
let msg: Vec<u8> = if let LuaValue::Str(ref s) = errobj {
s.as_bytes().to_vec()
} else {
b"error object is not a string".to_vec()
};
warning(state, b"error in ", true);
warning(state, where_, true);
warning(state, b" (", true);
warning(state, &msg, true);
warning(state, b")", false);
}