* 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)
| 118 | * evalNode(scope: Object, args: Object, context: *) |
| 119 | */ |
| 120 | _compile (math, argNames) { |
| 121 | const evalObject = this.object._compile(math, argNames) |
| 122 | const evalIndex = this.index ? this.index._compile(math, argNames) : null |
| 123 | const evalValue = this.value._compile(math, argNames) |
| 124 | const name = this.object.name |
| 125 | |
| 126 | if (!this.index) { |
| 127 | // apply a variable to the scope, for example `a=2` |
| 128 | if (!isSymbolNode(this.object)) { |
| 129 | throw new TypeError('SymbolNode expected as object') |
| 130 | } |
| 131 | |
| 132 | return function evalAssignmentNode (scope, args, context) { |
| 133 | const value = evalValue(scope, args, context) |
| 134 | scope.set(name, value) |
| 135 | return value |
| 136 | } |
| 137 | } else if (this.index.isObjectProperty()) { |
| 138 | // apply an object property for example `a.b=2` |
| 139 | const prop = this.index.getObjectProperty() |
| 140 | |
| 141 | return function evalAssignmentNode (scope, args, context) { |
| 142 | const object = evalObject(scope, args, context) |
| 143 | const value = evalValue(scope, args, context) |
| 144 | setSafeProperty(object, prop, value) |
| 145 | return value |
| 146 | } |
| 147 | } else if (isSymbolNode(this.object)) { |
| 148 | // update a matrix subset, for example `a[2]=3` |
| 149 | return function evalAssignmentNode (scope, args, context) { |
| 150 | const childObject = evalObject(scope, args, context) |
| 151 | const value = evalValue(scope, args, context) |
| 152 | // Important: we pass childObject instead of context: |
| 153 | const index = evalIndex(scope, args, childObject) |
| 154 | scope.set(name, assign(childObject, index, value)) |
| 155 | return value |
| 156 | } |
| 157 | } else { // isAccessorNode(node.object) === true |
| 158 | // update a matrix subset, for example `a.b[2]=3` |
| 159 | |
| 160 | // we will not use the compile function of the AccessorNode, but |
| 161 | // compile it ourselves here as we need the parent object of the |
| 162 | // AccessorNode: |
| 163 | // wee need to apply the updated object to parent object |
| 164 | const evalParentObject = this.object.object._compile(math, argNames) |
| 165 | |
| 166 | if (this.object.index.isObjectProperty()) { |
| 167 | const parentProp = this.object.index.getObjectProperty() |
| 168 | |
| 169 | return function evalAssignmentNode (scope, args, context) { |
| 170 | const parent = evalParentObject(scope, args, context) |
| 171 | const childObject = getSafeProperty(parent, parentProp) |
| 172 | // Important: we pass childObject instead of context: |
| 173 | const index = evalIndex(scope, args, childObject) |
| 174 | const value = evalValue(scope, args, context) |
| 175 | setSafeProperty( |
| 176 | parent, parentProp, assign(childObject, index, value)) |
| 177 | return value |
nothing calls this directly
no test coverage detected