Limitations¶
Compiled code behaves like the interpreter, with the few exceptions on this page. They all come from one fact: to the host, compiled functions are C functions, not Lua functions. Make sure to read this page before your first release, and check your project for the things it lists.
Coroutines¶
Compiled code can't yield. coroutine.yield in compiled code, or in anything a compiled coroutine
calls, raises this error when it runs:
In Lua 5.3 (AutoPlay Media Studio 9, Setup Factory 10), creating and resuming coroutines works, and
so does everything else in the coroutine library, as long as nothing yields. In Lua 5.1 (AutoPlay
Media Studio 8, Setup Factory 9.7), coroutine.create and coroutine.wrap only take Lua functions,
so a compiled function can't be a coroutine at all: they raise Lua function expected.
autoplay2c warns you about every coroutine.yield, coroutine.create and coroutine.wrap it finds
(see Diagnostics). Calls are dynamic, so it can't find every yield,
but the warnings show you where to look. Where you know the code never yields,
--#noyield silences these warnings for one statement or function.
If your project depends on coroutines, keep that code in an interpreted script, which is a plain
.lua file you load with dofile or require and that autoplay2c doesn't compile.
Error Messages¶
Errors that your compiled code raises itself are exactly the interpreter's, position and description included:
error("message") gets the line it was called on, too. However, errors raised inside the functions
you call, such as a wrong argument to a library function, take their position and the function's
name from the caller, which is now a C function. That's why these lose a little:
| Interpreter | Compiled |
|---|---|
core.lua:7: bad argument #1 to 'rep' (string expected, got nil) |
bad argument #1 to '?' (string expected, got nil) |
Error Levels¶
error("message", 2) blames the function that called yours, and higher levels blame functions
further out. Compiled code counts these levels on the host's call stack, where a call from one
compiled function to another often leaves no trace. A level above 1 can then name a function
further out than the interpreter would, and the message gets that function's position, or none.
local function check(value)
if type(value) ~= "number" then error("number expected", 3) end
end
local function set_volume(value)
check(value);
end
Here level 3 should name the caller of set_volume. Compiled, it names the function one further
out. The message itself is always the same; only the position in front of it can differ. Use
level 0 when the position doesn't matter, or level 1 for the line that raises the error, which is
always right.
The Debug Library¶
The debug library sees compiled functions as C functions:
debug.tracebackshows them as[C]entries without a line.debug.getinfogives no source or line for them.debug.getlocalanddebug.setlocaldon't see their locals.debug.getupvalueanddebug.setupvaluesee the compiler's own upvalues, not yours. Don't change them.debug.sethookdoesn't see lines or calls inside compiled code.
getfenv and setfenv¶
In Lua 5.1, getfenv and setfenv on compiled functions, by level or by function, aren't
supported. Code that runs a function in its own environment (a sandbox, for instance) should keep
that function in interpreted code.
Recursion Depth¶
Compiled code runs on the C stack of the host's thread, which is limited (1 MiB in AutoPlay Media Studio and Setup Factory). How deep recursion can go depends on how much each level needs:
| Host | About this many levels |
|---|---|
| 32-bit hosts | 17,000 to 25,000 |
| 64-bit hosts | 9,000 to 14,000 |
The Lua 5.1 interpreter stops at about 16,000 to 20,000 levels, and the Lua 5.3 interpreter
reaches about 500,000. When compiled code runs out of stack, it raises the interpreter's own error,
stack overflow, and it never crashes the host.
Real projects rarely recurse more than a few hundred levels deep. Only code that relies on very deep recursion, or on running out of stack at a particular depth, will see a difference.
Memory¶
collectgarbage("count") gives other numbers for compiled code, because compiled functions and
their data take a different amount of memory than interpreted ones. Everything is collected as
usual.
Source and Bytecode¶
There's no Lua source and no bytecode in the module, by design. This also means that string.dump
can't dump a compiled function, because to the host it's a C function.
Technical details
- Positions in errors come from
luaL_where, which looks at the calling function's line. For a compiled caller there's none, which is why errors raised by called functions have no position. Compiled code checks its own operations before the host does, so it can raise the interpreter's message with the right line and description itself. - Lua 5.1 can't yield from C functions at all. Lua 5.3 can, through continuations, which compiled code doesn't use yet.
- Before every call, compiled code compares the stack pointer with the thread's stack limit,
and raises
stack overflowwhile 128 KiB are still left.