/
operators.js
157 lines (119 loc) · 3.75 KB
/
operators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { default as T } from './Table';
import { coerceToNumber, coerceToBoolean, coerceToString } from './utils';
import { getn } from './lib/table';
import { default as LuaError } from './LuaError';
function binaryArithmetic(left, right, metaMethodName, callback) {
let mt, f;
if ((left && left instanceof T && (mt = left.metatable) && (f = mt.rawget(metaMethodName)))
|| (right && right instanceof T && (mt = right.metatable) && (f = mt.rawget(metaMethodName)))) {
return f(left, right)[0];
}
if (typeof left !== 'number') {
left = coerceToNumber(left, 'attempt to perform arithmetic on a %type value');
}
if (typeof right !== 'number') {
right = coerceToNumber(right, 'attempt to perform arithmetic on a %type value');
}
return callback(left, right);
}
function binaryStringArithmetic(left, right, metaMethodName, callback) {
if (typeof left == 'string' && typeof right == 'string') {
return callback(left, right);
}
return binaryArithmetic(left, right, metaMethodName, callback);
}
function concat(left, right) {
let mt, f;
if (
(left && left instanceof T && (mt = left.metatable) && (f = mt.rawget('__concat')))
|| (right && right instanceof T && (mt = right.metatable) && (f = mt.rawget('__concat')))
) {
return f(left, right)[0];
} else {
right = coerceToString(right, 'attempt to concatenate a %type value');
left = coerceToString(left, 'attempt to concatenate a %type value');
return `${left}${right}`;
}
}
function equal(left, right) {
var mtl, mtr, f, result;
if (right !== left
&& left && left instanceof T
&& right && right instanceof T
&& (mtl = left.metatable)
&& (mtr = right.metatable)
&& mtl === mtr
&& (f = mtl.rawget('__eq'))
) {
return !!f(left, right)[0];
}
return (left === right);
}
function mod(left, right) {
if (
right === 0
|| right === -Infinity
|| right === Infinity
|| global.isNaN(left)
|| global.isNaN(right)
) {
return NaN;
}
let absRight = Math.abs(right);
let result = Math.abs(left) % absRight;
if (left * right < 0) result = absRight - result;
if (right < 0) result *= -1;
return result;
}
function len(value) {
let length, i;
if (value === undefined) throw new LuaError('attempt to get length of a nil value');
if (value instanceof T) return getn(value);
if (typeof value == 'object') {
let length = 0;
for (let key in value) {
if (value.hasOwnProperty(key)) {
length++;
}
}
return length;
}
return value.length;
}
function unaryMinus(value) {
var mt, f, result;
if (value && value instanceof T && (mt = value.metatable) && (f = mt.rawget('__unm'))) {
return f(value)[0];
}
if (typeof value !== 'number') {
value = coerceToNumber(value, 'attempt to perform arithmetic on a %type value');
}
return -value;
}
const op = {
concat,
len,
eq: equal,
unm: unaryMinus,
bool: coerceToBoolean,
neq: (...args) => !equal(...args),
not: (...args) => !coerceToBoolean(...args),
add: (left, right) => binaryArithmetic(left, right, '__add', (l, r) => l + r),
sub: (left, right) => binaryArithmetic(left, right, '__sub', (l, r) => l - r),
mul: (left, right) => binaryArithmetic(left, right, '__mul', (l, r) => l * r),
div: (left, right) => {
if (right === undefined) throw new LuaError('attempt to perform arithmetic on a nil value');
return binaryArithmetic(left, right, '__div', (l, r) => l / r);
},
mod: (left, right) => binaryArithmetic(left, right, '__mod', mod),
pow: (left, right) => binaryArithmetic(left, right, '__pow', Math.pow),
lt: (left, right) => binaryStringArithmetic(left, right, '__lt', (l, r) => l < r),
lte: (left, right) => binaryStringArithmetic(left, right, '__le', (l, r) => l <= r),
gt(left, right) {
return !op.lte(left, right);
},
gte(left, right) {
return !op.lt(left, right);
}
};
export default op;