* Recursively evaluates and returns value of `node` and its children. * @param {./bind-expr-defines.AstNode} node * @param {!JsonObject} scope * @throws {Error} * @return {BindExpressionResultDef} * @private
(node, scope)
| 323 | * @private |
| 324 | */ |
| 325 | eval_(node, scope) { |
| 326 | if (!node) { |
| 327 | return null; |
| 328 | } |
| 329 | |
| 330 | const {args, type, value} = node; |
| 331 | |
| 332 | // `value` should always exist for literals. |
| 333 | if (type === AstNodeType.LITERAL && value !== undefined) { |
| 334 | return value; |
| 335 | } |
| 336 | |
| 337 | switch (type) { |
| 338 | case AstNodeType.EXPRESSION: |
| 339 | return this.eval_(args[0], scope); |
| 340 | |
| 341 | case AstNodeType.INVOCATION: |
| 342 | // Built-in functions and macros don't have a caller object. |
| 343 | const isBuiltInOrMacro = args[0] === undefined; |
| 344 | |
| 345 | const caller = this.eval_(args[0], scope); |
| 346 | const params = this.eval_(args[1], scope); |
| 347 | const method = String(value); |
| 348 | |
| 349 | let validFunction; |
| 350 | let unsupportedError; |
| 351 | |
| 352 | if (isBuiltInOrMacro) { |
| 353 | const macro = this.macros_[method]; |
| 354 | if (macro) { |
| 355 | validFunction = function () { |
| 356 | return macro.evaluate( |
| 357 | scope, |
| 358 | Array.prototype.slice.call(arguments) |
| 359 | ); |
| 360 | }; |
| 361 | } else { |
| 362 | validFunction = FUNCTION_ALLOWLIST[CUSTOM_FUNCTIONS][method]; |
| 363 | } |
| 364 | if (!validFunction) { |
| 365 | unsupportedError = `${method} is not a supported function.`; |
| 366 | } |
| 367 | } else { |
| 368 | if (caller === null) { |
| 369 | user().warn( |
| 370 | TAG, |
| 371 | `Cannot invoke method ${method} on null; returning null.` |
| 372 | ); |
| 373 | return null; |
| 374 | } |
| 375 | const callerType = Object.prototype.toString.call(caller); |
| 376 | const allowlist = FUNCTION_ALLOWLIST[callerType]; |
| 377 | if (allowlist) { |
| 378 | const f = caller[method]; |
| 379 | if (f && f === allowlist[method]) { |
| 380 | validFunction = f; |
| 381 | } else if (this.isCustomInstanceFunction_(method)) { |
| 382 | validFunction = allowlist[method]; |
no test coverage detected