Skip to content

Commit 84cb0d3

Browse files
committed
Outputted strings now have more characters escaped. onAfterMeta() can now prepend shebang without causing a code validation error.
1 parent 723b8f8 commit 84cb0d3

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

misc/runTests.lua

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ local function writeFile(path, data)
4343
end
4444

4545
local function assertCodeOutput(codeOut, codeExpected, message)
46-
assert(trim(codeOut) == codeExpected, (message or "Unexpected output: "..codeOut))
46+
if trim(codeOut) ~= codeExpected then
47+
error(message or "Unexpected output: "..codeOut, 2)
48+
end
4749
end
4850
local function assertCmd(cmd)
4951
local code = os.execute(cmd)
@@ -110,10 +112,7 @@ doTest("Generate code", function()
110112
]]
111113

112114
local luaOut = assert(pp.processString{ code=luaIn })
113-
assertCodeOutput(luaOut, ("local s = %q"):format("\n"))
114-
115-
local luaOut = assert(pp.processString{ code=luaIn, debug=true })
116-
assertCodeOutput(luaOut, [[local s = "\n"]]) -- Debug mode changes how newlines appear in string values.
115+
assertCodeOutput(luaOut, [[local s = "\n"]])
117116
end)
118117

119118
doTest("Parsing extended preprocessor line", function()
@@ -296,7 +295,7 @@ print("Results:")
296295

297296
for _, result in ipairs(results) do
298297
if result.label then
299-
print("------ "..result.label)
298+
print("----- "..result.label)
300299
elseif result.ok then
301300
print("ok "..result.description)
302301
else

preprocess.lua

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,14 @@ function serialize(buffer, v)
761761
table.insert(buffer, "}")
762762

763763
elseif vType == "string" then
764-
local s = F("%q", v)
765-
if isDebug then
766-
s = s:gsub("\\\n", "\\n")
767-
end
768-
table.insert(buffer, s)
764+
-- @Incomplete: Add an option specifically for nice string serialization?
765+
local s = v:gsub("[%c\128-\255\"\\]", function(c)
766+
local str = ESCAPE_SEQUENCES[c] or F("\\%03d", c:byte())
767+
ESCAPE_SEQUENCES[c] = str
768+
return str
769+
end)
770+
771+
table.insert(buffer, '"'..s..'"')
769772

770773
elseif v == math.huge then
771774
table.insert(buffer, "math.huge")
@@ -1979,7 +1982,8 @@ local function _processFileOrString(params, isFile)
19791982
-- @Incomplete: Maybe add an option to disable this? It might be useful if
19801983
-- e.g. Lua 5.1 is used to generate Lua 5.3 code (for whatever reason).
19811984
--
1982-
local mainChunk, err = loadLuaString(lua, (isFile and params.pathMeta and "@" or "")..pathOut)
1985+
local luaToCheck = lua:gsub("^#![^\n]*", "")
1986+
local mainChunk, err = loadLuaString(luaToCheck, (isFile and params.pathMeta and "@" or "")..pathOut)
19831987
if not mainChunk then
19841988
local ln, _err = err:match'^.-:(%d+): (.*)'
19851989
errorOnLine(pathOut, (tonumber(ln) or 0), nil, "%s", (_err or err))

0 commit comments

Comments
 (0)