js.eval

From RPTools Wiki
Revision as of 04:59, 4 August 2021 by LPerkins (talk | contribs) (Created page with "{{MacroFunction |name=js.eval |trusted=true |version=1.9.4 |description= Evaluates a string as a javascript expression. Supports ECMA 2021 standard. For security, access to...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

js.eval() Function

 Note: This function can only be used in a Trusted Macro

Introduced in version 1.9.4
Evaluates a string as a javascript expression. Supports ECMA 2021 standard. For security, access to native types and external resources are limited. Similarly, all arrays passed in to the javascript environment are shallow copied, so changes to the array do not directly change MapTool's state.

Usage

js.eval(expression, arg1, arg2...)

Parameters

  • expression - A valid javascript fragment. It is wrapped inside an anonymous function.
  • arg1, arg2... - Any. Will be converted to the equivalent javascript type as needed.
  • return - Any primitive type; Complex types are converted to (JSON) strings.

Example

Using js.eval on an expression.
[r: js.eval("let a = [1,2,3]; return a[1] + args[0]"), 5]

Output:

7

See Also