summaryrefslogtreecommitdiff
path: root/src/util.rs
blob: d731df600de36369d6cfe3067b6bff94f9a254e2 (plain)
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
use std::mem;
use std::ptr;
use std::process;
use std::sync::Arc;
use std::ffi::CStr;
use std::any::Any;
use std::os::raw::{c_char, c_int, c_void};
use std::panic::{catch_unwind, resume_unwind, UnwindSafe};

use ffi;
use error::{Result, Error};

macro_rules! cstr {
  ($s:expr) => (
    concat!($s, "\0") as *const str as *const [c_char] as *const c_char
  );
}

// A panic that clears the given lua stack before panicking
macro_rules! lua_panic {
    ($state:expr) => {
        {
            $crate::ffi::lua_settor($state, 0);
            panic!("rlua internal error");
        }
    };

    ($state:expr, $msg:expr) => {
        {
            $crate::ffi::lua_settop($state, 0);
            panic!(concat!("rlua: ", $msg));
        }
    };

    ($state:expr, $fmt:expr, $($arg:tt)+) => {
        {
            $crate::ffi::lua_settop($state, 0);
            panic!(concat!("rlua: ", $fmt), $($arg)+);
        }
    };
}

// An assert that clears the given lua stack before panicking
macro_rules! lua_assert {
    ($state:expr, $cond:expr) => {
        if !$cond {
            $crate::ffi::lua_settop($state, 0);
            panic!("rlua internal error");
        }
    };

    ($state:expr, $cond:expr, $msg:expr) => {
        if !$cond {
            $crate::ffi::lua_settop($state, 0);
            panic!(concat!("rlua: ", $msg));
        }
    };

    ($state:expr, $cond:expr, $fmt:expr, $($arg:tt)+) => {
        if !$cond {
            $crate::ffi::lua_settop($state, 0);
            panic!(concat!("rlua: ", $fmt), $($arg)+);
        }
    };
}

// Checks that Lua has enough free stack space for future stack operations.
// On failure, this will clear the stack and panic.
pub unsafe fn check_stack(state: *mut ffi::lua_State, amount: c_int) {
    lua_assert!(
        state,
        ffi::lua_checkstack(state, amount) != 0,
        "out of stack space"
    );
}

// Run an operation on a lua_State and check that the stack change is what is
// expected.  If the stack change does not match, clears the stack and panics.
pub unsafe fn stack_guard<F, R>(state: *mut ffi::lua_State, change: c_int, op: F) -> R
where
    F: FnOnce() -> R,
{
    let expected = ffi::lua_gettop(state) + change;
    lua_assert!(
        state,
        expected >= 0,
        "internal stack error: too many values would be popped"
    );

    let res = op();

    let top = ffi::lua_gettop(state);
    lua_assert!(
        state,
        ffi::lua_gettop(state) == expected,
        "internal stack error: expected stack to be {}, got {}",
        expected,
        top
    );

    res
}

// Run an operation on a lua_State and automatically clean up the stack before
// returning.  Takes the lua_State, the expected stack size change, and an
// operation to run.  If the operation results in success, then the stack is
// inspected to make sure the change in stack size matches the expected change
// and otherwise this is a logic error and will panic.  If the operation results
// in an error, the stack is shrunk to the value before the call.  If the
// operation results in an error and the stack is smaller than the value before
// the call, then this is unrecoverable and this will panic.  If this function
// panics, it will clear the stack before panicking.
pub unsafe fn stack_err_guard<F, R>(state: *mut ffi::lua_State, change: c_int, op: F) -> Result<R>
where
    F: FnOnce() -> Result<R>,
{
    let expected = ffi::lua_gettop(state) + change;
    lua_assert!(
        state,
        expected >= 0,
        "internal stack error: too many values would be popped"
    );

    let res = op();

    let top = ffi::lua_gettop(state);
    if res.is_ok() {
        lua_assert!(
            state,
            ffi::lua_gettop(state) == expected,
            "internal stack error: expected stack to be {}, got {}",
            expected,
            top
        );
    } else {
        lua_assert!(
            state,
            top >= expected,
            "internal stack error: {} too many values popped",
            top - expected
        );
        if top > expected {
            ffi::lua_settop(state, expected);
        }
    }

    res
}

// Protected version of lua_gettable, uses 3 stack spaces, does not call checkstack.
pub unsafe fn pgettable(state: *mut ffi::lua_State, index: c_int) -> Result<c_int> {
    unsafe extern "C" fn gettable(state: *mut ffi::lua_State) -> c_int {
        ffi::lua_gettable(state, -2);
        1
    }

    let table_index = ffi::lua_absindex(state, index);

    ffi::lua_pushcfunction(state, gettable);
    ffi::lua_pushvalue(state, table_index);
    ffi::lua_pushvalue(state, -3);
    ffi::lua_remove(state, -4);

    handle_error(state, pcall_with_traceback(state, 2, 1))?;
    Ok(ffi::lua_type(state, -1))
}

// Protected version of lua_settable, uses 4 stack spaces, does not call checkstack.
pub unsafe fn psettable(state: *mut ffi::lua_State, index: c_int) -> Result<()> {
    unsafe extern "C" fn settable(state: *mut ffi::lua_State) -> c_int {
        ffi::lua_settable(state, -3);
        0
    }

    let table_index = ffi::lua_absindex(state, index);

    ffi::lua_pushcfunction(state, settable);
    ffi::lua_pushvalue(state, table_index);
    ffi::lua_pushvalue(state, -4);
    ffi::lua_pushvalue(state, -4);
    ffi::lua_remove(state, -5);
    ffi::lua_remove(state, -5);

    handle_error(state, pcall_with_traceback(state, 3, 0))?;
    Ok(())
}

// Protected version of luaL_len, uses 2 stack spaces, does not call checkstack.
pub unsafe fn plen(state: *mut ffi::lua_State, index: c_int) -> Result<ffi::lua_Integer> {
    unsafe extern "C" fn len(state: *mut ffi::lua_State) -> c_int {
        ffi::lua_pushinteger(state, ffi::luaL_len(state, -1));
        1
    }

    let table_index = ffi::lua_absindex(state, index);

    ffi::lua_pushcfunction(state, len);
    ffi::lua_pushvalue(state, table_index);

    handle_error(state, pcall_with_traceback(state, 1, 1))?;
    let len = ffi::lua_tointeger(state, -1);
    ffi::lua_pop(state, 1);
    Ok(len)
}

// Protected version of lua_geti, uses 3 stack spaces, does not call checkstack.
pub unsafe fn pgeti(
    state: *mut ffi::lua_State,
    index: c_int,
    i: ffi::lua_Integer,
) -> Result<c_int> {
    unsafe extern "C" fn geti(state: *mut ffi::lua_State) -> c_int {
        let i = ffi::lua_tointeger(state, -1);
        ffi::lua_geti(state, -2, i);
        1
    }

    let table_index = ffi::lua_absindex(state, index);

    ffi::lua_pushcfunction(state, geti);
    ffi::lua_pushvalue(state, table_index);
    ffi::lua_pushinteger(state, i);

    handle_error(state, pcall_with_traceback(state, 2, 1))?;
    Ok(ffi::lua_type(state, -1))
}

// Protected version of lua_next, uses 3 stack spaces, does not call checkstack.
pub unsafe fn pnext(state: *mut ffi::lua_State, index: c_int) -> Result<c_int> {
    unsafe extern "C" fn next(state: *mut ffi::lua_State) -> c_int {
        if ffi::lua_next(state, -2) == 0 { 0 } else { 2 }
    }

    let table_index = ffi::lua_absindex(state, index);

    ffi::lua_pushcfunction(state, next);
    ffi::lua_pushvalue(state, table_index);
    ffi::lua_pushvalue(state, -3);
    ffi::lua_remove(state, -4);

    let stack_start = ffi::lua_gettop(state) - 3;
    handle_error(state, pcall_with_traceback(state, 2, ffi::LUA_MULTRET))?;
    let nresults = ffi::lua_gettop(state) - stack_start;
    if nresults == 0 { Ok(0) } else { Ok(1) }
}

// If the return code indicates an error, pops the error off of the stack and
// returns Err. If the error is actually a WrappedPaic, clears the current lua
// stack continues the panic.  If the error on the top of the stack is actually
// a WrappedError, just returns it.  Otherwise, interprets the error as the
// appropriate lua error.
pub unsafe fn handle_error(state: *mut ffi::lua_State, err: c_int) -> Result<()> {
    if err == ffi::LUA_OK || err == ffi::LUA_YIELD {
        Ok(())
    } else {
        if let Some(err) = pop_wrapped_error(state) {
            Err(err)

        } else if is_wrapped_panic(state, -1) {
            let panic = &mut *get_userdata::<WrappedPanic>(state, -1);
            if let Some(p) = panic.0.take() {
                ffi::lua_settop(state, 0);
                resume_unwind(p);
            } else {
                lua_panic!(state, "internal error: panic was resumed twice")
            }

        } else {
            let err_string =
                if let Some(s) = ffi::lua_tolstring(state, -1, ptr::null_mut()).as_ref() {
                    CStr::from_ptr(s)
                        .to_str()
                        .unwrap_or_else(|_| "<unprintable error>")
                        .to_owned()
                } else {
                    "<unprintable error>".to_owned()
                };
            ffi::lua_pop(state, 1);

            Err(match err {
                ffi::LUA_ERRRUN => Error::RuntimeError(err_string),
                ffi::LUA_ERRSYNTAX => {
                    // This seems terrible, but as far as I can tell, this is exactly what the stock
                    // lua repl does.
                    if err_string.ends_with("<eof>") {
                        Error::IncompleteStatement(err_string)
                    } else {
                        Error::SyntaxError(err_string)
                    }
                }
                ffi::LUA_ERRERR => Error::ErrorError(err_string),
                ffi::LUA_ERRMEM => {
                    // This is not impossible to hit, but this library is not set up
                    // to handle this properly.  Lua does a longjmp on out of memory
                    // (like all lua errors), but it can do this from a huge number
                    // of lua functions, and it is extremely difficult to set up the
                    // pcall protection for every lua function that might allocate.
                    // If lua does this in an unprotected context, it will abort
                    // anyway, so the best we can do right now is guarantee an abort
                    // even in a protected context.
                    println!("Lua memory error, aborting!");
                    process::abort()
                }
                ffi::LUA_ERRGCMM => {
                    // This should be impossible, or at least is indicative of an
                    // internal bug.  Similarly to LUA_ERRMEM, this could indicate a
                    // longjmp out of rust code, so we just abort.
                    println!("Lua error during __gc, aborting!");
                    process::abort()
                }
                _ => lua_panic!(state, "internal error: unrecognized lua error code"),
            })
        }
    }
}

pub unsafe fn push_string(state: *mut ffi::lua_State, s: &str) {
    ffi::lua_pushlstring(state, s.as_ptr() as *const c_char, s.len());
}

pub unsafe fn push_userdata<T>(state: *mut ffi::lua_State, t: T) -> *mut T {
    let ud = ffi::lua_newuserdata(state, mem::size_of::<Option<T>>()) as *mut Option<T>;
    ptr::write(ud, None);
    *ud = Some(t);
    (*ud).as_mut().unwrap()
}

pub unsafe fn get_userdata<T>(state: *mut ffi::lua_State, index: c_int) -> *mut T {
    let ud = ffi::lua_touserdata(state, index) as *mut Option<T>;
    lua_assert!(state, !ud.is_null());
    (*ud).as_mut().expect("access of expired userdata")
}

pub unsafe extern "C" fn userdata_destructor<T>(state: *mut ffi::lua_State) -> c_int {
    match catch_unwind(|| {
        *(ffi::lua_touserdata(state, 1) as *mut Option<T>) = None;
        0
    }) {
        Ok(r) => r,
        Err(p) => {
            push_wrapped_panic(state, p);
            ffi::lua_error(state)
        }
    }
}

// In the context of a lua callback, this will call the given function and if the given function
// returns an error, *or if the given function panics*, this will result in a call to lua_error (a
// longjmp).  The error or panic is wrapped in such a way that when calling handle_error back on
// the rust side, it will resume the panic.
pub unsafe fn callback_error<R, F>(state: *mut ffi::lua_State, f: F) -> R
where
    F: FnOnce() -> Result<R> + UnwindSafe,
{
    match catch_unwind(f) {
        Ok(Ok(r)) => r,
        Ok(Err(err)) => {
            push_wrapped_error(state, err);
            ffi::lua_error(state)
        }
        Err(p) => {
            push_wrapped_panic(state, p);
            ffi::lua_error(state)
        }
    }
}

// ffi::lua_pcall with a message handler that gives a nice traceback.  If the
// caught error is actually a Error, will simply pass the error along.  Does
// not call checkstack, and uses 2 extra stack spaces.
pub unsafe fn pcall_with_traceback(
    state: *mut ffi::lua_State,
    nargs: c_int,
    nresults: c_int,
) -> c_int {
    unsafe extern "C" fn message_handler(state: *mut ffi::lua_State) -> c_int {
        if let Some(error) = pop_wrapped_error(state) {
            ffi::luaL_traceback(state, state, ptr::null(), 0);
            let traceback = CStr::from_ptr(ffi::lua_tolstring(state, -1, ptr::null_mut()))
                .to_str()
                .unwrap_or_else(|_| "<could not capture traceback>")
                .to_owned();
            push_wrapped_error(state, Error::CallbackError(traceback, Arc::new(error)));
            ffi::lua_remove(state, -2);
        } else if !is_wrapped_panic(state, 1) {
            let s = ffi::lua_tolstring(state, 1, ptr::null_mut());
            if !s.is_null() {
                ffi::luaL_traceback(state, state, s, 0);
            } else {
                ffi::luaL_traceback(state, state, cstr!("<unprintable lua error>"), 0);
            }
            ffi::lua_remove(state, -2);
        }
        1
    }

    let msgh_position = ffi::lua_gettop(state) - nargs;
    ffi::lua_pushcfunction(state, message_handler);
    ffi::lua_insert(state, msgh_position);
    let ret = ffi::lua_pcall(state, nargs, nresults, msgh_position);
    ffi::lua_remove(state, msgh_position);
    ret
}

pub unsafe fn resume_with_traceback(
    state: *mut ffi::lua_State,
    from: *mut ffi::lua_State,
    nargs: c_int,
) -> c_int {
    let res = ffi::lua_resume(state, from, nargs);
    if res != ffi::LUA_OK && res != ffi::LUA_YIELD {
        if let Some(error) = pop_wrapped_error(state) {
            ffi::luaL_traceback(state, state, ptr::null(), 0);
            let traceback = CStr::from_ptr(ffi::lua_tolstring(state, -1, ptr::null_mut()))
                .to_str()
                .unwrap_or_else(|_| "<could not capture traceback>")
                .to_owned();
            push_wrapped_error(state, Error::CallbackError(traceback, Arc::new(error)));
            ffi::lua_remove(state, -2);
        } else if !is_wrapped_panic(state, 1) {
            let s = ffi::lua_tolstring(state, 1, ptr::null_mut());
            if !s.is_null() {
                ffi::luaL_traceback(state, state, s, 0);
            } else {
                ffi::luaL_traceback(state, state, cstr!("<unprintable lua error>"), 0);
            }
            ffi::lua_remove(state, -2);
        }
    }
    res
}

// A variant of pcall that does not allow lua to catch panic errors from callback_error
pub unsafe extern "C" fn safe_pcall(state: *mut ffi::lua_State) -> c_int {
    if ffi::lua_pcall(state, ffi::lua_gettop(state) - 1, ffi::LUA_MULTRET, 0) != ffi::LUA_OK {
        if is_wrapped_panic(state, -1) {
            ffi::lua_error(state);
        }
        ffi::lua_pushboolean(state, 0);
        ffi::lua_insert(state, -2);
        2
    } else {
        ffi::lua_pushboolean(state, 1);
        ffi::lua_insert(state, 1);
        ffi::lua_gettop(state)
    }
}

// A variant of xpcall that does not allow lua to catch panic errors from callback_error
pub unsafe extern "C" fn safe_xpcall(state: *mut ffi::lua_State) -> c_int {
    unsafe extern "C" fn xpcall_msgh(state: *mut ffi::lua_State) -> c_int {
        if is_wrapped_panic(state, -1) {
            1
        } else {
            ffi::lua_pushvalue(state, ffi::lua_upvalueindex(1));
            ffi::lua_insert(state, 1);
            ffi::lua_call(state, ffi::lua_gettop(state) - 1, ffi::LUA_MULTRET);
            ffi::lua_gettop(state)
        }
    }

    ffi::lua_pushvalue(state, 2);
    ffi::lua_pushcclosure(state, xpcall_msgh, 1);
    ffi::lua_copy(state, 1, 2);
    ffi::lua_insert(state, 1);

    let res = ffi::lua_pcall(state, ffi::lua_gettop(state) - 2, ffi::LUA_MULTRET, 1);
    if res != ffi::LUA_OK {
        if is_wrapped_panic(state, -1) {
            ffi::lua_error(state);
        }
        ffi::lua_pushboolean(state, 0);
        ffi::lua_insert(state, -2);
        2
    } else {
        ffi::lua_pushboolean(state, 1);
        ffi::lua_insert(state, 1);
        ffi::lua_gettop(state)
    }
}

/// Does not call checkstack, uses 1 stack space
pub unsafe fn main_state(state: *mut ffi::lua_State) -> *mut ffi::lua_State {
    ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, ffi::LUA_RIDX_MAINTHREAD);
    let main_state = ffi::lua_tothread(state, -1);
    ffi::lua_pop(state, 1);
    main_state
}

pub struct WrappedError(pub Error);
pub struct WrappedPanic(pub Option<Box<Any + Send>>);

// Pushes a WrappedError::Error to the top of the stack
pub unsafe fn push_wrapped_error(state: *mut ffi::lua_State, err: Error) {
    unsafe extern "C" fn error_tostring(state: *mut ffi::lua_State) -> c_int {
        callback_error(state, || if is_wrapped_error(state, -1) {
            let error = &*get_userdata::<WrappedError>(state, -1);
            push_string(state, &error.0.to_string());
            ffi::lua_remove(state, -2);

            Ok(1)

        } else {
            panic!("internal error: userdata mismatch in Error metamethod");
        })
    }

    ffi::luaL_checkstack(state, 2, ptr::null());

    push_userdata(state, WrappedError(err));

    get_error_metatable(state);
    if ffi::lua_isnil(state, -1) != 0 {
        ffi::lua_pop(state, 1);

        ffi::luaL_checkstack(state, 7, ptr::null());

        ffi::lua_newtable(state);
        ffi::lua_pushlightuserdata(
            state,
            &ERROR_METATABLE_REGISTRY_KEY as *const u8 as *mut c_void,
        );
        ffi::lua_pushvalue(state, -2);

        push_string(state, "__gc");
        ffi::lua_pushcfunction(state, userdata_destructor::<WrappedError>);
        ffi::lua_settable(state, -3);

        push_string(state, "__tostring");
        ffi::lua_pushcfunction(state, error_tostring);
        ffi::lua_settable(state, -3);

        push_string(state, "__metatable");
        ffi::lua_pushboolean(state, 0);
        ffi::lua_settable(state, -3);

        ffi::lua_settable(state, ffi::LUA_REGISTRYINDEX);
    }

    ffi::lua_setmetatable(state, -2);
}

// Pushes a WrappedError::Panic to the top of the stack
pub unsafe fn push_wrapped_panic(state: *mut ffi::lua_State, panic: Box<Any + Send>) {
    ffi::luaL_checkstack(state, 2, ptr::null());

    push_userdata(state, WrappedPanic(Some(panic)));

    get_panic_metatable(state);
    if ffi::lua_isnil(state, -1) != 0 {
        ffi::lua_pop(state, 1);

        ffi::luaL_checkstack(state, 7, ptr::null());

        ffi::lua_newtable(state);
        ffi::lua_pushlightuserdata(
            state,
            &PANIC_METATABLE_REGISTRY_KEY as *const u8 as *mut c_void,
        );
        ffi::lua_pushvalue(state, -2);

        push_string(state, "__gc");
        ffi::lua_pushcfunction(state, userdata_destructor::<WrappedPanic>);
        ffi::lua_settable(state, -3);

        push_string(state, "__metatable");
        ffi::lua_pushboolean(state, 0);
        ffi::lua_settable(state, -3);

        ffi::lua_settable(state, ffi::LUA_REGISTRYINDEX);
    }

    ffi::lua_setmetatable(state, -2);
}

// Pops a WrappedError off of the top of the stack, if it is a WrappedError.  If
// it is not a WrappedError, returns None and does not pop anything.
pub unsafe fn pop_wrapped_error(state: *mut ffi::lua_State) -> Option<Error> {
    if ffi::lua_gettop(state) == 0 || !is_wrapped_error(state, -1) {
        None
    } else {
        let err = &*get_userdata::<WrappedError>(state, -1);
        let err = err.0.clone();
        ffi::lua_pop(state, 1);
        Some(err)
    }
}

// Checks if the value at the given index is a WrappedError
pub unsafe fn is_wrapped_error(state: *mut ffi::lua_State, index: c_int) -> bool {
    assert_ne!(
        ffi::lua_checkstack(state, 2),
        0,
        "somehow not enough stack space to check if a value is a WrappedError"
    );

    let index = ffi::lua_absindex(state, index);

    let userdata = ffi::lua_touserdata(state, index);
    if userdata.is_null() {
        return false;
    }

    if ffi::lua_getmetatable(state, index) == 0 {
        return false;
    }

    get_error_metatable(state);
    let res = ffi::lua_rawequal(state, -1, -2) != 0;
    ffi::lua_pop(state, 2);
    res
}

// Checks if the value at the given index is a WrappedPanic
pub unsafe fn is_wrapped_panic(state: *mut ffi::lua_State, index: c_int) -> bool {
    assert_ne!(
        ffi::lua_checkstack(state, 2),
        0,
        "somehow not enough stack space to check if a value is a wrapped panic"
    );

    let index = ffi::lua_absindex(state, index);

    let userdata = ffi::lua_touserdata(state, index);
    if userdata.is_null() {
        return false;
    }

    if ffi::lua_getmetatable(state, index) == 0 {
        return false;
    }

    get_panic_metatable(state);
    let res = ffi::lua_rawequal(state, -1, -2) != 0;
    ffi::lua_pop(state, 2);
    return res;
}

pub unsafe fn get_error_metatable(state: *mut ffi::lua_State) -> c_int {
    ffi::lua_pushlightuserdata(
        state,
        &ERROR_METATABLE_REGISTRY_KEY as *const u8 as *mut c_void,
    );
    ffi::lua_gettable(state, ffi::LUA_REGISTRYINDEX)
}

pub unsafe fn get_panic_metatable(state: *mut ffi::lua_State) -> c_int {
    ffi::lua_pushlightuserdata(
        state,
        &PANIC_METATABLE_REGISTRY_KEY as *const u8 as *mut c_void,
    );
    ffi::lua_gettable(state, ffi::LUA_REGISTRYINDEX)
}

static ERROR_METATABLE_REGISTRY_KEY: u8 = 0;
static PANIC_METATABLE_REGISTRY_KEY: u8 = 0;