aboutsummaryrefslogtreecommitdiff
path: root/src/traceback.inc
diff options
context:
space:
mode:
Diffstat (limited to 'src/traceback.inc')
-rw-r--r--src/traceback.inc56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/traceback.inc b/src/traceback.inc
new file mode 100644
index 0000000..af2f5a1
--- /dev/null
+++ b/src/traceback.inc
@@ -0,0 +1,56 @@
1/******************************************************************************
2* traceback() function from Lua 5.1/5.2 source.
3* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved.
4*
5* Permission is hereby granted, free of charge, to any person obtaining
6* a copy of this software and associated documentation files (the
7* "Software"), to deal in the Software without restriction, including
8* without limitation the rights to use, copy, modify, merge, publish,
9* distribute, sublicense, and/or sell copies of the Software, and to
10* permit persons to whom the Software is furnished to do so, subject to
11* the following conditions:
12*
13* The above copyright notice and this permission notice shall be
14* included in all copies or substantial portions of the Software.
15*
16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23******************************************************************************/
24#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM == 501)
25/* from Lua 5.1 */
26static int traceback (lua_State *L) {
27 if (!lua_isstring(L, 1)) /* 'message' not a string? */
28 return 1; /* keep it intact */
29 lua_getglobal(L, "debug");
30 if (!lua_istable(L, -1)) {
31 lua_pop(L, 1);
32 return 1;
33 }
34 lua_getfield(L, -1, "traceback");
35 if (!lua_isfunction(L, -1)) {
36 lua_pop(L, 2);
37 return 1;
38 }
39 lua_pushvalue(L, 1); /* pass error message */
40 lua_pushinteger(L, 2); /* skip this function and traceback */
41 lua_call(L, 2, 1); /* call debug.traceback */
42 return 1;
43}
44#else
45/* from Lua 5.2 */
46static int traceback (lua_State *L) {
47 const char *msg = lua_tostring(L, 1);
48 if (msg)
49 luaL_traceback(L, L, msg, 1);
50 else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */
51 if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */
52 lua_pushliteral(L, "(no error message)");
53 }
54 return 1;
55}
56#endif