debug/squish.debug.lua

Fri, 05 May 2017 09:47:52 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 05 May 2017 09:47:52 +0100
branch
lua5.2
changeset 95
828e814152e0
parent 34
0e34461ab2a6
permissions
-rw-r--r--

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.


local cs = require "minichunkspy"

local function ___adjust_chunk(chunk, newname, lineshift)
	local c = cs.disassemble(string.dump(chunk));
	c.body.name = newname;

	lineshift = -c.body.line;
	local function shiftlines(c)
		c.line = c.line + lineshift;
		c.last_line = c.last_line + lineshift;
		for i, line in ipairs(c.source_lines) do
			c.source_lines[i] = line+lineshift;
		end
		for i, f in ipairs(c.prototypes) do
			shiftlines(f);
		end
	end
	shiftlines(c.body);

	return assert(loadstring(cs.assemble(c), newname))();
end

mercurial