From f95c114f3b7c16d67845be77ce674d5ba37d71ca Mon Sep 17 00:00:00 2001 From: Rain Gloom Date: Mon, 20 Mar 2017 18:44:10 +0100 Subject: [PATCH 1/2] added hacky 5.2+ workaround example to readme IDK if this would be the obvious way for others to do it, but I used to have a main and a "true main" file in Love2d projects because having to wrap my main module in one big function was just ugly. I think this is a nice way to abuse the debug library. --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 1f81bb7..aee1b2d 100644 --- a/README.md +++ b/README.md @@ -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 errpr + 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. From b7c3a7e7391d129382ee25e923d652fe22199199 Mon Sep 17 00:00:00 2001 From: Rain Gloom Date: Mon, 20 Mar 2017 18:46:59 +0100 Subject: [PATCH 2/2] typo fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aee1b2d..c6a32f9 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ That script will output (only with Lua 5.1): if not package.loaded.StackTracePlus then local STP = require 'StackTracePlus' debug.traceback = STP.stacktrace - assert(--rethrows errpr + 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)