Skip to content

Commit 5d01c06

Browse files
committed
Adds support for Lua 5.2 and LuaJIT 2.0.2
1 parent fd0a35d commit 5d01c06

5 files changed

Lines changed: 147 additions & 52 deletions

File tree

.travis.yml

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
language: erlang
22

33
env:
4-
- LUA=""
4+
global:
5+
- LUAROCKS_BASE=luarocks-2.1.0
6+
matrix:
7+
- LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_VER=5.1 LUA_SFX=5.1 LUA_INCDIR=/usr/include/lua5.1
8+
- LUA=lua5.2 LUA_DEV=liblua5.2-dev LUA_VER=5.2 LUA_SFX=5.2 LUA_INCDIR=/usr/include/lua5.2
9+
- LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_VER=5.1 LUA_SFX=jit LUA_INCDIR=/usr/include/luajit-2.0
10+
11+
before_install:
12+
- if [ $LUA = "luajit" ]; then
13+
sudo add-apt-repository ppa:mwild1/ppa -y && sudo apt-get update -y;
14+
fi
15+
- sudo apt-get install $LUA
16+
- sudo apt-get install $LUA_DEV
17+
- lua$LUA_SFX -v
18+
# Install a recent luarocks release
19+
- wget http://luarocks.org/releases/$LUAROCKS_BASE.tar.gz
20+
- tar zxvpf $LUAROCKS_BASE.tar.gz
21+
- cd $LUAROCKS_BASE
22+
- ./configure
23+
--lua-version=$LUA_VER --lua-suffix=$LUA_SFX --with-lua-include="$LUA_INCDIR"
24+
- sudo make
25+
- sudo make install
26+
- cd $TRAVIS_BUILD_DIR
527

628
install:
7-
- sudo apt-get install luarocks
829
- sudo luarocks install lunit
930

1031
script:
1132
- cd unittest
12-
- lua5.1 run.lua
33+
- lua$LUA_SFX run.lua

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://travis-ci.org/ignacio/StackTracePlus.png?branch=master)](https://travis-ci.org/ignacio/StackTracePlus)
44

5-
StackTracePlus provides enhanced stack traces for Lua.
5+
StackTracePlus provides enhanced stack traces for [Lua 5.1, Lua 5.2][1] and [LuaJIT][2].
66

77
StackTracePlus can be used as a replacement for debug.traceback. It gives detailed information about locals, tries to guess
88
function names when they're not available, etc, so, instead of
@@ -34,7 +34,8 @@ you'll get
3434

3535
## Usage #
3636

37-
StackTracePlus can be used as a replacement for `debug.traceback`, as an `xpcall` error handler or even from C code.
37+
StackTracePlus can be used as a replacement for `debug.traceback`, as an `xpcall` error handler or even from C code. Note that
38+
only the Lua 5.1 interpreter allows the traceback function to be replaced "on the fly". Lua 5.2 always calls luaL_traceback.
3839

3940
```lua
4041
local STP = require "StackTracePlus"
@@ -110,3 +111,18 @@ Will output:
110111
s = string: "this is a string"
111112
(5) main chunk of file '..\test\example2.lua' at line 19
112113
(6) C function 'function: 00317B30'
114+
115+
116+
## Installation #
117+
The easiest way to install is with [LuaRocks][3].
118+
119+
- luarocks install stacktraceplus
120+
121+
If you don't want to use LuaRocks, just copy StackTracePlus.lua to Lua's path.
122+
123+
## License #
124+
**StackTracePlus** is available under the MIT license.
125+
126+
[1]: http://www.lua.org/
127+
[2]: http://luajit.org/
128+
[3]: http://luarocks.org/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package = "StackTracePlus"
2+
version = "0.1.1-1"
3+
source = {
4+
url = "https://github.com/ignacio/StackTracePlus/archive/0.1.1-1.tar.gz",
5+
dir = "StackTracePlus-0.1.1-1"
6+
}
7+
description = {
8+
summary = "StackTracePlus provides enhanced stack traces for Lua",
9+
detailed = [[
10+
StackTracePlus can be used as a replacement for debug.traceback. It gives detailed information about locals, tries to guess
11+
function names when they're not available, etc.
12+
]],
13+
license = "MIT/X11",
14+
homepage = "http://github.com/ignacio/StackTracePlus"
15+
}
16+
17+
dependencies = { "lua >= 5.1, < 5.3" }
18+
19+
build = {
20+
type = "builtin",
21+
modules = {
22+
StackTracePlus = "src/StackTracePlus.lua"
23+
}
24+
}

src/StackTracePlus.lua

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
-- tables
22
local _G = _G
3-
local string, io, os, table, math, package, debug, coroutine = string, io, os, table, math, package, debug, coroutine
3+
local string, io, debug, coroutine = string, io, debug, coroutine
44

55
-- functions
6-
local xpcall, tostring, print, unpack, require, getfenv, setmetatable = xpcall, tostring, print, unpack, require, getfenv, setmetatable
7-
local next, assert, tonumber, rawequal, collectgarbage, getmetatable = next, assert, tonumber, rawequal, collectgarbage, getmetatable
8-
local module, rawset, pcall, newproxy, type, select, gcinfo, pairs = module, rawset, pcall, newproxy, type, select, gcinfo, pairs
9-
local rawget, loadstring, ipairs, dofile, setfenv, load, error, loadfile = rawget, loadstring, ipairs, dofile, setfenv, load, error, loadfile
6+
local tostring, print, require = tostring, print, require
7+
local next, assert = next, assert
8+
local pcall, type, pairs, ipairs = pcall, type, pairs, ipairs
9+
local error = error
1010

1111
assert(debug, "debug table must be available at this point")
1212

@@ -18,51 +18,75 @@ local table_concat = table.concat
1818
local _M = {}
1919

2020
-- this tables should be weak so the elements in them won't become uncollectable
21-
local m_known_tables = {
22-
[_G] = "_G (global table)",
23-
[string] = "string module",
24-
[io] = "io module",
25-
[os] = "os module",
26-
[table] = "table module",
27-
[math] = "math module",
28-
[package] = "package table",
29-
[debug] = "debug table",
30-
[coroutine] = "coroutine table"
31-
}
21+
local m_known_tables = { [_G] = "_G (global table)" }
22+
local function add_known_module(name, desc)
23+
local ok, mod = pcall(require, name)
24+
if ok then
25+
m_known_tables[mod] = desc
26+
end
27+
end
28+
29+
add_known_module("string", "string module")
30+
add_known_module("io", "io module")
31+
add_known_module("os", "os module")
32+
add_known_module("table", "table module")
33+
add_known_module("math", "math module")
34+
add_known_module("package", "package module")
35+
add_known_module("debug", "debug module")
36+
add_known_module("coroutine", "coroutine module")
37+
38+
-- lua5.2
39+
add_known_module("bit32", "bit32 module")
40+
-- luajit
41+
add_known_module("bit", "bit module")
42+
add_known_module("jit", "jit module")
43+
44+
3245
local m_user_known_tables = {}
3346

34-
local m_known_functions = {
35-
[xpcall] = "xpcall",
36-
[tostring] = "tostring",
37-
[print] = "print",
38-
[unpack] = "unpack",
39-
[require] = "require",
40-
[getfenv] = "getfenv",
41-
[setmetatable] = "setmetatable",
42-
[next] = "next",
43-
[assert] = "assert",
44-
[tonumber] = "tonumber",
45-
[rawequal] = "rawequal",
46-
[collectgarbage] = "collectgarbage",
47-
[getmetatable] = "getmetatable",
48-
[module] = "module",
49-
[rawset] = "rawset",
50-
[pcall] = "pcall",
51-
[newproxy] = "newproxy",
52-
[type] = "type",
53-
[select] = "select",
54-
[gcinfo] = "gcinfo",
55-
[pairs] = "pairs",
56-
[rawget] = "rawget",
57-
[loadstring] = "loadstring",
58-
[ipairs] = "ipairs",
59-
[dofile] = "dofile",
60-
[setfenv] = "setfenv",
61-
[load] = "load",
62-
[error] = "error",
63-
[loadfile] = "loadfile",
47+
local m_known_functions = {}
48+
for _, name in ipairs{
49+
-- Lua 5.2, 5.1
50+
"assert",
51+
"collectgarbage",
52+
"dofile",
53+
"error",
54+
"getmetatable",
55+
"ipairs",
56+
"load",
57+
"loadfile",
58+
"next",
59+
"pairs",
60+
"pcall",
61+
"print",
62+
"rawequal",
63+
"rawget",
64+
"rawlen",
65+
"rawset",
66+
"require",
67+
"select",
68+
"setmetatable",
69+
"tonumber",
70+
"tostring",
71+
"type",
72+
"xpcall",
73+
74+
-- Lua 5.1
75+
"gcinfo",
76+
"getfenv",
77+
"loadstring",
78+
"module",
79+
"newproxy",
80+
"setfenv",
81+
"unpack",
6482
-- TODO: add table.* etc functions
65-
}
83+
} do
84+
if _G[name] then
85+
m_known_functions[_G[name]] = name
86+
end
87+
end
88+
89+
6690

6791
local m_user_known_functions = {}
6892

unittest/test.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ local STP = require "StackTracePlus"
22

33
module(..., lunit.testcase, package.seeall)
44

5+
function testLuaModule()
6+
local f = function()
7+
local t = table
8+
error("an error")
9+
end
10+
11+
local ok, err = xpcall(f, STP.stacktrace)
12+
assert_match( [[t = table module]], err, "" )
13+
end
14+
515
function testKnownFunction()
616
local my_function = function()
717
end

0 commit comments

Comments
 (0)