Pub.Lua
"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 supported by BlitzMax).
The Pub.Lua package 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 not required.
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 manual
Setting 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 Pub.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<br/> ' 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))
Sets a new panic function and returns the old one.
Function lua_call (lua_state:Byte Ptr, nargs:Int, nresults:Int)
Calls a function.
Function lua_checkstack:Int (lua_state:Byte Ptr, extra:Int)
Ensures that there are at least extra free stack slots in the stack.
Function lua_close (lua_state:Byte Ptr)
Destroys all objects in the given Lua state and frees all dynamic memory used by this state.
Function lua_concat (lua_state:Byte Ptr, n:Int)
Concatenates the n values at the top of the stack, pops them, and leaves the result at the top.
Function lua_cpcall:Int (lua_state:Byte Ptr, func:Int(ls:Byte Ptr), ud:Byte Ptr)
Calls the function func in protected mode.
Function lua_createtable (lua_state:Byte Ptr, narr:Int, nrec:Int)
Creates a new empty table and pushes it onto the stack.
Function lua_equal:Int (lua_state:Byte Ptr, index1:Int, index2:Int)
Returns 1 if the two values in acceptable indices index1 and index2 are equal.
Function lua_error:Int (lua_state:Byte Ptr)
Generates a Lua error.
Function lua_gc:Int (lua_state:Byte Ptr, what:Int, data:Int)
Controls the garbage collector.
Function lua_getallocf:Byte Ptr (lua_state:Byte Ptr, ud:Byte Ptr Ptr)
Returns the memory-allocation function of a given state.
Function lua_getfenv (lua_state:Byte Ptr, index:Int)
Pushes onto the stack the environment table of the value at the given index.
Function lua_getfield (lua_state:Byte Ptr, index:Int, k$z)
Pushes onto the stack the value t[k]
, where t is the value at the given valid index.
Function lua_gethook:Byte Ptr (lua_state:Byte Ptr)
Returns the current hook function.
Function lua_gethookcount:Int (lua_state:Byte Ptr)
Returns the current hook count.
Function lua_gethookmask:Int (lua_state:Byte Ptr)
Returns the current hook mask.
Function lua_getinfo:Int (lua_state:Byte Ptr, what$z, ar:lua_Debug Ptr)
Returns information about a specific function or function invocation.
Function lua_getlocal$z (lua_state:Byte Ptr, ar:lua_Debug Ptr, n:Int)
Gets information about a local variable of a given activation record.
Function lua_getmetatable:Int (lua_state:Byte Ptr, index:Int)
Pushes onto the stack the metatable of the value at the given acceptable index.
Function lua_getstack:Int (lua_state:Byte Ptr, level:Int, ar:lua_Debug Ptr)
Get information about the interpreter runtime stack.
Function lua_gettable (lua_state:Byte Ptr, index:Int)
Pushes onto the stack the value t[k]
, where t is the value at the given valid index and k is the value at the top of the stack.
Function lua_gettop:Int (lua_state:Byte Ptr)
Returns the index of the top element in the stack.
Function lua_getupvalue$z (lua_state:Byte Ptr, funcindex:Int, n:Int)
Gets information about a closure's upvalue.
Function lua_insert (lua_state:Byte Ptr, index:Int)
Moves the top element into the given valid index, shifting up the elements above this index to open space.
Function lua_iscfunction:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the value at the given acceptable index is a C function, and 0 otherwise.
Function lua_isnumber:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the value at the given acceptable index is a number or a string convertible to a number, and 0 otherwise.
Function lua_isstring:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the value at the given acceptable index is a string or a number (which is always convertible to a string), and 0 otherwise.
Function lua_isuserdata:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the value at the given acceptable index is a userdata (either full or light), and 0 otherwise.
Function lua_lessthan:Int (lua_state:Byte Ptr, index1:Int, index2:Int)
Returns 1 if the value at acceptable index index1 is smaller than the value at acceptable index index2.
Function lua_newthread:Byte Ptr (lua_state:Byte Ptr)
Creates a new thread, pushes it on the stack, and returns a pointer to a lua_State that represents this new thread.
Function lua_next:Int (lua_state:Byte Ptr, index:Int)
Pops a key from the stack, and pushes a key-value pair from the table at the given index.
Function lua_pcall:Int (lua_state:Byte Ptr, nargs:Int, nresults:Int, errfunc:Int)
Calls a function in protected mode.
Function lua_pushboolean (lua_state:Byte Ptr, b:Int)
Pushes a boolean value with value b onto the stack.
Function lua_pushcclosure (lua_state:Byte Ptr, fn:Int(ls:Byte Ptr), n:Int)
Pushes a new C closure onto the stack.
Function lua_pushlightuserdata (lua_state:Byte Ptr, p:Byte Ptr)
Pushes a light userdata onto the stack.
Function lua_pushnil (lua_state:Byte Ptr)
Pushes a nil value onto the stack.
Function lua_pushnumber (lua_state:Byte Ptr, n:Double)
Pushes a number with value n onto the stack.
Function lua_pushstring (lua_state:Byte Ptr, s$z)
Pushes the String pointed to by s onto the stack.
Function lua_pushthread:Int (lua_state:Byte Ptr)
Pushes the thread onto the stack.
Function lua_pushvalue (lua_state:Byte Ptr, index:Int)
Pushes a copy of the element at the given valid index onto the stack.
Function lua_rawequal:Int (lua_state:Byte Ptr, index1:Int, index2:Int)
Returns 1 if the two values in acceptable indices index1 and index2 are primitively equal.
Function lua_rawget (lua_state:Byte Ptr, index:Int)
Similar to lua_gettable, but does a raw access
Function lua_rawgeti (lua_state:Byte Ptr, index:Int, n:Int)
Pushes onto the stack the value t[n]
, where t is the value at the given valid index.
Function lua_rawset (lua_state:Byte Ptr, index:Int)
Similar to lua_settable, but does a raw assignment.
Function lua_rawseti (lua_state:Byte Ptr, index:Int, n:Int)
Does the equivalent of t[n] = v
, where t is the value at the given valid index and v is the value at the top of the stack.
Function lua_remove (lua_state:Byte Ptr, index:Int)
Removes the element at the given valid index, shifting down the elements above this index to fill the gap.
Function lua_replace (lua_state:Byte Ptr, index:Int)
Moves the top element into the given position (and pops it), without shifting any element.
Function lua_resume:Int (lua_state:Byte Ptr, narg:Int)
Starts and resumes a coroutine in a given thread.
Function lua_setfenv:Int (lua_state:Byte Ptr, index:Int)
Pops a table from the stack and sets it as the new environment for the value at the given index.
Function lua_setfield (lua_state:Byte Ptr, index:Int, k$z)
Does the equivalent to t[k] = v
, where t is the value at the given valid index and v is the value at the top of the stack.
Function lua_sethook:Int (lua_state:Byte Ptr, f(ls:Byte Ptr,ar:lua_Debug Ptr), mask:Int, count:Int)
Sets the debugging hook function.
Function lua_setlocal$z (lua_state:Byte Ptr, ar:lua_Debug Ptr, n:Int)
Sets the value of a local variable of a given activation record.
Function lua_setmetatable:Int (lua_state:Byte Ptr, index:Int)
Pops a table from the stack and sets it as the new metatable for the value at the given acceptable index.
Function lua_settable (lua_state:Byte Ptr, index:Int)
Does the equivalent to t[k] = v
, where t is the value at the given valid index, v is the value at the top of the stack, and k is the value just below the top.
Function lua_settop (lua_state:Byte Ptr, index:Int)
Accepts any acceptable index, or 0, and sets the stack top to this index.
Function lua_setupvalue$z (lua_state:Byte Ptr, funcindex:Int, n:Int)
Sets the value of a closure's upvalue.
Function lua_status:Int (lua_state:Byte Ptr)
Returns the status of the thread lua_state.
Function lua_toboolean:Int (lua_state:Byte Ptr, index:Int)
Converts the Lua value at the given acceptable index to a boolean value (0 or 1).
Function lua_tocfunction:Byte Ptr (lua_state:Byte Ptr, index:Int)
Converts a value at the given acceptable index to a function.
Function lua_tolstring:Byte Ptr (lua_state:Byte Ptr, index:Int, size:Size_T Ptr) ' without any conversion!
Converts the Lua value at the given acceptable index to a C string.
Function lua_tonumber:Double (lua_state:Byte Ptr, index:Int)
Converts the Lua value at the given acceptable index to the C type lua_Number.
Function lua_topointer:Byte Ptr (lua_state:Byte Ptr, index:Int)
Converts the value at the given acceptable index to a generic C pointer.
Function lua_tothread:Byte Ptr (lua_state:Byte Ptr, index:Int)
Converts the value at the given acceptable index to a Lua thread.
Function lua_touserdata:Byte Ptr (lua_state:Byte Ptr, index:Int)
If the value at the given acceptable index is a full userdata, returns its block address.
Function lua_type:Int (lua_state:Byte Ptr, index:Int)
Returns the type of the value in the given acceptable index, or LUA_TNONE for a non-valid index.
Function lua_typename$z (lua_state:Byte Ptr, tp:Int)
Returns the name of the type encoded by the value tp, which must be one the values returned by lua_type.
Function lua_xmove(fromState:Byte Ptr, toState:Byte Ptr, n:Int)
Exchange values between different threads of the same global state.
Function lua_yield:Int (lua_state:Byte Ptr, nresults:Int)
Yields a coroutine.
Function lua_isboolean:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the value at the given acceptable index has type boolean, and 0 otherwise.
Function lua_isfunction:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the value at the given acceptable index is a function (either C or Lua), and 0 otherwise.
Function lua_islightuserdata:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the value at the given acceptable index is a light userdata, and 0 otherwise.
Function lua_isnil:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the value at the given acceptable index is nil, and 0 otherwise.
Function lua_isnone:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the given acceptable index is not valid (that is, it refers to an element outside the current stack), and 0 otherwise.
Function lua_isnoneornil:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the given acceptable index is not valid (that is, it refers to an element outside the current stack) or if the value at this index is nil, and 0 otherwise.
Function lua_istable:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the value at the given acceptable index is a table, and 0 otherwise.
Function lua_isthread:Int (lua_state:Byte Ptr, index:Int)
Returns 1 if the value at the given acceptable index is a thread, and 0 otherwise.
Function lua_newtable (lua_state:Byte Ptr)
Creates a new empty table and pushes it onto the stack.
Function lua_pop (lua_state:Byte Ptr, n:Int)
Pops n elements from the stack.
Function lua_pushcfunction (lua_state:Byte Ptr, fn:Int(ls:Byte Ptr))
Pushes a function onto the stack.
Function lua_register (lua_state:Byte Ptr, name:String, f:Int(ls:Byte Ptr))
Sets the function f as the new value of global name.
Function lua_setglobal (lua_state:Byte Ptr, name:String)
Pops a value from the stack and sets it as the new value of global name.
Function lua_tostring:String (lua_state:Byte Ptr, index:Int)
Equivalent to lua_tolstring with len equal to NULL.
Function luaL_addlstring (B:Byte Ptr, s:Byte Ptr, l:Int)
Adds the string pointed to by s with length l to the buffer B.
Function luaL_addsize (B:Byte Ptr, size:Int)
Adds to the buffer B (see luaL_Buffer) a string of length size previously copied to the buffer area
Function luaL_addstring (B:Byte Ptr, s$z)
Adds the zero-terminated string pointed to by s to the buffer B.
Function luaL_addvalue (B:Byte Ptr)
Adds the value at the top of the stack to the buffer B.
Function luaL_argerror:Int (lua_state:Byte Ptr, narg:Int, extramsg$z)
Raises an error with the following message, where func is retrieved from the call stack.
Function luaL_buffinit (lua_state:Byte Ptr, B:Byte Ptr)
Initializes a buffer B.
Function luaL_callmeta:Int (lua_state:Byte Ptr, obj:Int, e$z)
Calls a metamethod.
Function luaL_checkany (lua_state:Byte Ptr, narg:Int)
Checks whether the function has an argument of any type (including nil) at position narg.
Function luaL_checklstring:Byte Ptr (lua_state:Byte Ptr, narg:Int, size:Int Ptr)
Checks whether the function argument narg is a string and returns this string.
Function luaL_checknumber:Double (lua_state:Byte Ptr, narg:Int)
Checks whether the function argument narg is a number and returns this number.
Function luaL_checkstack (lua_state:Byte Ptr, sz:Int, msg$z)
Grows the stack size to top + sz elements, raising an error if the stack cannot grow to that size.
Function luaL_checktype (lua_state:Byte Ptr, narg:Int, t:Int)
Checks whether the function argument narg has type t.
Function luaL_checkudata:Byte Ptr (lua_state:Byte Ptr, narg:Int, tname$z)
Checks whether the function argument narg is a userdata of the type tname.
Function luaL_getmetafield:Int (lua_state:Byte Ptr, obj:Int, e$z)
Pushes onto the stack the field e from the metatable of the object at index obj.
Function luaL_gsub$z (lua_state:Byte Ptr, s$z, p$z, r$z)
Creates a copy of string s by replacing any occurrence of the string p with the string r.
Function luaL_loadbuffer:Int (lua_state:Byte Ptr, buff:Byte Ptr, sz:Int, name$z)
Loads a buffer as a Lua chunk.
Function luaL_loadfile:Int (lua_state:Byte Ptr, filename$z)
Loads a file as a Lua chunk.
Function luaL_loadstring:Int (lua_state:Byte Ptr, s$z)
Loads a string as a Lua chunk.
Function luaL_newmetatable:Int (lua_state:Byte Ptr, tname$z)
If the registry already has the key tname, returns 0.
Function luaL_newstate:Byte Ptr ()
Creates a new Lua state.
Function luaL_openlibs (lua_state:Byte Ptr)
Opens all standard Lua libraries into the given state.
Function luaL_optlstring:Byte Ptr (lua_state:Byte Ptr, narg:Int, d$z, size:Int Ptr)
If the function argument narg is a string, returns this string.
Function luaL_optnumber:Double (lua_state:Byte Ptr, narg:Int, d:Double)
If the function argument narg is a number, returns this number.
Function luaL_prepbuffer:Byte Ptr (B:Byte Ptr)
Returns an address to a space of size LUAL_BUFFERSIZE where you can copy a string to be added to buffer B.
Function luaL_pushresult (B:Byte Ptr)
Finishes the use of buffer B leaving the final string on the top of the stack.
Function luaL_ref:Int (lua_state:Byte Ptr, t:Int)
Creates and returns a reference, in the table at index t, for the object at the top of the stack (and pops the object).
Function luaL_register (lua_state:Byte Ptr, libname$z, l:lua_Reg Ptr)
Opens a library.
Function luaL_typerror:Int (lua_state:Byte Ptr, narg:Int, tname$z)
Generates an error with a message.
Function luaL_unref (lua_state:Byte Ptr, t:Int, ref:Int)
Releases reference ref from the table at index t.
Function luaL_where (lua_state:Byte Ptr, lvl:Int)
Pushes onto the stack a string identifying the current position of the control at level lvl in the call stack.
Function luaL_addchar (B:Byte Ptr, c:String)
Adds the character c to the buffer B. (see luaL_Buffer).
Function luaL_argcheck (lua_state:Byte Ptr, cond:Int, narg:Int, extramsg:String)
Checks whether cond is True.
Function luaL_checkint:Int (lua_state:Byte Ptr, narg:Int)
Checks whether the function argument narg is a number and returns this number cast to an Int.
Function luaL_checklong:Long (lua_state:Byte Ptr, narg:Int)
Checks whether the function argument narg is a number and returns this number cast to a Long.
Function luaL_checkstring:String (lua_state:Byte Ptr, narg:Int)
Checks whether the function argument narg is a string and returns this string.
Function luaL_dofile:Int (lua_state:Byte Ptr, filename:String)
Loads and runs the given file.
Function luaL_dostring:Int (lua_state:Byte Ptr, str:String)
Loads and runs the given string.
Function luaL_getmetatable (lua_state:Byte Ptr, tname:String)
Pushes onto the stack the metatable associated with name tname in the registry (see luaL_newmetatable).
Function luaL_optint:Int (lua_state:Byte Ptr, narg:Int, d:Int)
If the function argument narg is a number, returns this number cast to an Int.
Function luaL_optlong:Long (lua_state:Byte Ptr, narg:Int, d:Long)
If the function argument narg is a number, returns this number cast to a Long.
Function luaL_optstring:String (lua_state:Byte Ptr, narg:Int, d:String)
If the function argument narg is a String, returns this String.
Function luaL_typename:String (lua_state:Byte Ptr, idx:Int)
Returns the name of the type of the value at the given index.