js.eval: Difference between revisions
Jump to navigation
Jump to search
(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...") |
m (Add explicit mention of GraalVM) |
||
(9 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
{{Experimental}} | |||
{{MacroFunction | {{MacroFunction | ||
|name=js.eval | |name=js.eval | ||
|trusted=true | |trusted=true | ||
|version=1. | |version=1.10.0 | ||
|description= | |description= | ||
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. | Evaluates a string as a javascript expression within the embedded GraalVM interpreter (this is NOT the same JavaScript environment as that provided by the HTML5 functions). 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. | ||
Starting in 1.10.0, the context in which the javascript fragment executes is discarded when <syntaxhighlight>js.eval</syntaxhighlight> returns. For setting up persistent structures, see {{func|js.evalNS}} or {{func|js.evalURI}} | |||
|usage= | |usage= | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
js.eval(expression, arg1, arg2...) | js.eval(expression, arg1, arg2...) | ||
</ | </syntaxhighlight> | ||
'''Parameters''' | '''Parameters''' | ||
* {{code|expression}} - A valid javascript fragment. It is wrapped inside an anonymous function. | * {{code|expression}} - A valid javascript fragment. It is wrapped inside an anonymous function. | ||
* {{code|arg1, arg2...}} - Any. Will be converted to the equivalent javascript type as needed. | * {{code|arg1, arg2...}} - Any. Will be converted to the equivalent javascript type as needed. Arguments are accessible via the {{code|args}} variable. | ||
* {{code|return}} - Any primitive type; Complex types are converted to (JSON) strings. | * {{code|return}} - Any primitive type; Complex types are converted to (JSON) strings. | ||
|example= | |example= | ||
Using {{code|js.eval}} on an {{code|expression}}. | Using {{code|js.eval}} on an {{code|expression}}. | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[r: js.eval("let a = [1,2,3]; return a[1] + args[0]" | [r: js.eval("let a = [1,2,3]; return a[1] + args[0]", 5)] | ||
</ | </syntaxhighlight> | ||
'''Output:''' | '''Output:''' | ||
< | <syntaxhighlight lang="mtmacro"> | ||
7 | 7 | ||
</ | </syntaxhighlight> | ||
|also= | |also= | ||
[[js:MapTool|JS: MapTool]]<br/> | [[js:MapTool|JS: MapTool]]<br/> | ||
[[js:MTScript|JS: MTScript]] | [[js:MTScript|JS: MTScript]]<br/> | ||
{{func|js.evalNS}}<br/> | |||
{{func|js.evalURI}} | |||
}} | }} | ||
[[Category:Javascript Function]] | [[Category:Javascript Function]] |
Latest revision as of 23:59, 19 September 2024
This article describes a feature or macro function that is experimental and may be subject to change.
js.eval() Function
Note: This function can only be used in a Trusted Macro
• Introduced in version 1.10.0
Evaluates a string as a javascript expression within the embedded GraalVM interpreter (this is NOT the same JavaScript environment as that provided by the HTML5 functions). 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.
Starting in 1.10.0, the context in which the javascript fragment executes is discarded when returns. For setting up persistent structures, see js.evalNS() or js.evalURI()
js.eval
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. Arguments are accessible via theargs
variable.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