forked from donmccurdy/expression-eval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
293 lines (244 loc) · 8.24 KB
/
index.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
const jsep = require('jsep');
/**
* Evaluation code from JSEP project, under MIT License.
* Copyright (c) 2013 Stephen Oney, http://jsep.from.so/
*/
const binops = {
'||': function (a, b) { return a || b; },
'&&': function (a, b) { return a && b; },
'|': function (a, b) { return a | b; },
'^': function (a, b) { return a ^ b; },
'&': function (a, b) { return a & b; },
'==': function (a, b) { return a == b; }, // jshint ignore:line
'!=': function (a, b) { return a != b; }, // jshint ignore:line
'===': function (a, b) { return a === b; },
'!==': function (a, b) { return a !== b; },
'<': function (a, b) { return a < b; },
'>': function (a, b) { return a > b; },
'<=': function (a, b) { return a <= b; },
'>=': function (a, b) { return a >= b; },
'<<': function (a, b) { return a << b; },
'>>': function (a, b) { return a >> b; },
'>>>': function (a, b) { return a >>> b; },
'+': function (a, b) { return a + b; },
'-': function (a, b) { return a - b; },
'*': function (a, b) { return a * b; },
'/': function (a, b) { return a / b; },
'%': function (a, b) { return a % b; }
};
const unops = {
'-' : function (a) { return -a; },
'+' : function (a) { return a; },
'~' : function (a) { return ~a; },
'!' : function (a) { return !a; },
};
function evaluateArray ( list, context ) {
return list.map(function (v) { return evaluate(v, context); });
}
async function evaluateArrayAsync( list, context ) {
const res = await Promise.all(list.map((v) => evaluateAsync(v, context)));
return res;
}
function evaluateMember ( node, context ) {
const object = evaluate(node.object, context);
if ( node.computed ) {
return [object, object[evaluate(node.property, context)]];
} else {
return [object, object[node.property.name]];
}
}
async function evaluateMemberAsync( node, context ) {
const object = await evaluateAsync(node.object, context);
if ( node.computed) {
return [object, object[await evaluateAsync(node.property, context)]];
} else {
return [object, object[node.property.name]];
}
}
function evaluate ( node, context ) {
switch ( node.type ) {
case 'ArrayExpression':
return evaluateArray( node.elements, context );
case 'BinaryExpression':
return binops[ node.operator ]( evaluate( node.left, context ), evaluate( node.right, context ) );
case 'CallExpression':
let caller, fn, assign;
if (node.callee.type === 'MemberExpression') {
assign = evaluateMember( node.callee, context );
caller = assign[0];
fn = assign[1];
} else {
fn = evaluate( node.callee, context );
}
if (typeof fn !== 'function') { return undefined; }
return fn.apply( caller, evaluateArray( node.arguments, context ) );
case 'ConditionalExpression':
return evaluate( node.test, context )
? evaluate( node.consequent, context )
: evaluate( node.alternate, context );
case 'Identifier':
if( context.hasOwnProperty( node.name ) ) {
return context[ node.name ];
} else if( typeof context.getValue == 'function' ) {
return context.getValue( node.name );
} else {
return undefined;
}
case 'Literal':
return node.value;
case 'LogicalExpression':
if (node.operator === '||') {
return evaluate( node.left, context ) || evaluate( node.right, context );
} else if (node.operator === '&&') {
return evaluate( node.left, context ) && evaluate( node.right, context );
}
return binops[ node.operator ]( evaluate( node.left, context ), evaluate( node.right, context ) );
case 'MemberExpression':
return evaluateMember(node, context)[1];
case 'ThisExpression':
return context;
case 'UnaryExpression':
return unops[ node.operator ]( evaluate( node.argument, context ) );
default:
return undefined;
}
}
function readIdentifiers( node, context ) {
let idents = [];
function buildIdentifiers( node ) {
switch( node.type ) {
case 'ArrayExpression':
node.elements.forEach( ( elem ) => {
buildIdentifiers( elem );
} );
break;
case 'BinaryExpression':
buildIdentifiers( node.left );
buildIdentifiers( node.right );
break;
case 'CallExpression':
node.arguments.forEach( ( arg ) => {
buildIdentifiers( arg );
} );
if( node.callee.type !== 'Identifier' ) {
buildIdentifiers( node.callee );
}
else if( node.callee.name == 'getValue' ) {
// user is calling getValue within expression - first parameter is identifier
if( node.arguments.length > 0 ) {
idents.push( evaluate( node.arguments[ 0 ], context || {} ) );
}
}
break;
case 'ConditionalExpression':
buildIdentifiers( node.consequent );
buildIdentifiers( node.alternate );
break;
case 'Identifier':
idents.push( node.name );
break;
case 'Literal':
break;
case 'LogicalExpression':
buildIdentifiers( node.left );
buildIdentifiers( node.right );
break;
case 'MemberExpression':
buildIdentifiers( node.object );
if( node.property.type !== 'Identifier' ) {
buildIdentifiers( node.property );
}
break;
case 'ThisExpression':
break;
case 'UnaryExpression':
buildIdentifiers( node.argument );
break;
}
}
buildIdentifiers( node );
return idents;
}
async function evaluateAsync( node, context ) {
switch ( node.type ) {
case 'ArrayExpression':
return await evaluateArrayAsync( node.elements, context );
case 'BinaryExpression': {
const [left, right] = await Promise.all([
evaluateAsync( node.left, context ),
evaluateAsync( node.right, context )
]);
return binops[ node.operator ]( left, right );
}
case 'CallExpression':
let caller, fn, assign;
if (node.callee.type === 'MemberExpression') {
assign = await evaluateMemberAsync( node.callee, context );
caller = assign[0];
fn = assign[1];
} else {
fn = await evaluateAsync( node.callee, context );
}
if (typeof fn !== 'function') {
return undefined;
}
return await fn.apply(
caller,
await evaluateArrayAsync( node.arguments, context ),
);
case 'ConditionalExpression':
return (await evaluateAsync( node.test, context ))
? await evaluateAsync( node.consequent, context )
: await evaluateAsync( node.alternate, context );
case 'Identifier':
if( context.hasOwnProperty( node.name ) ) {
return context[ node.name ];
} else if( typeof context.getValue == 'function' ) {
return context.getValue( node.name );
} else {
return undefined;
}
case 'Literal':
return node.value;
case 'LogicalExpression': {
if (node.operator === '||') {
return (
(await evaluateAsync( node.left, context )) ||
(await evaluateAsync( node.right, context ))
);
} else if (node.operator === '&&') {
return (
(await evaluateAsync( node.left, context )) &&
(await evaluateAsync( node.right, context ))
);
}
const [left, right] = await Promise.all([
evaluateAsync( node.left, context ),
evaluateAsync( node.right, context )
]);
return binops[ node.operator ]( left, right );
}
case 'MemberExpression':
return (await evaluateMemberAsync(node, context))[1];
case 'ThisExpression':
return context;
case 'UnaryExpression':
return unops[ node.operator ](await evaluateAsync( node.argument, context ));
default:
return undefined;
}
}
function compile (expression) {
return evaluate.bind(null, jsep(expression));
}
function compileAsync(expression) {
return evaluateAsync.bind(null, jsep(expression));
}
module.exports = {
parse: jsep,
readIdentifiers: readIdentifiers,
eval: evaluate,
evalAsync: evaluateAsync,
compile: compile,
compileAsync: compileAsync
};