MCPcopy
hub / github.com/josdejong/mathjs / _compile

Method _compile

src/expression/node/OperatorNode.js:292–335  ·  view source on GitHub ↗

* Compile a node into a JavaScript function. * This basically pre-calculates as much as possible and only leaves open * calculations which depend on a dynamic scope with variables. * @param {Object} math Math.js namespace with functions and constants. * @param {Object} argNam

(math, argNames)

Source from the content-addressed store, hash-verified

290 * evalNode(scope: Object, args: Object, context: *)
291 */
292 _compile (math, argNames) {
293 // validate fn
294 if (typeof this.fn !== 'string' || !isSafeMethod(math, this.fn)) {
295 if (!math[this.fn]) {
296 throw new Error(
297 'Function ' + this.fn + ' missing in provided namespace "math"')
298 } else {
299 throw new Error('No access to function "' + this.fn + '"')
300 }
301 }
302
303 const fn = getSafeProperty(math, this.fn)
304 const evalArgs = map(this.args, function (arg) {
305 return arg._compile(math, argNames)
306 })
307
308 if (typeof fn === 'function' && fn.rawArgs === true) {
309 // pass unevaluated parameters (nodes) to the function
310 // "raw" evaluation
311 const rawArgs = this.args
312 return function evalOperatorNode (scope, args, context) {
313 return fn(rawArgs, math, createSubScope(scope, args))
314 }
315 } else if (evalArgs.length === 1) {
316 const evalArg0 = evalArgs[0]
317 return function evalOperatorNode (scope, args, context) {
318 return fn(evalArg0(scope, args, context))
319 }
320 } else if (evalArgs.length === 2) {
321 const evalArg0 = evalArgs[0]
322 const evalArg1 = evalArgs[1]
323 return function evalOperatorNode (scope, args, context) {
324 return fn(
325 evalArg0(scope, args, context),
326 evalArg1(scope, args, context))
327 }
328 } else {
329 return function evalOperatorNode (scope, args, context) {
330 return fn.apply(null, map(evalArgs, function (evalArg) {
331 return evalArg(scope, args, context)
332 }))
333 }
334 }
335 }
336
337 /**
338 * Execute a callback for each of the child nodes of this node

Callers

nothing calls this directly

Calls 4

isSafeMethodFunction · 0.90
getSafePropertyFunction · 0.90
mapFunction · 0.90
createSubScopeFunction · 0.90

Tested by

no test coverage detected