Skip to content

Commit 2d62c0a

Browse files
committed
Added getOutputSoFar(), getOutputSizeSoFar(), getCurrentLineNumberInOutput().
Added --nogc option. outputValue() outputs commas.
1 parent f833bdc commit 2d62c0a

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

preprocess-cl.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ exec lua "$0" "$@"
6464
an error happens when the metaprogram runs. This file is removed
6565
if there's no error and --debug isn't enabled.
6666
67+
--nogc
68+
Stop the garbage collector. This may speed up the preprocessing.
69+
6770
--nonil
6871
Disallow !(...) and outputValue(...) from outputting nil.
6972
@@ -317,6 +320,9 @@ for _, arg in ipairs(args) do
317320
elseif arg == "--faststrings" then
318321
fastStrings = true
319322

323+
elseif arg == "--nogc" then
324+
collectgarbage("stop")
325+
320326
else
321327
errorLine("Unknown option '"..arg:gsub("=.*", "").."'.")
322328
end

preprocess.lua

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- toLua, serialize
2525
Only during processing:
2626
- getCurrentPathIn, getCurrentPathOut
27+
- getOutputSoFar, getOutputSizeSoFar, getCurrentLineNumberInOutput
2728
- outputValue, outputLua, outputLuaTemplate
2829
Search this file for 'EnvironmentTable' for more info.
2930
@@ -122,7 +123,7 @@
122123

123124

124125

125-
local PP_VERSION = "1.14.0"
126+
local PP_VERSION = "1.14.0-dev"
126127

127128
local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString().
128129

@@ -201,7 +202,7 @@ local _concatTokens
201202
local _tokenize
202203
local assertarg
203204
local cleanError
204-
local copyTable
205+
local copyArray, copyTable
205206
local countString, countSubString
206207
local errorf, errorLine, errorfLine, errorOnLine, errorInFile, errorAtToken, errorAfterToken
207208
local errorIfNotRunningMeta
@@ -1089,6 +1090,14 @@ end
10891090

10901091

10911092

1093+
function copyArray(t)
1094+
local copy = {}
1095+
for i, v in ipairs(table_name) do
1096+
copy[i] = v
1097+
end
1098+
return copy
1099+
end
1100+
10921101
-- copy = copyTable( table [, deep=false ] )
10931102
do
10941103
local function deepCopy(t, copy, tableCopies)
@@ -1371,7 +1380,8 @@ end
13711380

13721381
-- outputValue()
13731382
-- Output one or more values, like strings or tables, as literals.
1374-
-- outputValue( value1, ... )
1383+
-- outputValue( value )
1384+
-- outputValue( value1, value2, ... ) -- Outputted values will be separated by commas.
13751385
function metaFuncs.outputValue(...)
13761386
errorIfNotRunningMeta(2)
13771387

@@ -1388,6 +1398,10 @@ function metaFuncs.outputValue(...)
13881398
errorOnLine(metaPathForErrorMessages, ln, "MetaProgram", "Trying to output nil which is disallowed through params.canOutputNil .")
13891399
end
13901400

1401+
if i > 1 then
1402+
tableInsert(outputFromMeta, (isDebug and ", " or ","))
1403+
end
1404+
13911405
local ok, err = serialize(outputFromMeta, v)
13921406

13931407
if not ok then
@@ -1444,6 +1458,46 @@ function metaFuncs.outputLuaTemplate(lua, ...)
14441458
tableInsert(outputFromMeta, lua)
14451459
end
14461460

1461+
-- getOutputSoFar() @Doc
1462+
-- Get Lua code that's been outputted so far.
1463+
-- output = getOutputSoFar( [ asTable=false ] )
1464+
-- If asTable is false then the full Lua code string is returned.
1465+
-- If asTable is true then an array of Lua code segments is returned. (This avoids allocating, possibly large, strings.)
1466+
function metaFuncs.getOutputSoFar(asTable)
1467+
errorIfNotRunningMeta(2)
1468+
return asTable and copyArray(outputFromMeta) or table.concat(outputFromMeta)
1469+
end
1470+
1471+
-- getOutputSizeSoFar() @Doc
1472+
-- Get the amount of bytes outputted so far.
1473+
-- size = getOutputSizeSoFar( )
1474+
function metaFuncs.getOutputSizeSoFar()
1475+
errorIfNotRunningMeta(2)
1476+
1477+
local size = 0
1478+
1479+
for _, lua in ipairs(outputFromMeta) do
1480+
size = size + #lua
1481+
end
1482+
1483+
return size
1484+
end
1485+
1486+
-- getCurrentLineNumberInOutput() @Doc
1487+
-- Get the current line number in the output.
1488+
-- lineNumber = getCurrentLineNumberInOutput( )
1489+
function metaFuncs.getCurrentLineNumberInOutput()
1490+
errorIfNotRunningMeta(2)
1491+
1492+
local ln = 1
1493+
1494+
for _, lua in ipairs(outputFromMeta) do
1495+
ln = ln + countString(lua, "\n", true)
1496+
end
1497+
1498+
return ln
1499+
end
1500+
14471501
-- getCurrentPathIn()
14481502
-- Get what file is currently being processed, if any.
14491503
-- path = getCurrentPathIn( )

tests/suite.lua

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,22 @@ end)
105105

106106
doTest("Generate code", function()
107107
local pp = ppChunk()
108-
local luaIn = [[
108+
109+
local luaOut = assert(pp.processString{ code=[[
109110
!(
110111
outputLua("local s = ")
111112
outputValue("\n")
112113
)
113-
]]
114-
115-
local luaOut = assert(pp.processString{ code=luaIn })
114+
]] })
116115
assertCodeOutput(luaOut, [[local s = "\n"]])
116+
117+
local luaOut = assert(pp.processString{ code=[[
118+
!(
119+
outputLua("local s1, s2 = ")
120+
outputValue("\001", "\0002")
121+
)
122+
]] })
123+
assertCodeOutput(luaOut, [[local s1, s2 = "\1","\0002"]])
117124
end)
118125

119126
doTest("Parsing extended preprocessor line", function()

0 commit comments

Comments
 (0)