Skip to content

Commit 5dab0b2

Browse files
committed
Checking that params.pathMeta isn't the same as the input or output path.
Added the metaprogram code as an argument to params.onBeforeMeta(). params.onBeforeMeta() is now called before the metaprogram is even loaded.
1 parent 93bdb5e commit 5dab0b2

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

preprocess-cl.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Handler messages:
147147
Sent before a file's metaprogram runs.
148148
Arguments:
149149
path: The file being processed.
150+
luaString: The generated metaprogram.
150151
151152
"aftermeta"
152153
Sent after a file's metaprogram has produced output (before the output is written to a file).
@@ -544,8 +545,8 @@ for i, pathIn in ipairs(pathsIn) do
544545
return lua
545546
end,
546547

547-
onBeforeMeta = messageHandler and function()
548-
sendMessage("beforemeta", pathIn)
548+
onBeforeMeta = messageHandler and function(lua)
549+
sendMessage("beforemeta", pathIn, lua)
549550
end,
550551

551552
onAfterMeta = messageHandler and function(lua)

preprocess.lua

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,6 +3429,14 @@ local function _processFileOrString(params, isFile)
34293429
error("'pathIn' and 'pathOut' are the same in params.", 2)
34303430
end
34313431

3432+
if (params.pathMeta or "-") ~= "-" then
3433+
if params.pathMeta == params.pathIn then
3434+
error("'pathIn' and 'pathMeta' are the same in params.", 2)
3435+
elseif params.pathMeta == params.pathOut then
3436+
error("'pathOut' and 'pathMeta' are the same in params.", 2)
3437+
end
3438+
end
3439+
34323440
else
34333441
if not params.code then error("Missing 'code' in params.", 2) end
34343442
end
@@ -3536,14 +3544,14 @@ local function _processFileOrString(params, isFile)
35363544
file:close()
35373545
end
35383546

3547+
if params.onBeforeMeta then params.onBeforeMeta(luaMeta) end
3548+
35393549
local main_chunk, err = loadLuaString(luaMeta, "@"..current_meta_pathForErrorMessages, metaEnv)
35403550
if not main_chunk then
35413551
local ln, _err = err:match"^.-:(%d+): (.*)"
35423552
errorOnLine(current_meta_pathForErrorMessages, (tonumber(ln) or 0), nil, "%s", (_err or err))
35433553
end
35443554

3545-
if params.onBeforeMeta then params.onBeforeMeta() end
3546-
35473555
current_anytime_isRunningMeta = true
35483556
main_chunk() -- Note: Our caller should clean up current_meta_pathForErrorMessages etc. on error.
35493557
current_anytime_isRunningMeta = false
@@ -3644,7 +3652,7 @@ end
36443652

36453653
local function processFileOrString(params, isFile)
36463654
if current_parsingAndMeta_isProcessing then
3647-
error("Cannot process recursively.", 3)
3655+
error("Cannot process recursively.", 3) -- Note: We don't return failure in this case - it's a critical error!
36483656
end
36493657

36503658
-- local startTime = os.clock() -- DEBUG
@@ -3751,7 +3759,7 @@ local pp = {
37513759
-- logLevel = levelName -- [Optional] Maximum log level for the @@LOG() macro. Can be "off", "error", "warning", "info", "debug" or "trace". (Default: "trace", which enables all logging)
37523760
--
37533761
-- onInsert = function( name ) -- [Optional] Called for each @insert"name" instruction. It's expected to return a Lua code string. By default 'name' is a path to a file to be inserted.
3754-
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
3762+
-- onBeforeMeta = function( luaString ) -- [Optional] Called before the metaprogram runs. luaString contains the metaprogram.
37553763
-- onAfterMeta = function( luaString ) -- [Optional] Here you can modify and return the Lua code before it's written to 'pathOut'.
37563764
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as what is returned from processFile().
37573765
--
@@ -3784,7 +3792,7 @@ local pp = {
37843792
-- logLevel = levelName -- [Optional] Maximum log level for the @@LOG() macro. Can be "off", "error", "warning", "info", "debug" or "trace". (Default: "trace", which enables all logging)
37853793
--
37863794
-- onInsert = function( name ) -- [Optional] Called for each @insert"name" instruction. It's expected to return a Lua code string. By default 'name' is a path to a file to be inserted.
3787-
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
3795+
-- onBeforeMeta = function( luaString ) -- [Optional] Called before the metaprogram runs. luaString contains the metaprogram.
37883796
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as the second returned value from processString().
37893797
--
37903798
processString = processString,

tests/quickTestHandlerFunction.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ end
3030

3131
return function(message, ...)
3232
if message == "beforemeta" then
33-
local path = ...
34-
print("... Now processing "..path)
33+
local path, luaString = ...
34+
print("... Now running metaprogram for "..path)
3535

3636
elseif message == "aftermeta" then
3737
local path, luaString = ...

tests/quickTestHandlerTable.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ end
2929
--
3030

3131
return {
32-
beforemeta = function(path)
33-
print("... Now processing "..path)
32+
beforemeta = function(path, luaString)
33+
print("... Now running metaprogram for "..path)
3434
end,
3535

3636
aftermeta = function(path, luaString)

tests/suite.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -708,12 +708,21 @@ doTest("Indentation", function()
708708
local indent, expect = pp.getIndentation("\t \t", 4), 12 ; if indent ~= expect then error(expect.." "..indent) end
709709
end)
710710

711-
doTest("Recursive processing", function()
711+
doTest("Processing calls", function()
712712
local pp = ppChunk()
713713
pp.metaEnvironment.pp = pp
714714

715+
-- Path collisions.
716+
writeFile("temp/generatedTest.lua2p", [[]])
717+
assert( pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta=nil })
718+
assert( pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta="temp/generatedTest.meta.lua" })
719+
assert(not pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua2p", pathMeta=nil })
720+
assert(not pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta="temp/generatedTest.lua2p" })
721+
assert(not pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta="temp/generatedTest.lua" })
722+
723+
-- Recursive processing. (Not supported!)
715724
assert(not pp.processString{ code=[[
716-
!pp.processString{ code="" } -- Not supported!
725+
!pp.processString{ code="" }
717726
]]})
718727
end)
719728

@@ -946,7 +955,7 @@ doTest("Messages", function()
946955
cycle == 1 and [[ return {
947956
init = function(inPaths, outPaths ) assert(not outPaths) ; table.insert(inPaths, "temp/generatedTest.lua2p") end,
948957
insert = function(path, name ) assert(name == "foo()") ; return "un"..name end,
949-
beforemeta = function(path ) end,
958+
beforemeta = function(path, lua ) end,
950959
aftermeta = function(path, lua ) return "-- Hello\n"..lua end,
951960
filedone = function(path, outPath, info) assert(outPath == "temp/generatedTest.lua") ; assert(type(info) == "table") end,
952961
fileerror = function(path, err ) end,
@@ -955,7 +964,7 @@ doTest("Messages", function()
955964
or [[ return function(message, ...)
956965
if message == "init" then local inPaths, outPaths = ... ; assert(not outPaths) ; table.insert(inPaths, "temp/generatedTest.lua2p")
957966
elseif message == "insert" then local path, name = ... ; assert(name == "foo()") ; return "un"..name
958-
elseif message == "beforemeta" then local path = ...
967+
elseif message == "beforemeta" then local path, lua = ...
959968
elseif message == "aftermeta" then local path, lua = ... ; return "-- Hello\n"..lua
960969
elseif message == "filedone" then local path, outPath, info = ... ; assert(outPath == "temp/generatedTest.lua") ; assert(type(info) == "table")
961970
elseif message == "fileerror" then local path, err = ...

0 commit comments

Comments
 (0)