Skip to content

Commit 1bda5e4

Browse files
committed
Command line program works with Lua 5.2 and 5.3
1 parent 6e96dce commit 1bda5e4

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

main.lua

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ exec lua "$0" "$@"
1010
--= License: MIT (see the bottom of this file)
1111
--= Website: https://github.com/ReFreezed/LuaPreprocess
1212
--=
13-
--= Tested for Lua 5.1.
13+
--= Tested with Lua 5.1, 5.2 and 5.3.
1414
--=
1515
--==============================================================
1616
@@ -91,6 +91,20 @@ local startClock = os.clock()
9191

9292
local args = arg
9393

94+
local major, minor = _VERSION:match"Lua (%d+)%.(%d+)"
95+
if not major then
96+
print("[LuaPreprocess] Warning: Could not detect Lua version.")
97+
else
98+
major = tonumber(major)
99+
minor = tonumber(minor)
100+
end
101+
local IS_LUA_51 = (major == 5 and minor == 1)
102+
local IS_LUA_52 = (major == 5 and minor == 2)
103+
local IS_LUA_53 = (major == 5 and minor == 3)
104+
local IS_LUA_51_OR_LATER = (major == 5 and minor >= 1) or (major ~= nil and major > 5)
105+
local IS_LUA_52_OR_LATER = (major == 5 and minor >= 2) or (major ~= nil and major > 5)
106+
local IS_LUA_53_OR_LATER = (major == 5 and minor >= 3) or (major ~= nil and major > 5)
107+
94108
if not args[0] then error("Expected to run from the Lua interpreter.") end
95109
local pp = dofile((args[0]:gsub("[^/\\]+$", "preprocess.lua")))
96110

@@ -107,6 +121,7 @@ local outputMeta = false
107121
--==============================================================
108122
local errorline
109123
local F, formatBytes, formatInt
124+
local loadLuaFile
110125
local printf, printfNoise
111126

112127
F = string.format
@@ -142,12 +157,28 @@ function errorline(err)
142157
os.exit(1)
143158
end
144159

160+
if IS_LUA_52_OR_LATER then
161+
function loadLuaFile(path, env)
162+
return loadfile(path, "bt", env)
163+
end
164+
else
165+
function loadLuaFile(path, env)
166+
local chunk, err = loadfile(path)
167+
if not chunk then return chunk, err end
168+
169+
if env then setfenv(chunk, env) end
170+
171+
return chunk
172+
end
173+
end
174+
145175
--==============================================================
146176
--= Preprocessor Script ========================================
147177
--==============================================================
148178

149179
io.stdout:setvbuf("no")
150180
io.stderr:setvbuf("no")
181+
151182
math.randomseed(os.time()) -- In case math.random() is used anywhere.
152183
math.random() -- Must kickstart...
153184

@@ -206,15 +237,13 @@ printfNoise(("="):rep(#header))
206237
local messageHandler = nil
207238

208239
if messageHandlerPath ~= "" then
209-
local chunk, err = loadfile(messageHandlerPath)
240+
-- Make the message handler and the metaprogram share the same environment.
241+
-- This way the message handler can easily define globals that the metaprogram uses.
242+
local chunk, err = loadLuaFile(messageHandlerPath, pp.metaEnvironment)
210243
if not chunk then
211244
errorline("Could not load message handler: "..err)
212245
end
213246

214-
-- Make the message handler and the metaprogram share the same environment.
215-
-- This way the message handler can easily define globals that the metaprogram uses.
216-
setfenv(chunk, pp.metaEnvironment)
217-
218247
messageHandler = chunk()
219248
if type(messageHandler) ~= "function" then
220249
errorline(messageHandlerPath..": File did not return a message handler function.")

0 commit comments

Comments
 (0)