Skip to content

Commit 94b7e49

Browse files
committed
$symbol accepts callable tables.
1 parent ec45b86 commit 94b7e49

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<a href="https://github.com/ReFreezed/LuaPreprocess/releases/latest">
55
<img src="https://img.shields.io/github/release/ReFreezed/LuaPreprocess.svg" alt="">
66
</a>
7-
<a href="https://github.com/ReFreezed/LuaPreprocess/blob/master/LICENSE">
7+
<a href="https://github.com/ReFreezed/LuaPreprocess/blob/master/LICENSE.txt">
88
<img src="https://img.shields.io/github/license/ReFreezed/LuaPreprocess.svg" alt="">
99
</a>
1010
</p>

preprocess.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1974,7 +1974,13 @@ function metaEnv.__MACRO()
19741974
end
19751975

19761976
function metaEnv.__EVALSYMBOL(v)
1977-
if type(v) == "function" then
1977+
if
1978+
type(v) == "function"
1979+
-- We use debug.getmetatable instead of _G.getmetatable because we
1980+
-- don't want to potentially invoke used code right here - we just
1981+
-- want to know if the value is callable.
1982+
or (type(v) == "table" and debug.getmetatable(v) and debug.getmetatable(v).__call)
1983+
then
19781984
v = v()
19791985
end
19801986
return v

tests/quickTest.lua2p

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,19 @@ other = 500921
167167
!local RANDOM = "math.random()"
168168
local rand = $RANDOM
169169

170-
!local function EQUATION() return "x*3+y" end
170+
!local EQUATION_FROM_FUNCTION = (function()
171+
return "x*3+y"
172+
end)
173+
!local EQUATION_FROM_TABLE = setmetatable({}, {
174+
__call = function()
175+
return "z*-8"
176+
end,
177+
})
171178
local x = 5
172179
local y = 89
173-
local z = $EQUATION
180+
local z = $EQUATION_FROM_FUNCTION
181+
local w = $EQUATION_FROM_TABLE
182+
print("w is "..w)
174183

175184

176185

tests/quickTest.output.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ local rand = math.random()
118118
local x = 5
119119
local y = 89
120120
local z = x*3+y
121+
local w = z*-8
122+
print("w is "..w)
121123

122124

123125

0 commit comments

Comments
 (0)