use super::R;
use crate::value::Value;
use crate::vm::{Args, Lua, LuaError, Tm};
pub(super) fn open(lua: &mut Lua) {
let t = lua.lib_table("table");
let fns: [(&str, &'static str, crate::vm::NativeFn); 7] = [
("insert", "table.insert", insert),
("remove", "table.remove", remove),
("move", "table.move", move_),
("concat", "table.concat", concat),
("unpack", "table.unpack", unpack),
("pack", "table.pack", pack),
("sort", "table.sort", sort),
];
for (n, q, f) in fns {
lua.set_fn(t, n, q, f);
}
for (i, n) in IN_PLACE.iter().enumerate() {
let f = lua.new_str(n.as_bytes()).map_or(Value::Nil, |k| lua.raw_get(t, k));
if let Some(slot) = lua.names.table_fast.get_mut(i) {
*slot = f.as_func();
}
}
lua.names.table_mask = crate::fast::math_mask(&lua.names.table_fast);
lua.ctx_epoch += 1;
}
pub(crate) const IN_PLACE: [&str; 2] = ["insert", "remove"];
#[inline(never)]
pub(crate) fn in_place(heap: (&mut crate::heap::Slab<crate::table::Table>, crate::heap::Phase, &mut Vec<crate::heap::Gray>, u32), op: usize, args: &[Value], fuel: i64) -> Option<(Value, usize, i64)> {
let (tables, phase, grayagain, part) = heap;
let (&Value::Table(tr), want_args) = (args.first()?, if op == 0 { 2 } else { 1 }) else { return None };
if args.len() != want_args || tr.idx == part {
return None;
}
let tab = tables.get_mut(tr.idx)?;
if tab.meta.is_some() || tab.resized >= 16 {
return None;
}
let n = tab.border();
let probes = i64::try_from(crate::table::take_probes()).unwrap_or(i64::MAX / 2);
if fuel <= probes + 1 {
return None;
}
let (pos, val) = match op {
0 => (n.wrapping_add(1), *args.get(1)?),
_ if n >= 1 => (n, Value::Nil),
_ => return None,
};
let u = tab.arr_index(pos)?;
let slot = tab.arr.get_mut(u)?;
let old = std::mem::replace(slot, val);
tab.absent.set(0);
if val.is_collectable()
&& phase == crate::heap::Phase::Propagate
&& let Some(c) = tables.color.get_mut(tr.idx as usize)
&& *c == crate::heap::BLACK
{
*c = crate::heap::GRAY;
grayagain.push(crate::heap::Gray::Table(tr.idx));
}
Some(if op == 0 { (Value::Nil, 0, probes) } else { (old, 1, probes) })
}
fn check_tab(lua: &Lua, a: Args, i: usize, need: &[Tm]) -> Result<Value, LuaError> {
let v = lua.arg(a, i);
if let Value::Table(_) = v {
return Ok(v);
}
if lua.metatable(v).is_some() && need.iter().all(|&e| !lua.tm(v, e).is_nil()) {
return Ok(v);
}
Err(lua.type_error(a, i, "table"))
}
const RWL: &[Tm] = &[Tm::Index, Tm::NewIndex, Tm::Len];
fn geti(lua: &mut Lua, t: Value, i: i64) -> Result<Value, LuaError> {
let v = geti_used_now(lua, t, i)?;
lua.hold(v);
Ok(v)
}
#[inline]
fn geti_used_now(lua: &mut Lua, t: Value, i: i64) -> Result<Value, LuaError> {
let v = match t {
Value::Table(tr) if lua.metatable(t).is_none() => {
if let Some(v) = lua.heap.tables.get(tr.idx).and_then(|tab| in_array(tab, i)) {
return Ok(v);
}
lua.raw_get(tr, Value::Int(i))
}
_ => lua.index_nested(t, Value::Int(i))?,
};
lua.charge_probes()?;
Ok(v)
}
#[inline]
fn in_array(tab: &crate::table::Table, i: i64) -> Option<Value> {
if (i as u64).wrapping_sub(1) < tab.arr.live_len() as u64 { Some(tab.get_int_inline(i)) } else { None }
}
fn seti(lua: &mut Lua, t: Value, i: i64, v: Value) -> Result<(), LuaError> {
match t {
Value::Table(tr) if lua.metatable(t).is_none() => lua.raw_set(tr, Value::Int(i), v),
_ => lua.set_index_nested(t, Value::Int(i), v),
}?;
lua.charge_probes()
}
#[inline(always)]
fn copy_elem(lua: &mut Lua, src: Value, from: i64, dst: Value, to: i64) -> Result<(), LuaError> {
if let (Value::Table(sr), Value::Table(dr)) = (src, dst)
&& dr.idx != lua.heap.part
&& lua.metatable(src).is_none()
&& lua.metatable(dst).is_none()
&& let Some(v) = lua.heap.tables.get(sr.idx).and_then(|tab| in_array(tab, from))
{
let heap = &mut lua.heap;
let written = match heap.tables.get_mut(dr.idx) {
Some(tab) => {
tab.absent.set(0);
match tab.arr_index(to).and_then(|u| tab.arr.get_mut(u)) {
Some(slot) => {
*slot = v;
true
}
None => false,
}
}
None => false,
};
if !written {
return seti(lua, dst, to, v);
}
if v.is_collectable() {
lua.heap.barrier_table(dr);
}
let heap = &mut lua.heap;
let resized = heap.tables.get_mut(dr.idx).map_or(0, |tab| tab.take_resized(&heap.mem));
let probes = crate::table::take_probes() + if resized >= 16 { resized as u64 / 16 } else { 0 };
return if probes == 0 { Ok(()) } else { lua.charge(probes) };
}
let v = geti(lua, src, from)?;
seti(lua, dst, to, v)
}
fn getn(lua: &mut Lua, t: Value) -> Result<i64, LuaError> {
match t {
Value::Table(tr) if lua.metatable(t).is_none() => Ok(lua.raw_len(tr)),
_ => {
let n = lua.len_nested(t)?;
lua.tointeger(n).ok_or_else(|| LuaError::msg("object length is not an integer"))
}
}
}
fn insert(lua: &mut Lua, a: Args) -> R {
let t = check_tab(lua, a, 0, RWL)?;
let e = getn(lua, t)?.wrapping_add(1);
match a.n {
2 => {
let v = lua.arg(a, 1);
seti(lua, t, e, v)?;
}
3 => {
let pos = lua.check_int(a, 1)?;
if (pos as u64).wrapping_sub(1) >= e as u64 {
return Err(LuaError::Arg(2, "position out of bounds".into()));
}
let mut i = e;
while i > pos {
lua.charge(1)?;
copy_elem(lua, t, i - 1, t, i)?;
i -= 1;
}
let v = lua.arg(a, 2);
seti(lua, t, pos, v)?;
}
_ => return Err(LuaError::msg("wrong number of arguments to 'insert'")),
}
lua.ret(&[])
}
fn remove(lua: &mut Lua, a: Args) -> R {
let t = check_tab(lua, a, 0, RWL)?;
let size = getn(lua, t)?;
let mut pos = lua.opt_int(a, 1, size)?;
if a.n >= 2 && pos != size && (pos as u64).wrapping_sub(1) > size as u64 {
return Err(LuaError::Arg(2, "position out of bounds".into()));
}
let v = geti(lua, t, pos)?;
while pos < size {
lua.charge(1)?;
copy_elem(lua, t, pos + 1, t, pos)?;
pos += 1;
}
seti(lua, t, pos, Value::Nil)?;
lua.ret1(v)
}
fn move_(lua: &mut Lua, a: Args) -> R {
let f = lua.check_int(a, 1)?;
let e = lua.check_int(a, 2)?;
let t = lua.check_int(a, 3)?;
let a1 = check_tab(lua, a, 0, &[Tm::Index])?;
let to_self = lua.arg(a, 4).is_nil();
let a2 = check_tab(lua, a, if to_self { 0 } else { 4 }, &[Tm::NewIndex])?;
if e >= f {
if !(f > 0 || e < i64::MAX + f) {
return Err(LuaError::Arg(3, "too many elements to move".into()));
}
if t > i64::MAX - (e - f) {
return Err(LuaError::Arg(4, "destination wrap around".into()));
}
let n = e - f;
let forward = t > e || t <= f || (!to_self && !lua.eq_nested(a1, a2)?);
for k in 0..=n {
let i = if forward { k } else { n - k };
lua.charge(1)?;
copy_elem(lua, a1, f + i, a2, t + i)?;
}
}
lua.ret1(a2)
}
fn concat(lua: &mut Lua, a: Args) -> R {
let t = check_tab(lua, a, 0, &[Tm::Index, Tm::Len])?;
let sep = match lua.opt_str(a, 1)? {
Some(s) => lua.bytes(s).to_vec(),
None => Vec::new(),
};
let i = lua.opt_int(a, 2, 1)?;
let last = if lua.arg(a, 3).is_nil() { getn(lua, t)? } else { lua.check_int(a, 3)? };
let mut out = super::OutBuf::new(64);
concat_into(lua, t, &sep, i, last, &mut out)?;
let b = out.into_vec(lua);
lua.ret_owned(b)
}
fn concat_into(lua: &mut Lua, t: Value, sep: &[u8], i: i64, last: i64, out: &mut super::OutBuf) -> Result<(), LuaError> {
let mut k = i;
while k <= last {
lua.charge(1)?;
let v = geti_used_now(lua, t, k)?;
if let Value::Num(_) = v {
lua.charge(crate::vm::FLOAT_TEXT_FUEL)?;
}
match v {
Value::Str(s) if lua.bytes(s).len() > super::COPY_PIECE => {
let len = lua.bytes(s).len();
let mut at = 0;
while at < len {
let end = (at + super::COPY_PIECE).min(len);
out.extend_from_slice(lua.bytes(s).get(at..end).unwrap_or_default());
out.pay(lua)?;
at = end;
if at < len {
lua.check_time()?;
}
}
}
Value::Str(_) | Value::Int(_) | Value::Num(_) => lua.append_stringable(&mut **out, v),
other => {
return Err(LuaError::msg(format!(
"invalid value ({}) at index {k} in table for 'concat'",
other.type_name()
)));
}
}
if k != last {
out.extend_from_slice(sep);
}
out.pay(lua)?;
lua.check_len(out.len())?;
if k == i64::MAX {
break;
}
k += 1;
}
Ok(())
}
fn unpack(lua: &mut Lua, a: Args) -> R {
let t = lua.arg(a, 0);
let i = lua.opt_int(a, 1, 1)?;
let e = if lua.arg(a, 2).is_nil() { getn(lua, t)? } else { lua.check_int(a, 2)? };
if i > e {
return lua.ret(&[]);
}
let n = (e as i128 - i as i128 + 1) as u128;
if n >= (lua.limits.stack_slots as u128) / 2 || n >= i32::MAX as u128 {
return Err(LuaError::msg("too many results to unpack"));
}
lua.charge(n as u64)?;
lua.charge_mem(n as usize * 16)?;
lua.heap.mem.release(n as usize * 16);
if let Value::Table(tr) = t
&& lua.metatable(t).is_none()
{
lua.rets.clear();
lua.rets.reserve_exact(n as usize);
let mut k = i;
loop {
let v = match lua.heap.tables.get(tr.idx).and_then(|tab| in_array(tab, k)) {
Some(v) => v,
None => {
let v = lua.raw_get(tr, Value::Int(k));
lua.charge_probes()?;
v
}
};
lua.rets.push(v);
if k == e {
break;
}
k += 1;
if lua.rets.len().is_multiple_of(super::NATIVE_CHUNK) {
lua.check_time()?;
}
}
return Ok(crate::vm::Flow::Return);
}
let mut hold = super::MemHold::default();
hold.cover(lua, n as usize * 16)?;
let mut out = Vec::with_capacity(n as usize);
let mut k = i;
loop {
out.push(geti(lua, t, k)?);
if k == e {
break;
}
k += 1;
if out.len() % super::NATIVE_CHUNK == 0 {
lua.check_time()?;
}
}
hold.give_back(lua);
lua.ret(&out)
}
fn pack(lua: &mut Lua, a: Args) -> R {
let t = lua.new_table(a.n, 1)?;
lua.charge(a.n as u64)?;
let heap = &mut lua.heap;
if let (Some(tab), Some(vals)) = (heap.tables.get_mut(t.idx), lua.cur.stack.get(a.base..a.base + a.n)) {
tab.set_list(0, vals, &mut heap.mem).map_err(|_| LuaError::Limit(crate::LimitKind::Memory))?;
}
let n = Value::Str(lua.names.n);
lua.raw_set(t, n, Value::Int(a.n as i64))?;
lua.ret1(Value::Table(t))
}
struct Sorter {
comp: Value,
}
impl Sorter {
fn less(&self, lua: &mut Lua, x: Value, y: Value) -> Result<bool, LuaError> {
lua.charge(1)?;
if self.comp.is_nil() {
lua.less_than(x, y)
} else {
Ok(lua.call_nested1(self.comp, &[x, y])?.truthy())
}
}
}
struct InPlace {
t: Value,
roots: usize,
}
impl InPlace {
#[inline]
fn get(&mut self, lua: &mut Lua, i: usize, slot: usize) -> Result<Value, LuaError> {
let mut in_arr = false;
let plain = match self.t {
Value::Table(tr) => match lua.heap.tables.get(tr.idx) {
Some(tab) if tab.meta.is_none() => {
in_arr = (i as u64).wrapping_sub(1) < tab.arr.live_len() as u64;
Some(tab.get_int_inline(i as i64))
}
Some(_) => None,
None => Some(Value::Nil),
},
_ => None,
};
let v = match plain {
Some(v) => v,
None => lua.index_nested(self.t, Value::Int(i as i64))?,
};
if let Some(r) = lua.scratch.get_mut(self.roots + slot) {
*r = v;
}
if !in_arr {
lua.charge_probes()?;
}
Ok(v)
}
fn set2(&mut self, lua: &mut Lua, i: usize, vi: Value, j: usize, vj: Value) -> Result<(), LuaError> {
for (k, v) in [(i, vi), (j, vj)] {
match self.t {
Value::Table(tr) if lua.metatable(self.t).is_none() => {
let heap = &mut lua.heap;
let done = match heap.tables.get_mut(tr.idx) {
Some(tab) if tr.idx != heap.part => match tab.arr_index(k as i64) {
Some(u) => {
tab.absent.set(0);
match tab.arr.get_mut(u) {
Some(s) => {
*s = v;
true
}
None => false,
}
}
None => false,
},
_ => false,
};
if !done {
lua.raw_set(tr, Value::Int(k as i64), v)?;
lua.charge_probes()?;
} else if v.is_collectable() {
lua.heap.barrier_table(tr);
}
}
t => {
lua.set_index_nested(t, Value::Int(k as i64), v)?;
lua.charge_probes()?;
}
}
}
Ok(())
}
}
fn auxsort(lua: &mut Lua, s: &Sorter, v: &mut InPlace, lo: usize, up: usize, mut rnd: u64) -> Result<(), LuaError> {
let bad = || LuaError::msg("invalid order function for sorting");
let (mut lo, mut up) = (lo, up);
while lo < up {
let (a_lo, a_up) = (v.get(lua, lo, 1)?, v.get(lua, up, 2)?);
if s.less(lua, a_up, a_lo)? {
v.set2(lua, lo, a_up, up, a_lo)?;
}
if up - lo == 1 {
break;
}
let mut p = if up - lo < 100 || rnd == 0 {
(lo + up) / 2
} else {
let r4 = (up - lo) / 4;
(rnd as usize) % (r4 * 2) + lo + r4
};
let (a_p, a_lo) = (v.get(lua, p, 3)?, v.get(lua, lo, 1)?);
if s.less(lua, a_p, a_lo)? {
v.set2(lua, p, a_lo, lo, a_p)?;
} else {
let a_up = v.get(lua, up, 2)?;
if s.less(lua, a_up, a_p)? {
v.set2(lua, p, a_up, up, a_p)?;
}
}
if up - lo == 2 {
break;
}
let pivot = v.get(lua, p, 0)?;
let a_up1 = v.get(lua, up - 1, 1)?;
v.set2(lua, p, a_up1, up - 1, pivot)?;
let mut i = lo;
let mut j = up - 1;
loop {
let ai = loop {
i += 1;
let ai = v.get(lua, i, 1)?;
if !s.less(lua, ai, pivot)? {
break ai;
}
if i == up - 1 {
return Err(bad());
}
};
let aj = loop {
j -= 1;
let aj = v.get(lua, j, 2)?;
if !s.less(lua, pivot, aj)? {
break aj;
}
if j < i {
return Err(bad());
}
};
if j < i {
v.set2(lua, up - 1, ai, i, pivot)?;
break;
}
v.set2(lua, i, aj, j, ai)?;
}
p = i;
let n;
if p - lo < up - p {
if p > lo {
auxsort(lua, s, v, lo, p - 1, rnd)?;
}
n = p - lo;
lo = p + 1;
} else {
auxsort(lua, s, v, p + 1, up, rnd)?;
n = up - p;
if p == 0 {
break;
}
up = p - 1;
}
if up > lo && (up - lo) / 128 > n {
rnd = lua.next_random() | 1;
}
}
Ok(())
}
fn sort(lua: &mut Lua, a: Args) -> R {
let t = check_tab(lua, a, 0, RWL)?;
let n = getn(lua, t)?;
if n > 1 {
if n >= i64::from(i32::MAX) {
return Err(LuaError::Arg(1, "array too big".into()));
}
let comp = lua.arg(a, 1);
if !comp.is_nil() && !matches!(comp, Value::Func(_)) {
return Err(lua.type_error(a, 1, "function"));
}
lua.charge(n as u64)?;
let roots = lua.scratch.len();
lua.scratch.extend([Value::Nil; 4]);
lua.scratch_floor = lua.scratch.len();
auxsort(lua, &Sorter { comp }, &mut InPlace { t, roots }, 1, n as usize, 0)?;
}
lua.ret(&[])
}