Skip to content

added hacky example for 5.2 and above #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ That script will output (only with Lua 5.1):
(5) main chunk of file 'example.lua' at line 14
(6) C function 'function: 00637B30'

### Usage in Lua 5.2 and newer
*(without having to wrap the main function)*
```
if not package.loaded.StackTracePlus then
local STP = require 'StackTracePlus'
debug.traceback = STP.stacktrace
assert(--rethrows error
xpcall(--calls main function
debug.getinfo( 1 ).func,--retrieves main function from call stack
STP.stacktrace, ... ))--passes through all arguments (may not work with 5.1, should work with LuaJIT)
os.exit( true, true )--terminate, so control flow won't move on to running the rest of the main function again (we already ran that)
end
function test()
local s = "this is a string"
local n = 42
local t = { foo = "bar" }
local co = coroutine
local cr = coroutine.create

error("an error")
end
test()
```

**StackTracePlus** is aware of the usual Lua libraries, like *coroutine*, *table*, *string*, *io*, etc and functions like
*print*, *pcall*, *assert*, and so on.

Expand Down