3 ** Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h
21 ** LuaJIT can either use internal or external frame unwinding:
23 ** - Internal frame unwinding (INT) is free-standing and doesn't require
24 ** any OS or library support.
26 ** - External frame unwinding (EXT) uses the system-provided unwind handler.
30 ** - EXT requires unwind tables for *all* functions on the C stack between
31 ** the pcall/catch and the error/throw. This is the default on x64,
32 ** but needs to be manually enabled on x86/PPC for non-C++ code.
34 ** - INT is faster when actually throwing errors (but this happens rarely).
35 ** Setting up error handlers is zero-cost in any case.
37 ** - EXT provides full interoperability with C++ exceptions. You can throw
38 ** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames.
39 ** C++ destructors are called as needed. C++ exceptions caught by pcall
40 ** are converted to the string "C++ exception". Lua errors can be caught
41 ** with catch (...) in C++.
43 ** - INT has only limited support for automatically catching C++ exceptions
44 ** on POSIX systems using DWARF2 stack unwinding. Other systems may use
45 ** the wrapper function feature. Lua errors thrown through C++ frames
46 ** cannot be caught by C++ code and C++ destructors are not run.
48 ** EXT is the default on x64 systems, INT is the default on all other systems.
50 ** EXT can be manually enabled on POSIX systems using GCC and DWARF2 stack
51 ** unwinding with -DLUAJIT_UNWIND_EXTERNAL. *All* C code must be compiled
52 ** with -funwind-tables (or -fexceptions). This includes LuaJIT itself (set
53 ** TARGET_CFLAGS), all of your C/Lua binding code, all loadable C modules
54 ** and all C libraries that have callbacks which may be used to call back
55 ** into Lua. C++ code must *not* be compiled with -fno-exceptions.
57 ** EXT cannot be enabled on WIN32 since system exceptions use code-driven SEH.
58 ** EXT is mandatory on WIN64 since the calling convention has an abundance
59 ** of callee-saved registers (rbx, rbp, rsi, rdi, r12-r15, xmm6-xmm15).
60 ** The POSIX/x64 interpreter only saves r12/r13 for INT (e.g. PS4).
63 #if defined(__GNUC__) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL)) && !LJ_NO_UNWIND
64 #define LJ_UNWIND_EXT 1
65 #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS
66 #define LJ_UNWIND_EXT 1
69 /* -- Error messages ------------------------------------------------------ */
71 /* Error message strings. */
72 LJ_DATADEF
const char *lj_err_allmsg
=
73 #define ERRDEF(name, msg) msg "\0"
74 #include "lj_errmsg.h"
77 /* -- Internal frame unwinding -------------------------------------------- */
79 /* Unwind Lua stack and move error message to new top. */
80 LJ_NOINLINE
static void unwindstack(lua_State
*L
, TValue
*top
)
82 lj_func_closeuv(L
, top
);
84 copyTV(L
, top
, L
->top
-1);
87 lj_state_relimitstack(L
);
90 /* Unwind until stop frame. Optionally cleanup frames. */
91 static void *err_unwind(lua_State
*L
, void *stopcf
, int errcode
)
93 TValue
*frame
= L
->base
-1;
96 int32_t nres
= cframe_nres(cframe_raw(cf
));
97 if (nres
< 0) { /* C frame without Lua frame? */
98 TValue
*top
= restorestack(L
, -nres
);
99 if (frame
< top
) { /* Frame reached? */
101 L
->cframe
= cframe_prev(cf
);
108 if (frame
<= tvref(L
->stack
))
110 switch (frame_typep(frame
)) {
111 case FRAME_LUA
: /* Lua frame. */
113 frame
= frame_prevl(frame
);
115 case FRAME_C
: /* C frame. */
121 L
->cframe
= cframe_prev(cf
);
122 L
->base
= frame_prevd(frame
) + 1;
123 unwindstack(L
, frame
);
124 } else if (cf
!= stopcf
) {
125 cf
= cframe_prev(cf
);
126 frame
= frame_prevd(frame
);
129 return NULL
; /* Continue unwinding. */
132 cf
= cframe_prev(cf
);
133 frame
= frame_prevd(frame
);
136 case FRAME_CP
: /* Protected C frame. */
137 if (cframe_canyield(cf
)) { /* Resume? */
139 hook_leave(G(L
)); /* Assumes nobody uses coroutines inside hooks. */
141 L
->status
= (uint8_t)errcode
;
146 L
->cframe
= cframe_prev(cf
);
147 L
->base
= frame_prevd(frame
) + 1;
148 unwindstack(L
, frame
);
151 case FRAME_CONT
: /* Continuation frame. */
153 if ((frame
-1)->u32
.lo
== LJ_CONT_FFI_CALLBACK
)
156 case FRAME_VARG
: /* Vararg frame. */
157 frame
= frame_prevd(frame
);
159 case FRAME_PCALL
: /* FF pcall() frame. */
160 case FRAME_PCALLH
: /* FF pcall() frame inside hook. */
162 if (errcode
== LUA_YIELD
) {
163 frame
= frame_prevd(frame
);
166 if (frame_typep(frame
) == FRAME_PCALL
)
169 L
->base
= frame_prevd(frame
) + 1;
170 unwindstack(L
, L
->base
);
172 return (void *)((intptr_t)cf
| CFRAME_UNWIND_FF
);
178 L
->base
= tvref(L
->stack
)+1;
179 unwindstack(L
, L
->base
);
184 return L
; /* Anything non-NULL will do. */
187 /* -- External frame unwinding -------------------------------------------- */
189 #if defined(__GNUC__) && !LJ_NO_UNWIND && !LJ_ABI_WIN
192 ** We have to use our own definitions instead of the mandatory (!) unwind.h,
193 ** since various OS, distros and compilers mess up the header installation.
196 typedef struct _Unwind_Exception
199 void (*excleanup
)(int, struct _Unwind_Exception
*);
201 } __attribute__((__aligned__
)) _Unwind_Exception
;
203 typedef struct _Unwind_Context _Unwind_Context
;
206 #define _URC_FATAL_PHASE1_ERROR 3
207 #define _URC_HANDLER_FOUND 6
208 #define _URC_INSTALL_CONTEXT 7
209 #define _URC_CONTINUE_UNWIND 8
210 #define _URC_FAILURE 9
214 extern uintptr_t _Unwind_GetCFA(_Unwind_Context
*);
215 extern void _Unwind_SetGR(_Unwind_Context
*, int, uintptr_t);
216 extern void _Unwind_SetIP(_Unwind_Context
*, uintptr_t);
217 extern void _Unwind_DeleteException(_Unwind_Exception
*);
218 extern int _Unwind_RaiseException(_Unwind_Exception
*);
220 #define _UA_SEARCH_PHASE 1
221 #define _UA_CLEANUP_PHASE 2
222 #define _UA_HANDLER_FRAME 4
223 #define _UA_FORCE_UNWIND 8
225 #define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */
226 #define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c))
227 #define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff)
228 #define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff))
230 /* DWARF2 personality handler referenced from interpreter .eh_frame. */
231 LJ_FUNCA
int lj_err_unwind_dwarf(int version
, int actions
,
232 uint64_t uexclass
, _Unwind_Exception
*uex
, _Unwind_Context
*ctx
)
237 return _URC_FATAL_PHASE1_ERROR
;
239 cf
= (void *)_Unwind_GetCFA(ctx
);
241 if ((actions
& _UA_SEARCH_PHASE
)) {
243 if (err_unwind(L
, cf
, 0) == NULL
)
244 return _URC_CONTINUE_UNWIND
;
246 if (!LJ_UEXCLASS_CHECK(uexclass
)) {
247 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
249 return _URC_HANDLER_FOUND
;
251 if ((actions
& _UA_CLEANUP_PHASE
)) {
253 if (LJ_UEXCLASS_CHECK(uexclass
)) {
254 errcode
= LJ_UEXCLASS_ERRCODE(uexclass
);
256 if ((actions
& _UA_HANDLER_FRAME
))
257 _Unwind_DeleteException(uex
);
258 errcode
= LUA_ERRRUN
;
261 cf
= err_unwind(L
, cf
, errcode
);
262 if ((actions
& _UA_FORCE_UNWIND
)) {
263 return _URC_CONTINUE_UNWIND
;
265 _Unwind_SetGR(ctx
, LJ_TARGET_EHRETREG
, errcode
);
266 _Unwind_SetIP(ctx
, (uintptr_t)(cframe_unwind_ff(cf
) ?
269 return _URC_INSTALL_CONTEXT
;
271 #if LJ_TARGET_X86ORX64
272 else if ((actions
& _UA_HANDLER_FRAME
)) {
273 /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/
274 ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h
276 _Unwind_SetGR(ctx
, LJ_TARGET_EHRETREG
, errcode
);
277 _Unwind_SetIP(ctx
, (uintptr_t)lj_vm_unwind_rethrow
);
278 return _URC_INSTALL_CONTEXT
;
282 /* This is not the proper way to escape from the unwinder. We get away with
283 ** it on non-x64 because the interpreter restores all callee-saved regs.
285 lj_err_throw(L
, errcode
);
288 return _URC_CONTINUE_UNWIND
;
292 #if LJ_TARGET_OSX || defined(__OpenBSD__)
293 /* Sorry, no thread safety for OSX. Complain to Apple, not me. */
294 static _Unwind_Exception static_uex
;
296 static __thread _Unwind_Exception static_uex
;
299 /* Raise DWARF2 exception. */
300 static void err_raise_ext(int errcode
)
302 static_uex
.exclass
= LJ_UEXCLASS_MAKE(errcode
);
303 static_uex
.excleanup
= NULL
;
304 _Unwind_RaiseException(&static_uex
);
310 extern void _Unwind_DeleteException(void *);
311 extern int __gnu_unwind_frame (void *, _Unwind_Context
*);
312 extern int _Unwind_VRS_Set(_Unwind_Context
*, int, uint32_t, int, void *);
313 extern int _Unwind_VRS_Get(_Unwind_Context
*, int, uint32_t, int, void *);
315 static inline uint32_t _Unwind_GetGR(_Unwind_Context
*ctx
, int r
)
318 _Unwind_VRS_Get(ctx
, 0, r
, 0, &v
);
322 static inline void _Unwind_SetGR(_Unwind_Context
*ctx
, int r
, uint32_t v
)
324 _Unwind_VRS_Set(ctx
, 0, r
, 0, &v
);
327 #define _US_VIRTUAL_UNWIND_FRAME 0
328 #define _US_UNWIND_FRAME_STARTING 1
329 #define _US_ACTION_MASK 3
330 #define _US_FORCE_UNWIND 8
332 /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */
333 LJ_FUNCA
int lj_err_unwind_arm(int state
, void *ucb
, _Unwind_Context
*ctx
)
335 void *cf
= (void *)_Unwind_GetGR(ctx
, 13);
336 lua_State
*L
= cframe_L(cf
);
337 if ((state
& _US_ACTION_MASK
) == _US_VIRTUAL_UNWIND_FRAME
) {
338 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
339 return _URC_HANDLER_FOUND
;
341 if ((state
&(_US_ACTION_MASK
|_US_FORCE_UNWIND
)) == _US_UNWIND_FRAME_STARTING
) {
342 _Unwind_DeleteException(ucb
);
343 _Unwind_SetGR(ctx
, 15, (uint32_t)(void *)lj_err_throw
);
344 _Unwind_SetGR(ctx
, 0, (uint32_t)L
);
345 _Unwind_SetGR(ctx
, 1, (uint32_t)LUA_ERRRUN
);
346 return _URC_INSTALL_CONTEXT
;
348 if (__gnu_unwind_frame(ucb
, ctx
) != _URC_OK
)
350 return _URC_CONTINUE_UNWIND
;
355 #elif LJ_TARGET_X64 && LJ_ABI_WIN
358 ** Someone in Redmond owes me several days of my life. A lot of this is
359 ** undocumented or just plain wrong on MSDN. Some of it can be gathered
360 ** from 3rd party docs or must be found by trial-and-error. They really
361 ** don't want you to write your own language-specific exception handler
362 ** or to interact gracefully with MSVC. :-(
364 ** Apparently MSVC doesn't call C++ destructors for foreign exceptions
365 ** unless you compile your C++ code with /EHa. Unfortunately this means
366 ** catch (...) also catches things like access violations. The use of
367 ** _set_se_translator doesn't really help, because it requires /EHa, too.
370 #define WIN32_LEAN_AND_MEAN
373 /* Taken from: http://www.nynaeve.net/?p=99 */
374 typedef struct UndocumentedDispatcherContext
{
377 PRUNTIME_FUNCTION FunctionEntry
;
378 ULONG64 EstablisherFrame
;
380 PCONTEXT ContextRecord
;
381 void (*LanguageHandler
)(void);
383 PUNWIND_HISTORY_TABLE HistoryTable
;
386 } UndocumentedDispatcherContext
;
388 /* Another wild guess. */
389 extern void __DestructExceptionObject(EXCEPTION_RECORD
*rec
, int nothrow
);
391 #ifdef MINGW_SDK_INIT
392 /* Workaround for broken MinGW64 declaration. */
393 VOID
RtlUnwindEx_FIXED(PVOID
,PVOID
,PVOID
,PVOID
,PVOID
,PVOID
) asm("RtlUnwindEx");
394 #define RtlUnwindEx RtlUnwindEx_FIXED
397 #define LJ_MSVC_EXCODE ((DWORD)0xe06d7363)
398 #define LJ_GCC_EXCODE ((DWORD)0x20474343)
400 #define LJ_EXCODE ((DWORD)0xe24c4a00)
401 #define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c))
402 #define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff)
403 #define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff))
405 /* Win64 exception handler for interpreter frame. */
406 LJ_FUNCA EXCEPTION_DISPOSITION
lj_err_unwind_win64(EXCEPTION_RECORD
*rec
,
407 void *cf
, CONTEXT
*ctx
, UndocumentedDispatcherContext
*dispatch
)
409 lua_State
*L
= cframe_L(cf
);
410 int errcode
= LJ_EXCODE_CHECK(rec
->ExceptionCode
) ?
411 LJ_EXCODE_ERRCODE(rec
->ExceptionCode
) : LUA_ERRRUN
;
412 if ((rec
->ExceptionFlags
& 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */
413 /* Unwind internal frames. */
414 err_unwind(L
, cf
, errcode
);
416 void *cf2
= err_unwind(L
, cf
, 0);
417 if (cf2
) { /* We catch it, so start unwinding the upper frames. */
418 if (rec
->ExceptionCode
== LJ_MSVC_EXCODE
||
419 rec
->ExceptionCode
== LJ_GCC_EXCODE
) {
420 #if LJ_TARGET_WINDOWS
421 __DestructExceptionObject(rec
, 1);
423 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
424 } else if (!LJ_EXCODE_CHECK(rec
->ExceptionCode
)) {
425 /* Don't catch access violations etc. */
426 return ExceptionContinueSearch
;
428 /* Unwind the stack and call all handlers for all lower C frames
429 ** (including ourselves) again with EH_UNWINDING set. Then set
430 ** rsp = cf, rax = errcode and jump to the specified target.
432 RtlUnwindEx(cf
, (void *)((cframe_unwind_ff(cf2
) && errcode
!= LUA_YIELD
) ?
435 rec
, (void *)(uintptr_t)errcode
, ctx
, dispatch
->HistoryTable
);
436 /* RtlUnwindEx should never return. */
439 return ExceptionContinueSearch
;
442 /* Raise Windows exception. */
443 static void err_raise_ext(int errcode
)
445 RaiseException(LJ_EXCODE_MAKE(errcode
), 1 /* EH_NONCONTINUABLE */, 0, NULL
);
450 /* -- Error handling ------------------------------------------------------ */
452 /* Throw error. Find catch frame, unwind stack and continue. */
453 LJ_NOINLINE
void LJ_FASTCALL
lj_err_throw(lua_State
*L
, int errcode
)
455 global_State
*g
= G(L
);
457 setgcrefnull(g
->jit_L
);
460 err_raise_ext(errcode
);
462 ** A return from this function signals a corrupt C stack that cannot be
463 ** unwound. We have no choice but to call the panic function and exit.
465 ** Usually this is caused by a C function without unwind information.
466 ** This should never happen on x64, but may happen if you've manually
467 ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every*
468 ** non-C++ file with -funwind-tables.
474 void *cf
= err_unwind(L
, NULL
, errcode
);
475 if (cframe_unwind_ff(cf
))
476 lj_vm_unwind_ff(cframe_raw(cf
));
478 lj_vm_unwind_c(cframe_raw(cf
), errcode
);
484 /* Return string object for error message. */
485 LJ_NOINLINE GCstr
*lj_err_str(lua_State
*L
, ErrMsg em
)
487 return lj_str_newz(L
, err2msg(em
));
490 /* Out-of-memory error. */
491 LJ_NOINLINE
void lj_err_mem(lua_State
*L
)
493 if (L
->status
== LUA_ERRERR
+1) /* Don't touch the stack during lua_open. */
494 lj_vm_unwind_c(L
->cframe
, LUA_ERRMEM
);
495 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRMEM
));
496 lj_err_throw(L
, LUA_ERRMEM
);
499 /* Find error function for runtime errors. Requires an extra stack traversal. */
500 static ptrdiff_t finderrfunc(lua_State
*L
)
502 cTValue
*frame
= L
->base
-1, *bot
= tvref(L
->stack
);
503 void *cf
= L
->cframe
;
504 while (frame
> bot
&& cf
) {
505 while (cframe_nres(cframe_raw(cf
)) < 0) { /* cframe without frame? */
506 if (frame
>= restorestack(L
, -cframe_nres(cf
)))
508 if (cframe_errfunc(cf
) >= 0) /* Error handler not inherited (-1)? */
509 return cframe_errfunc(cf
);
510 cf
= cframe_prev(cf
); /* Else unwind cframe and continue searching. */
514 switch (frame_typep(frame
)) {
517 frame
= frame_prevl(frame
);
520 cf
= cframe_prev(cf
);
523 frame
= frame_prevd(frame
);
527 if ((frame
-1)->u32
.lo
== LJ_CONT_FFI_CALLBACK
)
528 cf
= cframe_prev(cf
);
530 frame
= frame_prevd(frame
);
533 if (cframe_canyield(cf
)) return 0;
534 if (cframe_errfunc(cf
) >= 0)
535 return cframe_errfunc(cf
);
536 frame
= frame_prevd(frame
);
540 if (frame_ftsz(frame
) >= (ptrdiff_t)(2*sizeof(TValue
))) /* xpcall? */
541 return savestack(L
, frame
-1); /* Point to xpcall's errorfunc. */
552 LJ_NOINLINE
void lj_err_run(lua_State
*L
)
554 ptrdiff_t ef
= finderrfunc(L
);
556 TValue
*errfunc
= restorestack(L
, ef
);
557 TValue
*top
= L
->top
;
558 lj_trace_abort(G(L
));
559 if (!tvisfunc(errfunc
) || L
->status
== LUA_ERRERR
) {
560 setstrV(L
, top
-1, lj_err_str(L
, LJ_ERR_ERRERR
));
561 lj_err_throw(L
, LUA_ERRERR
);
563 L
->status
= LUA_ERRERR
;
564 copyTV(L
, top
, top
-1);
565 copyTV(L
, top
-1, errfunc
);
567 lj_vm_call(L
, top
, 1+1); /* Stack: |errfunc|msg| -> |msg| */
569 lj_err_throw(L
, LUA_ERRRUN
);
572 /* Formatted runtime error message. */
573 LJ_NORET LJ_NOINLINE
static void err_msgv(lua_State
*L
, ErrMsg em
, ...)
578 if (curr_funcisL(L
)) L
->top
= curr_topL(L
);
579 msg
= lj_str_pushvf(L
, err2msg(em
), argp
);
581 lj_debug_addloc(L
, msg
, L
->base
-1, NULL
);
585 /* Non-vararg variant for better calling conventions. */
586 LJ_NOINLINE
void lj_err_msg(lua_State
*L
, ErrMsg em
)
592 LJ_NOINLINE
void lj_err_lex(lua_State
*L
, GCstr
*src
, const char *tok
,
593 BCLine line
, ErrMsg em
, va_list argp
)
595 char buff
[LUA_IDSIZE
];
597 lj_debug_shortname(buff
, src
);
598 msg
= lj_str_pushvf(L
, err2msg(em
), argp
);
599 msg
= lj_str_pushf(L
, "%s:%d: %s", buff
, line
, msg
);
601 lj_str_pushf(L
, err2msg(LJ_ERR_XNEAR
), msg
, tok
);
602 lj_err_throw(L
, LUA_ERRSYNTAX
);
605 /* Typecheck error for operands. */
606 LJ_NOINLINE
void lj_err_optype(lua_State
*L
, cTValue
*o
, ErrMsg opm
)
608 const char *tname
= lj_typename(o
);
609 const char *opname
= err2msg(opm
);
610 if (curr_funcisL(L
)) {
611 GCproto
*pt
= curr_proto(L
);
612 const BCIns
*pc
= cframe_Lpc(L
) - 1;
613 const char *oname
= NULL
;
614 const char *kind
= lj_debug_slotname(pt
, pc
, (BCReg
)(o
-L
->base
), &oname
);
616 err_msgv(L
, LJ_ERR_BADOPRT
, opname
, kind
, oname
, tname
);
618 err_msgv(L
, LJ_ERR_BADOPRV
, opname
, tname
);
621 /* Typecheck error for ordered comparisons. */
622 LJ_NOINLINE
void lj_err_comp(lua_State
*L
, cTValue
*o1
, cTValue
*o2
)
624 const char *t1
= lj_typename(o1
);
625 const char *t2
= lj_typename(o2
);
626 err_msgv(L
, t1
== t2
? LJ_ERR_BADCMPV
: LJ_ERR_BADCMPT
, t1
, t2
);
627 /* This assumes the two "boolean" entries are commoned by the C compiler. */
630 /* Typecheck error for __call. */
631 LJ_NOINLINE
void lj_err_optype_call(lua_State
*L
, TValue
*o
)
633 /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object:
634 ** L->base still points to the caller. So add a dummy frame with L instead
635 ** of a function. See lua_getstack().
637 const BCIns
*pc
= cframe_Lpc(L
);
638 if (((ptrdiff_t)pc
& FRAME_TYPE
) != FRAME_LUA
) {
639 const char *tname
= lj_typename(o
);
641 setframe_gc(o
, obj2gco(L
));
642 L
->top
= L
->base
= o
+1;
643 err_msgv(L
, LJ_ERR_BADCALL
, tname
);
645 lj_err_optype(L
, o
, LJ_ERR_OPCALL
);
648 /* Error in context of caller. */
649 LJ_NOINLINE
void lj_err_callermsg(lua_State
*L
, const char *msg
)
651 TValue
*frame
= L
->base
-1;
652 TValue
*pframe
= NULL
;
653 if (frame_islua(frame
)) {
654 pframe
= frame_prevl(frame
);
655 } else if (frame_iscont(frame
)) {
657 if ((frame
-1)->u32
.lo
== LJ_CONT_FFI_CALLBACK
) {
663 pframe
= frame_prevd(frame
);
665 /* Remove frame for FFI metamethods. */
666 if (frame_func(frame
)->c
.ffid
>= FF_ffi_meta___index
&&
667 frame_func(frame
)->c
.ffid
<= FF_ffi_meta___tostring
) {
670 setcframe_pc(cframe_raw(L
->cframe
), frame_contpc(frame
));
675 lj_debug_addloc(L
, msg
, pframe
, frame
);
679 /* Formatted error in context of caller. */
680 LJ_NOINLINE
void lj_err_callerv(lua_State
*L
, ErrMsg em
, ...)
685 msg
= lj_str_pushvf(L
, err2msg(em
), argp
);
687 lj_err_callermsg(L
, msg
);
690 /* Error in context of caller. */
691 LJ_NOINLINE
void lj_err_caller(lua_State
*L
, ErrMsg em
)
693 lj_err_callermsg(L
, err2msg(em
));
696 /* Argument error message. */
697 LJ_NORET LJ_NOINLINE
static void err_argmsg(lua_State
*L
, int narg
,
700 const char *fname
= "?";
701 const char *ftype
= lj_debug_funcname(L
, L
->base
- 1, &fname
);
702 if (narg
< 0 && narg
> LUA_REGISTRYINDEX
)
703 narg
= (int)(L
->top
- L
->base
) + narg
+ 1;
704 if (ftype
&& ftype
[3] == 'h' && --narg
== 0) /* Check for "method". */
705 msg
= lj_str_pushf(L
, err2msg(LJ_ERR_BADSELF
), fname
, msg
);
707 msg
= lj_str_pushf(L
, err2msg(LJ_ERR_BADARG
), narg
, fname
, msg
);
708 lj_err_callermsg(L
, msg
);
711 /* Formatted argument error. */
712 LJ_NOINLINE
void lj_err_argv(lua_State
*L
, int narg
, ErrMsg em
, ...)
717 msg
= lj_str_pushvf(L
, err2msg(em
), argp
);
719 err_argmsg(L
, narg
, msg
);
722 /* Argument error. */
723 LJ_NOINLINE
void lj_err_arg(lua_State
*L
, int narg
, ErrMsg em
)
725 err_argmsg(L
, narg
, err2msg(em
));
728 /* Typecheck error for arguments. */
729 LJ_NOINLINE
void lj_err_argtype(lua_State
*L
, int narg
, const char *xname
)
731 const char *tname
, *msg
;
732 if (narg
<= LUA_REGISTRYINDEX
) {
733 if (narg
>= LUA_GLOBALSINDEX
) {
734 tname
= lj_obj_itypename
[~LJ_TTAB
];
736 GCfunc
*fn
= curr_func(L
);
737 int idx
= LUA_GLOBALSINDEX
- narg
;
738 if (idx
<= fn
->c
.nupvalues
)
739 tname
= lj_typename(&fn
->c
.upvalue
[idx
-1]);
741 tname
= lj_obj_typename
[0];
744 TValue
*o
= narg
< 0 ? L
->top
+ narg
: L
->base
+ narg
-1;
745 tname
= o
< L
->top
? lj_typename(o
) : lj_obj_typename
[0];
747 msg
= lj_str_pushf(L
, err2msg(LJ_ERR_BADTYPE
), xname
, tname
);
748 err_argmsg(L
, narg
, msg
);
751 /* Typecheck error for arguments. */
752 LJ_NOINLINE
void lj_err_argt(lua_State
*L
, int narg
, int tt
)
754 lj_err_argtype(L
, narg
, lj_obj_typename
[tt
+1]);
757 /* -- Public error handling API ------------------------------------------- */
759 LUA_API lua_CFunction
lua_atpanic(lua_State
*L
, lua_CFunction panicf
)
761 lua_CFunction old
= G(L
)->panic
;
762 G(L
)->panic
= panicf
;
766 /* Forwarders for the public API (C calling convention and no LJ_NORET). */
767 LUA_API
int lua_error(lua_State
*L
)
770 return 0; /* unreachable */
773 LUALIB_API
int luaL_argerror(lua_State
*L
, int narg
, const char *msg
)
775 err_argmsg(L
, narg
, msg
);
776 return 0; /* unreachable */
779 LUALIB_API
int luaL_typerror(lua_State
*L
, int narg
, const char *xname
)
781 lj_err_argtype(L
, narg
, xname
);
782 return 0; /* unreachable */
785 LUALIB_API
void luaL_where(lua_State
*L
, int level
)
788 cTValue
*frame
= lj_debug_frame(L
, level
, &size
);
789 lj_debug_addloc(L
, "", frame
, size
? frame
+size
: NULL
);
792 LUALIB_API
int luaL_error(lua_State
*L
, const char *fmt
, ...)
797 msg
= lj_str_pushvf(L
, fmt
, argp
);
799 lj_err_callermsg(L
, msg
);
800 return 0; /* unreachable */