intro.bbdoc 5.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <div style="text-align: justify;">"<span style="font-style: italic;">Lua is an extension programming
  2. language designed to support general procedural programming with data
  3. description facilities. It also offers good support for object-oriented
  4. programming, functional programming, and data-driven programming. Lua
  5. is intended to be used as a powerful, light-weight scripting language
  6. for any program that needs one.</span> " (from "<a href="../lua-5.1.4/doc/contents.html">Lua 5.1 Reference
  7. Manual</a>" by Roberto Ierusalimschy, Luiz Henrique de
  8. Figueiredo, Waldemar Celes)<br><br>This module provides a
  9. BlitzMax interface to the Lua main and auxiliary APIs. It is almost
  10. complete, the only functions missing are those with variable argument
  11. lists (which are not yet supported by BlitzMax).<br><br>The
  12. axe.lua package also contains the full Lua 5.1.4 distribution. The Lua
  13. source code is completely integrated into the module, additional DLLs
  14. (or shared libraries, resp.) are no longer required.<br><h2>Lua
  15. Documentation</h2>The Lua Reference Manual is part of this
  16. distribution and may be directly visited from here:<br><ul><li><a href="../lua-5.1.4/doc/contents.html">Lua 5.1 Contents</a></li><li><a href="../lua-5.1.4/doc/manual.html">Lua 5.1 Reference Manual</a></li><li><a href="../lua-5.1.4/doc/readme.html">Lua 5.1 ReadMe</a></li></ul>Additional
  17. information can be found on the <a href="http://www.lua.org">Lua
  18. web site</a>; the <a href="http://lua-users.org/wiki/">Lua
  19. wiki</a> contains further material <a href="http://lua-users.org/wiki/BlitzMax">about this module</a>
  20. and related packages.<br><h2>Introduction</h2>The
  21. following description is not meant as a tutorial, but may still help
  22. you to start Lua programming within BlitzMax. More API-related
  23. information can be found in the <a href="../lua-5.1.4/doc/manual.html#3">Lua Reference manual</a><br><h3>Setting
  24. up a Lua VM</h3>Running&nbsp;Lua scripts always require the
  25. instantiation of at least one Lua VM:<br><pre> local LuaState:byte ptr = luaL_newstate()</pre>The
  26. result of this instantiation is a pointer to a structure
  27. which&nbsp;contains the full state of the new VM and has to be
  28. passed as the first argument to almost any other Lua API function.<br><br>It
  29. is now time to "open" the intrinsic Lua libraries (note: these
  30. libraries are part of axe.lua now, external DLLs or shared libraries
  31. are not necessary):<br><pre> luaL_openlibs(LuaState)</pre>Always
  32. initialize the Lua VM by opening its libraries unless you really know
  33. what you are doing!<br><br>Usually, creating a single Lua
  34. state is sufficient, even further (Lua) threads share the basic Lua
  35. state (and its associated environment).<br><h3>Shutting
  36. down a Lua VM</h3>At the end, it's always a good idea to shut
  37. down the Lua VM<br><pre> lua_close(LuaState)</pre>The
  38. Lua interpreter has now been terminated and its state variable is no
  39. longer valid.<br><h3>Accessing Lua Globals</h3>The
  40. code<br><pre>&nbsp; lua_pushstring(LuaState, "BMXString")<br> lua_setglobal (LuaState, "luaglobal")</pre>defines
  41. a global Lua variable (called&nbsp;<span style="font-family: Courier New,Courier,monospace;">luaglobal</span>)
  42. which contains a string (namely "<span style="font-family: Courier New,Courier,monospace;">BMXString</span>").<h3>Registering
  43. BlitzMax Functions</h3>In order to access BlitzMax features from
  44. within a Lua script, it is necessary to implement suitable BlitzMax
  45. functions and register them in the Lua VM.<br><br>Such
  46. BlitzMax functions typically look as follows:<br><pre> function BMXName:int (LuaState:Byte Ptr)<br> ... ' handling of parameters passed from Lua (if required)<br> ... ' actual function body<br> ... ' passing results back to Lua (if required)<br> return 0 ' number of values returned to Lua<br> end function</pre>Such
  47. a function is then registered using<br><pre> lua_register(LuaState, "luaname", BMXName)</pre>This
  48. command registers a BlitzMax function (called&nbsp;<span style="font-family: Courier New,Courier,monospace;">BMXName</span>)
  49. with the (global) name&nbsp;<span style="font-family: Courier New,Courier,monospace;">luaname</span>
  50. under Lua. Both names may be equal, but they do not have to.<br><h3>Running
  51. Lua Scripts From Within BlitzMax</h3>The code<br><pre> local Result:int = luaL_loadString(LuaState,LuaScript)<br> if (Result &lt;&gt; 0) then<br> ' ERROR!!!<br> lua_close(LuaState) ' just to be complete<br> end<br> end if</pre>loads
  52. and compiles a (BlitzMax) string containing a LuaScript. The result (of
  53. compilation) is left on the (Lua) stack.<br><pre> lua_getfield(LuaState, LUA_GLOBALSINDEX, "debug")' get global "debug"<br> lua_getfield(LuaState, -1, "traceback") ' get "debug.traceback"<br> lua_remove (LuaState, -2) ' remove "debug" table from stack<br><br> Result = lua_pcall(LuaState,1,-1,-1)' use "debug.traceback" as err.hdlr<br> if (Result &lt;&gt; 0) then<br> ' ERROR<br> lua_close(LuaState) ' just to be complete<br> end<br> end if</pre>actually
  54. evaluates the previously loaded script. The initially mentioned Lua
  55. commands just prepare for proper error messages should the Lua script
  56. fail.</div>