=proto void duk_compile(duk_context *ctx, int flags); =stack [ ... source! filename! ] -> [ ... function! ] =summary
Compile Ecmascript source code and replace it with a compiled function
object (the code is not executed). The filename
argument is stored
as the fileName
property of the resulting function, and is the name
used in e.g. tracebacks to identify the function. May throw a SyntaxError
for any compile-time errors (in addition to the usual internal errors like out-of-memory,
internal limit errors, etc).
The following flags may be given:
DUK_COMPILE_EVAL
: compile the input as eval code instead of
as an Ecmascript programDUK_COMPILE_FUNCTION
: compile the input as a function instead
of as an Ecmascript programDUK_COMPILE_STRICT
: force the input to be compiled in strict
modeThe source code being compiled may be:
eval
call
(flag DUK_COMPILE_EVAL
)DUK_COMPILE_FUNCTION
)All of these have slightly different semantics in Ecmascript. See Establishing an Execution Context for a detailed discussion. One major difference is that program and eval contexts have an implicit return value: the last non-empty statement value is an automatic return value for the program or eval code, whereas functions don't have an automatic return value.
Program and eval code don't have an explicit function
syntax.
For instance, the following can be compiled both as a program and as an
eval expression:
print("Hello world!"); 123; // implicit return value
Function code follows the Ecmascript function
syntax
(the function name is optional):
function adder(x,y) { return x+y; }
Compiling a function is equivalent to compiling eval code which contains a function expression. Note that the outermost parentheses are required, otherwise the eval code will register a global function named "adder" instead of returning a plain function value:
(function adder(x,y) { return x+y; })
The bytecode generated for program and eval code is currently slower than that generated for functions: a "slow path" is used for all variable accesses in program and eval code, and the implicit return value handling of program and eval code generates much unnecessary bytecode. From a performance point of view (both memory and execution performance) it is thus preferable to have as much code inside functions as possible.
When compiling eval and program expressions, be careful to avoid the usual Ecmascript gotchas, such as:
/* Function at top level is a function declaration which registers a global * function, and is different from a function expression. Use parentheses * around the top level expression. */ eval("function adder(x,y) { return x+y; }"); /* registers 'adder' to global */ eval("function (x,y) { return x+y; }"); /* not allowed */ eval("(function (x,y) { return x+y; })"); /* function expression (anonymous) */ eval("(function adder(x,y) { return x+y; })"); /* function expression (named) */ /* Opening curly brace at top level is interpreted as start of a block * expression, not an object literal. Use parentheses around the top * level expression. */ eval("{ myfunc: 1 }"); /* block with -label- "myfunc" and statement "1" (!) */ eval("({ myfunc: 1 })"; /* object literal { myfunc: 1 } */