Fri, 05 May 2017 09:47:52 +0100
squish: Optionally add in a 5.2-compatible module() function (5.2's own compat function is broken)
--module-compat or --no-module-compat, default is --module-compat if running under 5.2+ (detected at runtime).
When compiled with the appropriate flags, Lua 5.2 provides a module() function for backwards compatibility with
5.1. However 5.1's version of the function changed function environments, while 5.2's version changes the global
environment, which breaks through squish's per-module sandbox functions.
34 | 1 | |
2 | local cs = require "minichunkspy" | |
3 | ||
4 | local function ___adjust_chunk(chunk, newname, lineshift) | |
5 | local c = cs.disassemble(string.dump(chunk)); | |
6 | c.body.name = newname; | |
7 | ||
8 | lineshift = -c.body.line; | |
9 | local function shiftlines(c) | |
10 | c.line = c.line + lineshift; | |
11 | c.last_line = c.last_line + lineshift; | |
12 | for i, line in ipairs(c.source_lines) do | |
13 | c.source_lines[i] = line+lineshift; | |
14 | end | |
15 | for i, f in ipairs(c.prototypes) do | |
16 | shiftlines(f); | |
17 | end | |
18 | end | |
19 | shiftlines(c.body); | |
20 | ||
21 | return assert(loadstring(cs.assemble(c), newname))(); | |
22 | end |