"
Lua is an extension programming
language designed to support general procedural programming with data
description facilities. It also offers good support for object-oriented
programming, functional programming, and data-driven programming. Lua
is intended to be used as a powerful, light-weight scripting language
for any program that needs one. " (from "
Lua 5.1 Reference
Manual" by Roberto Ierusalimschy, Luiz Henrique de
Figueiredo, Waldemar Celes)
This module provides a
BlitzMax interface to the Lua main and auxiliary APIs. It is almost
complete, the only functions missing are those with variable argument
lists (which are not yet supported by BlitzMax).
The
axe.lua package also contains the full Lua 5.1.4 distribution. The Lua
source code is completely integrated into the module, additional DLLs
(or shared libraries, resp.) are no longer required.
Lua
Documentation
The Lua Reference Manual is part of this
distribution and may be directly visited from here:
Additional
information can be found on the
Lua
web site; the
Lua
wiki contains further material
about this module
and related packages.
Introduction
The
following description is not meant as a tutorial, but may still help
you to start Lua programming within BlitzMax. More API-related
information can be found in the
Lua Reference manualSetting
up a Lua VM
Running Lua scripts always require the
instantiation of at least one Lua VM:
local LuaState:byte ptr = luaL_newstate()
The
result of this instantiation is a pointer to a structure
which contains the full state of the new VM and has to be
passed as the first argument to almost any other Lua API function.
It
is now time to "open" the intrinsic Lua libraries (note: these
libraries are part of axe.lua now, external DLLs or shared libraries
are not necessary):
luaL_openlibs(LuaState)
Always
initialize the Lua VM by opening its libraries unless you really know
what you are doing!
Usually, creating a single Lua
state is sufficient, even further (Lua) threads share the basic Lua
state (and its associated environment).
Shutting
down a Lua VM
At the end, it's always a good idea to shut
down the Lua VM
lua_close(LuaState)
The
Lua interpreter has now been terminated and its state variable is no
longer valid.
Accessing Lua Globals
The
code
lua_pushstring(LuaState, "BMXString")
lua_setglobal (LuaState, "luaglobal")
defines
a global Lua variable (called
luaglobal)
which contains a string (namely "
BMXString").
Registering
BlitzMax Functions
In order to access BlitzMax features from
within a Lua script, it is necessary to implement suitable BlitzMax
functions and register them in the Lua VM.
Such
BlitzMax functions typically look as follows:
function BMXName:int (LuaState:Byte Ptr)
... ' handling of parameters passed from Lua (if required)
... ' actual function body
... ' passing results back to Lua (if required)
return 0 ' number of values returned to Lua
end function
Such
a function is then registered using
lua_register(LuaState, "luaname", BMXName)
This
command registers a BlitzMax function (called
BMXName)
with the (global) name
luaname
under Lua. Both names may be equal, but they do not have to.
Running
Lua Scripts From Within BlitzMax
The code
local Result:int = luaL_loadString(LuaState,LuaScript)
if (Result <> 0) then
' ERROR!!!
lua_close(LuaState) ' just to be complete
end
end if
loads
and compiles a (BlitzMax) string containing a LuaScript. The result (of
compilation) is left on the (Lua) stack.
lua_getfield(LuaState, LUA_GLOBALSINDEX, "debug")' get global "debug"
lua_getfield(LuaState, -1, "traceback") ' get "debug.traceback"
lua_remove (LuaState, -2) ' remove "debug" table from stack
Result = lua_pcall(LuaState,1,-1,-1)' use "debug.traceback" as err.hdlr
if (Result <> 0) then
' ERROR
lua_close(LuaState) ' just to be complete
end
end if
actually
evaluates the previously loaded script. The initially mentioned Lua
commands just prepare for proper error messages should the Lua script
fail.
## Functions
### `Function lua_atpanic:Byte Ptr (lua_state:Byte Ptr, panicf:Int(ls:Byte Ptr))`
see