Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

12 Commits

Repository files navigation

Script.apex

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.

Features

  • Parse JavaScript code into semantic nodes
  • Evaluate nodes in Apex
  • Parsed nodes are cached to improve performance

Usage

Here is how we evaluate arithmatic expressions.

Objectresult = ScriptEngine.getInstance().eval('1 + 2 * (3 - 1)');

Or we can evaluate expressions consuming variables like this:

Map<String, Object> context = newMap<String, Object>{
'a' => 1,
'b' => 2
};
Objectresult = ScriptEngine.getInstance().eval('a + b', context);

Or we can parse the nodes first, and the evaluate them:

Jsep.Nodenode = newJsep('1 + 2').parse();
Objectresult = ScriptEngine.getInstance().eval(node);

Supported Operations

Supported Unary Operator

NameDescription
-Negate number
!Negate boolean
++Only prefix supported
--Only prefix supported

Supported Logical Operator

NameDescription
&&Logical and
||Logical or

Supported Binary Operator

NameDescription
==Apex ==
!=Apex !=
===Apex ==
!==Apex !=
<Apex <
>Apex >
<=Apex <=
>=Apex >=
+Apex +
-Apex -
*Apex *
/Apex /
%Apex Math.mod

Supported Structure

NameDescription
Conditional Expression(? :)Yes
Array LiteralYes
Object LiteralYes

For example,

ScriptEngine.getInstance.eval('["a", "b"]'); // Array literalScriptEngine.getInstance.eval('{ "name": "test", age: 18 }'); // Object literal

Todos

Assignment Expression Evaluation

Assignment expressions can be parsed, but cannot be evaluated yet.

Function Invocation

Functions(methods) can be parsed now, but invocation is not implemented yet.

Releases

Packages

Used by

Contributors

Languages