lua_vm/object.rs
1//! Generic functions over Lua objects.
2//!
3//! Ported from `reference/lua-5.4.7/src/lobject.c` (602 lines, ~20 functions).
4
5#[allow(unused_imports)]
6use crate::prelude::*;
7use crate::state::LuaState;
8use lua_types::arith::ArithOp;
9use lua_types::error::LuaError;
10use lua_types::{GcRef, LuaString, LuaValue, StackIdx};
11
12// ──────────────────────────────────────────────────────────────────────────
13// Module-level constants
14// ──────────────────────────────────────────────────────────────────────────
15
16/// Maximum number of significant hex digits to read (avoids overflow even for
17/// single-precision floats).
18const MAX_SIG_DIG: usize = 30;
19
20/// Maximum size of a number-to-string conversion buffer.
21/// Accommodates both `%.14g` float formatting and `%lld` integer formatting.
22pub const MAX_NUMBER_2_STR: usize = 44;
23
24/// Buffer size (bytes) for UTF-8 encoding; encoded backwards into this buffer.
25pub const UTF8_BUF_SZ: usize = 8;
26
27/// Maximum length of a chunk source identifier in error messages.
28/// Matches `LUA_IDSIZE` in upstream `luaconf.h`.
29pub const LUA_ID_SIZE: usize = 60;
30
31/// Internal buffer size for `push_vfstring`.
32const BUF_VFS: usize = LUA_ID_SIZE + MAX_NUMBER_2_STR + 95;
33
34/// Truncation marker for long chunk source strings.
35const RETS: &[u8] = b"...";
36
37/// Prefix for [string "..."] chunk identifiers.
38const PRE: &[u8] = b"[string \"";
39
40/// Suffix for [string "..."] chunk identifiers.
41const POS: &[u8] = b"\"]";
42
43// ──────────────────────────────────────────────────────────────────────────
44// ceil_log2
45// ──────────────────────────────────────────────────────────────────────────
46
47/// Computes `ceil(log2(x))`; returns the minimum `k` such that `2^k >= x`.
48///
49pub fn ceil_log2(x: u32) -> i32 {
50 static LOG_2: [u8; 256] = [
51 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
52 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
53 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
54 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
55 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
56 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
57 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
58 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
59 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
60 ];
61 let mut l: i32 = 0;
62 let mut x = x.wrapping_sub(1);
63 while x >= 256 {
64 l += 8;
65 x >>= 8;
66 }
67 l + LOG_2[x as usize] as i32
68}
69
70// ──────────────────────────────────────────────────────────────────────────
71// Integer arithmetic dispatcher
72// ──────────────────────────────────────────────────────────────────────────
73
74/// Performs integer arithmetic for opcode `op` on operands `v1`, `v2`.
75/// Returns `Result` because floor-mod and floor-div can raise on zero divisor.
76///
77fn int_arith(state: &mut LuaState, op: ArithOp, v1: i64, v2: i64) -> Result<i64, LuaError> {
78 match op {
79 ArithOp::Add => Ok((v1 as u64).wrapping_add(v2 as u64) as i64),
80 ArithOp::Sub => Ok((v1 as u64).wrapping_sub(v2 as u64) as i64),
81 ArithOp::Mul => Ok((v1 as u64).wrapping_mul(v2 as u64) as i64),
82 ArithOp::Mod => crate::vm::int_floor_mod(state, v1, v2),
83 ArithOp::Idiv => crate::vm::int_floor_div(state, v1, v2),
84 ArithOp::Band => Ok(v1 & v2),
85 ArithOp::Bor => Ok(v1 | v2),
86 ArithOp::Bxor => Ok(v1 ^ v2),
87 ArithOp::Shl => Ok(crate::vm::shiftl(v1, v2)),
88 ArithOp::Shr => Ok(crate::vm::shiftl(v1, -v2)),
89 ArithOp::Unm => Ok((0u64).wrapping_sub(v1 as u64) as i64),
90 // l_castS2U(0) → 0u64, ~0u64 = 0xFFFFFFFFFFFFFFFF = !0u64
91 ArithOp::Bnot => Ok((!0u64 ^ v1 as u64) as i64),
92 _ => {
93 debug_assert!(false, "int_arith called with non-integer op");
94 Ok(0)
95 }
96 }
97}
98
99// ──────────────────────────────────────────────────────────────────────────
100// Float arithmetic dispatcher
101// ──────────────────────────────────────────────────────────────────────────
102
103/// Performs float arithmetic for opcode `op` on operands `v1`, `v2`.
104/// Returns `Result` because float floor-mod can raise on zero divisor.
105///
106fn float_arith(state: &mut LuaState, op: ArithOp, v1: f64, v2: f64) -> Result<f64, LuaError> {
107 match op {
108 ArithOp::Add => Ok(v1 + v2),
109 ArithOp::Sub => Ok(v1 - v2),
110 ArithOp::Mul => Ok(v1 * v2),
111 ArithOp::Div => Ok(v1 / v2),
112 ArithOp::Pow => Ok(if v2 == 2.0 { v1 * v1 } else { v1.powf(v2) }),
113 ArithOp::Idiv => Ok((v1 / v2).floor()),
114 ArithOp::Unm => Ok(-v1),
115 ArithOp::Mod => crate::vm::float_floor_mod(state, v1, v2),
116 _ => {
117 debug_assert!(false, "float_arith called with non-float op");
118 Ok(0.0)
119 }
120 }
121}
122
123// ──────────────────────────────────────────────────────────────────────────
124// Raw arithmetic (no metamethods)
125// ──────────────────────────────────────────────────────────────────────────
126
127/// Attempts raw (no-metamethod) arithmetic on two Lua values.
128/// Writes the result to `res` and returns `true` on success, `false` if the
129/// operation cannot be performed with the given types (caller should invoke
130/// a metamethod instead).
131///
132pub fn raw_arith(
133 state: &mut LuaState,
134 op: ArithOp,
135 p1: &LuaValue,
136 p2: &LuaValue,
137 res: &mut LuaValue,
138) -> Result<bool, LuaError> {
139 match op {
140 // case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: — integer-only ops
141 ArithOp::Band
142 | ArithOp::Bor
143 | ArithOp::Bxor
144 | ArithOp::Shl
145 | ArithOp::Shr
146 | ArithOp::Bnot => {
147 // setivalue(res, intarith(L, op, i1, i2)); return 1; }
148 // else return 0;
149 if let (Some(i1), Some(i2)) = (p1.to_integer_no_strconv(), p2.to_integer_no_strconv()) {
150 *res = LuaValue::Int(int_arith(state, op, i1, i2)?);
151 Ok(true)
152 } else {
153 Ok(false)
154 }
155 }
156
157 ArithOp::Div | ArithOp::Pow => {
158 // setfltvalue(res, numarith(L, op, n1, n2)); return 1; }
159 // else return 0;
160 if let (Some(n1), Some(n2)) = (p1.to_number_no_strconv(), p2.to_number_no_strconv()) {
161 *res = LuaValue::Float(float_arith(state, op, n1, n2)?);
162 Ok(true)
163 } else {
164 Ok(false)
165 }
166 }
167
168 _ => {
169 // setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); return 1; }
170 if let (LuaValue::Int(i1), LuaValue::Int(i2)) = (p1, p2) {
171 *res = LuaValue::Int(int_arith(state, op, *i1, *i2)?);
172 return Ok(true);
173 }
174 if let (Some(n1), Some(n2)) = (p1.to_number_no_strconv(), p2.to_number_no_strconv()) {
175 *res = LuaValue::Float(float_arith(state, op, n1, n2)?);
176 Ok(true)
177 } else {
178 Ok(false)
179 }
180 }
181 }
182}
183
184// ──────────────────────────────────────────────────────────────────────────
185// Arithmetic (with metamethod fallback)
186// ──────────────────────────────────────────────────────────────────────────
187
188/// Performs arithmetic for opcode `op`, writing the result to the stack slot
189/// `res`. Falls back to a binary tag-method if raw arithmetic is not possible.
190///
191pub fn arith(
192 state: &mut LuaState,
193 op: ArithOp,
194 p1: &LuaValue,
195 p2: &LuaValue,
196 res: StackIdx,
197) -> Result<(), LuaError> {
198 // luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); }
199 //
200 // PORT NOTE: raw_arith writes to a local `temp` first; we then set the stack
201 // slot. This avoids holding a &mut borrow into the stack across try_bin_tm,
202 // which would violate the StackIdx rule (PORTING.md §2 #5).
203 let mut temp = LuaValue::Nil;
204 if raw_arith(state, op, p1, p2, &mut temp)? {
205 state.set_at(res, temp);
206 } else {
207 let _ = (p1, p2);
208 return Err(LuaError::runtime(format_args!(
209 "arithmetic metamethod dispatch not yet implemented for opcode {:?}",
210 op
211 )));
212 }
213 Ok(())
214}
215
216// ──────────────────────────────────────────────────────────────────────────
217// hex_value
218// ──────────────────────────────────────────────────────────────────────────
219
220/// Converts a hexadecimal digit byte to its numeric value (0–15).
221/// Caller must ensure `c` is a valid hex digit.
222///
223pub fn hex_value(c: u8) -> u8 {
224 if c.is_ascii_digit() {
225 c - b'0'
226 } else {
227 c.to_ascii_lowercase() - b'a' + 10
228 }
229}
230
231// ──────────────────────────────────────────────────────────────────────────
232// Sign helper
233// ──────────────────────────────────────────────────────────────────────────
234
235/// Checks for and consumes a leading sign byte (`+` or `-`) in `s` starting
236/// at `*idx`. Returns `true` if a minus sign was consumed.
237///
238fn is_neg(s: &[u8], idx: &mut usize) -> bool {
239 // else if (**s == '+') (*s)++;
240 // return 0;
241 if *idx < s.len() && s[*idx] == b'-' {
242 *idx += 1;
243 return true;
244 }
245 if *idx < s.len() && s[*idx] == b'+' {
246 *idx += 1;
247 }
248 false
249}
250
251// ──────────────────────────────────────────────────────────────────────────
252// Hexadecimal float parser
253// ──────────────────────────────────────────────────────────────────────────
254
255/// Converts a hexadecimal float literal (C99 `0x…p…` form) in `s` to `f64`.
256/// Returns `Some((value, end_index))` on success, `None` on failure.
257///
258/// (conditionally compiled when the platform doesn't provide it)
259fn str_x2number(s: &[u8]) -> Option<(f64, usize)> {
260 let mut idx = 0;
261 while idx < s.len() && s[idx].is_ascii_whitespace() {
262 idx += 1;
263 }
264 let neg = is_neg(s, &mut idx);
265 if idx + 1 >= s.len() || s[idx] != b'0' || (s[idx + 1] != b'x' && s[idx + 1] != b'X') {
266 return None;
267 }
268 idx += 2;
269 let mut r: f64 = 0.0;
270 let mut sigdig: usize = 0;
271 let mut nosigdig: usize = 0;
272 let mut e: i32 = 0;
273 let mut hasdot = false;
274
275 // PORT NOTE: `lua_getlocaledecpoint()` returns the locale decimal separator.
276 // Rust has no locale; we always treat '.' as the separator here.
277 let dot = b'.';
278
279 loop {
280 if idx >= s.len() {
281 break;
282 }
283 let ch = s[idx];
284 if ch == dot {
285 if hasdot {
286 break;
287 }
288 hasdot = true;
289 } else if ch.is_ascii_hexdigit() {
290 // else if (++sigdig <= MAXSIGDIG) r = (r * 16.0) + luaO_hexavalue(*s);
291 // else e++;
292 // if (hasdot) e--;
293 if sigdig == 0 && ch == b'0' {
294 nosigdig += 1;
295 } else if {
296 sigdig += 1;
297 sigdig <= MAX_SIG_DIG
298 } {
299 r = r * 16.0 + hex_value(ch) as f64;
300 } else {
301 e += 1;
302 }
303 if hasdot {
304 e -= 1;
305 }
306 } else {
307 break;
308 }
309 idx += 1;
310 }
311
312 if nosigdig + sigdig == 0 {
313 return None;
314 }
315 e *= 4;
316
317 if idx < s.len() && (s[idx] == b'p' || s[idx] == b'P') {
318 idx += 1;
319 let neg1 = is_neg(s, &mut idx);
320 if idx >= s.len() || !s[idx].is_ascii_digit() {
321 return None;
322 }
323 let mut exp1: i32 = 0;
324 while idx < s.len() && s[idx].is_ascii_digit() {
325 exp1 = exp1 * 10 + (s[idx] - b'0') as i32;
326 idx += 1;
327 }
328 if neg1 {
329 exp1 = -exp1;
330 }
331 e += exp1;
332 }
333 let result = if neg { -r } else { r };
334 Some((result * (2.0f64).powi(e), idx))
335}
336
337// ──────────────────────────────────────────────────────────────────────────
338// String-to-float helpers
339// ──────────────────────────────────────────────────────────────────────────
340
341/// Inner conversion: tries to parse the bytes `s` as a float using the given
342/// `mode` (`b'x'` for hex, anything else for decimal).
343/// Returns `Some((value, end_index))` or `None`.
344///
345fn str2dloc(s: &[u8], mode: u8) -> Option<(f64, usize)> {
346 let (result, end) = if mode == b'x' {
347 str_x2number(s)?
348 } else {
349 // PORT NOTE: from_utf8 used here because numeric string literals are
350 // guaranteed to be ASCII (a strict subset of UTF-8).
351 // TODO(port): replace with a bytes-native float parser in Phase B
352 // (e.g., the `fast-float` crate) to satisfy the from_utf8 ban fully.
353 let text = core::str::from_utf8(s).ok()?;
354 let trimmed = text.trim();
355 // Reject "inf", "infinity", "nan" — Lua does not accept these.
356 let lower = trimmed.to_ascii_lowercase();
357 if lower.starts_with("inf") || lower.starts_with("nan") {
358 return None;
359 }
360 let f: f64 = trimmed.parse().ok()?;
361 (f, s.len()) // strtod parses as many chars as possible; we consumed all
362 };
363 if end == 0 {
364 return None;
365 }
366 let mut end2 = end;
367 while end2 < s.len() && s[end2].is_ascii_whitespace() {
368 end2 += 1;
369 }
370 if end2 == s.len() {
371 Some((result, end2))
372 } else {
373 None
374 }
375}
376
377/// Converts bytes `s` to a Lua float value.
378/// Returns `Some((value, end_index))` on success, `None` on failure.
379///
380fn str2d(s: &[u8]) -> Option<(f64, usize)> {
381 // int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
382 let pmode = s
383 .iter()
384 .position(|&b| b == b'.' || b == b'x' || b == b'X' || b == b'n' || b == b'N');
385 let mode = pmode.map(|i| s[i].to_ascii_lowercase()).unwrap_or(0);
386
387 if mode == b'n' {
388 return None;
389 }
390
391 if let Some(result) = str2dloc(s, mode) {
392 return Some(result);
393 }
394
395 // PORT NOTE: Lua retries by replacing '.' with the locale decimal separator.
396 // Rust has no locale support; we skip this retry path and always use '.'.
397 // TODO(port): add locale retry if locale-aware float parsing is needed.
398
399 None
400}
401
402// ──────────────────────────────────────────────────────────────────────────
403// String-to-integer helper
404// ──────────────────────────────────────────────────────────────────────────
405
406/// Converts bytes `s` to a Lua integer value (decimal or `0x` hex).
407/// Returns `Some(value)` on success (the entire byte slice was consumed),
408/// `None` on failure or overflow.
409///
410fn str2int(s: &[u8]) -> Option<i64> {
411 let mut idx = 0;
412 while idx < s.len() && s[idx].is_ascii_whitespace() {
413 idx += 1;
414 }
415 let neg = is_neg(s, &mut idx);
416
417 let mut a: u64 = 0;
418 let mut empty = true;
419
420 if idx + 1 < s.len() && s[idx] == b'0' && (s[idx + 1] == b'x' || s[idx + 1] == b'X') {
421 idx += 2;
422 while idx < s.len() && s[idx].is_ascii_hexdigit() {
423 a = a.wrapping_mul(16).wrapping_add(hex_value(s[idx]) as u64);
424 empty = false;
425 idx += 1;
426 }
427 } else {
428 // MAXBY10 = cast(lua_Unsigned, LUA_MAXINTEGER / 10)
429 // MAXLASTD = cast_int(LUA_MAXINTEGER % 10)
430 // if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) return NULL;
431 const MAX_BY10: u64 = (i64::MAX / 10) as u64;
432 const MAX_LAST_D: u64 = (i64::MAX % 10) as u64;
433 while idx < s.len() && s[idx].is_ascii_digit() {
434 let d = (s[idx] - b'0') as u64;
435 if a >= MAX_BY10 && (a > MAX_BY10 || d > MAX_LAST_D + if neg { 1 } else { 0 }) {
436 return None; // overflow
437 }
438 a = a.wrapping_mul(10).wrapping_add(d);
439 empty = false;
440 idx += 1;
441 }
442 }
443
444 while idx < s.len() && s[idx].is_ascii_whitespace() {
445 idx += 1;
446 }
447 if empty || idx != s.len() {
448 return None;
449 }
450 let result = if neg {
451 (0u64).wrapping_sub(a) as i64
452 } else {
453 a as i64
454 };
455 Some(result)
456}
457
458// ──────────────────────────────────────────────────────────────────────────
459// str2num — main public string-to-number conversion
460// ──────────────────────────────────────────────────────────────────────────
461
462/// Tries to convert the byte string `s` to a Lua number (integer first, then
463/// float). Writes the result to `o` and returns `consumed_bytes + 1` on
464/// success (matching the C convention of including the null terminator in the
465/// count), or `0` on failure.
466///
467pub fn str2num(s: &[u8], o: &mut LuaValue) -> usize {
468 if let Some(i) = str2int(s) {
469 *o = LuaValue::Int(i);
470 return s.len() + 1; // entire string consumed; +1 for C null-terminator convention
471 }
472 if let Some((n, end)) = str2d(s) {
473 *o = LuaValue::Float(n);
474 return end + 1;
475 }
476 0
477}
478
479// ──────────────────────────────────────────────────────────────────────────
480// UTF-8 encoder
481// ──────────────────────────────────────────────────────────────────────────
482
483/// Encodes Unicode codepoint `x` as UTF-8 into `buff` (filled backwards from
484/// index `UTF8_BUF_SZ - 1`). Returns the number of bytes written.
485/// The valid bytes occupy `buff[UTF8_BUF_SZ - n .. UTF8_BUF_SZ]`.
486///
487pub fn utf8_esc(buff: &mut [u8; UTF8_BUF_SZ], x: u32) -> usize {
488 debug_assert!(x <= 0x7FFF_FFFF, "codepoint out of range");
489 let mut n: usize = 1;
490 if x < 0x80 {
491 buff[UTF8_BUF_SZ - 1] = x as u8;
492 } else {
493 let mut mfb: u32 = 0x3f;
494 let mut x = x;
495 loop {
496 buff[UTF8_BUF_SZ - n] = 0x80 | (x & 0x3f) as u8;
497 n += 1;
498 x >>= 6;
499 mfb >>= 1;
500 if x <= mfb {
501 break;
502 }
503 }
504 buff[UTF8_BUF_SZ - n] = ((!mfb << 1) | x) as u8;
505 }
506 n
507}
508
509// ──────────────────────────────────────────────────────────────────────────
510// Number → string conversion
511// ──────────────────────────────────────────────────────────────────────────
512
513/// Formats `f` as C's `printf("%.*g", precision, f)` would, returning the bytes.
514///
515/// PORT NOTE: Rust has no built-in `%g` format. This replicates the C99
516/// `%g` algorithm: pick scientific or fixed-point based on the value's
517/// exponent, strip trailing zeros, normalize the exponent to `e[+-]NN` with at
518/// least two digits (matching C's output). The precision is the float
519/// `tostring` precision: 14 for Lua 5.1-5.4 (`%.14g`), 17 for 5.5
520/// (`LUA_NUMBER_FMT_N` = `%.17g`, the shortest round-trip form).
521fn fmt_g(f: f64, precision: i32) -> Vec<u8> {
522 if f.is_nan() {
523 return b"nan".to_vec();
524 }
525 if f.is_infinite() {
526 return if f > 0.0 {
527 b"inf".to_vec()
528 } else {
529 b"-inf".to_vec()
530 };
531 }
532 if f == 0.0 {
533 return if f.is_sign_negative() {
534 b"-0".to_vec()
535 } else {
536 b"0".to_vec()
537 };
538 }
539
540 let abs = f.abs();
541 let exp = abs.log10().floor() as i32;
542
543 let s = if exp < -4 || exp >= precision {
544 let mantissa_decimals = (precision - 1) as usize;
545 let raw = format!("{:.*e}", mantissa_decimals, f);
546 let e_idx = raw
547 .find('e')
548 .expect("Rust scientific format always contains 'e'");
549 let mantissa = strip_fixed_trailing_zeros(&raw[..e_idx]);
550 let exp_num: i32 = raw[e_idx + 1..]
551 .parse()
552 .expect("Rust formats integer exponents");
553 let sign = if exp_num < 0 { '-' } else { '+' };
554 let abs_exp = exp_num.abs();
555 if abs_exp < 10 {
556 format!("{}e{}0{}", mantissa, sign, abs_exp)
557 } else {
558 format!("{}e{}{}", mantissa, sign, abs_exp)
559 }
560 } else {
561 let decimals = (precision - 1 - exp).max(0) as usize;
562 let raw = format!("{:.*}", decimals, f);
563 strip_fixed_trailing_zeros(&raw)
564 };
565
566 s.into_bytes()
567}
568
569/// Lua 5.5 float `tostring` (`tostringbuffFloat`): format with `%.15g`
570/// (`LUA_NUMBER_FMT`), read it back, and only if that doesn't round-trip to the
571/// same double reformat with `%.17g` (`LUA_NUMBER_FMT_N`). This yields the
572/// shortest of the two that is exact — e.g. `3.14`/`1e+16` stay short while
573/// `1/3` needs the 17-digit form. Pre-5.5 uses plain `%.14g` (no readback).
574fn fmt_float_55(f: f64) -> Vec<u8> {
575 let short = fmt_g(f, 15);
576 if f.is_finite() {
577 let round_trips = std::str::from_utf8(&short)
578 .ok()
579 .and_then(|t| t.parse::<f64>().ok())
580 .map_or(false, |back| back == f);
581 if !round_trips {
582 return fmt_g(f, 17);
583 }
584 }
585 short
586}
587
588fn strip_fixed_trailing_zeros(s: &str) -> String {
589 if !s.contains('.') {
590 return s.to_string();
591 }
592 let mut out = s.to_string();
593 while out.ends_with('0') {
594 out.pop();
595 }
596 if out.ends_with('.') {
597 out.pop();
598 }
599 out
600}
601
602/// Formats the numeric `LuaValue` `val` (must be Int or Float) into a byte
603/// buffer and returns it.
604///
605pub(crate) fn number_to_str_buf(val: &LuaValue, version: lua_types::LuaVersion) -> Vec<u8> {
606 use lua_types::LuaVersion;
607 debug_assert!(
608 matches!(val, LuaValue::Int(_) | LuaValue::Float(_)),
609 "number_to_str_buf: value is not a number"
610 );
611
612 match val {
613 LuaValue::Int(i) => {
614 // lua_integer2str → l_sprintf with LUA_INTEGER_FMT ("%lld")
615 // PORT NOTE: using Rust's default i64 Display formatting, which
616 // matches C's `%lld` for all values in [i64::MIN, i64::MAX].
617 let s = format!("{}", i);
618 s.into_bytes()
619 }
620 LuaValue::Float(f) => {
621 // 5.5: shortest round-trip; 5.1-5.4: %.14g.
622 let mut bytes = if version == LuaVersion::V55 {
623 fmt_float_55(*f)
624 } else {
625 fmt_g(*f, 14)
626 };
627
628 // 5.3+ append ".0" to an integer-valued float so it reads back as a
629 // float (the int/float distinction). 5.1/5.2 are float-only and
630 // have no such distinction, so they print `5`, not `5.0`.
631 let dual_model = !matches!(version, LuaVersion::V51 | LuaVersion::V52);
632 let looks_like_int = bytes.iter().all(|&b| b == b'-' || b.is_ascii_digit());
633 if dual_model && looks_like_int {
634 bytes.push(b'.');
635 bytes.push(b'0');
636 }
637 bytes
638 }
639 // Unreachable — guarded by debug_assert above.
640 _ => Vec::new(),
641 }
642}
643
644/// Largest byte length of a base-10 `i64` rendering: `-9223372036854775808`.
645const INT_STR_CAP: usize = 20;
646
647/// Render an `i64` into a fixed stack buffer in base 10, returning the filled
648/// suffix slice. Matches C's `lua_integer2str` (`l_sprintf` with `"%lld"`),
649/// which for every `i64` is the same as Rust's default `Display`, but writes
650/// into a caller-owned `[u8]` instead of heap-allocating a `Vec`/`String` — so
651/// the concat/coercion hot path interns straight from the stack with no heap
652/// temporary, mirroring `luaO_tostr` filling a stack `buff[]`.
653fn int_to_str_buf(i: i64, buf: &mut [u8; INT_STR_CAP]) -> &[u8] {
654 use std::io::Write;
655 let mut cursor = std::io::Cursor::new(&mut buf[..]);
656 write!(cursor, "{}", i).expect("i64 always fits in INT_STR_CAP bytes");
657 let len = cursor.position() as usize;
658 &buf[..len]
659}
660
661/// Converts a numeric `LuaValue` to an interned `LuaString`, returning a
662/// `GcRef<LuaString>` handle. Callers are responsible for updating the
663/// `LuaValue` (or stack slot) with `LuaValue::Str(s)`.
664///
665/// in place; in Rust we return the string because holding `&mut LuaValue`
666/// across a `state.intern_str` call would borrow `state` twice.
667///
668/// Integers stringify through a stack buffer so the common case (and the
669/// number-coercion arm of `OP_CONCAT`) allocates nothing beyond the interned
670/// string itself; floats stay on the existing formatting path, which produces
671/// the identical bytes.
672pub fn num_to_string(state: &mut LuaState, val: &LuaValue) -> Result<GcRef<LuaString>, LuaError> {
673 // int len = tostringbuff(obj, buff);
674 // setsvalue(L, obj, luaS_newlstr(L, buff, len));
675 match val {
676 LuaValue::Int(i) => {
677 let mut buf = [0u8; INT_STR_CAP];
678 let bytes = int_to_str_buf(*i, &mut buf);
679 state.intern_str(bytes)
680 }
681 _ => {
682 let version = state.global().lua_version;
683 let bytes = number_to_str_buf(val, version);
684 state.intern_str(&bytes)
685 }
686 }
687}
688
689// ──────────────────────────────────────────────────────────────────────────
690// push_vfstring infrastructure
691// ──────────────────────────────────────────────────────────────────────────
692
693/// Typed format argument for `push_vfstring`.
694///
695/// PORT NOTE: replaces the C `va_list` variadic interface. C callers of
696/// `luaO_pushfstring(L, fmt, ...)` must be updated to pass structured
697/// `FmtArg` slices. The format-string scanning logic is preserved in
698/// `push_vfstring`; only the argument-list type changes.
699pub enum FmtArg<'a> {
700 /// `%s` — a byte string (replaces `const char *` from va_list).
701 Str(&'a [u8]),
702 /// `%c` — a single byte character.
703 Char(u8),
704 /// `%d` — a 32-bit integer.
705 Int(i32),
706 /// `%I` — a Lua integer (i64).
707 LuaInt(i64),
708 /// `%f` — a Lua float (f64).
709 Float(f64),
710 /// `%U` — a Unicode codepoint (u32), encoded as UTF-8.
711 Utf8Codepoint(u32),
712}
713
714/// Internal accumulator for `push_vfstring`.
715///
716///
717/// PORT NOTE: `space` is a `Vec<u8>` rather than a fixed-size array; the
718/// BUF_VFS threshold is still respected for flushing behaviour.
719struct BufFs {
720 /// Whether at least one partial result has been pushed onto the stack.
721 pushed: bool,
722 /// Accumulated bytes not yet pushed to the stack.
723 space: Vec<u8>,
724}
725
726impl BufFs {
727 fn new() -> Self {
728 BufFs {
729 pushed: false,
730 space: Vec::with_capacity(BUF_VFS),
731 }
732 }
733}
734
735/// Pushes the byte string `str_bytes` to the Lua stack and concatenates with
736/// any prior partial result.
737///
738fn pushstr(buf: &mut BufFs, state: &mut LuaState, str_bytes: &[u8]) -> Result<(), LuaError> {
739 // L->top.p++;
740 // if (!buff->pushed) buff->pushed = 1;
741 // else luaV_concat(L, 2);
742 let s = state.intern_str(str_bytes)?;
743 state.push(LuaValue::Str(s));
744 if !buf.pushed {
745 buf.pushed = true;
746 } else {
747 crate::vm::concat(state, 2)?;
748 }
749 Ok(())
750}
751
752/// Flushes the internal buffer to the Lua stack.
753///
754fn clearbuff(buf: &mut BufFs, state: &mut LuaState) -> Result<(), LuaError> {
755 let bytes: Vec<u8> = buf.space.drain(..).collect();
756 pushstr(buf, state, &bytes)
757}
758
759/// Adds `str_bytes` to the internal buffer, flushing first if it won't fit.
760///
761fn addstr2buff(buf: &mut BufFs, state: &mut LuaState, str_bytes: &[u8]) -> Result<(), LuaError> {
762 // else { clearbuff; pushstr directly; }
763 if str_bytes.len() <= BUF_VFS {
764 if str_bytes.len() > BUF_VFS - buf.space.len() {
765 clearbuff(buf, state)?;
766 }
767 buf.space.extend_from_slice(str_bytes);
768 } else {
769 clearbuff(buf, state)?;
770 pushstr(buf, state, str_bytes)?;
771 }
772 Ok(())
773}
774
775/// Formats the numeric value `num` and appends it to the buffer.
776///
777fn addnum2buff(buf: &mut BufFs, state: &mut LuaState, num: &LuaValue) -> Result<(), LuaError> {
778 // int len = tostringbuff(num, numbuff);
779 // addsize(buff, len);
780 let version = state.global().lua_version;
781 let bytes = number_to_str_buf(num, version);
782 addstr2buff(buf, state, &bytes)
783}
784
785// ──────────────────────────────────────────────────────────────────────────
786// push_vfstring / push_fstring
787// ──────────────────────────────────────────────────────────────────────────
788
789/// Builds a formatted Lua string from a format byte string and structured
790/// arguments, pushes it onto the stack, and returns the top-of-stack value.
791///
792/// Supported format specifiers (same subset as C's `luaO_pushvfstring`):
793/// `%s`, `%c`, `%d`, `%I`, `%f`, `%U`, `%%`.
794/// `%p` is **not** supported; see [`FmtArg`] documentation.
795///
796///
797/// PORT NOTE: `va_list` replaced by `&[FmtArg]`. Call sites that previously
798/// passed variadic arguments must be updated to build a `&[FmtArg]` slice.
799pub fn push_vfstring<'a>(
800 state: &mut LuaState,
801 fmt: &[u8],
802 args: &[FmtArg<'a>],
803) -> Result<GcRef<LuaString>, LuaError> {
804 let mut buf = BufFs::new();
805 let mut arg_idx = 0usize;
806 let mut pos = 0usize;
807
808 while let Some(rel) = fmt[pos..].iter().position(|&b| b == b'%') {
809 let e = pos + rel;
810 addstr2buff(&mut buf, state, &fmt[pos..e])?;
811
812 let spec = if e + 1 < fmt.len() { fmt[e + 1] } else { 0 };
813 match spec {
814 b's' => {
815 // addstr2buff(&buff, s, strlen(s));
816 let s = match args.get(arg_idx) {
817 Some(FmtArg::Str(b)) => *b,
818 None => b"(null)",
819 _ => b"(null)",
820 };
821 arg_idx += 1;
822 addstr2buff(&mut buf, state, s)?;
823 }
824 b'c' => {
825 // addstr2buff(&buff, &c, sizeof(char));
826 let c = match args.get(arg_idx) {
827 Some(FmtArg::Char(b)) => *b,
828 _ => b'?',
829 };
830 arg_idx += 1;
831 addstr2buff(&mut buf, state, &[c])?;
832 }
833 b'd' => {
834 let n = match args.get(arg_idx) {
835 Some(FmtArg::Int(i)) => *i as i64,
836 _ => 0,
837 };
838 arg_idx += 1;
839 addnum2buff(&mut buf, state, &LuaValue::Int(n))?;
840 }
841 b'I' => {
842 // addnum2buff(&buff, &num);
843 let n = match args.get(arg_idx) {
844 Some(FmtArg::LuaInt(i)) => *i,
845 _ => 0,
846 };
847 arg_idx += 1;
848 addnum2buff(&mut buf, state, &LuaValue::Int(n))?;
849 }
850 b'f' => {
851 // addnum2buff(&buff, &num);
852 let f = match args.get(arg_idx) {
853 Some(FmtArg::Float(f)) => *f,
854 _ => 0.0,
855 };
856 arg_idx += 1;
857 addnum2buff(&mut buf, state, &LuaValue::Float(f))?;
858 }
859 b'p' => {
860 // TODO(port): %p pointer formatting not implemented in safe Rust;
861 // callers that need it should pre-format the pointer and pass FmtArg::Str.
862 arg_idx += 1; // consume the argument slot
863 addstr2buff(&mut buf, state, b"<ptr>")?;
864 }
865 b'U' => {
866 // addstr2buff(&buff, bf + UTF8BUFFSZ - len, len);
867 let cp = match args.get(arg_idx) {
868 Some(FmtArg::Utf8Codepoint(u)) => *u,
869 _ => b'?' as u32,
870 };
871 arg_idx += 1;
872 let mut bf = [0u8; UTF8_BUF_SZ];
873 let n = utf8_esc(&mut bf, cp);
874 addstr2buff(&mut buf, state, &bf[UTF8_BUF_SZ - n..])?;
875 }
876 b'%' => {
877 addstr2buff(&mut buf, state, b"%")?;
878 }
879 other => {
880 return Err(LuaError::runtime(format_args!(
881 "invalid option '%%{}' to 'lua_pushfstring'",
882 other as char
883 )));
884 }
885 }
886 pos = e + 2;
887 }
888
889 addstr2buff(&mut buf, state, &fmt[pos..])?;
890 clearbuff(&mut buf, state)?;
891 debug_assert!(buf.pushed, "push_vfstring: no string was pushed");
892
893 // Return the interned string at the top of the stack.
894 // PORT NOTE: in C this returns a `const char *` into the TString; in Rust
895 // we return the GcRef<LuaString> directly.
896 Ok(state.peek_string_at_top())
897}
898
899/// Variadic entry point; delegates to `push_vfstring`.
900///
901///
902/// PORT NOTE: callers that previously used `luaO_pushfstring` for error
903/// messages should collapse the call into `LuaError::runtime(format_args!(...))`;
904/// see PORTING.md §4.2 and error_sites.tsv.
905pub fn push_fstring<'a>(
906 state: &mut LuaState,
907 fmt: &[u8],
908 args: &[FmtArg<'a>],
909) -> Result<GcRef<LuaString>, LuaError> {
910 push_vfstring(state, fmt, args)
911}
912
913// ──────────────────────────────────────────────────────────────────────────
914// chunk_id — human-readable chunk identifier
915// ──────────────────────────────────────────────────────────────────────────
916
917/// Fills `out` with a human-readable identifier derived from `source` and
918/// returns the number of bytes written (not including any null terminator).
919///
920/// Rules (matching C):
921/// - `=...` → literal text (everything after `=`), truncated to `LUA_ID_SIZE - 1`.
922/// - `@...` → file name (everything after `@`), prefixed with `...` if too long.
923/// - anything else → `[string "..."]`, with the first line truncated.
924///
925pub fn chunk_id(out: &mut [u8], source: &[u8]) -> usize {
926 let bufflen = LUA_ID_SIZE;
927 let mut written = 0usize;
928
929 let write_bytes = |out: &mut [u8], written: &mut usize, bytes: &[u8]| {
930 let avail = out.len().saturating_sub(*written);
931 let n = bytes.len().min(avail);
932 out[*written..*written + n].copy_from_slice(&bytes[..n]);
933 *written += n;
934 };
935
936 let first = source.first().copied();
937 let srclen = source.len();
938
939 match first {
940 Some(b'=') => {
941 let body = &source[1..];
942 if srclen <= bufflen {
943 write_bytes(out, &mut written, body);
944 } else {
945 write_bytes(out, &mut written, &body[..bufflen - 1]);
946 if written < out.len() {
947 out[written] = 0;
948 }
949 }
950 }
951 Some(b'@') => {
952 let body = &source[1..];
953 if srclen <= bufflen {
954 write_bytes(out, &mut written, body);
955 } else {
956 write_bytes(out, &mut written, RETS);
957 let tail_len = bufflen - RETS.len() - 1;
958 let tail_start = body.len() - tail_len;
959 write_bytes(out, &mut written, &body[tail_start..tail_start + tail_len]);
960 }
961 }
962 _ => {
963 let nl_pos = source.iter().position(|&b| b == b'\n');
964 write_bytes(out, &mut written, PRE);
965 let reserved = PRE.len() + RETS.len() + POS.len() + 1;
966 let inner_limit = bufflen.saturating_sub(reserved);
967
968 if srclen < inner_limit && nl_pos.is_none() {
969 write_bytes(out, &mut written, source);
970 } else {
971 let take = nl_pos.unwrap_or(srclen).min(inner_limit);
972 write_bytes(out, &mut written, &source[..take]);
973 write_bytes(out, &mut written, RETS);
974 }
975 write_bytes(out, &mut written, POS);
976 }
977 }
978
979 written
980}
981
982// ──────────────────────────────────────────────────────────────────────────
983// PORT STATUS
984// source: src/lobject.c (602 lines, ~20 functions)
985// target_crate: lua-vm
986// confidence: medium
987// todos: 15
988// port_notes: 12
989// unsafe_blocks: 0
990// notes: All import paths are speculative (crate::state, lua_types::*);
991// Phase B must reconcile. va_list replaced by FmtArg enum —
992// call sites of push_fstring/push_vfstring need updating.
993// Float formatting (%.14g) is approximated with {:.14e}; needs
994// proper %g in Phase B. Locale decimal-point handling is
995// stubbed (always '.'). str2dloc uses from_utf8 for ASCII
996// number strings (flagged TODO). int_floor_mod, int_floor_div,
997// shiftl, float_floor_mod, concat are assumed to exist in
998// crate::vm; Phase B must confirm or create them.
999// ──────────────────────────────────────────────────────────────────────────