forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind-expression.js
410 lines (355 loc) · 11.8 KB
/
bind-expression.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {AstNodeType} from './bind-expr-defines';
import {getMode} from '../../../src/mode';
import {isArray, isObject} from '../../../src/types';
import {parser} from './bind-expr-impl';
import {user} from '../../../src/log';
const TAG = 'amp-bind';
/**
* Possible types of a Bind expression evaluation.
* @typedef {(null|boolean|string|number|Array|Object)}
*/
export let BindExpressionResultDef;
/**
* Default maximum number of nodes in an expression AST.
* Double size of a "typical" expression in examples/bind/performance.amp.html.
* @const @private {number}
*/
const DEFAULT_MAX_AST_SIZE = 50;
/** @const @private {string} */
const BUILT_IN_FUNCTIONS = 'built-in-functions';
/**
* Map of object type to function name to whitelisted function.
* @private {!Object<string, !Object<string, Function>>}
*/
let FUNCTION_WHITELIST;
/**
* @return {!Object<string, !Object<string, Function>>}
* @private
*/
function generateFunctionWhitelist() {
/**
* Similar to Array.prototype.splice, except it returns a copy of the
* passed-in array with the desired modifications.
* @param {!Array} array
* @param {number=} start
* @param {number=} deleteCount
* @param {...?} items
*/
/*eslint "no-unused-vars": 0*/
function copyAndSplice(array, start, deleteCount, items) {
if (!isArray(array)) {
throw new Error(
`copyAndSplice: ${array} is not an array.`);
}
const copy = Array.prototype.slice.call(array);
Array.prototype.splice.apply(
copy,
Array.prototype.slice.call(arguments, 1));
return copy;
}
const whitelist = {
'[object Array]':
[
Array.prototype.concat,
Array.prototype.indexOf,
Array.prototype.join,
Array.prototype.lastIndexOf,
Array.prototype.slice,
Array.prototype.includes,
],
'[object String]':
[
String.prototype.charAt,
String.prototype.charCodeAt,
String.prototype.concat,
String.prototype.indexOf,
String.prototype.lastIndexOf,
String.prototype.slice,
String.prototype.split,
String.prototype.substr,
String.prototype.substring,
String.prototype.toLowerCase,
String.prototype.toUpperCase,
],
};
whitelist[BUILT_IN_FUNCTIONS] = [
Math.abs,
Math.ceil,
Math.floor,
Math.max,
Math.min,
Math.random,
Math.round,
Math.sign,
encodeURI,
encodeURIComponent,
];
// Creates a prototype-less map of function name to the function itself.
// This makes function lookups faster (compared to Array.indexOf).
const out = Object.create(null);
Object.keys(whitelist).forEach(type => {
out[type] = Object.create(null);
whitelist[type].forEach((fn, i) => {
if (fn) {
out[type][fn.name] = fn;
} else {
// This can happen if a browser doesn't support a built-in function.
throw new Error(`Unsupported function for ${type} at index ${i}.`);
}
});
});
// Custom functions (non-js-built-ins) must be added manually as their names
// will be minified at compile time.
out[BUILT_IN_FUNCTIONS]['copyAndSplice'] = copyAndSplice;
return out;
}
/**
* A single Bind expression.
*/
export class BindExpression {
/**
* @param {string} expressionString
* @param {number=} opt_maxAstSize
* @throws {Error} On malformed expressions.
*/
constructor(expressionString, opt_maxAstSize) {
if (!FUNCTION_WHITELIST) {
FUNCTION_WHITELIST = generateFunctionWhitelist();
}
/** @const {string} */
this.expressionString = expressionString;
/** @const @private {!./bind-expr-defines.AstNode} */
this.ast_ = parser.parse(this.expressionString);
// Check if this expression string is too large (for performance).
const size = this.numberOfNodesInAst_(this.ast_);
const maxSize = opt_maxAstSize || DEFAULT_MAX_AST_SIZE;
const skipConstraint = getMode().localDev && !getMode().test;
if (size > maxSize && !skipConstraint) {
throw new Error(`Expression size (${size}) exceeds max (${maxSize}). ` +
`Please reduce number of operands.`);
}
}
/**
* Evaluates the expression given a scope.
* @param {!Object} scope
* @throws {Error} On illegal function invocation.
* @return {BindExpressionResultDef}
*/
evaluate(scope) {
return this.eval_(this.ast_, scope);
}
/**
* @param {!./bind-expr-defines.AstNode} ast
* @return {number}
* @private
*/
numberOfNodesInAst_(ast) {
let nodes = 1;
if (ast.args) {
ast.args.forEach(arg => {
if (arg) {
nodes += this.numberOfNodesInAst_(arg);
}
});
}
return nodes;
}
/**
* Recursively evaluates and returns value of `node` and its children.
* @param {./bind-expr-defines.AstNode} node
* @param {!Object} scope
* @throws {Error}
* @return {BindExpressionResultDef}
* @private
*/
eval_(node, scope) {
if (!node) {
return null;
}
const {type, args, value} = node;
// `value` should always exist for literals.
if (type === AstNodeType.LITERAL && value !== undefined) {
return value;
}
switch (type) {
case AstNodeType.EXPRESSION:
return this.eval_(args[0], scope);
case AstNodeType.INVOCATION:
// Built-in functions don't have a caller object.
const isBuiltIn = (args[0] === undefined);
const caller = this.eval_(args[0], scope);
const params = this.eval_(args[1], scope);
const method = String(value);
let validFunction;
let unsupportedError;
if (isBuiltIn) {
validFunction = FUNCTION_WHITELIST[BUILT_IN_FUNCTIONS][method];
if (!validFunction) {
unsupportedError = `${method} is not a supported function.`;
}
} else {
if (caller === null) {
user().warn(TAG, `Cannot invoke method ${method} on null; ` +
`returning null.`);
return null;
}
const callerType = Object.prototype.toString.call(caller);
const whitelist = FUNCTION_WHITELIST[callerType];
if (whitelist) {
const f = caller[method];
if (f && f === whitelist[method]) {
validFunction = f;
}
}
if (!validFunction) {
unsupportedError =
`${callerType}.${method} is not a supported function.`;
}
}
if (validFunction) {
if (Array.isArray(params) && !this.containsObject_(params)) {
return validFunction.apply(caller, params);
} else {
throw new Error(`Unexpected argument type in ${method}().`);
}
}
throw new Error(unsupportedError);
case AstNodeType.MEMBER_ACCESS:
const target = this.eval_(args[0], scope);
const member = this.eval_(args[1], scope);
if (target === null || member === null) {
this.memberAccessWarning_(target, member);
return null;
}
const targetType = typeof target;
if (targetType !== 'string' && targetType !== 'object') {
this.memberAccessWarning_(target, member);
return null;
}
const memberType = typeof member;
if (memberType !== 'string' && memberType !== 'number') {
this.memberAccessWarning_(target, member);
return null;
}
// Ignore Closure's type constraint for `hasOwnProperty`.
if (Object.prototype.hasOwnProperty.call(
/** @type {Object} */ (target), member)) {
return target[member];
} else {
this.memberAccessWarning_(target, member);
}
return null;
case AstNodeType.MEMBER:
return value || this.eval_(args[0], scope);
case AstNodeType.VARIABLE:
const variable = value;
if (Object.prototype.hasOwnProperty.call(scope, variable)) {
return scope[variable];
} else {
user().warn(TAG, `${variable} is not defined; returning null.`);
}
return null;
case AstNodeType.ARGS:
case AstNodeType.ARRAY_LITERAL:
return (args.length > 0) ? this.eval_(args[0], scope) : [];
case AstNodeType.ARRAY:
return args.map(element => this.eval_(element, scope));
case AstNodeType.OBJECT_LITERAL:
return (args.length > 0)
? this.eval_(args[0], scope)
: Object.create(null);
case AstNodeType.OBJECT:
const object = Object.create(null);
args.forEach(keyValue => {
const {k, v} = this.eval_(keyValue, scope);
object[k] = v;
});
return object;
case AstNodeType.KEY_VALUE:
return {
k: this.eval_(args[0], scope),
v: this.eval_(args[1], scope),
};
case AstNodeType.NOT:
return !this.eval_(args[0], scope);
case AstNodeType.UNARY_MINUS:
return -Number(this.eval_(args[0], scope));
case AstNodeType.UNARY_PLUS:
return +Number(this.eval_(args[0], scope));
case AstNodeType.PLUS:
return this.eval_(args[0], scope) + this.eval_(args[1], scope);
case AstNodeType.MINUS:
return Number(this.eval_(args[0], scope)) -
Number(this.eval_(args[1], scope));
case AstNodeType.MULTIPLY:
return Number(this.eval_(args[0], scope)) *
Number(this.eval_(args[1], scope));
case AstNodeType.DIVIDE:
return Number(this.eval_(args[0], scope)) /
Number(this.eval_(args[1], scope));
case AstNodeType.MODULO:
return Number(this.eval_(args[0], scope)) %
Number(this.eval_(args[1], scope));
case AstNodeType.LOGICAL_AND:
return this.eval_(args[0], scope) && this.eval_(args[1], scope);
case AstNodeType.LOGICAL_OR:
return this.eval_(args[0], scope) || this.eval_(args[1], scope);
case AstNodeType.LESS_OR_EQUAL:
return this.eval_(args[0], scope) <= this.eval_(args[1], scope);
case AstNodeType.LESS:
return this.eval_(args[0], scope) < this.eval_(args[1], scope);
case AstNodeType.GREATER_OR_EQUAL:
return this.eval_(args[0], scope) >= this.eval_(args[1], scope);
case AstNodeType.GREATER:
return this.eval_(args[0], scope) > this.eval_(args[1], scope);
case AstNodeType.NOT_EQUAL:
return this.eval_(args[0], scope) != this.eval_(args[1], scope);
case AstNodeType.EQUAL:
return this.eval_(args[0], scope) == this.eval_(args[1], scope);
case AstNodeType.TERNARY:
return this.eval_(args[0], scope)
? this.eval_(args[1], scope)
: this.eval_(args[2], scope);
default:
throw new Error(`Unexpected AstNodeType: ${type}.`);
}
}
/**
* @param {*} target
* @param {*} member
* @private
*/
memberAccessWarning_(target, member) {
user().warn(TAG, `Cannot read property ${JSON.stringify(member)} of ` +
`${JSON.stringify(target)}; returning null.`);
}
/**
* Returns true if input array contains a plain object.
* @param {!Array} array
* @return {boolean}
* @private
*/
containsObject_(array) {
for (let i = 0; i < array.length; i++) {
if (isObject(array[i])) {
return true;
}
}
return false;
}
}