UNPKG

numenor

Version:

Customizable, safe evaluator of JavaScript-like expressions.

73 lines (53 loc) 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Call = void 0; var _ = require("./"); var _Parser = require("../../Parser"); var _Error = require("../Error"); var _util = require("./util"); function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } } function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } var Call = function Call(expr, options, compile) { if (expr.type !== _Parser.ExpressionType.Call) { throw new TypeError((0, _Error.UnknownExpression)(expr)); } var marshallValue = (0, _util.makeValueMarshaller)(options); var lhs = compile(expr.lhs, options, compile); var steps = [[lhs, function (callee) { if (typeof callee !== 'function') { throw new TypeError(_Error.CantInvoke); } return callee; }]]; var isConst = (0, _.hasConstValue)(lhs); var isAsync = (0, _.hasAsyncValue)(lhs); expr.args.forEach(function (argExpr) { var arg = compile(argExpr, options, compile); if (isConst && !(0, _.hasConstValue)(arg)) { isConst = false; } if (!isAsync && (0, _.hasAsyncValue)(arg)) { isAsync = true; } steps.push([arg, marshallValue]); }); return (0, _.mark)({ isAsync: isAsync, isConst: isConst }, function (context, stack) { return (0, _util.evalMaybeAsyncSteps)(context, stack, steps).then(function (_ref) { var _ref2 = _toArray(_ref), callee = _ref2[0], params = _ref2.slice(1); return callee.apply(void 0, _toConsumableArray(params)); }); }); }; exports.Call = Call;