Skip to content

Commit 89ac477

Browse files
author
bainchild
committed
Refurbish old mods
1 parent 3172c67 commit 89ac477

File tree

2 files changed

+31
-59
lines changed

2 files changed

+31
-59
lines changed

Diff for: flavor/FiOne-mid.lua

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ GNU General Public License for more details.
1515
You should have received a copy of the GNU General Public License
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
]] --
18+
-- what if you wanted to do as John Lua never intended?
19+
-- what if you wanted to (probably) severly impact vm speed?
20+
-- then this is the mod for you!
21+
-- you want to disable metamethods? sure!
22+
-- you want to insert opcodes in source code? we've got that!
23+
-- what if you wanted to hook into libraries? all that and more!
1824
local bit = bit or bit32 or require('bit')
1925

2026
if not table.create then function table.create(_) return {} end end

Diff for: flavor/FiOne-mt.lua

+25-59
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ GNU General Public License for more details.
1515
You should have received a copy of the GNU General Public License
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
]] --
18+
-- Metamethod safety. Will error instead of calling the metamethod.
19+
-- No idea on performance.
1820
---@diagnostic disable-next-line: undefined-global
1921
local bit = bit or bit32 or require('bit')
2022

@@ -170,73 +172,37 @@ local OPCODE_M = {
170172
{b = 'OpArgU', c = 'OpArgN'},
171173
{b = 'OpArgU', c = 'OpArgN'},
172174
}
173-
-- safety
174-
-- WARNING: EXTREMELY REPETITIVE CODE
175-
-- there is no particular reason why
176175
local rawget = rawget
177176
local rawset = rawset
178177
local rawequal = rawequal
179-
local function rawlower(a,b)
180-
if type(a)==type(b) then
178+
local function make_comparison(op)
179+
return function(a,b)
180+
if type(a)==type(b) then
181181
local t = type(a)
182182
if t=="number" or t=="string" then
183-
return a<b
183+
return op(a,b)
184184
end
185-
end
186-
error("attempt to compare "..type(a).." with "..type(b),3)
187-
end
188-
local function rawlowereq(a,b)
189-
if type(a)==type(b) then
190-
local t = type(a)
191-
if t=="number" or t=="string" then
192-
return a<=b
193-
end
194-
end
195-
error("attempt to compare "..type(a).." with "..type(b),3)
196-
end
197-
local function rawadd(a,b)
198-
if (type(a)=="string" or type(a)=="number") and (type(b)=="string" or type(b)=="number") then
199-
return a+b
200-
end
201-
error("attempt to perform arithmetic on a "..type(a).." value",3)
202-
end
203-
local function rawsub(a,b)
204-
if (type(a)=="string" or type(a)=="number") and (type(b)=="string" or type(b)=="number") then
205-
return a-b
206-
end
207-
error("attempt to perform arithmetic on a "..type(a).." value",3)
208-
end
209-
local function rawmul(a,b)
210-
if (type(a)=="string" or type(a)=="number") and (type(b)=="string" or type(b)=="number") then
211-
return a*b
212-
end
213-
error("attempt to perform arithmetic on a "..type(a).." value",3)
214-
end
215-
local function rawdiv(a,b)
216-
if (type(a)=="string" or type(a)=="number") and (type(b)=="string" or type(b)=="number") then
217-
return a/b
218-
end
219-
error("attempt to perform arithmetic on a "..type(a).." value",3)
220-
end
221-
local function rawpow(a,b)
222-
if (type(a)=="string" or type(a)=="number") and (type(b)=="string" or type(b)=="number") then
223-
return a^b
224-
end
225-
error("attempt to perform arithmetic on a "..type(a).." value",3)
226-
end
227-
local function rawmod(a,b)
228-
if (type(a)=="string" or type(a)=="number") and (type(b)=="string" or type(b)=="number") then
229-
return a%b
230-
end
231-
error("attempt to perform arithmetic on a "..type(a).." value",3)
185+
end
186+
error("attempt to compare "..type(a).." with "..type(b),4)
187+
end
232188
end
233-
local function rawconcat(a,b)
234-
if (type(a)=="string" or type(a)=="number") and (type(b)=="string" or type(b)=="number") then
235-
return a..b
236-
end
237-
error("attempt to concatenate a "..type(a).." value",3)
189+
local function make_arith(op)
190+
return function(a,b)
191+
if (type(a)=="string" or type(a)=="number") and (type(b)=="string" or type(b)=="number") then
192+
return op(a,b)
193+
end
194+
error("attempt to perform arithmetic on a "..type(a).." value",4)
195+
end
238196
end
239-
197+
local rawlower = make_comparison(function(a,b)return a<b end)
198+
local rawlowereq = make_comparison(function(a,b)return a<=b end)
199+
local rawadd = make_arith(function(a,b)return a+b end)
200+
local rawsub = make_arith(function(a,b)return a-b end)
201+
local rawmul = make_arith(function(a,b)return a*b end)
202+
local rawdiv = make_arith(function(a,b)return a/b end)
203+
local rawpow = make_arith(function(a,b)return a^b end)
204+
local rawmod = make_arith(function(a,b)return a%b end)
205+
local rawconcat = make_arith(function(a,b)return a..b end)
240206
local function rawlen(a)
241207
if type(a)=="string" then
242208
return #a

0 commit comments

Comments
 (0)