This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The third edition targets Lua 5.2 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.
|Programming in Lua|
|Part IV. The C API Chapter 24. An Overview of the C API|
your application code runs unprotected.
Because its code is not called by Lua,
Lua cannot set an appropriate context to catch errors
(that is, it cannot call
In such environments, when Lua faces an error
like "not enough memory",
there is not much that it can do.
It calls a panic function and, if the function returns,
exits the application.
(You can set your own panic function with the
Not all API functions throw exceptions.
are all safe.
most other functions can only throw an exception in
case of memory-allocation failure:
luaL_loadfile fails if there is not enough
memory for a copy of the file name.
Several programs have nothing to do when they run out of memory,
so they may ignore these exceptions.
For those programs,
if Lua runs out of memory, it is OK to panic.
If you do not want your application to exit,
even in case of a memory-allocation failure,
then you must run your code in protected mode.
Most (or all) of your Lua code typically
runs through a call to
therefore, it runs in protected mode.
Even in case of memory-allocation failure,
lua_pcall returns an error code,
leaving the interpreter in a consistent state.
If you also want to protect all your C code that interacts with Lua,
then you can use
(See the reference manual
for further details of this function;
lua.c in the Lua distribution for an example
of its use.)
|Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.|