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/// Converts a numeric `LuaValue` to an interned `LuaString`, returning a
645/// `GcRef<LuaString>` handle. Callers are responsible for updating the
646/// `LuaValue` (or stack slot) with `LuaValue::Str(s)`.
647///
648/// in place; in Rust we return the string because holding `&mut LuaValue`
649/// across a `state.intern_str` call would borrow `state` twice.
650pub fn num_to_string(state: &mut LuaState, val: &LuaValue) -> Result<GcRef<LuaString>, LuaError> {
651 // int len = tostringbuff(obj, buff);
652 // setsvalue(L, obj, luaS_newlstr(L, buff, len));
653 let version = state.global().lua_version;
654 let bytes = number_to_str_buf(val, version);
655 state.intern_str(&bytes)
656}
657
658// ──────────────────────────────────────────────────────────────────────────
659// push_vfstring infrastructure
660// ──────────────────────────────────────────────────────────────────────────
661
662/// Typed format argument for `push_vfstring`.
663///
664/// PORT NOTE: replaces the C `va_list` variadic interface. C callers of
665/// `luaO_pushfstring(L, fmt, ...)` must be updated to pass structured
666/// `FmtArg` slices. The format-string scanning logic is preserved in
667/// `push_vfstring`; only the argument-list type changes.
668pub enum FmtArg<'a> {
669 /// `%s` — a byte string (replaces `const char *` from va_list).
670 Str(&'a [u8]),
671 /// `%c` — a single byte character.
672 Char(u8),
673 /// `%d` — a 32-bit integer.
674 Int(i32),
675 /// `%I` — a Lua integer (i64).
676 LuaInt(i64),
677 /// `%f` — a Lua float (f64).
678 Float(f64),
679 /// `%U` — a Unicode codepoint (u32), encoded as UTF-8.
680 Utf8Codepoint(u32),
681}
682
683/// Internal accumulator for `push_vfstring`.
684///
685///
686/// PORT NOTE: `space` is a `Vec<u8>` rather than a fixed-size array; the
687/// BUF_VFS threshold is still respected for flushing behaviour.
688struct BufFs {
689 /// Whether at least one partial result has been pushed onto the stack.
690 pushed: bool,
691 /// Accumulated bytes not yet pushed to the stack.
692 space: Vec<u8>,
693}
694
695impl BufFs {
696 fn new() -> Self {
697 BufFs {
698 pushed: false,
699 space: Vec::with_capacity(BUF_VFS),
700 }
701 }
702}
703
704/// Pushes the byte string `str_bytes` to the Lua stack and concatenates with
705/// any prior partial result.
706///
707fn pushstr(buf: &mut BufFs, state: &mut LuaState, str_bytes: &[u8]) -> Result<(), LuaError> {
708 // L->top.p++;
709 // if (!buff->pushed) buff->pushed = 1;
710 // else luaV_concat(L, 2);
711 let s = state.intern_str(str_bytes)?;
712 state.push(LuaValue::Str(s));
713 if !buf.pushed {
714 buf.pushed = true;
715 } else {
716 crate::vm::concat(state, 2)?;
717 }
718 Ok(())
719}
720
721/// Flushes the internal buffer to the Lua stack.
722///
723fn clearbuff(buf: &mut BufFs, state: &mut LuaState) -> Result<(), LuaError> {
724 let bytes: Vec<u8> = buf.space.drain(..).collect();
725 pushstr(buf, state, &bytes)
726}
727
728/// Adds `str_bytes` to the internal buffer, flushing first if it won't fit.
729///
730fn addstr2buff(buf: &mut BufFs, state: &mut LuaState, str_bytes: &[u8]) -> Result<(), LuaError> {
731 // else { clearbuff; pushstr directly; }
732 if str_bytes.len() <= BUF_VFS {
733 if str_bytes.len() > BUF_VFS - buf.space.len() {
734 clearbuff(buf, state)?;
735 }
736 buf.space.extend_from_slice(str_bytes);
737 } else {
738 clearbuff(buf, state)?;
739 pushstr(buf, state, str_bytes)?;
740 }
741 Ok(())
742}
743
744/// Formats the numeric value `num` and appends it to the buffer.
745///
746fn addnum2buff(buf: &mut BufFs, state: &mut LuaState, num: &LuaValue) -> Result<(), LuaError> {
747 // int len = tostringbuff(num, numbuff);
748 // addsize(buff, len);
749 let version = state.global().lua_version;
750 let bytes = number_to_str_buf(num, version);
751 addstr2buff(buf, state, &bytes)
752}
753
754// ──────────────────────────────────────────────────────────────────────────
755// push_vfstring / push_fstring
756// ──────────────────────────────────────────────────────────────────────────
757
758/// Builds a formatted Lua string from a format byte string and structured
759/// arguments, pushes it onto the stack, and returns the top-of-stack value.
760///
761/// Supported format specifiers (same subset as C's `luaO_pushvfstring`):
762/// `%s`, `%c`, `%d`, `%I`, `%f`, `%U`, `%%`.
763/// `%p` is **not** supported; see [`FmtArg`] documentation.
764///
765///
766/// PORT NOTE: `va_list` replaced by `&[FmtArg]`. Call sites that previously
767/// passed variadic arguments must be updated to build a `&[FmtArg]` slice.
768pub fn push_vfstring<'a>(
769 state: &mut LuaState,
770 fmt: &[u8],
771 args: &[FmtArg<'a>],
772) -> Result<GcRef<LuaString>, LuaError> {
773 let mut buf = BufFs::new();
774 let mut arg_idx = 0usize;
775 let mut pos = 0usize;
776
777 while let Some(rel) = fmt[pos..].iter().position(|&b| b == b'%') {
778 let e = pos + rel;
779 addstr2buff(&mut buf, state, &fmt[pos..e])?;
780
781 let spec = if e + 1 < fmt.len() { fmt[e + 1] } else { 0 };
782 match spec {
783 b's' => {
784 // addstr2buff(&buff, s, strlen(s));
785 let s = match args.get(arg_idx) {
786 Some(FmtArg::Str(b)) => *b,
787 None => b"(null)",
788 _ => b"(null)",
789 };
790 arg_idx += 1;
791 addstr2buff(&mut buf, state, s)?;
792 }
793 b'c' => {
794 // addstr2buff(&buff, &c, sizeof(char));
795 let c = match args.get(arg_idx) {
796 Some(FmtArg::Char(b)) => *b,
797 _ => b'?',
798 };
799 arg_idx += 1;
800 addstr2buff(&mut buf, state, &[c])?;
801 }
802 b'd' => {
803 let n = match args.get(arg_idx) {
804 Some(FmtArg::Int(i)) => *i as i64,
805 _ => 0,
806 };
807 arg_idx += 1;
808 addnum2buff(&mut buf, state, &LuaValue::Int(n))?;
809 }
810 b'I' => {
811 // addnum2buff(&buff, &num);
812 let n = match args.get(arg_idx) {
813 Some(FmtArg::LuaInt(i)) => *i,
814 _ => 0,
815 };
816 arg_idx += 1;
817 addnum2buff(&mut buf, state, &LuaValue::Int(n))?;
818 }
819 b'f' => {
820 // addnum2buff(&buff, &num);
821 let f = match args.get(arg_idx) {
822 Some(FmtArg::Float(f)) => *f,
823 _ => 0.0,
824 };
825 arg_idx += 1;
826 addnum2buff(&mut buf, state, &LuaValue::Float(f))?;
827 }
828 b'p' => {
829 // TODO(port): %p pointer formatting not implemented in safe Rust;
830 // callers that need it should pre-format the pointer and pass FmtArg::Str.
831 arg_idx += 1; // consume the argument slot
832 addstr2buff(&mut buf, state, b"<ptr>")?;
833 }
834 b'U' => {
835 // addstr2buff(&buff, bf + UTF8BUFFSZ - len, len);
836 let cp = match args.get(arg_idx) {
837 Some(FmtArg::Utf8Codepoint(u)) => *u,
838 _ => b'?' as u32,
839 };
840 arg_idx += 1;
841 let mut bf = [0u8; UTF8_BUF_SZ];
842 let n = utf8_esc(&mut bf, cp);
843 addstr2buff(&mut buf, state, &bf[UTF8_BUF_SZ - n..])?;
844 }
845 b'%' => {
846 addstr2buff(&mut buf, state, b"%")?;
847 }
848 other => {
849 return Err(LuaError::runtime(format_args!(
850 "invalid option '%%{}' to 'lua_pushfstring'",
851 other as char
852 )));
853 }
854 }
855 pos = e + 2;
856 }
857
858 addstr2buff(&mut buf, state, &fmt[pos..])?;
859 clearbuff(&mut buf, state)?;
860 debug_assert!(buf.pushed, "push_vfstring: no string was pushed");
861
862 // Return the interned string at the top of the stack.
863 // PORT NOTE: in C this returns a `const char *` into the TString; in Rust
864 // we return the GcRef<LuaString> directly.
865 Ok(state.peek_string_at_top())
866}
867
868/// Variadic entry point; delegates to `push_vfstring`.
869///
870///
871/// PORT NOTE: callers that previously used `luaO_pushfstring` for error
872/// messages should collapse the call into `LuaError::runtime(format_args!(...))`;
873/// see PORTING.md §4.2 and error_sites.tsv.
874pub fn push_fstring<'a>(
875 state: &mut LuaState,
876 fmt: &[u8],
877 args: &[FmtArg<'a>],
878) -> Result<GcRef<LuaString>, LuaError> {
879 push_vfstring(state, fmt, args)
880}
881
882// ──────────────────────────────────────────────────────────────────────────
883// chunk_id — human-readable chunk identifier
884// ──────────────────────────────────────────────────────────────────────────
885
886/// Fills `out` with a human-readable identifier derived from `source` and
887/// returns the number of bytes written (not including any null terminator).
888///
889/// Rules (matching C):
890/// - `=...` → literal text (everything after `=`), truncated to `LUA_ID_SIZE - 1`.
891/// - `@...` → file name (everything after `@`), prefixed with `...` if too long.
892/// - anything else → `[string "..."]`, with the first line truncated.
893///
894pub fn chunk_id(out: &mut [u8], source: &[u8]) -> usize {
895 let bufflen = LUA_ID_SIZE;
896 let mut written = 0usize;
897
898 let write_bytes = |out: &mut [u8], written: &mut usize, bytes: &[u8]| {
899 let avail = out.len().saturating_sub(*written);
900 let n = bytes.len().min(avail);
901 out[*written..*written + n].copy_from_slice(&bytes[..n]);
902 *written += n;
903 };
904
905 let first = source.first().copied();
906 let srclen = source.len();
907
908 match first {
909 Some(b'=') => {
910 let body = &source[1..];
911 if srclen <= bufflen {
912 write_bytes(out, &mut written, body);
913 } else {
914 write_bytes(out, &mut written, &body[..bufflen - 1]);
915 if written < out.len() {
916 out[written] = 0;
917 }
918 }
919 }
920 Some(b'@') => {
921 let body = &source[1..];
922 if srclen <= bufflen {
923 write_bytes(out, &mut written, body);
924 } else {
925 write_bytes(out, &mut written, RETS);
926 let tail_len = bufflen - RETS.len() - 1;
927 let tail_start = body.len() - tail_len;
928 write_bytes(out, &mut written, &body[tail_start..tail_start + tail_len]);
929 }
930 }
931 _ => {
932 let nl_pos = source.iter().position(|&b| b == b'\n');
933 write_bytes(out, &mut written, PRE);
934 let reserved = PRE.len() + RETS.len() + POS.len() + 1;
935 let inner_limit = bufflen.saturating_sub(reserved);
936
937 if srclen < inner_limit && nl_pos.is_none() {
938 write_bytes(out, &mut written, source);
939 } else {
940 let take = nl_pos.unwrap_or(srclen).min(inner_limit);
941 write_bytes(out, &mut written, &source[..take]);
942 write_bytes(out, &mut written, RETS);
943 }
944 write_bytes(out, &mut written, POS);
945 }
946 }
947
948 written
949}
950
951// ──────────────────────────────────────────────────────────────────────────
952// PORT STATUS
953// source: src/lobject.c (602 lines, ~20 functions)
954// target_crate: lua-vm
955// confidence: medium
956// todos: 15
957// port_notes: 12
958// unsafe_blocks: 0
959// notes: All import paths are speculative (crate::state, lua_types::*);
960// Phase B must reconcile. va_list replaced by FmtArg enum —
961// call sites of push_fstring/push_vfstring need updating.
962// Float formatting (%.14g) is approximated with {:.14e}; needs
963// proper %g in Phase B. Locale decimal-point handling is
964// stubbed (always '.'). str2dloc uses from_utf8 for ASCII
965// number strings (flagged TODO). int_floor_mod, int_floor_div,
966// shiftl, float_floor_mod, concat are assumed to exist in
967// crate::vm; Phase B must confirm or create them.
968// ──────────────────────────────────────────────────────────────────────────