3 ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
19 #include "lj_strfmt.h"
22 ** LuaJIT can either use internal or external frame unwinding:
24 ** - Internal frame unwinding (INT) is free-standing and doesn't require
25 ** any OS or library support.
27 ** - External frame unwinding (EXT) uses the system-provided unwind handler.
31 ** - EXT requires unwind tables for *all* functions on the C stack between
32 ** the pcall/catch and the error/throw. This is the default on x64,
33 ** but needs to be manually enabled on x86/PPC for non-C++ code.
35 ** - INT is faster when actually throwing errors (but this happens rarely).
36 ** Setting up error handlers is zero-cost in any case.
38 ** - EXT provides full interoperability with C++ exceptions. You can throw
39 ** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames.
40 ** C++ destructors are called as needed. C++ exceptions caught by pcall
41 ** are converted to the string "C++ exception". Lua errors can be caught
42 ** with catch (...) in C++.
44 ** - INT has only limited support for automatically catching C++ exceptions
45 ** on POSIX systems using DWARF2 stack unwinding. Other systems may use
46 ** the wrapper function feature. Lua errors thrown through C++ frames
47 ** cannot be caught by C++ code and C++ destructors are not run.
49 ** EXT is the default on x64 systems, INT is the default on all other systems.
51 ** EXT can be manually enabled on POSIX systems using GCC and DWARF2 stack
52 ** unwinding with -DLUAJIT_UNWIND_EXTERNAL. *All* C code must be compiled
53 ** with -funwind-tables (or -fexceptions). This includes LuaJIT itself (set
54 ** TARGET_CFLAGS), all of your C/Lua binding code, all loadable C modules
55 ** and all C libraries that have callbacks which may be used to call back
56 ** into Lua. C++ code must *not* be compiled with -fno-exceptions.
58 ** EXT cannot be enabled on WIN32 since system exceptions use code-driven SEH.
59 ** EXT is mandatory on WIN64 since the calling convention has an abundance
60 ** of callee-saved registers (rbx, rbp, rsi, rdi, r12-r15, xmm6-xmm15).
61 ** EXT is mandatory on POSIX/x64 since the interpreter doesn't save r12/r13.
64 #if defined(__GNUC__) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL))
65 #define LJ_UNWIND_EXT 1
66 #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS
67 #define LJ_UNWIND_EXT 1
70 /* -- Error messages ------------------------------------------------------ */
72 /* Error message strings. */
73 LJ_DATADEF
const char *lj_err_allmsg
=
74 #define ERRDEF(name, msg) msg "\0"
75 #include "lj_errmsg.h"
78 /* -- Internal frame unwinding -------------------------------------------- */
80 /* Unwind Lua stack and move error message to new top. */
81 LJ_NOINLINE
static void unwindstack(lua_State
*L
, TValue
*top
)
83 lj_func_closeuv(L
, top
);
85 copyTV(L
, top
, L
->top
-1);
88 lj_state_relimitstack(L
);
91 /* Unwind until stop frame. Optionally cleanup frames. */
92 static void *err_unwind(lua_State
*L
, void *stopcf
, int errcode
)
94 TValue
*frame
= L
->base
-1;
97 int32_t nres
= cframe_nres(cframe_raw(cf
));
98 if (nres
< 0) { /* C frame without Lua frame? */
99 TValue
*top
= restorestack(L
, -nres
);
100 if (frame
< top
) { /* Frame reached? */
103 L
->cframe
= cframe_prev(cf
);
109 if (frame
<= tvref(L
->stack
))
111 switch (frame_typep(frame
)) {
112 case FRAME_LUA
: /* Lua frame. */
114 frame
= frame_prevl(frame
);
116 case FRAME_C
: /* C frame. */
122 L
->base
= frame_prevd(frame
) + 1;
123 L
->cframe
= cframe_prev(cf
);
124 unwindstack(L
, frame
);
125 } else if (cf
!= stopcf
) {
126 cf
= cframe_prev(cf
);
127 frame
= frame_prevd(frame
);
130 return NULL
; /* Continue unwinding. */
133 cf
= cframe_prev(cf
);
134 frame
= frame_prevd(frame
);
137 case FRAME_CP
: /* Protected C frame. */
138 if (cframe_canyield(cf
)) { /* Resume? */
140 hook_leave(G(L
)); /* Assumes nobody uses coroutines inside hooks. */
142 L
->status
= (uint8_t)errcode
;
147 L
->base
= frame_prevd(frame
) + 1;
148 L
->cframe
= cframe_prev(cf
);
149 unwindstack(L
, frame
);
152 case FRAME_CONT
: /* Continuation frame. */
154 if ((frame
-1)->u32
.lo
== LJ_CONT_FFI_CALLBACK
)
157 case FRAME_VARG
: /* Vararg frame. */
158 frame
= frame_prevd(frame
);
160 case FRAME_PCALL
: /* FF pcall() frame. */
161 case FRAME_PCALLH
: /* FF pcall() frame inside hook. */
163 if (errcode
== LUA_YIELD
) {
164 frame
= frame_prevd(frame
);
167 if (frame_typep(frame
) == FRAME_PCALL
)
169 L
->base
= frame_prevd(frame
) + 1;
171 unwindstack(L
, L
->base
);
173 return (void *)((intptr_t)cf
| CFRAME_UNWIND_FF
);
178 L
->base
= tvref(L
->stack
)+1;
180 unwindstack(L
, L
->base
);
185 return L
; /* Anything non-NULL will do. */
188 /* -- External frame unwinding -------------------------------------------- */
190 #if defined(__GNUC__) && !LJ_NO_UNWIND && !LJ_TARGET_WINDOWS
193 ** We have to use our own definitions instead of the mandatory (!) unwind.h,
194 ** since various OS, distros and compilers mess up the header installation.
197 typedef struct _Unwind_Exception
200 void (*excleanup
)(int, struct _Unwind_Exception
*);
202 } __attribute__((__aligned__
)) _Unwind_Exception
;
204 typedef struct _Unwind_Context _Unwind_Context
;
207 #define _URC_FATAL_PHASE1_ERROR 3
208 #define _URC_HANDLER_FOUND 6
209 #define _URC_INSTALL_CONTEXT 7
210 #define _URC_CONTINUE_UNWIND 8
211 #define _URC_FAILURE 9
215 extern uintptr_t _Unwind_GetCFA(_Unwind_Context
*);
216 extern void _Unwind_SetGR(_Unwind_Context
*, int, uintptr_t);
217 extern void _Unwind_SetIP(_Unwind_Context
*, uintptr_t);
218 extern void _Unwind_DeleteException(_Unwind_Exception
*);
219 extern int _Unwind_RaiseException(_Unwind_Exception
*);
221 #define _UA_SEARCH_PHASE 1
222 #define _UA_CLEANUP_PHASE 2
223 #define _UA_HANDLER_FRAME 4
224 #define _UA_FORCE_UNWIND 8
226 #define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */
227 #define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c))
228 #define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff)
229 #define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff))
231 /* DWARF2 personality handler referenced from interpreter .eh_frame. */
232 LJ_FUNCA
int lj_err_unwind_dwarf(int version
, int actions
,
233 uint64_t uexclass
, _Unwind_Exception
*uex
, _Unwind_Context
*ctx
)
238 return _URC_FATAL_PHASE1_ERROR
;
240 cf
= (void *)_Unwind_GetCFA(ctx
);
242 if ((actions
& _UA_SEARCH_PHASE
)) {
244 if (err_unwind(L
, cf
, 0) == NULL
)
245 return _URC_CONTINUE_UNWIND
;
247 if (!LJ_UEXCLASS_CHECK(uexclass
)) {
248 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
250 return _URC_HANDLER_FOUND
;
252 if ((actions
& _UA_CLEANUP_PHASE
)) {
254 if (LJ_UEXCLASS_CHECK(uexclass
)) {
255 errcode
= LJ_UEXCLASS_ERRCODE(uexclass
);
257 if ((actions
& _UA_HANDLER_FRAME
))
258 _Unwind_DeleteException(uex
);
259 errcode
= LUA_ERRRUN
;
262 cf
= err_unwind(L
, cf
, errcode
);
263 if ((actions
& _UA_FORCE_UNWIND
)) {
264 return _URC_CONTINUE_UNWIND
;
266 _Unwind_SetGR(ctx
, LJ_TARGET_EHRETREG
, errcode
);
267 _Unwind_SetIP(ctx
, (uintptr_t)(cframe_unwind_ff(cf
) ?
270 return _URC_INSTALL_CONTEXT
;
272 #if LJ_TARGET_X86ORX64
273 else if ((actions
& _UA_HANDLER_FRAME
)) {
274 /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/
275 ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h
277 _Unwind_SetGR(ctx
, LJ_TARGET_EHRETREG
, errcode
);
278 _Unwind_SetIP(ctx
, (uintptr_t)lj_vm_unwind_rethrow
);
279 return _URC_INSTALL_CONTEXT
;
283 /* This is not the proper way to escape from the unwinder. We get away with
284 ** it on non-x64 because the interpreter restores all callee-saved regs.
286 lj_err_throw(L
, errcode
);
289 return _URC_CONTINUE_UNWIND
;
293 #if LJ_TARGET_OSX || defined(__OpenBSD__)
294 /* Sorry, no thread safety for OSX. Complain to Apple, not me. */
295 static _Unwind_Exception static_uex
;
297 static __thread _Unwind_Exception static_uex
;
300 /* Raise DWARF2 exception. */
301 static void err_raise_ext(int errcode
)
303 static_uex
.exclass
= LJ_UEXCLASS_MAKE(errcode
);
304 static_uex
.excleanup
= NULL
;
305 _Unwind_RaiseException(&static_uex
);
311 extern void _Unwind_DeleteException(void *);
312 extern int __gnu_unwind_frame (void *, _Unwind_Context
*);
313 extern int _Unwind_VRS_Set(_Unwind_Context
*, int, uint32_t, int, void *);
314 extern int _Unwind_VRS_Get(_Unwind_Context
*, int, uint32_t, int, void *);
316 static inline uint32_t _Unwind_GetGR(_Unwind_Context
*ctx
, int r
)
319 _Unwind_VRS_Get(ctx
, 0, r
, 0, &v
);
323 static inline void _Unwind_SetGR(_Unwind_Context
*ctx
, int r
, uint32_t v
)
325 _Unwind_VRS_Set(ctx
, 0, r
, 0, &v
);
328 #define _US_VIRTUAL_UNWIND_FRAME 0
329 #define _US_UNWIND_FRAME_STARTING 1
330 #define _US_ACTION_MASK 3
331 #define _US_FORCE_UNWIND 8
333 /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */
334 LJ_FUNCA
int lj_err_unwind_arm(int state
, void *ucb
, _Unwind_Context
*ctx
)
336 void *cf
= (void *)_Unwind_GetGR(ctx
, 13);
337 lua_State
*L
= cframe_L(cf
);
338 if ((state
& _US_ACTION_MASK
) == _US_VIRTUAL_UNWIND_FRAME
) {
339 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
340 return _URC_HANDLER_FOUND
;
342 if ((state
&(_US_ACTION_MASK
|_US_FORCE_UNWIND
)) == _US_UNWIND_FRAME_STARTING
) {
343 _Unwind_DeleteException(ucb
);
344 _Unwind_SetGR(ctx
, 15, (uint32_t)(void *)lj_err_throw
);
345 _Unwind_SetGR(ctx
, 0, (uint32_t)L
);
346 _Unwind_SetGR(ctx
, 1, (uint32_t)LUA_ERRRUN
);
347 return _URC_INSTALL_CONTEXT
;
349 if (__gnu_unwind_frame(ucb
, ctx
) != _URC_OK
)
351 return _URC_CONTINUE_UNWIND
;
356 #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS
359 ** Someone in Redmond owes me several days of my life. A lot of this is
360 ** undocumented or just plain wrong on MSDN. Some of it can be gathered
361 ** from 3rd party docs or must be found by trial-and-error. They really
362 ** don't want you to write your own language-specific exception handler
363 ** or to interact gracefully with MSVC. :-(
365 ** Apparently MSVC doesn't call C++ destructors for foreign exceptions
366 ** unless you compile your C++ code with /EHa. Unfortunately this means
367 ** catch (...) also catches things like access violations. The use of
368 ** _set_se_translator doesn't really help, because it requires /EHa, too.
371 #define WIN32_LEAN_AND_MEAN
374 /* Taken from: http://www.nynaeve.net/?p=99 */
375 typedef struct UndocumentedDispatcherContext
{
378 PRUNTIME_FUNCTION FunctionEntry
;
379 ULONG64 EstablisherFrame
;
381 PCONTEXT ContextRecord
;
382 void (*LanguageHandler
)(void);
384 PUNWIND_HISTORY_TABLE HistoryTable
;
387 } UndocumentedDispatcherContext
;
389 /* Another wild guess. */
390 extern void __DestructExceptionObject(EXCEPTION_RECORD
*rec
, int nothrow
);
392 #ifdef MINGW_SDK_INIT
393 /* Workaround for broken MinGW64 declaration. */
394 VOID
RtlUnwindEx_FIXED(PVOID
,PVOID
,PVOID
,PVOID
,PVOID
,PVOID
) asm("RtlUnwindEx");
395 #define RtlUnwindEx RtlUnwindEx_FIXED
398 #define LJ_MSVC_EXCODE ((DWORD)0xe06d7363)
399 #define LJ_GCC_EXCODE ((DWORD)0x20474343)
401 #define LJ_EXCODE ((DWORD)0xe24c4a00)
402 #define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c))
403 #define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff)
404 #define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff))
406 /* Win64 exception handler for interpreter frame. */
407 LJ_FUNCA EXCEPTION_DISPOSITION
lj_err_unwind_win64(EXCEPTION_RECORD
*rec
,
408 void *cf
, CONTEXT
*ctx
, UndocumentedDispatcherContext
*dispatch
)
410 lua_State
*L
= cframe_L(cf
);
411 int errcode
= LJ_EXCODE_CHECK(rec
->ExceptionCode
) ?
412 LJ_EXCODE_ERRCODE(rec
->ExceptionCode
) : LUA_ERRRUN
;
413 if ((rec
->ExceptionFlags
& 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */
414 /* Unwind internal frames. */
415 err_unwind(L
, cf
, errcode
);
417 void *cf2
= err_unwind(L
, cf
, 0);
418 if (cf2
) { /* We catch it, so start unwinding the upper frames. */
419 if (rec
->ExceptionCode
== LJ_MSVC_EXCODE
||
420 rec
->ExceptionCode
== LJ_GCC_EXCODE
) {
421 __DestructExceptionObject(rec
, 1);
422 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
423 } else if (!LJ_EXCODE_CHECK(rec
->ExceptionCode
)) {
424 /* Don't catch access violations etc. */
425 return ExceptionContinueSearch
;
427 /* Unwind the stack and call all handlers for all lower C frames
428 ** (including ourselves) again with EH_UNWINDING set. Then set
429 ** rsp = cf, rax = errcode and jump to the specified target.
431 RtlUnwindEx(cf
, (void *)((cframe_unwind_ff(cf2
) && errcode
!= LUA_YIELD
) ?
434 rec
, (void *)(uintptr_t)errcode
, ctx
, dispatch
->HistoryTable
);
435 /* RtlUnwindEx should never return. */
438 return ExceptionContinueSearch
;
441 /* Raise Windows exception. */
442 static void err_raise_ext(int errcode
)
444 RaiseException(LJ_EXCODE_MAKE(errcode
), 1 /* EH_NONCONTINUABLE */, 0, NULL
);
449 /* -- Error handling ------------------------------------------------------ */
451 /* Throw error. Find catch frame, unwind stack and continue. */
452 LJ_NOINLINE
void LJ_FASTCALL
lj_err_throw(lua_State
*L
, int errcode
)
454 global_State
*g
= G(L
);
456 setmref(g
->jit_base
, NULL
);
459 err_raise_ext(errcode
);
461 ** A return from this function signals a corrupt C stack that cannot be
462 ** unwound. We have no choice but to call the panic function and exit.
464 ** Usually this is caused by a C function without unwind information.
465 ** This should never happen on x64, but may happen if you've manually
466 ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every*
467 ** non-C++ file with -funwind-tables.
473 void *cf
= err_unwind(L
, NULL
, errcode
);
474 if (cframe_unwind_ff(cf
))
475 lj_vm_unwind_ff(cframe_raw(cf
));
477 lj_vm_unwind_c(cframe_raw(cf
), errcode
);
483 /* Return string object for error message. */
484 LJ_NOINLINE GCstr
*lj_err_str(lua_State
*L
, ErrMsg em
)
486 return lj_str_newz(L
, err2msg(em
));
489 /* Out-of-memory error. */
490 LJ_NOINLINE
void lj_err_mem(lua_State
*L
)
492 if (L
->status
== LUA_ERRERR
+1) /* Don't touch the stack during lua_open. */
493 lj_vm_unwind_c(L
->cframe
, LUA_ERRMEM
);
494 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRMEM
));
495 lj_err_throw(L
, LUA_ERRMEM
);
498 /* Find error function for runtime errors. Requires an extra stack traversal. */
499 static ptrdiff_t finderrfunc(lua_State
*L
)
501 cTValue
*frame
= L
->base
-1, *bot
= tvref(L
->stack
);
502 void *cf
= L
->cframe
;
503 while (frame
> bot
) {
504 lua_assert(cf
!= NULL
);
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_strfmt_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
, line
);
598 msg
= lj_strfmt_pushvf(L
, err2msg(em
), argp
);
599 msg
= lj_strfmt_pushf(L
, "%s:%d: %s", buff
, line
, msg
);
601 lj_strfmt_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_strfmt_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_strfmt_pushf(L
, err2msg(LJ_ERR_BADSELF
), fname
, msg
);
707 msg
= lj_strfmt_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_strfmt_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 TValue
*o
= narg
< 0 ? L
->top
+ narg
: L
->base
+ narg
-1;
732 const char *tname
= o
< L
->top
? lj_typename(o
) : lj_obj_typename
[0];
733 const char *msg
= lj_strfmt_pushf(L
, err2msg(LJ_ERR_BADTYPE
), xname
, tname
);
734 err_argmsg(L
, narg
, msg
);
737 /* Typecheck error for arguments. */
738 LJ_NOINLINE
void lj_err_argt(lua_State
*L
, int narg
, int tt
)
740 lj_err_argtype(L
, narg
, lj_obj_typename
[tt
+1]);
743 /* -- Public error handling API ------------------------------------------- */
745 LUA_API lua_CFunction
lua_atpanic(lua_State
*L
, lua_CFunction panicf
)
747 lua_CFunction old
= G(L
)->panic
;
748 G(L
)->panic
= panicf
;
752 /* Forwarders for the public API (C calling convention and no LJ_NORET). */
753 LUA_API
int lua_error(lua_State
*L
)
756 return 0; /* unreachable */
759 LUALIB_API
int luaL_argerror(lua_State
*L
, int narg
, const char *msg
)
761 err_argmsg(L
, narg
, msg
);
762 return 0; /* unreachable */
765 LUALIB_API
int luaL_typerror(lua_State
*L
, int narg
, const char *xname
)
767 lj_err_argtype(L
, narg
, xname
);
768 return 0; /* unreachable */
771 LUALIB_API
void luaL_where(lua_State
*L
, int level
)
774 cTValue
*frame
= lj_debug_frame(L
, level
, &size
);
775 lj_debug_addloc(L
, "", frame
, size
? frame
+size
: NULL
);
778 LUALIB_API
int luaL_error(lua_State
*L
, const char *fmt
, ...)
783 msg
= lj_strfmt_pushvf(L
, fmt
, argp
);
785 lj_err_callermsg(L
, msg
);
786 return 0; /* unreachable */