Giter VIP home page Giter VIP logo

script.apex's Introduction

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.

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

Or we can evaluate expressions consuming variables like this:

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

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

Jsep.Node node = new Jsep('1 + 2').parse();
Object result = ScriptEngine.getInstance().eval(node);

Supported Operations

Supported Unary Operator

Name Description
- Negate number
! Negate boolean
++ Only prefix supported
-- Only prefix supported

Supported Logical Operator

Name Description
&& Logical and
|| Logical or

Supported Binary Operator

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

Supported Structure

Name Description
Conditional Expression(? :) Yes
Array Literal Yes
Object Literal Yes

For example,

ScriptEngine.getInstance.eval('["a", "b"]'); // Array literal

ScriptEngine.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.

script.apex's People

Contributors

liumiaowilson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.