@@ -84,6 +84,7 @@ The result of the parse is an AST (abstract syntax tree), like:
84
84
```
85
85
86
86
### Evaluation
87
+ Evaluation executes the AST using the given context (` eval(ast, context) ` . By default, the context is empty.
87
88
88
89
``` javascript
89
90
import { parse , evaluate } from ' jse-eval' ;
@@ -94,6 +95,26 @@ const value = eval(ast, {a: 2, b: 2, c: 5}); // 2.4
94
95
const value = await evalAsync (ast, {a: 2 , b: 2 , c: 5 }); // 2.4
95
96
```
96
97
98
+ Since the default context is empty, it prevents using built-in JS functions.
99
+ To allow those functions, they can be added to the ` context ` argument passed into the ` eval ` method:
100
+ ``` javascript
101
+ const context = {
102
+ Date ,
103
+ Array ,
104
+ Object ,
105
+ encodeURI,
106
+ decodeURI,
107
+ isFinite,
108
+ isNaN,
109
+ JSON ,
110
+ Math ,
111
+ parseFloat,
112
+ parseInt,
113
+ RegExp ,
114
+ // ...myCustomPropertiesAndFunctions,
115
+ };
116
+ ```
117
+
97
118
### Compilation
98
119
99
120
``` javascript
@@ -137,7 +158,7 @@ const { jsep } = require('jse-eval');
137
158
jsep .plugins .register (
138
159
require (' @jsep-plugin/arrow' ),
139
160
require (' @jsep-plugin/assignment' ),
140
- ...
161
+ // ...
141
162
);
142
163
```
143
164
@@ -151,6 +172,7 @@ precedence (if provided), and the function to evaluate the operator
151
172
for each node type. This evaluator will be called with the ExpressionEval instance bound to it.
152
173
The evaluator is responsible for handling both sync and async, as needed, but can use the ` this.isAsync `
153
174
or ` this.evalSyncAsync() ` to help.
175
+ - * to prevent unsafe code execution, redefine ` CallExpression ` and ` ArrowFunctionExpression ` to throw an error*
154
176
- If the node type is unknown, jse-eval will check for a ` default ` node type handler before
155
177
throwing an error for an unknown node type. If any other behavior is desired, this can be overridden
156
178
by providing a new ` default ` evaluator.
@@ -194,7 +216,7 @@ console.log(expr.evalExpr('2 ** 3 ** 2')); // 512
194
216
This project will try to stay current with all JSEP's node types::
195
217
- ` ArrayExpression `
196
218
- ` LogicalExpression ` /` BinaryExpression `
197
- - ` CallExpression `
219
+ - ` CallExpression ` * potentially unsafe *
198
220
- ` ConditionalExpression `
199
221
- ` Compound ` * Compound support will evaluate each expression and return the result of the final one*
200
222
- ` Identifier `
@@ -204,7 +226,7 @@ This project will try to stay current with all JSEP's node types::
204
226
- ` UnaryExpression `
205
227
206
228
As well as the optional plugin node types:
207
- - ` ArrowFunctionExpression `
229
+ - ` ArrowFunctionExpression ` * potentially unsafe *
208
230
- ` AssignmentExpression ` /` UpdateExpression `
209
231
- ` AwaitExpression `
210
232
- ` NewExpression `
@@ -220,7 +242,7 @@ related packages available, including:
220
242
- [ eval-estree-expression] ( https://github.com/jonschlinkert/eval-estree-expression )
221
243
- [ es-tree-walker] ( https://github.com/Rich-Harris/estree-walker )
222
244
- [ acorn] ( https://github.com/acornjs/acorn )
223
- - [ astree ] ( https://github.com/davidbonnet/astring )
245
+ - [ astring ] ( https://github.com/davidbonnet/astring )
224
246
225
247
## Security
226
248
0 commit comments