js.evalNS: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(Created page with "{{Experimental}} {{MacroFunction |name=js.evalNS |version=1.10.0 |description= Evaluates a string as a javascript expression. Supports ECMA 2021 standard. For security, acce...")
 
No edit summary
 
(7 intermediate revisions by 4 users not shown)
Line 9: Line 9:


|usage=
|usage=
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
js.eval(namespace, expression, arg1, arg2...)
js.evalNS(namespace, expression, arg1, arg2...)
</source>
</syntaxhighlight>


'''Parameters'''
'''Parameters'''
Line 24: Line 24:
|example=
|example=
Using {{code|js.eval}} on an {{code|expression}}.
Using {{code|js.eval}} on an {{code|expression}}.
<source lang="mtmacro" line>
<syntaxhighlight lang="mtmacro" line>
[r: js.evalNS("demo", "let a = [1,2,3]; a[1] + args[0]"), 5]
[r: js.evalNS("demo", "var a = [1,2,3]; a[1] + (MTScript.getMTScriptCallingArgs()[0]|0)", 5)]
</source>
</syntaxhighlight>
'''Output:'''
'''Output:'''
<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
7
7
</source>
</syntaxhighlight>




|also=
|also=
[[js:MapTool|JS: MapTool]]<br/>
[[js:MapTool|JS: MapTool]]<br/>
[[js:MTScript|JS: MTScript]]
[[js:MTScript|JS: MTScript]]<br/>
{{func|js.eval}}<br/>
{{func|js.evalURI}}<br/>
{{func|js.createNS}}<br/>
{{func|js.removeNS}}




}}
}}
[[Category:Javascript Function]]
[[Category:Javascript Function]]

Latest revision as of 23:59, 15 March 2023

 This article describes a feature or macro function that is experimental and may be subject to change.

js.evalNS() Function

Introduced in version 1.10.0
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. Unlike js.eval(), js.evalNS() does not wrap the fragment in an anonymous function. The statement in tail position (usually the last statement) is the return value for js.evalNS().

Usage

js.evalNS(namespace, expression, arg1, arg2...)

Parameters

  • namespace - The namespace to run the fragment in. It will be created if it does not yet exist.
  • expression - A valid javascript fragment.
  • arg1, arg2... - Any. Will be converted to the equivalent javascript type as needed. Arguments are accessible via the args variable.
  • return - Any primitive type; Complex types are converted to (JSON) strings.

Example

Using js.eval on an expression.
[r: js.evalNS("demo", "var a = [1,2,3]; a[1] + (MTScript.getMTScriptCallingArgs()[0]|0)", 5)]

Output:

7

See Also