ezlua 0.5.4

Ergonomic, efficient and Zero-cost rust bindings to Lua5.4
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
use crate::{
    convert::*,
    error::{Error, Result},
    ffi::*,
    luaapi::{ThreadStatus, Type},
    marker::RegVal,
    str::*,
    value::{ValRef, Value},
};

use alloc::{collections::BinaryHeap as Slots, format};
use core::{cell::Cell, cell::RefCell, ffi::c_int, str};

/// Safe wrapper for operation to lua_State
#[derive(Debug)]
pub struct State {
    pub base: Index,
    pub from_index: Cell<Index>,
    pub(crate) state: *mut lua_State,
    pub(crate) free: RefCell<Slots<i32>>,
}

#[cfg(feature = "unsafe_send_sync")]
unsafe impl Send for State {}
#[cfg(feature = "unsafe_send_sync")]
unsafe impl Sync for State {}

impl State {
    /// Load lua script and execute it
    #[inline]
    pub fn do_string<S: AsRef<[u8]>>(&self, script: S, name: Option<&str>) -> Result<()> {
        self.load(script, name)?.pcall_void(())
    }

    #[inline(always)]
    pub fn registry_value<V: ToLua>(&self, val: V) -> Result<RegVal> {
        self.registry().reference(val).map(|r| RegVal {
            reference: r,
            inner: self.lua_inner(),
        })
    }

    #[inline(always)]
    pub fn safe_index(&self, i: Index) -> bool {
        i <= self.base
    }

    #[inline(always)]
    pub(crate) fn stack_guard(&self) -> StackGuard {
        StackGuard::from(self)
    }

    #[track_caller]
    pub(crate) fn drop_valref<'a>(&'a self, val: &ValRef<'a>) {
        if val.index > self.base {
            self.give_back_slot(val.index);
        }
    }

    #[inline(always)]
    #[track_caller]
    pub(crate) fn slot_exists(&self, i: Index) -> bool {
        self.free.borrow().iter().find(|&n| *n == i).is_some()
    }

    #[inline(always)]
    #[track_caller]
    pub(crate) fn give_back_slot(&self, i: Index) {
        #[cfg(feature = "std")]
        if debug_ezlua() {
            let loc = core::panic::Location::caller();
            assert!(
                !self.slot_exists(i),
                "[give back]: {i} from: {}:{}",
                loc.file(),
                loc.line()
            );
            std::println!("[give back]: {i} from: {}:{}", loc.file(), loc.line());
        }
        self.free.borrow_mut().push(i);
    }

    pub fn free_slots(&self) -> core::cell::Ref<Slots<i32>> {
        self.free.borrow()
    }
}

#[derive(Debug)]
pub(crate) struct StackGuard<'a> {
    state: &'a State,
    top: Index,
}

impl<'a> StackGuard<'a> {
    #[inline(always)]
    pub fn top(&self) -> i32 {
        self.top
    }
}

impl<'a> From<&'a State> for StackGuard<'a> {
    fn from(state: &'a State) -> Self {
        let top = state.stack_top();
        Self { state, top }
    }
}

pub(crate) const fn debug_ezlua() -> bool {
    option_env!("DEBUG_EZLUA").is_some()
}

pub mod unsafe_impl {
    #[cfg(feature = "std")]
    use std::path::Path;

    use alloc::string::String;

    use super::*;
    use crate::{
        luaapi::{GCMode, GcOption, UnsafeLuaApi},
        value::{Function, LuaString, LuaThread, Table},
    };

    impl<'a> Drop for StackGuard<'a> {
        fn drop(&mut self) {
            self.state.set_top(self.top);
        }
    }

    impl State {
        pub unsafe fn from_raw_state(state: *mut lua_State) -> Self {
            let base = lua_gettop(state);
            Self {
                base,
                state,
                from_index: 0.into(),
                free: Default::default(),
            }
        }

        #[inline(always)]
        pub fn raw_state(&self) -> *mut lua_State {
            self.state
        }

        #[inline(always)]
        pub fn stack_top(&self) -> i32 {
            self.get_top()
        }

        pub(crate) fn check_nil_pop(&self) -> Result<()> {
            if self.is_nil(-1) {
                self.pop(1);
                Err(Error::Runtime("key is nil".into()))
            } else {
                Ok(())
            }
        }

        pub fn check_stack(&self, n: i32) -> Result<()> {
            if UnsafeLuaApi::check_stack(self, n) {
                Ok(())
            } else {
                Err(Error::runtime(format!("check stack {n}")))
            }
        }

        #[inline]
        pub fn check_type(&self, i: Index, ty: Type) -> Result<()> {
            let t = self.type_of(i);
            if t != ty {
                return Err(Error::TypeNotMatch(t));
            }
            Ok(())
        }

        #[inline(always)]
        pub fn up_value(&self, i: Index) -> ValRef {
            ValRef {
                state: self,
                index: lua_upvalueindex(i),
            }
        }

        pub(crate) fn top_val(&self) -> ValRef {
            self.try_replace_top().unwrap_or_else(|| {
                let top = self.get_top();
                ValRef {
                    state: self,
                    index: top,
                }
            })
        }

        pub(crate) fn try_replace_top(&self) -> Option<ValRef> {
            let top = self.get_top();
            while let Some(slot) = self.free.borrow_mut().pop() {
                if slot < top {
                    #[cfg(feature = "std")]
                    if debug_ezlua() {
                        std::println!("[borrow slot] {slot} top: {top}");
                    }
                    self.replace(slot);
                    return Some(ValRef {
                        state: self,
                        index: slot,
                    });
                } else {
                    #[cfg(feature = "std")]
                    if debug_ezlua() {
                        std::println!("[drop slot] {slot}");
                    }
                }
            }
            None
        }

        /// Clear the free slots, assign nil, and shrink the lua stack as much as possible
        pub fn clear_slots(&self) -> Result<()> {
            let mut top = self.stack_top();
            loop {
                let free = self.free.borrow().peek().copied();
                if free == Some(top) {
                    self.pop(1);
                    top -= 1;
                    self.free.borrow_mut().pop();
                } else {
                    break;
                }
            }

            let slots = self.free.borrow();
            if !slots.is_empty() {
                self.check_stack(1)?;
                for &i in slots.iter() {
                    self.push_nil();
                    self.replace(i);
                }
            }

            Ok(())
        }

        pub(crate) fn val(&self, i: Index) -> ValRef {
            debug_assert!(i > 0);
            if i <= self.base {
                ValRef {
                    state: self,
                    index: i,
                }
            } else {
                self.check_stack(1).expect("stack");
                self.push_value(i);
                self.top_val()
            }
        }

        #[inline(always)]
        pub fn arg_val(&self, i: Index) -> Option<ValRef> {
            self.safe_index(i).then(|| ValRef {
                state: self,
                index: self.abs_index(i),
            })
        }

        pub fn to_safe_bytes(&self, i: Index) -> Option<&[u8]> {
            self.safe_index(i).then(|| self.to_bytes(i)).flatten()
        }

        #[inline(always)]
        pub(crate) fn val_without_push(&self, i: Index) -> ValRef {
            ValRef {
                state: self,
                index: self.abs_index(i),
            }
        }

        /// Get the C registry table
        #[inline(always)]
        pub fn registry(&self) -> Table {
            Table(ValRef {
                state: self,
                index: LUA_REGISTRYINDEX,
            })
        }

        /// Create a new lua value
        pub fn new_val<V: ToLua>(&self, val: V) -> Result<ValRef> {
            self.check_stack(2)?;
            self.push(val)?;
            Ok(self.top_val())
        }

        /// Create a new lua value, return as [`Value`] rather than [`ValRef`]
        #[inline(always)]
        pub fn new_value<V: ToLua>(&self, val: V) -> Result<Value> {
            self.new_val(val).map(ValRef::into_value)
        }

        /// Create a lua table and specify the size
        pub fn new_table_with_size(&self, narr: c_int, nrec: c_int) -> Result<Table> {
            self.check_stack(2)?;
            self.create_table(narr, nrec);
            Ok(self.top_val().try_into().expect("table"))
        }

        /// Create an array table
        #[inline]
        pub fn new_array_table(&self, narr: usize) -> Result<Table> {
            let t = self.new_table_with_size(narr as _, 0)?;
            t.set_metatable(self.array_metatable()?)?;
            Ok(t)
        }

        /// Create a lua table
        #[inline(always)]
        pub fn new_table(&self) -> Result<Table> {
            self.new_table_with_size(0, 0)
        }

        /// Create a lua string
        pub fn new_string<S: AsRef<[u8]>>(&self, s: S) -> Result<LuaString> {
            self.check_stack(2)?;
            self.push_bytes(s.as_ref());
            Ok(self.top_val().try_into().expect("string"))
        }

        /// Load script string or bytecode
        pub fn load<S: AsRef<[u8]>>(&self, s: S, name: Option<&str>) -> Result<Function> {
            self.check_stack(2)?;
            let guard = self.stack_guard();
            self.statuscode_to_error(self.load_buffer(s, name))?;
            core::mem::forget(guard);
            Ok(self.top_val().try_into().expect("function"))
        }

        /// Create function from script file
        #[cfg(feature = "std")]
        #[inline]
        pub fn load_file<P: AsRef<Path>>(&self, path: P) -> Result<Function> {
            let path = path.as_ref();
            self.load(
                std::fs::read(path).map_err(Error::from_debug)?,
                Some(format!("@{}", path.to_string_lossy()).as_str()),
            )
        }

        /// Register your own lua module, which can be load by `require` function in lua
        #[inline(always)]
        pub fn register_module<'a, F: Fn(&'a State) -> Result<Table<'a>> + 'static>(
            &self,
            name: &str,
            init: F,
            global: bool,
        ) -> Result<()> {
            self.check_stack(5)?;
            let _guard = self.stack_guard();
            self.requiref(
                &CString::new(name).map_err(Error::runtime_debug)?,
                crate::convert::module_function_wrapper(init),
                global,
            );
            Ok(())
        }

        /// Get the lua global table
        pub fn global(&self) -> Table {
            self.check_stack(1).expect("stack");
            self.raw_geti(LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
            self.top_val().try_into().expect("global table")
        }

        pub fn main_state(&self) -> LuaThread {
            self.check_stack(1).expect("stack");
            self.raw_geti(LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD);
            self.top_val().try_into().expect("main thread")
        }

        /// Returns the amount of memory (in bytes) currently used inside this Lua state
        pub fn used_memory(&self) -> usize {
            let used_kbytes = self.gc(GcOption::Count, 0);
            let used_kbytes_rem = self.gc(GcOption::CountBytes, 0);
            (used_kbytes as usize) * 1024 + (used_kbytes_rem as usize)
        }

        /// Do a full GC for lua
        pub fn gc_collect(&self) -> Result<()> {
            self.gc(GcOption::Collect, 0);

            Ok(())
        }

        /// Returns true if the garbage collector is currently running automatically
        pub fn gc_is_running(&self) -> bool {
            self.gc(GcOption::IsRunning, 0) != 0
        }

        /// Stop the Lua GC from running
        pub fn gc_stop(&self) {
            self.gc(GcOption::Stop, 0);
        }

        /// Restarts the Lua GC if it is not running
        pub fn gc_restart(&self) {
            self.gc(GcOption::Restart, 0);
        }

        /// Steps the garbage collector one indivisible step.
        ///
        /// Returns true if this has finished a collection cycle.
        pub fn gc_step(&self) -> Result<bool> {
            self.gc_step_kbytes(0)
        }

        /// Steps the garbage collector as though memory had been allocated.
        ///
        /// if `kbytes` is 0, then this is the same as calling `gc_step`. Returns true if this step has
        /// finished a collection cycle.
        pub fn gc_step_kbytes(&self, kbytes: c_int) -> Result<bool> {
            unsafe extern "C-unwind" fn protect(l: *mut lua_State) -> i32 {
                lua_pushboolean(l, lua_gc(l, LUA_GCSTEP, lua_tointeger(l, 1) as i32));
                1
            }
            self.protect_call(kbytes, protect)
        }

        /// Sets the 'pause' value of the collector.
        ///
        /// Returns the previous value of 'pause'. More information can be found in the Lua
        /// [documentation](https://www.lua.org/manual/5.4/manual.html#2.5)
        pub fn gc_set_pause(&self, pause: c_int) -> c_int {
            self.gc(GcOption::SetPause, pause)
        }

        /// Sets the 'step multiplier' value of the collector.
        ///
        /// Returns the previous value of the 'step multiplier'. More information can be found in the
        /// Lua [documentation](https://www.lua.org/manual/5.4/manual.html#2.5)
        pub fn gc_set_step_multiplier(&self, step_multiplier: c_int) -> c_int {
            self.gc(GcOption::SetStepMul, step_multiplier)
        }

        /// Changes the collector to incremental mode with the given parameters.
        ///
        /// Returns the previous mode (always `GCMode::Incremental` in Lua < 5.4).
        /// More information can be found in the Lua [documentation](https://www.lua.org/manual/5.4/manual.html#2.5.1)
        pub fn gc_inc(&self, pause: c_int, step_multiplier: c_int, step_size: c_int) -> GCMode {
            let prev_mode =
                unsafe { lua_gc(self.state, LUA_GCINC, pause, step_multiplier, step_size) };
            match prev_mode {
                LUA_GCINC => GCMode::Incremental,
                LUA_GCGEN => GCMode::Generational,
                _ => unreachable!(),
            }
        }

        /// Changes the collector to generational mode with the given parameters.
        ///
        /// Returns the previous mode. More information about the generational GC
        /// can be found in the Lua 5.4 [documentation](https://www.lua.org/manual/5.4/manual.html#2.5.2)
        pub fn gc_gen(&self, minor_multiplier: c_int, major_multiplier: c_int) -> GCMode {
            let prev_mode =
                unsafe { lua_gc(self.state, LUA_GCGEN, minor_multiplier, major_multiplier) };
            match prev_mode {
                LUA_GCGEN => GCMode::Generational,
                LUA_GCINC => GCMode::Incremental,
                _ => unreachable!(),
            }
        }

        /// Stack backtrace info
        pub fn backtrace(&self, co: Option<&State>, msg: &str, level: i32) -> Result<String> {
            self.check_stack(4)?;
            self.traceback(
                co.map(|s| s.as_ptr()).unwrap_or(core::ptr::null_mut()),
                CString::new(msg).unwrap().as_c_str(),
                level,
            );
            let result = self.to_string_lossy(-1).unwrap_or_default().into_owned();
            self.pop(1);
            Ok(result)
        }

        /// [-0, +1, -]
        pub(crate) fn get_or_init_metatable(&self, callback: MetatableKey) -> Result<()> {
            let top = self.get_top();
            let reg = self.registry();
            let p = callback as *const usize;
            self.check_stack(6)?;
            let metatable = self.raw_getp(LUA_REGISTRYINDEX, p);
            if metatable.is_none_or_nil() {
                let mt = self.new_table_with_size(0, 4)?;
                self.balance_with(|_| callback(&mt))?;
                debug_assert_eq!(mt.type_of(), Type::Table);

                if self.get_field(mt.index, crate::cstr!("__name")) == Type::String {
                    self.push_value(mt.index);
                    self.set_table(LUA_REGISTRYINDEX);
                } else {
                    self.pop(1);
                }

                self.push_value(mt.index);
                self.raw_setp(LUA_REGISTRYINDEX, p);
                mt.0.ensure_top();
                self.replace(-2);
            }
            debug_assert_eq!(self.get_top(), top + 1);

            Ok(())
        }

        /// [-0, +0, -]
        #[inline]
        pub(crate) fn set_or_init_metatable(&self, callback: MetatableKey) -> Result<()> {
            let ty = self.type_of(-1);
            assert!(ty == Type::Userdata || ty == Type::Table);
            self.get_or_init_metatable(callback)?;
            self.set_metatable(-2);
            Ok(())
        }

        #[inline(always)]
        pub unsafe fn test_userdata_meta<T>(&self, i: Index, meta: MetatableKey) -> Option<&mut T> {
            let _guard = self.stack_guard();

            self.check_stack(2).expect("stack");
            let p = if self.get_metatable(i) && {
                self.raw_getp(LUA_REGISTRYINDEX, meta as *const ());
                self.raw_equal(-1, -2)
            } {
                self.to_userdata(i) as _
            } else {
                core::ptr::null_mut()
            };
            (p as *mut T).as_mut()
        }

        #[inline(always)]
        pub(crate) fn balance_with<'a, T: 'a, F: FnOnce(&'a State) -> T>(
            &'a self,
            callback: F,
        ) -> T {
            let top = self.get_top();
            let result = callback(self);
            self.set_top(top);
            self.drop_slots_greater(top);
            result
        }

        pub fn stack(&self, n: i32) -> Option<lua_Debug> {
            self.get_stack(n)
        }

        #[inline(always)]
        pub(crate) fn raise_with<T, F: FnOnce(&State) -> Result<T>>(self, fun: F) -> T {
            match fun(&self) {
                Ok(result) => result,
                Err(err) => unsafe {
                    self.raise_error(err);
                },
            }
        }

        #[deprecated = "it will cause memory leak when T = Result<Option<_>>, please embed this code to avoid"]
        #[inline(always)]
        pub(crate) unsafe fn return_result<T: ToLuaMulti>(self, t: T) -> usize {
            match t.push_multi(&self) {
                Ok(result) => result,
                Err(err) => self.raise_error(err),
            }
        }

        #[inline(always)]
        pub(crate) fn protect_call<'a, T: ToLuaMulti, R: FromLuaMulti<'a>>(
            &'a self,
            args: T,
            callback: CFunction,
        ) -> Result<R> {
            self.pcall_trace(callback, args)
        }

        // tracebacked pcall
        #[inline(always)]
        pub(crate) fn pcall_trace<'a, F: ToLua, T: ToLuaMulti, R: FromLuaMulti<'a>>(
            &'a self,
            func: F,
            args: T,
        ) -> Result<R> {
            let guard = self.stack_guard();

            self.check_stack(args.value_count().unwrap_or(10) as i32 + 2)?;
            self.push_fn(Some(Self::traceback_c));
            self.push(func)?;
            self.statuscode_to_error(unsafe {
                lua_pcall(self.state, self.push_multi(args)? as _, -1, guard.top() + 1)
            })?;

            let result_base = guard.top() + 2;
            self.to_multi_balance(guard, result_base)
        }

        #[inline(always)]
        pub(crate) fn to_multi_balance<'a, R: FromLuaMulti<'a>>(
            &'a self,
            guard: StackGuard<'a>,
            result_base: i32,
        ) -> Result<R> {
            let top = self.get_top();
            let res = R::from_lua_multi(self, result_base);
            self.check_multi_balance(guard, top);
            res
        }

        fn check_multi_balance<'a>(&'a self, guard: StackGuard<'a>, top: i32) {
            if self.get_top() > top {
                // if the top increased, it indicates that a new slot higher than the result_base has been allocated to valref,
                // so recycle the slots between between old_top and top
                for i in guard.top() + 1..=top {
                    self.give_back_slot(i);
                }
                core::mem::forget(guard);
            } else {
                // there are no new higher slot allocated, balance the stack
                drop(guard);
            }
        }

        pub(crate) unsafe fn error_string(self, e: impl AsRef<str>) -> ! {
            self.push_string(e.as_ref());
            core::mem::drop(e);
            self.error()
        }

        #[inline(always)]
        pub(crate) unsafe fn raise_error(self, e: impl core::fmt::Debug) -> ! {
            self.error_string(format!("{e:?}"))
        }

        pub unsafe extern "C-unwind" fn traceback_c(l: *mut lua_State) -> i32 {
            luaL_traceback(l, l, lua_tostring(l, 1), 1);
            1
        }

        pub(crate) fn status_to_error(&self, ts: ThreadStatus) -> Result<()> {
            match ts {
                ThreadStatus::Ok => Ok(()),
                ThreadStatus::Yield => Err(Error::Yield),
                _ => {
                    let err = self.to_string_lossy(-1).unwrap_or_default().into_owned();
                    match ts {
                        ThreadStatus::RuntimeError | ThreadStatus::MessageHandlerError => {
                            Err(Error::runtime(err))
                        }
                        // ThreadStatus::GcError => Err(Error::Gc(err)),
                        ThreadStatus::SyntaxError => Err(Error::Syntax(err)),
                        ThreadStatus::MemoryError => Err(Error::Memory(err)),
                        ThreadStatus::FileError => Err(Error::runtime(err)),
                        _ => unreachable!(),
                    }
                }
            }
        }

        pub(crate) fn statuscode_to_error_and_pop(&self, ts: i32) -> Result<()> {
            let result = self.statuscode_to_error(ts);
            if result.is_err() {
                self.pop(1)
            };
            result
        }

        pub(crate) fn statuscode_to_error_with_traceback(&self, ts: i32, tb: bool) -> Result<()> {
            match ts {
                LUA_OK => Ok(()),
                LUA_YIELD => Err(Error::Yield),
                _ => {
                    if tb {
                        self.check_stack(10)?;
                        unsafe {
                            luaL_traceback(self.state, self.state, lua_tostring(self.state, -1), 1);
                        }
                    }
                    let err = self.to_string_lossy(-1).unwrap_or_default().into_owned();
                    match ts {
                        LUA_ERRRUN | LUA_ERRERR => Err(Error::runtime(err)),
                        // LUA_ERRGCMM => Err(Error::Gc(err)),
                        LUA_ERRSYNTAX => Err(Error::Syntax(err)),
                        LUA_ERRMEM => Err(Error::Memory(err)),
                        LUA_ERRFILE => Err(Error::runtime(err)),
                        _ => unreachable!(),
                    }
                }
            }
        }

        pub(crate) fn statuscode_to_error(&self, ts: i32) -> Result<()> {
            self.statuscode_to_error_with_traceback(ts, false)
        }

        /// Pushes the given value onto the stack.
        pub(crate) fn pushv(&self, value: Value) {
            match value {
                Value::None | Value::Nil => self.push_nil(),
                Value::Bool(b) => self.push_bool(b),
                Value::Integer(i) => self.push_integer(i),
                Value::Number(n) => self.push_number(n),
                Value::LightUserdata(ud) => self.push_light_userdata(ud),
                Value::String(r) => self.pushval(r.0),
                Value::Table(r) => self.pushval(r.0),
                Value::Function(r) => self.pushval(r.0),
                Value::UserData(r) => self.pushval(r.0),
                Value::Thread(r) => self.pushval(r.0),
            }
        }

        pub(crate) fn pushval(&self, val: ValRef) {
            self.pushvalref(&val)
        }

        pub(crate) fn pushvalref(&self, val: &ValRef) {
            let state = val.state.raw_state();
            val.state.push_value(val.index);
            if state != self.raw_state() {
                unsafe { crate::ffi::lua_xmove(state, self.raw_state(), 1) }
            }
        }

        /// clear the stack, but only retain the top value
        pub(crate) fn clear_with_keep_top_one(&self, base: Index) -> bool {
            let top = self.get_top();
            if top == base + 1 {
                return true;
            }
            if top > base + 1 {
                self.drop_slots_greater(base);
                self.replace(base + 1);
                self.set_top(base + 1);
                return true;
            }

            false
        }

        #[track_caller]
        pub(crate) fn dump_stack(&self, n: usize) -> String {
            let loc = core::panic::Location::caller();
            let mut info = format!("dump_stack from {}:{}\n", loc.file(), loc.line());
            for i in (1..=self.get_top()).rev().take(n) {
                let val = self.val_without_push(i);
                info += format!("  [{i}] {val:?}\n").as_str();
                core::mem::forget(val);
            }
            info
        }

        /// drop the slot > i
        pub(crate) fn drop_slots_greater(&self, i: Index) {
            let mut free = self.free.borrow_mut();
            while free.peek().filter(|&&s| s > i).is_some() {
                free.pop();
            }
        }
    }
}