Marco Pontello's Home Page
PowerBLua
Questa pagina in italiano

(Last updated: 29/01/20)
 

 

The Programming Language Lua        In Association with Amazon.co.uk cover

PowerBLua

PowerBLua is a little contribution of mine to the PowerBASIC community.
It's a wrapper for Lua, a powerful light-weight programming language designed for extending applications.

At the moment, only a subset of API is implemented, but thankful to the Lua flexibility, is already more than enough to do something useful with the language.

Lua API functions
State management: lua_open, lua_close
Libraries management: luaopen_base, luaopen_debug, luaopen_io, luaopen_loadlib, luaopen_math, luaopen_string, luaopen_table
Execute: lua_dostring, lua_dofile
Stack manipulation: lua_gettop, lua_settop, lua_pushvalue, lua_remove, lua_insert, lua_replace
Pushing onto the Stack: lua_pushnumber, lua_pushcclosure, lua_pushstring
Getting from the Stack: lua_toboolean, lua_tonumber, lua_tostring, lua_tocfunction, lua_touserdata
Stack values comparisions: lua_equal, lua_rawequal, lua_lessthan
Type checking: lua_type, lua_isnil, lua_isboolean, lua_isnumber, lua_isstring, lua_istable, lua_isfunction, lua_iscfunction, lua_isuserdata, lua_islightuserdata,
Misc: lua_settable, lua_strlen

PowerBASIC wrapper functions / helpers
PBLua_Register
PBLua_DoString
PBLua_DoFile
PBLua_GetString
PBLua_SetValue
PBLua_GetValue

Sample code

PowerBASIC source: minmax.bas

' ===================================================================
' This sample show how to register/export PowerBASIC functions to Lua
' ===================================================================


' Include the necessary declarations

#INCLUDE "PowerBLua.INC"


' This is the function exported to Lua

FUNCTION MinMax CDECL (BYVAL pLuaHandle AS DWORD) AS DWORD

  DIM NumPar AS LONG
  DIM i AS LONG
  DIM Num AS DOUBLE
  DIM MaxNum AS DOUBLE
  DIM MinNum AS DOUBLE

  ' Get the number of parameters passed on the Virtual Stack
  ' by the calling Lua code

  NumPar = Lua_GetTop(pLuaHandle)

  ' Pop all parameters from the VS and evaluate them

  FOR i = 1 TO NumPar

    ' Peek the specified VS element
    Num = Lua_ToNumber(pLuaHandle, i)

    MaxNum = MAX(Num, MaxNum)
    IF MinNum = 0 THEN
      MinNum = Num
    ELSE
      MinNum = MIN(Num, MinNum)
    END IF
  NEXT i

  ' Push the results on the VS
  Lua_PushNumber(pLuaHandle, MinNum)
  Lua_PushNumber(pLuaHandle, MaxNum)

  ' Set number of returned parameters
  FUNCTION = 2

END FUNCTION


FUNCTION PBMAIN

  DIM LuaHandle AS DWORD

  ' Create a Lua state
  LuaHandle = Lua_Open()

  ' Load the base Lua library (needed for 'print')
  LuaOpen_Base(LuaHandle)

  ' Register the function to be called from Lua as PBMinMax
  PBLua_Register(LuaHandle, "PBMinMax", CODEPTR(MinMax))

  ' Execute the Lua script from a file
  PBLua_DoFile(LuaHandle, "minmax.lua")

  ' Release the Lua state
  Lua_Close(LuaHandle)

  PRINT "PB : Finished! Press any key"
  WAITKEY$

END FUNCTION

Lua source: minmax.lua

-- Show how to call a function registered in the embedding application
-- PBMinMax is a PowerBASIC function

Min, Max = PBMinMax(42, 2, 17, 33, 15, 1.5)

print("Lua: Min= ", Min, ", Max= ", Max, "\n")

Console output:
 Lua: Min= 1.5, Max= 42

 PB : Finished! Press any key

Download

PowerBLua (source & samples), 10KB ZIP
Lua 5.0 binaries (Lua, LuaC & Lua.dlls), 182KB ZIP

Change Log

06/07/04:
+ Addedd another sample: a spellchecker.

03/03/04:
* Fixed a bug in the function PBLua_GetValue.

10/02/04:
+ Lua Do* function now returns the proper error code.

09/02/04:
+ Addedd code for some more API calls.
+ Addedd another sample (about using Lua to parse expressions).

08/02/04:
* Updated for Lua 5.0.
+ Added a downloadable package with Lua, LuaC and a custom Lua.DLL.

04/02/04:
+ Addedd code for some more API calls.
+ Addedd another sample.

03/02/04:
+ Addedd two samples and some info.

02/02/04:
- First public release - it's a work in progress!