Skip to content

Commit cf29567

Browse files
authored
Merge pull request #62 from 6utt3rfly/doc/context
doc: add context note, clarify usage
2 parents 687c2f1 + afaa979 commit cf29567

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

README.md

+26-4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ The result of the parse is an AST (abstract syntax tree), like:
8484
```
8585

8686
### Evaluation
87+
Evaluation executes the AST using the given context (`eval(ast, context)`. By default, the context is empty.
8788

8889
```javascript
8990
import { parse, evaluate } from 'jse-eval';
@@ -94,6 +95,26 @@ const value = eval(ast, {a: 2, b: 2, c: 5}); // 2.4
9495
const value = await evalAsync(ast, {a: 2, b: 2, c: 5}); // 2.4
9596
```
9697

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+
97118
### Compilation
98119

99120
```javascript
@@ -137,7 +158,7 @@ const { jsep } = require('jse-eval');
137158
jsep.plugins.register(
138159
require('@jsep-plugin/arrow'),
139160
require('@jsep-plugin/assignment'),
140-
...
161+
// ...
141162
);
142163
```
143164

@@ -151,6 +172,7 @@ precedence (if provided), and the function to evaluate the operator
151172
for each node type. This evaluator will be called with the ExpressionEval instance bound to it.
152173
The evaluator is responsible for handling both sync and async, as needed, but can use the `this.isAsync`
153174
or `this.evalSyncAsync()` to help.
175+
- *to prevent unsafe code execution, redefine `CallExpression` and `ArrowFunctionExpression` to throw an error*
154176
- If the node type is unknown, jse-eval will check for a `default` node type handler before
155177
throwing an error for an unknown node type. If any other behavior is desired, this can be overridden
156178
by providing a new `default` evaluator.
@@ -194,7 +216,7 @@ console.log(expr.evalExpr('2 ** 3 ** 2')); // 512
194216
This project will try to stay current with all JSEP's node types::
195217
- `ArrayExpression`
196218
- `LogicalExpression`/`BinaryExpression`
197-
- `CallExpression`
219+
- `CallExpression` *potentially unsafe*
198220
- `ConditionalExpression`
199221
- `Compound` *Compound support will evaluate each expression and return the result of the final one*
200222
- `Identifier`
@@ -204,7 +226,7 @@ This project will try to stay current with all JSEP's node types::
204226
- `UnaryExpression`
205227

206228
As well as the optional plugin node types:
207-
- `ArrowFunctionExpression`
229+
- `ArrowFunctionExpression` *potentially unsafe*
208230
- `AssignmentExpression`/`UpdateExpression`
209231
- `AwaitExpression`
210232
- `NewExpression`
@@ -220,7 +242,7 @@ related packages available, including:
220242
- [eval-estree-expression](https://github.com/jonschlinkert/eval-estree-expression)
221243
- [es-tree-walker](https://github.com/Rich-Harris/estree-walker)
222244
- [acorn](https://github.com/acornjs/acorn)
223-
- [astree](https://github.com/davidbonnet/astring)
245+
- [astring](https://github.com/davidbonnet/astring)
224246

225247
## Security
226248

0 commit comments

Comments
 (0)