Skip to content

Commit e9c109d

Browse files
committed
getOutputSoFar() can take a buffer.
1 parent ca77802 commit e9c109d

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

preprocess-cl.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ end
468468

469469

470470
-- Init stuff.
471-
sendMessage("init", pathsIn, (hasOutputPaths and pathsOut or nil))
471+
sendMessage("init", pathsIn, (hasOutputPaths and pathsOut or nil)) -- @Incomplete: Use pcall and format error message better?
472472

473473
if not hasOutputPaths then
474474
for i, pathIn in ipairs(pathsIn) do
@@ -635,7 +635,7 @@ printfNoise(
635635
formatBytes(byteCount)
636636
)
637637

638-
sendMessage("alldone")
638+
sendMessage("alldone") -- @Incomplete: Use pcall and format error message better?
639639

640640

641641

preprocess.lua

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ function metaFuncs.outputLuaTemplate(lua, ...)
17131713
errorIfNotRunningMeta(2)
17141714
assertarg(1, lua, "string")
17151715

1716-
local args = {...}
1716+
local args = {...} -- @Memory
17171717
local n = 0
17181718
local v, err
17191719

@@ -1733,20 +1733,32 @@ end
17331733

17341734
-- getOutputSoFar()
17351735
-- luaString = getOutputSoFar( [ asTable=false ] )
1736+
-- getOutputSoFar( buffer )
17361737
-- Get Lua code that's been outputted so far.
17371738
-- If asTable is false then the full Lua code string is returned.
17381739
-- If asTable is true then an array of Lua code segments is returned. (This avoids allocating, possibly large, strings.)
1740+
-- If a buffer array is given then Lua code segments are added to it.
17391741
-- Raises an error if no file or string is being processed.
1740-
function metaFuncs.getOutputSoFar(asTable)
1742+
function metaFuncs.getOutputSoFar(bufferOrAsTable)
17411743
errorIfNotRunningMeta(2)
1744+
17421745
-- Should there be a way to get the contents of current_meta_output etc.? :GetMoreOutputFromStack
1743-
return asTable and copyArray(current_meta_outputStack[1]) or table.concat(current_meta_outputStack[1])
1746+
1747+
if type(bufferOrAsTable) == "table" then
1748+
for _, lua in ipairs(current_meta_outputStack[1]) do
1749+
tableInsert(bufferOrAsTable, lua)
1750+
end
1751+
-- Return nothing!
1752+
1753+
else
1754+
return bufferOrAsTable and copyArray(current_meta_outputStack[1]) or table.concat(current_meta_outputStack[1])
1755+
end
17441756
end
17451757

17461758
local function getOutputSoFarOnLine()
17471759
errorIfNotRunningMeta(2)
17481760

1749-
local lineFragments = {}
1761+
local lineFragments = {} -- @Memory
17501762

17511763
-- Should there be a way to get the contents of current_meta_output etc.? :GetMoreOutputFromStack
17521764
for i = #current_meta_outputStack[1], 1, -1 do
@@ -2148,7 +2160,7 @@ end
21482160
function metaFuncs.startInterceptingOutput()
21492161
errorIfNotRunningMeta(2)
21502162

2151-
current_meta_output = {}
2163+
current_meta_output = {} -- @Memory (Especially if lots of macro calls are used!)
21522164
tableInsert(current_meta_outputStack, current_meta_output)
21532165
end
21542166

@@ -2234,7 +2246,7 @@ function metaFuncs.LOG(logLevelCode, valueOrFormatCode, ...)
22342246

22352247
if ... then
22362248
tableInsert(current_meta_output, "string.format(")
2237-
tableInsert(current_meta_output, table.concat({valueOrFormatCode, ...}, ", "))
2249+
tableInsert(current_meta_output, table.concat({valueOrFormatCode, ...}, ", ")) -- @Memory
22382250
tableInsert(current_meta_output, ")")
22392251
else
22402252
tableInsert(current_meta_output, valueOrFormatCode)

tests/suite.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,16 @@ doTest("Misc.", function()
739739
assertCodeOutput(assert(pp.processString{ code="x = 1 ; y = !(getOutputSoFar())" }), 'x = 1 ; y = "x = 1 ; y = "')
740740
assertCodeOutput(assert(pp.processString{ code="x = !(getOutputSoFarOnLine ())\n\ty = !(getOutputSoFarOnLine ())"}), 'x = "x = "\n\ty = "\\ty = "')
741741
assertCodeOutput(assert(pp.processString{ code="x = !(getCurrentLineNumberInOutput())\n\ty = !(getCurrentLineNumberInOutput())"}), "x = 1\n\ty = 2")
742+
743+
assert(pp.processString{ code=[[
744+
x = 1
745+
!(
746+
local buffer = {}
747+
getOutputSoFar(buffer)
748+
assert(table.concat(buffer):find"^%s*x = 1%s*$")
749+
)
750+
y = 2
751+
]]})
742752
end)
743753

744754

0 commit comments

Comments
 (0)