Author Topic: PowerBLua :)  (Read 17425 times)

Frankolino

  • Newbie
  • *
  • Posts: 1
PowerBLua :)
« on: January 05, 2010, 09:16:48 PM »
hi mark, I am quite new here. my question belongs to the topic "lua + powerbasic". can you tell me how I can get with PBWIN the "minmax" example to run ?

Quote
' ===================================================================
' 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)

  MSGBOX "PB : Finished! Press any key"


END FUNCTION

this example works for me, but not with your example below at your website.

Quote
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


I cannot use Console, but would like to take msgbox :)

other examples are running (exxparser and hello). - perhaps you can help, would be nice!

best regards, frankolino

Mark0

  • Administrator
  • Hero Member
  • *****
  • Posts: 2743
    • Mark0's Home Page
Re: PowerBLua :)
« Reply #1 on: January 05, 2010, 09:31:11 PM »
The problem is that Lua's print function write to the standard output.
But you could simply create a simple PB function that write on a message box, and register / export that to the Lua environment, just as done for the MinMax() function in the sample.