Skip to content

Commit 20df2fc

Browse files
author
Miao (Wilson) Liu
committed
add first version
1 parent 583e920 commit 20df2fc

9 files changed

Lines changed: 1808 additions & 1 deletion

README.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,88 @@
11
# Script.apex
2-
Evaluate Javascript expressions in Apex
2+
Script.apex help you run JavaScript code in Apex. Basically, it compiles JavaScript code into semantic nodes, which are then evaluated in native Apex. The JavaScript expression parser in Script.apex is porting from [jsep](http://jsep.from.so/).
3+
4+
## Features
5+
- Parse JavaScript code into semantic nodes
6+
- Evaluate nodes in Apex
7+
- Parsed nodes are cached to improve performance
8+
9+
## Usage
10+
11+
Here is how we evaluate arithmatic expressions.
12+
```java
13+
Object result = ScriptEngine.getInstance().eval('1 + 2 * (3 - 1)');
14+
```
15+
16+
Or we can evaluate expressions consuming variables like this:
17+
```java
18+
Map<String, Object> context = new Map<String, Object>{
19+
'a' => 1,
20+
'b' => 2
21+
};
22+
Object result = ScriptEngine.getInstance().eval('a + b', context);
23+
```
24+
25+
Or we can parse the nodes first, and the evaluate them:
26+
```java
27+
Jsep.Node node = new Jsep('1 + 2').parse();
28+
Object result = ScriptEngine.getInstance().eval(node);
29+
```
30+
31+
## Supported Operations
32+
33+
Supported Unary Operator
34+
35+
| Name | Description |
36+
| ---- | ----------- |
37+
| - | Negate number |
38+
| ! | Negate boolean |
39+
| ++ | Only prefix supported |
40+
| -- | Only prefix supported |
41+
42+
Supported Logical Operator
43+
44+
| Name | Description |
45+
| ---- | ----------- |
46+
| && | Logical and |
47+
| \|\| | Logical or |
48+
49+
Supported Binary Operator
50+
51+
| Name | Description |
52+
| ---- | ----------- |
53+
| == | Apex == |
54+
| != | Apex != |
55+
| === | Apex == |
56+
| !== | Apex != |
57+
| < | Apex < |
58+
| > | Apex > |
59+
| <= | Apex <= |
60+
| >= | Apex >= |
61+
| + | Apex + |
62+
| - | Apex - |
63+
| * | Apex * |
64+
| / | Apex / |
65+
| % | Apex Math.mod |
66+
67+
Supported Structure
68+
69+
| Name | Description |
70+
| ---- | ----------- |
71+
| Conditional Expression(? :) | Yes |
72+
| Array Literal | Yes |
73+
| Object Literal | Yes |
74+
75+
For example,
76+
```java
77+
ScriptEngine.getInstance.eval('["a", "b"]'); // Array literal
78+
79+
ScriptEngine.getInstance.eval('{ "name": "test", age: 18 }'); // Object literal
80+
```
81+
82+
## Todos
83+
84+
### Assignment Expression Evaluation
85+
Assignment expressions can be parsed, but cannot be evaluated yet.
86+
87+
### Function Invocation
88+
Functions(methods) can be parsed now, but invocation is not implemented yet.

0 commit comments

Comments
 (0)