Compilerobject | dynfunc.t[40] |
Superclass Tree | Property Summary | Method Summary | Property Details | Method Details |
The main advantage of using this object to compile code is that it automatically provides access to the global symbol table that was used to compile the current program, for use in dynamic code. Without the global symbol table, dynamic code won't have access to object names, property names, function names, and so on. That doesn't stop you from compiling code that only depends upon its own function parameters and local variables, but for most purposes the global symbols are useful to have around.
Note that including this object in a project will automatically save the global symbol table in the compiled .t3 file. This increases the size of the .t3 file, as well as memory usage during execution. If you're concerned about minimizing the .t3 file size or the run-time memory footprint, *and* you don't need global symbols for dynamic code (or you don't use the dynamic compiler at all), you can save some space by omitting this whole module from the build.
Compiler : PreinitObject
Compiler
PreinitObject
ModuleExecObject
object
Inherited from PreinitObject
:
execBeforeMe
reverseGlobalSymbols
Inherited from ModuleExecObject
:
execAfterMe
hasInitialized_
isDoingExec_
isExecuted_
compile
defineFunc
eval
execute
Inherited from ModuleExecObject
:
_execute
classExec
macros_ | dynfunc.t[149] |
symtab_ | dynfunc.t[146] |
compile (str, locals?) | dynfunc.t[73] |
'function(x, y, z) { ...body of function... }'
The body of the function can contain any executable code that you could write in a regular function in static code: if, while, switch, return, etc.
The return value is a DynamicFunc containing the compiled expression or function. You call it by using the return value as though it were a function:
local f = Compiler.compile('Me.location');
local loc = f();
If the source string was just an expression, it acts like a function that takes zero arguments, and returns the computed value of the expression. The expression is evaluated anew each time you invoke it, so you'll get the "live" value of an expression that refers to object properties or other external data. In the example above, we'd get the current value of Me.location every time we call f().
The source string is actually compiled immediately when you call this function. This means it's checked for errors, such as syntax errors and unknown symbol names. If the code contains any errors, this method throws a CompilerException describing the problem.
defineFunc (name, str, locals?) | dynfunc.t[85] |
eval (str, locals?) | dynfunc.t[118] |
This method compiles the source string and immediately calls the resulting compiled code. The return value is the value returned from the compiled code itself. This method thus provides a quick way to evaluate an expression.
If the string contains any syntax errors or other compilation errors, the method throws a CompilerException. In addition, it's possible for the compiled code to throw exceptions of its own; this method doesn't catch those, leaving it up to the caller to handle them.
If you expect to evaluate the same expression repeatedly, you're better off using compile() to get the compiled representation of the expression, and then call that compiled code each time the value is needed. That's more efficient than using eval() each time, since eval() to recompile the expression on every call, which is a fairly complex process.
execute ( ) OVERRIDDEN | dynfunc.t[138] |