Skip to content

Instantly share code, notes, and snippets.

@sw17ch
Created September 13, 2016 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sw17ch/ac5eb089353c7ceef25a41370c0fab59 to your computer and use it in GitHub Desktop.
Save sw17ch/ac5eb089353c7ceef25a41370c0fab59 to your computer and use it in GitHub Desktop.
local a = function ()
print("i am a")
end
error("no")
return a
local a = require('a')
local b = {}
function b.begin()
a()
end
local input = arg
-- This line defines the addition of a chunk to a preload entry.
local function fpreload(mod, func, strip)
local s = 'package.preload["%s"] = load(%q)();'
return s:format(mod, string.dump(func, strip))
end
local function fload(func, strip)
local s = 'load(%q)();'
return s:format(string.dump(func, strip))
end
local preloads = {}
for ix, mod in ipairs(arg) do
local file = mod .. ".lua"
local chunk = assert(loadfile(file))
local s
if ix < #arg then
s = fpreload(mod, chunk, false)
else
s = fload(chunk, false)
end
table.insert(preloads, s)
end
local final = table.concat(preloads, "\n")
local out = io.open("../out.luac", "w")
-- Dump the combined preload chunk. Strip debugging information out of
-- this final piece. User-defined files above maintain their debugging
-- information for now.
out:write(string.dump(assert(load(final)), true))
@sw17ch
Copy link
Author

sw17ch commented Sep 13, 2016

Example usage:

$ lua-5.3 builder.lua a b
$ cd ..
$ lua-5.3 out.luac
lua: a.lua:5: no
stack traceback:
    [C]: in function 'error'
    a.lua:5: in main chunk  <--- notice it saw the error in the combined file at the right place
    ?: in main chunk
    [C]: in ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment