LUA - Script with PureBasic

Share your advanced PureBasic knowledge/code with the community.
jack
Addict
Addict
Posts: 1336
Joined: Fri Apr 25, 2003 11:10 pm

Re: LUA - Script with PureBasic

Post by jack »

here are the LUA54rc3 binaries for Windows
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: LUA - Script with PureBasic

Post by Joubarbe »

Just a heads up on this, the following is the proper structure:

Image

It's weird to wrongly point the example code to the Binaries folder...
User avatar
Dadido3
User
User
Posts: 52
Joined: Sat Jan 12, 2008 11:50 pm
Location: Hessen, Germany
Contact:

Re: LUA - Script with PureBasic

Post by Dadido3 »

Just to clarify it, as it states in the repo:
To make Lua work on Windows you have to copy the according (x86 or x64) shared library (*.dll) into the same folder as your application. The *.dll files can be found in Libraries\Lua\Binaries\Windows\x__\, for Linux no files have to be copied because the static version of Lua is used there for easiness.
So on windows you have to copy one of the dll files next to where you compile and run your executable. The lib files don't need to be moved/copied to get it working. But you can still do so if you want to use another structure. In this case you have to change the #Lua_Library_File constant.

It would be ideal if i had statically linked lib files for windows, so no dll is needed, and the code/example would work without moving any files around. But i haven't got that working.
Joubarbe
Enthusiast
Enthusiast
Posts: 555
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: LUA - Script with PureBasic

Post by Joubarbe »

Oh ok! Thank you for this library, it's very useful, as PB doesn't have any built-in script language. Do you plan an update for LUA 5.4?
User avatar
Dadido3
User
User
Posts: 52
Joined: Sat Jan 12, 2008 11:50 pm
Location: Hessen, Germany
Contact:

Re: LUA - Script with PureBasic

Post by Dadido3 »

A change to Lua 5.4 is not planned. But you could generate a diff between the 5.3 and 5.4 header files of lua and then apply those changes to the PureBasic include.

Also, new minor versions in lua mostly mean breaking changes (Not only to the API, but also to the language itself). Some people even stick with Lua 5.1, because they don't want to rewrite scripts, or because there are more scripts/examples available (LuaJIT is staying compatible with 5.1, so there are probably more addons/libs/scripts for 5.1)

As long as 5.3 is getting updated, the include will stay with 5.3. Lua 5.4 isn't even officially released yet. Is there any function you need from 5.4, or do you just want to have the newest Lua version?
One thing i could update is the binaries to Lua 5.3.5, but i would need some time to either find working precompiled ones, or compile them myself.
lesserpanda
User
User
Posts: 65
Joined: Tue Feb 11, 2020 7:50 am

Re: LUA - Script with PureBasic

Post by lesserpanda »

Hi, I was wondering what you would use LUA for with PureBasic?

Am I right to say PureBasic, write a server like a HTTP API server, then run LUA?

Can you let me know the practical uses?

Thanks
User avatar
Tristano
Enthusiast
Enthusiast
Posts: 190
Joined: Thu Nov 26, 2015 6:52 pm
Location: Italy
Contact:

Re: LUA - Script with PureBasic

Post by Tristano »

lesserpanda wrote: Sun Dec 04, 2022 12:32 pm Hi, I was wondering what you would use LUA for with PureBasic?
The idea of using a scripting language along with a binary program is generally to allow users to customize or extend the program via external file, which is why video games (for example) often rely on a scripting language to control settings or allow custom extensions via scripted plug-ins.

Your binary application can thus remain closed source yet provide its end users ways to customize its behavior.
Another obvious advantage is that with external scripts you don't need to recompile the whole application for those changes to take effect, since scripts are loaded at runtime.
lesserpanda wrote: Sun Dec 04, 2022 12:32 pm Am I right to say PureBasic, write a server like a HTTP API server, then run LUA?
Sure, it's possible. Many server software (e.g. Apache) do provide support for Lua scripting via extra modules, although Lua is not often used on the server side (unlike JS, Perl, etc.).
But if you were to write your own custom HTTP server in PureBasic, adding support for Lua scripts would be a good example of how Lua can be used in combination with PB to allow end user customization, by exposing the server API to custom Lua plug-ins.
lesserpanda wrote: Sun Dec 04, 2022 12:32 pm Can you let me know the practical uses?
The practical uses are limited only by your imagination. Lua is often used to handle configuration files, for example, because the nature of the language allows to write neat definitions of variables and tables in a simple notation. Before the JSON standard came about, Lua was often used for handling configurations. The advantage of using Lua for configurations is that end users can exploit the full power of the Lua language in their configurations: instead of static definitions they can exploit Lua API calls, system calls, or write their custom functions to calculate settings on the fly.

Also worth keeping in mind, the Lua language supports some constructs which PureBasic doesn't (e.g. closures, to mention but one), so you might want to consider integrating Lua into your applications when the Lua language might simplify some tasks which are hard to achieve in PB.

Or maybe you want want to exploit some existing Lua library instead of writing it from scratch in PB, since the Lua community has written a considerable number of libraries (called "Lua rocks") over the years. No need to reinvent the wheel if you can save time by using something that already exists.
The PureBASIC Archives: FOSS Resources:
User avatar
tj1010
Enthusiast
Enthusiast
Posts: 623
Joined: Mon Feb 25, 2013 5:51 pm
Location: US or Estonia
Contact:

Re: LUA - Script with PureBasic

Post by tj1010 »

PB 6.0 Final. Lua 5.4 shared lib DLL. No library stuff..

I've never seen a use for Lua outside scripting for stuff with huge asset databases. Last time I touched it was to do this exact same thing in MingGW like 8 years ago... This just calls a PB function from Lua; it'll run full Lua scripts and you can change one line to make it run files instead of a string..

load individual modules instead of luaL_openlibs to sandbox Lua

Call PB 6.0(C backend) function from Lua 5.4 shared DLL

Code: Select all

Enumeration
  #LUA_OK
  #LUA_ERRRUN
  #LUA_ERRMEM
  #LUA_ERRERR
  #LUA_YIELD
  #LUA_ERRFILE
EndEnumeration

ProcedureC.l test(*L)
  If CallFunction(0,"lua_gettop",*L)>0
    Debug CallFunction(0,"lua_tolstring",*L,CallFunction(0,"lua_upvalueindex",1))
  EndIf
EndProcedure

If OpenLibrary(0,"lua54.dll")
  
  *L=CallFunction(0,"luaL_newstate")
  If *L
    CallFunction(0,"luaL_openlibs",*L)

    CallFunction(0,"lua_pushcclosure",*L,@test(),1)
    aaa.s="test('12345')"
    err=CallFunction(0,"luaL_dostring",*L,@aaa)
    Select err
      Case 0
        ;string pushed to stack now execute
        response=CallFunction(0,"lua_pcallk",*L,0,0,0)
        Select response
          Case 1
            Debug #LUA_ERRRUN
          Case 2
            Debug #LUA_ERRMEM
          Case 3
            Debug #LUA_ERRERR
          Case 4 
            Debug #LUA_YIELD
          Case 5
            Debug #LUA_ERRFILE
        EndSelect
      Case 1
        Debug #LUA_ERRRUN
      Case 2
        Debug #LUA_ERRMEM
      Case 3
        Debug #LUA_ERRERR
      Case 4 
        Debug #LUA_YIELD
      Case 5
        Debug #LUA_ERRFILE
    EndSelect
    CallFunction(0,"lua_close",*L)
  EndIf
  CloseLibrary(0)
EndIf
End
The truth hurts.
Post Reply