Skip to content

Commit

Permalink
Change math function lookup in the AST to use Map
Browse files Browse the repository at this point in the history
  • Loading branch information
jhugman committed Jun 26, 2021
1 parent fcaa242 commit a0c6a1b
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/expression/node/ArrayNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export const createArrayNode = /* #__PURE__ */ factory(name, dependencies, ({ No
return item._compile(math, argNames)
})

const asMatrix = (math.config.matrix !== 'Array')
const asMatrix = (math.get('config').matrix !== 'Array')
if (asMatrix) {
const matrix = math.matrix
const matrix = math.get('matrix')
return function evalArrayNode (scope, args, context) {
return matrix(map(evalItems, function (evalItem) {
return evalItem(scope, args, context)
Expand Down
8 changes: 4 additions & 4 deletions src/expression/node/FunctionNode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isAccessorNode, isFunctionAssignmentNode, isIndexNode, isNode, isSymbolNode } from '../../utils/is.js'
import { escape } from '../../utils/string.js'
import { hasOwnProperty } from '../../utils/object.js'
import { getSafeProperty, validateSafeMethod } from '../../utils/customs.js'
import { validateSafeMethod } from '../../utils/customs.js'
import { createSubScope } from '../../utils/scope.js'
import { factory } from '../../utils/factory.js'
import { defaultTemplate, latexFunctions } from '../../utils/latex.js'
Expand Down Expand Up @@ -81,15 +81,15 @@ export const createFunctionNode = /* #__PURE__ */ factory(name, dependencies, ({
if (isSymbolNode(this.fn)) {
// we can statically determine whether the function has an rawArgs property
const name = this.fn.name
const fn = name in math ? getSafeProperty(math, name) : undefined
const fn = math.has(name) ? math.get(name) : undefined
const isRaw = typeof fn === 'function' && fn.rawArgs === true

const resolveFn = (scope) => {
if (scope.has(name)) {
return scope.get(name)
}
if (name in math) {
return getSafeProperty(math, name)
if (math.has(name)) {
return math.get(name)
}
return FunctionNode.onUndefinedFunction(name)
}
Expand Down
3 changes: 1 addition & 2 deletions src/expression/node/IndexNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { isBigNumber, isConstantNode, isNode, isRangeNode, isSymbolNode } from '
import { map } from '../../utils/array.js'
import { escape } from '../../utils/string.js'
import { factory } from '../../utils/factory.js'
import { getSafeProperty } from '../../utils/customs.js'

const name = 'IndexNode'
const dependencies = [
Expand Down Expand Up @@ -134,7 +133,7 @@ export const createIndexNode = /* #__PURE__ */ factory(name, dependencies, ({ Ra
}
})

const index = getSafeProperty(math, 'index')
const index = math.get('index')

return function evalIndexNode (scope, args, context) {
const dimensions = map(evalDimensions, function (evalDimension) {
Expand Down
5 changes: 4 additions & 1 deletion src/expression/node/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const createNode = /* #__PURE__ */ factory(name, dependencies, ({ mathWit

Node.prototype.comment = ''

// Wrap the mathWithTransform object in a map
const math = createMap(mathWithTransform)

/**
* Compile the node into an optimized, evauatable JavaScript function
* @return {{evaluate: function([Object])}} object
Expand All @@ -42,7 +45,7 @@ export const createNode = /* #__PURE__ */ factory(name, dependencies, ({ mathWit
* variables.
*/
Node.prototype.compile = function () {
const expr = this._compile(mathWithTransform, {})
const expr = this._compile(math, {})
const args = {}
const context = null

Expand Down
15 changes: 7 additions & 8 deletions src/expression/node/OperatorNode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isNode } from '../../utils/is.js'
import { map } from '../../utils/array.js'
import { escape } from '../../utils/string.js'
import { getSafeProperty, isSafeMethod } from '../../utils/customs.js'
import { isSafeMethod } from '../../utils/customs.js'
import { getAssociativity, getPrecedence, isAssociativeWith, properties } from '../operators.js'
import { latexOperators } from '../../utils/latex.js'
import { factory } from '../../utils/factory.js'
Expand Down Expand Up @@ -65,15 +65,14 @@ export const createOperatorNode = /* #__PURE__ */ factory(name, dependencies, ({
*/
OperatorNode.prototype._compile = function (math, argNames) {
// validate fn
if (typeof this.fn !== 'string' || !isSafeMethod(math, this.fn)) {
if (!math[this.fn]) {
throw new Error('Function ' + this.fn + ' missing in provided namespace "math"')
} else {
throw new Error('No access to function "' + this.fn + '"')
}
if (typeof this.fn !== 'string' || isSafeMethod(this.fn)) {
throw new Error('No access to function "' + this.fn + '"')
}
if (!math.has(this.fn)) {
throw new Error('Function ' + this.fn + ' missing in provided namespace "math"')
}

const fn = getSafeProperty(math, this.fn)
const fn = math.get(this.fn)
const evalArgs = map(this.args, function (arg) {
return arg._compile(math, argNames)
})
Expand Down
2 changes: 1 addition & 1 deletion src/expression/node/RangeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const createRangeNode = /* #__PURE__ */ factory(name, dependencies, ({ No
* evalNode(scope: Object, args: Object, context: *)
*/
RangeNode.prototype._compile = function (math, argNames) {
const range = math.range
const range = math.get('range')
const evalStart = this.start._compile(math, argNames)
const evalEnd = this.end._compile(math, argNames)

Expand Down
3 changes: 1 addition & 2 deletions src/expression/node/RelationalNode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { getPrecedence } from '../operators.js'
import { escape } from '../../utils/string.js'
import { getSafeProperty } from '../../utils/customs.js'
import { latexOperators } from '../../utils/latex.js'
import { factory } from '../../utils/factory.js'

Expand Down Expand Up @@ -62,7 +61,7 @@ export const createRelationalNode = /* #__PURE__ */ factory(name, dependencies,
for (let i = 0; i < self.conditionals.length; i++) {
evalLhs = evalRhs
evalRhs = compiled[i + 1](scope, args, context)
const condFn = getSafeProperty(math, self.conditionals[i])
const condFn = math.get(self.conditionals[i])
if (!condFn(evalLhs, evalRhs)) {
return false
}
Expand Down
5 changes: 2 additions & 3 deletions src/expression/node/SymbolNode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { escape } from '../../utils/string.js'
import { getSafeProperty } from '../../utils/customs.js'
import { factory } from '../../utils/factory.js'
import { toSymbol } from '../../utils/latex.js'

Expand Down Expand Up @@ -66,11 +65,11 @@ export const createSymbolNode = /* #__PURE__ */ factory(name, dependencies, ({ m
return function (scope, args, context) {
return args[name]
}
} else if (name in math) {
} else if (math.has(name)) {
return function (scope, args, context) {
return scope.has(name)
? scope.get(name)
: getSafeProperty(math, name)
: math.get(name)
}
} else {
const isUnit = isValuelessUnit(name)
Expand Down
4 changes: 2 additions & 2 deletions src/expression/transform/utils/compileInlineExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function compileInlineExpression (expression, math, scope) {
// find an undefined symbol
const symbol = expression.filter(function (node) {
return isSymbolNode(node) &&
!(node.name in math) &&
!(scope.has(node.name))
!math.has(node.name) &&
!scope.has(node.name)
})[0]

if (!symbol) {
Expand Down

0 comments on commit a0c6a1b

Please sign in to comment.