UNPKG

hyperformula-dc

Version:

HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas

265 lines (214 loc) 11 kB
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } import "core-js/modules/es.array.map.js"; import "core-js/modules/es.array.slice.js"; import "core-js/modules/es.array.reduce.js"; import "core-js/modules/es.symbol.js"; import "core-js/modules/es.symbol.description.js"; import "core-js/modules/es.object.to-string.js"; import "core-js/modules/es.symbol.iterator.js"; import "core-js/modules/es.array.iterator.js"; import "core-js/modules/es.string.iterator.js"; import "core-js/modules/web.dom-collections.iterator.js"; import "core-js/modules/es.array.from.js"; import "core-js/modules/es.function.name.js"; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } /** * @license * Copyright (c) 2021 Handsoncode. All rights reserved. */ import { AbsoluteCellRange } from './AbsoluteCellRange'; import { InterpreterState } from './interpreter/InterpreterState'; import { ArgumentTypes } from './interpreter/plugin/FunctionPlugin'; import { AstNodeType } from './parser'; export var ArraySize = /*#__PURE__*/function () { function ArraySize(width, height) { var isRef = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; _classCallCheck(this, ArraySize); this.width = width; this.height = height; this.isRef = isRef; if (width <= 0 || height <= 0) { throw Error('Incorrect array size'); } } _createClass(ArraySize, [{ key: "isScalar", value: function isScalar() { return this.width <= 1 && this.height <= 1 || this.isRef; } }], [{ key: "fromArray", value: function fromArray(array) { return new ArraySize(array.length > 0 ? array[0].length : 0, array.length); } }, { key: "error", value: function error() { return new ArraySize(1, 1, true); } }, { key: "scalar", value: function scalar() { return new ArraySize(1, 1, false); } }]); return ArraySize; }(); function arraySizeForBinaryOp(leftArraySize, rightArraySize) { return new ArraySize(Math.max(leftArraySize.width, rightArraySize.width), Math.max(leftArraySize.height, rightArraySize.height)); } function arraySizeForUnaryOp(arraySize) { return new ArraySize(arraySize.width, arraySize.height); } export var ArraySizePredictor = /*#__PURE__*/function () { function ArraySizePredictor(config, functionRegistry) { _classCallCheck(this, ArraySizePredictor); this.config = config; this.functionRegistry = functionRegistry; } _createClass(ArraySizePredictor, [{ key: "checkArraySize", value: function checkArraySize(ast, formulaAddress) { return this.checkArraySizeForAst(ast, { formulaAddress: formulaAddress, arraysFlag: this.config.useArrayArithmetic }); } }, { key: "checkArraySizeForFunction", value: function checkArraySizeForFunction(ast, state) { var _this = this; var metadata = this.functionRegistry.getMetadata(ast.procedureName); var pluginArraySizeFunction = this.functionRegistry.getArraySizeFunction(ast.procedureName); if (pluginArraySizeFunction !== undefined) { return pluginArraySizeFunction(ast, state); } var subChecks = ast.args.map(function (arg) { var _a; return _this.checkArraySizeForAst(arg, new InterpreterState(state.formulaAddress, state.arraysFlag || ((_a = metadata === null || metadata === void 0 ? void 0 : metadata.arrayFunction) !== null && _a !== void 0 ? _a : false))); }); if (metadata === undefined || metadata.expandRanges || !state.arraysFlag || metadata.vectorizationForbidden || metadata.parameters === undefined) { return new ArraySize(1, 1); } var argumentDefinitions = _toConsumableArray(metadata.parameters); if (metadata.repeatLastArgs === undefined && argumentDefinitions.length < subChecks.length) { return ArraySize.error(); } if (metadata.repeatLastArgs !== undefined && argumentDefinitions.length < subChecks.length && (subChecks.length - argumentDefinitions.length) % metadata.repeatLastArgs !== 0) { return ArraySize.error(); } while (argumentDefinitions.length < subChecks.length) { argumentDefinitions.push.apply(argumentDefinitions, _toConsumableArray(argumentDefinitions.slice(argumentDefinitions.length - metadata.repeatLastArgs))); } var maxWidth = 1; var maxHeight = 1; for (var i = 0; i < subChecks.length; i++) { if (argumentDefinitions[i].argumentType !== ArgumentTypes.RANGE && argumentDefinitions[i].argumentType !== ArgumentTypes.ANY) { maxHeight = Math.max(maxHeight, subChecks[i].height); maxWidth = Math.max(maxWidth, subChecks[i].width); } } return new ArraySize(maxWidth, maxHeight); } }, { key: "checkArraySizeForAst", value: function checkArraySizeForAst(ast, state) { var _this2 = this; switch (ast.type) { case AstNodeType.FUNCTION_CALL: { return this.checkArraySizeForFunction(ast, state); } case AstNodeType.CELL_RANGE: { var range = AbsoluteCellRange.fromCellRangeOrUndef(ast, state.formulaAddress); if (range === undefined) { return ArraySize.error(); } else { return new ArraySize(range.width(), range.height(), true); } } case AstNodeType.ARRAY: { var heights = []; var widths = []; var _iterator = _createForOfIteratorHelper(ast.args), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var row = _step.value; var sizes = row.map(function (ast) { return _this2.checkArraySizeForAst(ast, state); }); var h = Math.min.apply(Math, _toConsumableArray(sizes.map(function (size) { return size.height; }))); var w = sizes.reduce(function (total, size) { return total + size.width; }, 0); heights.push(h); widths.push(w); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } var height = heights.reduce(function (total, h) { return total + h; }, 0); var width = Math.min.apply(Math, widths); return new ArraySize(width, height); } case AstNodeType.STRING: case AstNodeType.NUMBER: return ArraySize.scalar(); case AstNodeType.CELL_REFERENCE: return new ArraySize(1, 1, true); case AstNodeType.DIV_OP: case AstNodeType.CONCATENATE_OP: case AstNodeType.EQUALS_OP: case AstNodeType.GREATER_THAN_OP: case AstNodeType.GREATER_THAN_OR_EQUAL_OP: case AstNodeType.LESS_THAN_OP: case AstNodeType.LESS_THAN_OR_EQUAL_OP: case AstNodeType.MINUS_OP: case AstNodeType.NOT_EQUAL_OP: case AstNodeType.PLUS_OP: case AstNodeType.POWER_OP: case AstNodeType.TIMES_OP: { var left = this.checkArraySizeForAst(ast.left, state); var right = this.checkArraySizeForAst(ast.right, state); if (!state.arraysFlag && (left.height > 1 || left.width > 1 || right.height > 1 || right.width > 1)) { return ArraySize.error(); } return arraySizeForBinaryOp(left, right); } case AstNodeType.MINUS_UNARY_OP: case AstNodeType.PLUS_UNARY_OP: case AstNodeType.PERCENT_OP: { var val = this.checkArraySizeForAst(ast.value, state); if (!state.arraysFlag && (val.height > 1 || val.width > 1)) { return ArraySize.error(); } return arraySizeForUnaryOp(val); } case AstNodeType.EMPTY: return ArraySize.error(); default: return ArraySize.error(); } } }]); return ArraySizePredictor; }();