FunctionNode.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. import _defineProperty from "@babel/runtime/helpers/defineProperty";
  2. import { isAccessorNode, isFunctionAssignmentNode, isIndexNode, isNode, isSymbolNode } from '../../utils/is.js';
  3. import { escape, format } from '../../utils/string.js';
  4. import { hasOwnProperty } from '../../utils/object.js';
  5. import { getSafeProperty, validateSafeMethod } from '../../utils/customs.js';
  6. import { createSubScope } from '../../utils/scope.js';
  7. import { factory } from '../../utils/factory.js';
  8. import { defaultTemplate, latexFunctions } from '../../utils/latex.js';
  9. var name = 'FunctionNode';
  10. var dependencies = ['math', 'Node', 'SymbolNode'];
  11. export var createFunctionNode = /* #__PURE__ */factory(name, dependencies, _ref => {
  12. var {
  13. math,
  14. Node,
  15. SymbolNode
  16. } = _ref;
  17. /* format to fixed length */
  18. var strin = entity => format(entity, {
  19. truncate: 78
  20. });
  21. /*
  22. * Expand a LaTeX template
  23. *
  24. * @param {string} template
  25. * @param {Node} node
  26. * @param {Object} options
  27. * @private
  28. **/
  29. function expandTemplate(template, node, options) {
  30. var latex = '';
  31. // Match everything of the form ${identifier} or ${identifier[2]} or $$
  32. // while submatching identifier and 2 (in the second case)
  33. var regex = /\$(?:\{([a-z_][a-z_0-9]*)(?:\[([0-9]+)\])?\}|\$)/gi;
  34. var inputPos = 0; // position in the input string
  35. var match;
  36. while ((match = regex.exec(template)) !== null) {
  37. // go through all matches
  38. // add everything in front of the match to the LaTeX string
  39. latex += template.substring(inputPos, match.index);
  40. inputPos = match.index;
  41. if (match[0] === '$$') {
  42. // escaped dollar sign
  43. latex += '$';
  44. inputPos++;
  45. } else {
  46. // template parameter
  47. inputPos += match[0].length;
  48. var property = node[match[1]];
  49. if (!property) {
  50. throw new ReferenceError('Template: Property ' + match[1] + ' does not exist.');
  51. }
  52. if (match[2] === undefined) {
  53. // no square brackets
  54. switch (typeof property) {
  55. case 'string':
  56. latex += property;
  57. break;
  58. case 'object':
  59. if (isNode(property)) {
  60. latex += property.toTex(options);
  61. } else if (Array.isArray(property)) {
  62. // make array of Nodes into comma separated list
  63. latex += property.map(function (arg, index) {
  64. if (isNode(arg)) {
  65. return arg.toTex(options);
  66. }
  67. throw new TypeError('Template: ' + match[1] + '[' + index + '] is not a Node.');
  68. }).join(',');
  69. } else {
  70. throw new TypeError('Template: ' + match[1] + ' has to be a Node, String or array of Nodes');
  71. }
  72. break;
  73. default:
  74. throw new TypeError('Template: ' + match[1] + ' has to be a Node, String or array of Nodes');
  75. }
  76. } else {
  77. // with square brackets
  78. if (isNode(property[match[2]] && property[match[2]])) {
  79. latex += property[match[2]].toTex(options);
  80. } else {
  81. throw new TypeError('Template: ' + match[1] + '[' + match[2] + '] is not a Node.');
  82. }
  83. }
  84. }
  85. }
  86. latex += template.slice(inputPos); // append rest of the template
  87. return latex;
  88. }
  89. class FunctionNode extends Node {
  90. /**
  91. * @constructor FunctionNode
  92. * @extends {./Node}
  93. * invoke a list with arguments on a node
  94. * @param {./Node | string} fn
  95. * Item resolving to a function on which to invoke
  96. * the arguments, typically a SymboNode or AccessorNode
  97. * @param {./Node[]} args
  98. */
  99. constructor(fn, args) {
  100. super();
  101. if (typeof fn === 'string') {
  102. fn = new SymbolNode(fn);
  103. }
  104. // validate input
  105. if (!isNode(fn)) throw new TypeError('Node expected as parameter "fn"');
  106. if (!Array.isArray(args) || !args.every(isNode)) {
  107. throw new TypeError('Array containing Nodes expected for parameter "args"');
  108. }
  109. this.fn = fn;
  110. this.args = args || [];
  111. }
  112. // readonly property name
  113. get name() {
  114. return this.fn.name || '';
  115. }
  116. get type() {
  117. return name;
  118. }
  119. get isFunctionNode() {
  120. return true;
  121. }
  122. /**
  123. * Compile a node into a JavaScript function.
  124. * This basically pre-calculates as much as possible and only leaves open
  125. * calculations which depend on a dynamic scope with variables.
  126. * @param {Object} math Math.js namespace with functions and constants.
  127. * @param {Object} argNames An object with argument names as key and `true`
  128. * as value. Used in the SymbolNode to optimize
  129. * for arguments from user assigned functions
  130. * (see FunctionAssignmentNode) or special symbols
  131. * like `end` (see IndexNode).
  132. * @return {function} Returns a function which can be called like:
  133. * evalNode(scope: Object, args: Object, context: *)
  134. */
  135. _compile(math, argNames) {
  136. // compile arguments
  137. var evalArgs = this.args.map(arg => arg._compile(math, argNames));
  138. if (isSymbolNode(this.fn)) {
  139. var _name = this.fn.name;
  140. if (!argNames[_name]) {
  141. // we can statically determine whether the function
  142. // has the rawArgs property
  143. var fn = _name in math ? getSafeProperty(math, _name) : undefined;
  144. var isRaw = typeof fn === 'function' && fn.rawArgs === true;
  145. var resolveFn = scope => {
  146. var value;
  147. if (scope.has(_name)) {
  148. value = scope.get(_name);
  149. } else if (_name in math) {
  150. value = getSafeProperty(math, _name);
  151. } else {
  152. return FunctionNode.onUndefinedFunction(_name);
  153. }
  154. if (typeof value === 'function') {
  155. return value;
  156. }
  157. throw new TypeError("'".concat(_name, "' is not a function; its value is:\n ").concat(strin(value)));
  158. };
  159. if (isRaw) {
  160. // pass unevaluated parameters (nodes) to the function
  161. // "raw" evaluation
  162. var rawArgs = this.args;
  163. return function evalFunctionNode(scope, args, context) {
  164. var fn = resolveFn(scope);
  165. return fn(rawArgs, math, createSubScope(scope, args), scope);
  166. };
  167. } else {
  168. // "regular" evaluation
  169. switch (evalArgs.length) {
  170. case 0:
  171. return function evalFunctionNode(scope, args, context) {
  172. var fn = resolveFn(scope);
  173. return fn();
  174. };
  175. case 1:
  176. return function evalFunctionNode(scope, args, context) {
  177. var fn = resolveFn(scope);
  178. var evalArg0 = evalArgs[0];
  179. return fn(evalArg0(scope, args, context));
  180. };
  181. case 2:
  182. return function evalFunctionNode(scope, args, context) {
  183. var fn = resolveFn(scope);
  184. var evalArg0 = evalArgs[0];
  185. var evalArg1 = evalArgs[1];
  186. return fn(evalArg0(scope, args, context), evalArg1(scope, args, context));
  187. };
  188. default:
  189. return function evalFunctionNode(scope, args, context) {
  190. var fn = resolveFn(scope);
  191. var values = evalArgs.map(evalArg => evalArg(scope, args, context));
  192. return fn(...values);
  193. };
  194. }
  195. }
  196. } else {
  197. // the function symbol is an argName
  198. var _rawArgs = this.args;
  199. return function evalFunctionNode(scope, args, context) {
  200. var fn = args[_name];
  201. if (typeof fn !== 'function') {
  202. throw new TypeError("Argument '".concat(_name, "' was not a function; received: ").concat(strin(fn)));
  203. }
  204. if (fn.rawArgs) {
  205. // "Raw" evaluation
  206. return fn(_rawArgs, math, createSubScope(scope, args), scope);
  207. } else {
  208. var values = evalArgs.map(evalArg => evalArg(scope, args, context));
  209. return fn.apply(fn, values);
  210. }
  211. };
  212. }
  213. } else if (isAccessorNode(this.fn) && isIndexNode(this.fn.index) && this.fn.index.isObjectProperty()) {
  214. // execute the function with the right context:
  215. // the object of the AccessorNode
  216. var evalObject = this.fn.object._compile(math, argNames);
  217. var prop = this.fn.index.getObjectProperty();
  218. var _rawArgs2 = this.args;
  219. return function evalFunctionNode(scope, args, context) {
  220. var object = evalObject(scope, args, context);
  221. validateSafeMethod(object, prop);
  222. var isRaw = object[prop] && object[prop].rawArgs;
  223. if (isRaw) {
  224. // "Raw" evaluation
  225. return object[prop](_rawArgs2, math, createSubScope(scope, args), scope);
  226. } else {
  227. // "regular" evaluation
  228. var values = evalArgs.map(evalArg => evalArg(scope, args, context));
  229. return object[prop].apply(object, values);
  230. }
  231. };
  232. } else {
  233. // node.fn.isAccessorNode && !node.fn.index.isObjectProperty()
  234. // we have to dynamically determine whether the function has the
  235. // rawArgs property
  236. var fnExpr = this.fn.toString();
  237. var evalFn = this.fn._compile(math, argNames);
  238. var _rawArgs3 = this.args;
  239. return function evalFunctionNode(scope, args, context) {
  240. var fn = evalFn(scope, args, context);
  241. if (typeof fn !== 'function') {
  242. throw new TypeError("Expression '".concat(fnExpr, "' did not evaluate to a function; value is:") + "\n ".concat(strin(fn)));
  243. }
  244. if (fn.rawArgs) {
  245. // "Raw" evaluation
  246. return fn(_rawArgs3, math, createSubScope(scope, args), scope);
  247. } else {
  248. // "regular" evaluation
  249. var values = evalArgs.map(evalArg => evalArg(scope, args, context));
  250. return fn.apply(fn, values);
  251. }
  252. };
  253. }
  254. }
  255. /**
  256. * Execute a callback for each of the child nodes of this node
  257. * @param {function(child: Node, path: string, parent: Node)} callback
  258. */
  259. forEach(callback) {
  260. callback(this.fn, 'fn', this);
  261. for (var i = 0; i < this.args.length; i++) {
  262. callback(this.args[i], 'args[' + i + ']', this);
  263. }
  264. }
  265. /**
  266. * Create a new FunctionNode whose children are the results of calling
  267. * the provided callback function for each child of the original node.
  268. * @param {function(child: Node, path: string, parent: Node): Node} callback
  269. * @returns {FunctionNode} Returns a transformed copy of the node
  270. */
  271. map(callback) {
  272. var fn = this._ifNode(callback(this.fn, 'fn', this));
  273. var args = [];
  274. for (var i = 0; i < this.args.length; i++) {
  275. args[i] = this._ifNode(callback(this.args[i], 'args[' + i + ']', this));
  276. }
  277. return new FunctionNode(fn, args);
  278. }
  279. /**
  280. * Create a clone of this node, a shallow copy
  281. * @return {FunctionNode}
  282. */
  283. clone() {
  284. return new FunctionNode(this.fn, this.args.slice(0));
  285. }
  286. /**
  287. * Throws an error 'Undefined function {name}'
  288. * @param {string} name
  289. */
  290. /**
  291. * Get string representation. (wrapper function)
  292. * This overrides parts of Node's toString function.
  293. * If callback is an object containing callbacks, it
  294. * calls the correct callback for the current node,
  295. * otherwise it falls back to calling Node's toString
  296. * function.
  297. *
  298. * @param {Object} options
  299. * @return {string} str
  300. * @override
  301. */
  302. toString(options) {
  303. var customString;
  304. var name = this.fn.toString(options);
  305. if (options && typeof options.handler === 'object' && hasOwnProperty(options.handler, name)) {
  306. // callback is a map of callback functions
  307. customString = options.handler[name](this, options);
  308. }
  309. if (typeof customString !== 'undefined') {
  310. return customString;
  311. }
  312. // fall back to Node's toString
  313. return super.toString(options);
  314. }
  315. /**
  316. * Get string representation
  317. * @param {Object} options
  318. * @return {string} str
  319. */
  320. _toString(options) {
  321. var args = this.args.map(function (arg) {
  322. return arg.toString(options);
  323. });
  324. var fn = isFunctionAssignmentNode(this.fn) ? '(' + this.fn.toString(options) + ')' : this.fn.toString(options);
  325. // format the arguments like "add(2, 4.2)"
  326. return fn + '(' + args.join(', ') + ')';
  327. }
  328. /**
  329. * Get a JSON representation of the node
  330. * @returns {Object}
  331. */
  332. toJSON() {
  333. return {
  334. mathjs: name,
  335. fn: this.fn,
  336. args: this.args
  337. };
  338. }
  339. /**
  340. * Instantiate an AssignmentNode from its JSON representation
  341. * @param {Object} json An object structured like
  342. * `{"mathjs": "FunctionNode", fn: ..., args: ...}`,
  343. * where mathjs is optional
  344. * @returns {FunctionNode}
  345. */
  346. /**
  347. * Get HTML representation
  348. * @param {Object} options
  349. * @return {string} str
  350. */
  351. toHTML(options) {
  352. var args = this.args.map(function (arg) {
  353. return arg.toHTML(options);
  354. });
  355. // format the arguments like "add(2, 4.2)"
  356. return '<span class="math-function">' + escape(this.fn) + '</span><span class="math-paranthesis math-round-parenthesis">(</span>' + args.join('<span class="math-separator">,</span>') + '<span class="math-paranthesis math-round-parenthesis">)</span>';
  357. }
  358. /**
  359. * Get LaTeX representation. (wrapper function)
  360. * This overrides parts of Node's toTex function.
  361. * If callback is an object containing callbacks, it
  362. * calls the correct callback for the current node,
  363. * otherwise it falls back to calling Node's toTex
  364. * function.
  365. *
  366. * @param {Object} options
  367. * @return {string}
  368. */
  369. toTex(options) {
  370. var customTex;
  371. if (options && typeof options.handler === 'object' && hasOwnProperty(options.handler, this.name)) {
  372. // callback is a map of callback functions
  373. customTex = options.handler[this.name](this, options);
  374. }
  375. if (typeof customTex !== 'undefined') {
  376. return customTex;
  377. }
  378. // fall back to Node's toTex
  379. return super.toTex(options);
  380. }
  381. /**
  382. * Get LaTeX representation
  383. * @param {Object} options
  384. * @return {string} str
  385. */
  386. _toTex(options) {
  387. var args = this.args.map(function (arg) {
  388. // get LaTeX of the arguments
  389. return arg.toTex(options);
  390. });
  391. var latexConverter;
  392. if (latexFunctions[this.name]) {
  393. latexConverter = latexFunctions[this.name];
  394. }
  395. // toTex property on the function itself
  396. if (math[this.name] && (typeof math[this.name].toTex === 'function' || typeof math[this.name].toTex === 'object' || typeof math[this.name].toTex === 'string')) {
  397. // .toTex is a callback function
  398. latexConverter = math[this.name].toTex;
  399. }
  400. var customToTex;
  401. switch (typeof latexConverter) {
  402. case 'function':
  403. // a callback function
  404. customToTex = latexConverter(this, options);
  405. break;
  406. case 'string':
  407. // a template string
  408. customToTex = expandTemplate(latexConverter, this, options);
  409. break;
  410. case 'object':
  411. // an object with different "converters" for different
  412. // numbers of arguments
  413. switch (typeof latexConverter[args.length]) {
  414. case 'function':
  415. customToTex = latexConverter[args.length](this, options);
  416. break;
  417. case 'string':
  418. customToTex = expandTemplate(latexConverter[args.length], this, options);
  419. break;
  420. }
  421. }
  422. if (typeof customToTex !== 'undefined') {
  423. return customToTex;
  424. }
  425. return expandTemplate(defaultTemplate, this, options);
  426. }
  427. /**
  428. * Get identifier.
  429. * @return {string}
  430. */
  431. getIdentifier() {
  432. return this.type + ':' + this.name;
  433. }
  434. }
  435. _defineProperty(FunctionNode, "name", name);
  436. _defineProperty(FunctionNode, "onUndefinedFunction", function (name) {
  437. throw new Error('Undefined function ' + name);
  438. });
  439. _defineProperty(FunctionNode, "fromJSON", function (json) {
  440. return new FunctionNode(json.fn, json.args);
  441. });
  442. return FunctionNode;
  443. }, {
  444. isClass: true,
  445. isNode: true
  446. });