@@ -15,6 +15,8 @@ GNU General Public License for more details.
15
15
You should have received a copy of the GNU General Public License
16
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
17
17
]] --
18
+ -- Metamethod safety. Will error instead of calling the metamethod.
19
+ -- No idea on performance.
18
20
--- @diagnostic disable-next-line : undefined-global
19
21
local bit = bit or bit32 or require (' bit' )
20
22
@@ -170,73 +172,37 @@ local OPCODE_M = {
170
172
{b = ' OpArgU' , c = ' OpArgN' },
171
173
{b = ' OpArgU' , c = ' OpArgN' },
172
174
}
173
- -- safety
174
- -- WARNING: EXTREMELY REPETITIVE CODE
175
- -- there is no particular reason why
176
175
local rawget = rawget
177
176
local rawset = rawset
178
177
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
181
181
local t = type (a )
182
182
if t == " number" or t == " string" then
183
- return a < b
183
+ return op ( a , b )
184
184
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
232
188
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
238
196
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 )
240
206
local function rawlen (a )
241
207
if type (a )== " string" then
242
208
return # a
0 commit comments