Embedding LUA in PureBasic!?

Everything else that doesn't fall into one of the other PB categories.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

@FloHimself

Can you recommend a good "Lua for Dummies" english forum?

Thanks. :)
@}--`--,-- A rose by any other name ..
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

i only use resources found on http://www.lua.org/community.html
there is a mailling list you should use. or just search in the archiv. there is a forum too. but unavailable atm.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

k. Thanks.
@}--`--,-- A rose by any other name ..
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

This is cool, I can't wait to start using this!
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

Hm, I can't get this working it seems... (the lib version) It gives me memory access errors and crashes with the examples
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

Post some code, please.

//EDIT: most common error is not using "ProcedureCDLL" for functions exported to lua. that has changed, because now using CDECL as calling convention. if you want to use pblua, download the latest version from pblua.florian-s.com. the examples should be fixed there.
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

Ok great, it worked =) Btw, great job!
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

I'd like to take a look at this also. Flo...does your website work? I couldn't get it come up.
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

My favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
CameleonTH
New User
New User
Posts: 5
Joined: Sat Jan 28, 2006 6:47 pm
Location: Laon in France
Contact:

Post by CameleonTH »

I use LUA for a small project of script.
Exemple.
http://cameleonth.free.fr/DL/LUAGUI.zip
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

@CameleonTH

Thank you!

It is nice to see someone is using this library.
My favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
CameleonTH
New User
New User
Posts: 5
Joined: Sat Jan 28, 2006 6:47 pm
Location: Laon in France
Contact:

Post by CameleonTH »

And i have update my project today.
Two example with the program.

http://cameleonth.free.fr/DL/LUAGUI.zip
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

This is very nice! :D
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

For those who requested, here is an include file for Lua 5.1:

lua_conf.pbi:

Code: Select all

; START OF LUA_CONF_PBI
#LUA_LIB_PATH_WIN$   = "path to\lua51.lib"
#LUA_LIB_PATH_LINUX$ = ""
#LUA_LIB_PATH_MACOS$ = ""
; END OF LUA_CONF_PBI
lua.pbi:

Code: Select all

; START OF LUA_PBI
XIncludeFile "lua_conf.pbi"

; /* option for multiple returns in `lua_pcall' and `lua_call' */
#LUA_MULTRET        = (-1)

; /*
; ** pseudo-indices
; */
#LUA_REGISTRYINDEX  = (-10000)
#LUA_ENVIRONINDEX   = (-10001)
#LUA_GLOBALSINDEX   = (-10002)

Macro lua_upvalueindex(i)	
  (#LUA_GLOBALSINDEX-(i))
EndMacro

; /* thread status; 0 is OK */
Enumeration 1
    #LUA_YIELD          ;= 1
    #LUA_ERRRUN         ;= 2
    #LUA_ERRSYNTAX      ;= 3
    #LUA_ERRMEM         ;= 4
    #LUA_ERRERR         ;= 5
EndEnumeration

; /*
; ** basic types
; */

Enumeration -1
    #LUA_TNONE          ;= (-1)
    #LUA_TNIL           ;= 0
    #LUA_TBOOLEAN       ;= 1
    #LUA_TLIGHTUSERDATA ;= 2
    #LUA_TNUMBER        ;= 3
    #LUA_TSTRING        ;= 4
    #LUA_TTABLE         ;= 5
    #LUA_TFUNCTION      ;= 6
    #LUA_TUSERDATA      ;= 7
    #LUA_TTHREAD        ;= 8
EndEnumeration 

; /* minimum Lua stack available to a C function */
#LUA_MINSTACK       = 20

; /* type of numbers in Lua */
Define.d LUA_NUMBER, LUA_NUMBER 

; /* type For integer functions */
Define.l LUA_INTEGER, LUA_INTEGER

; /*
; ** garbage-collection constants
; */
Enumeration
    #LUA_GCSTOP         ;= 0
    #LUA_GCRESTART      ;= 1
    #LUA_GCCOLLECT      ;= 2
    #LUA_GCCOUNT        ;= 3
    #LUA_GCCOUNTB       ;= 4
    #LUA_GCSTEP         ;= 5
    #LUA_GCSETPAUSE     ;= 6
    #LUA_GCSETSTEPMUL   ;= 7
EndEnumeration

#LUA_ERRFILE  = (#LUA_ERRERR+1)
#LUA_NOREF    = (-2)
#LUA_REFNIL   = (-1)


#LUA_COLIBNAME   = "coroutine"
#LUA_TABLIBNAME  = "table"
#LUA_IOLIBNAME   = "io"
#LUA_OSLIBNAME   = "os"
#LUA_STRLIBNAME  = "string"
#LUA_MATHLIBNAME = "math"
#LUA_DBLIBNAME   = "debug"
#LUA_LOADLIBNAME = "package"


; Load OS depending Lua Library
CompilerSelect #PB_Compiler_OS 
  CompilerCase #PB_OS_Windows
    ImportC #LUA_LIB_PATH_WIN$
  CompilerCase #PB_OS_Linux
    ImportC #LUA_LIB_PATH_LINUX$
  CompilerCase #PB_OS_MacOS
    ImportC #LUA_LIB_PATH_MACOS$
CompilerEndSelect
        ; lua.h
        ; /*
        ; ** state manipulation
        ; */
        lua_newstate(*lua_Alloc.l, *ud.l)              As "_lua_newstate"
        lua_close(*lua_State.l)                        As "_lua_close"
        lua_newthread(*lua_State.l)                    As "_lua_newthread"
        lua_atpanic(*lua_State.l, *lua_CFunction.l)    As "_lua_atpanic"
        
        ; /*
        ; ** basic stack manipulation
        ; */
        lua_gettop(*lua_State.l)                       As "_lua_gettop"
        lua_settop(*lua_State.l, idx.l)                As "_lua_settip"
        lua_pushvalue(*lua_State.l, idx.l)             As "_lua_pushvalue" 
        lua_remove(*lua_State.l, idx.l)                As "_lua_remove"
        lua_insert(*lua_State.l, idx.l)                As "_lua_insert"
        lua_replace(*lua_State.l, idx.l)               As "_lua_replace"
        lua_checkstack(*lua_State.l, sz.l)             As "_lua_checkstack"
    
        lua_xmove(*lua_State_from.l, *lua_State_to.l, n.l) As "_lua_xmove"
        
        ; /*
        ; ** access functions (stack -> C)
        ; */
        lua_isnumber(*lua_State.l, idx.l)              As "_lua_isnumber"
        lua_isstring(*lua_State.l, idx.l)              As "_lua_isstring"
        lua_iscfunction(*lua_State.l, idx.l)           As "_lua_iscfunction"
        lua_isuserdata(*lua_State.l, idx.l)            As "_lua_isuserdata"
        lua_type(*lua_State.l, idx.l)                  As "_lua_type"
        lua_typename(*lua_State.l, tp.l)               As "_lua_typename"
    
        lua_equal(*lua_State.l, idx1.l, idx2.l)        As "_lua_equal"
        lua_rawequal(*lua_State.l, idx1.l, idx2.l)     As "_lua_rawequal"
        lua_lessthan(*lua_State.l, idx1.l, idx2.l)     As "_lua_lessthan"
    
        lua_tonumber(*lua_State.l, idx.l)              As "_lua_tonumber"
        lua_tointeger(*lua_State.l, idx.l)             As "_lua_tostring"
        lua_toboolean(*lua_State.l, idx.l)             As "_lua_toboolean"
        lua_tolstring(*lua_State.l, idx.l, len.l)      As "_lua_tolstring"
        lua_objlen(*lua_State.l, idx.l)                As "_lua_objlen"
        lua_tocfunction(*lua_State.l, idx.l)           As "_lua_tocfunction"
        lua_touserdata(*lua_State.l, idx.l)            As "_lua_touserdata"
        lua_tothread(*lua_State.l, idx.l)              As "_lua_tothread"
        lua_topointer(*lua_State.l, idx.l)             As "_lua_ispointer"
    
        ; /*
        ; ** push functions (C -> stack)
        ; */
        lua_pushnil(*lua_State.l)                        As "_lua_pushnil"
        lua_pushnumber(*lua_State.l, LUA_NUMBER.d)       As "_lua_pushnumber"
        lua_pushinteger(*lua_State.l, LUA_INTEGER.l)     As "_lua_pushinteger"
        lua_pushlstring(*lua_State.l, string.s, size.l)  As "_lua_pushlstring"
        lua_pushstring(*lua_State.l, string.s)           As "_lua_pushstring"
        ;lua_pushvfstring(*lua_State.L, const char *fmt, va_list argp)
        ;lua_pushfstring(*lua_State.L, const char *fmt, ...)
        lua_pushcclosure(*lua_State.l, *fn.l, n.l)       As "_lua_pushcclosure"
        lua_pushboolean(*lua_State.l, b.l)               As "_lua_pushboolean"
        lua_pushlightuserdata(*lua_State.l, *p.l)        As "_lua_pushlightuserdata"
        lua_pushthread(*lua_State.l)                     As "_lua_pushthread"
    
        ; /*
        ; ** get functions (Lua -> stack)
        ; */
        lua_gettable(*lua_State.l, idx.l)                As "_lua_gettable"
        lua_getfield(*lua_State.l, idx.l, k.s)           As "_lua_getfield"
        lua_rawget(*lua_State.l, idx.l)                  As "_lua_rawget"
        lua_rawgeti(*lua_State.l, idx.l, n.l)            As "_lua_rawgeti"
        lua_createtable(*lua_State.l, narr.l, nrec.l)    As "_lua_createtable"
        lua_newuserdata(*lua_State.l, sz.l)              As "_lua_newuserdata"
        lua_getmetatable(*lua_State.l, objindex.l)       As "_lua_getmetatable"
        lua_getfenv(*lua_State.l, idx.l)                 As "_lua_getfenv"
    
        ; /*
        ; ** set functions (stack -> Lua)
        ; */
        lua_settable(*lua_State.l, idx.l)                As "_lua_settable"
        lua_setfield(*lua_State.l, idx.l, k.s)           As "_lua_setfield"
        lua_rawset(*lua_State.l, idx.l)                  As "_lua_rawset"
        lua_rawseti(*lua_State.l, idx.l, n.l)            As "_lua_rawseti"
        lua_setmetatable(*lua_State.l, objindex.l)       As "_lua_setmetatable"
        lua_setfenv(*lua_State.l, idx.l)                 As "_lua_setfenv"
        
        ; /*
        ; ** `load' and `call' functions (load and run Lua code)
        ; */
        lua_call(*lua_State.l, nargs.l, nresults.l)                        As "_lua_call"
        lua_pcall(*lua_State.l, nargs.l, nresults.l, *lua_errCFunction.l)  As "_lua_pcall"
        lua_cpcall(*lua_State.l, *lua_CFunction.l, *ud.l)                  As "_lua_cpcall"
        lua_load(*lua_State.l, lua_Reader.l, *dt.l, chunkname.s)           As "_lua_load"
        
        lua_dump(*lua_State.l, lua_Writer.l, *vdata.l)   As "_lua_dump"
        
        ; /*
        ; ** coroutine functions
        ; */
        lua_yield(*lua_State.l, nresults.l)    As "_lua_yield"
        lua_resume(*lua_State.l, narg.l)       As "_lua_resume"
        lua_status(*lua_State.l)               As "_lua_status"
        
        ; /*
        ; ** garbage-collection function And options
        ; */
        lua_gc(*lua_State.l, what.l, idata.l)  As "_lua_gc"
    
        ; /*
        ; ** miscellaneous functions
        ; */
        lua_error(*lua_State.l)                           As "_lua_error"
        
        lua_next(*lua_State.l, idx.l)                     As "_lua_next"
        
        lua_concat(*lua_State.l, n.l)                     As "_lua_concat"
        
        lua_getallocf(*lua_State.l, *p_ud.l)              As "_lua_getallocf"
        lua_setallocf(*lua_State.l, lua_Alloc.l, *ud.l)   As "_lua_setallocf"
        
        ; lualib.h
        luaopen_base(*lua_State.l)    As "_luaopen_base"
        luaopen_table(*lua_State.l)   As "_luaopen_table"
        luaopen_io(*lua_State.l)      As "_luaopen_io"
        luaopen_os(*lua_State.l)      As "_luaopen_os"
        luaopen_string(*lua_State.l)  As "_luaopen_string"
        luaopen_math(*lua_State.l)    As "_luaopen_math"
        luaopen_debug(*lua_State.l)   As "_luaopen_debug"
        luaopen_package(*lua_State.l) As "_luaopen_package"
    
        ; /* open all previous libraries */
        luaL_openlibs(*lua_State.l)   As "_luaL_openlibs"
        
        ;lauxlib_h
        luaI_openlib(*lua_State.l, libname.s, *luaL_Reg.l, nup.l)  As "_luaI_openlib"
        luaL_register(*lua_State.l, libname.s, *luaL_Reg.l)        As "_luaL_register"
        luaL_getmetafield(*lua_State.l, obj.l, e.s)                As "_luaL_getmetafield"
        luaL_callmeta(*lua_State.l, obj.l, e.s)                    As "_luaL_callmeta"
        luaL_typerror(*lua_State.l, narg.l, tname.s)               As "_luaL_typerror"
        luaL_argerror(*lua_State.l, numarg.l, extramsg.s)          As "_luaL_argerror"
        luaL_checklstring(*lua_State.l, numarg.l, size.l)          As "_luaL_checklstring"
        luaL_optlstring(*lua_State.l, numarg.l, def.s, size.l)     As "_luaL_optlstring"
        luaL_checknumber(*lua_State.l, numarg.l)                   As "_luaL_checknumber"
        luaL_optnumber(*lua_State.l, narg, LUA_NUMBER.d)           As "_luaL_optnumber"
    
        luaL_checkinteger(*lua_State.l, numarg.l)                  As "_luaL_checkinteger"
        luaL_optinteger(*lua_State.l, narg.l, LUA_INTEGER.l)       As "_luaL_optinteger"
        
        luaL_checkstack(*lua_State.l, sz.l, msg.s)                 As "_luaL_checkstack"
        luaL_checktype(*lua_State.l, narg.l, t.l)                  As "_luaL_checktype"
        luaL_checkany(*lua_State.l, narg.l)                        As "_luaL_checkany"
        
        luaL_newmetatable(*lua_State.l, tname.s)                   As "_luaL_newmetatable"
        luaL_checkudata(*lua_State.l, ud.l, tname.s)               As "_luaL_checkudata"
        
        luaL_where(*lua_State.l, lvl.l)                            As "_luaL_where"
        ;luaL_error(*lua_State.L, const char *fmt, ...)
        
        luaL_checkoption(*lua_State.l, narg.l, def.s, *string_array.l)  As "_luaL_checkoption"
        
        luaL_ref(*lua_State.l, t.l)           As "_luaL_ref"
        luaL_unref(*lua_State.l, t.l, ref.l)  As "_luaL_unref"
        
        luaL_loadfile(*lua_State.l, filename.s)                As "_luaL_loadfile"
        luaL_loadbuffer(*lua_State.l, buff.l, size.l, name.s)  As "_luaL_loadbuffer"
        luaL_loadstring(*lua_State.l, string.s)                As "_luaL_loadstring"
        
        luaL_newstate() As "_luaL_newstate"
        
        
        luaL_gsub(*lua_State.l, s.s, p.s, r.s)       As "_luaL_gsub"
        
        luaL_findtable(*lua_State.l, idx.l, fname.s) As "_luaL_findtable"
        
        luaL_buffinit(*lua_State.l, *luaL_Buffer.l)  As "_luaL_buffinit"
        luaL_prepbuffer(*luaL_Buffer.l)              As "_luaL_prepbuffer"
        luaL_addlstring(*luaL_Buffer.l, s.s, size.l) As "_luaL_addlstring"
        luaL_addstring(*luaL_Buffer.l, s.s)          As "_luaL_addstring"
        luaL_addvalue(*luaL_Buffer.l)                As "_luaL_addvalue"
        luaL_pushresult(*luaL_Buffer.l)              As "_luaL_pushresult"
    EndImport

; Load additional Libraries needed for linking on Windows.
CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
    ;   ImportC "msvcrt.lib"
    ;   EndImport
    CompilerCase #PB_OS_Linux
    ;   ImportC ""
    ;   EndImport
    CompilerCase #PB_OS_MacOS
    ;   ImportC ""
    ;   EndImport
CompilerEndSelect

; /* 
; ** ===============================================================
; ** some useful macros
; ** ===============================================================
; */
Macro lua_pop(l, n)		
    lua_settop(l, -(n)-1)
EndMacro

Macro lua_newtable(l)
    lua_createtable(l, 0, 0)
EndMacro

Macro lua_register(l, n, f) 
    lua_pushcfunction(l, (f))
    lua_setglobal(l, (n))
EndMacro

Macro lua_pushcfunction(l, f)
	lua_pushcclosure(l, (f), 0)
EndMacro

Macro lua_strlen(l, i)
    lua_objlen(l, (i))
EndMacro

Macro lua_isfunction(l, n)	
    (lua_type(l, (n)) = #LUA_TFUNCTION)
EndMacro

Macro lua_istable(l, n)
    (lua_type(l, (n)) = #LUA_TTABLE)
EndMacro

Macro lua_islightuserdata(l, n)	
    (lua_type(l, (n)) = #LUA_TLIGHTUSERDATA)
EndMacro

Macro lua_isnil(l,n)
    (lua_type(l, (n)) = #LUA_TNIL)
EndMacro

Macro lua_isboolean(l, n)
    (lua_type(l, (n)) = #LUA_TBOOLEAN)
EndMacro

Macro lua_isthread(l, n)
    (lua_type(l, (n)) = #LUA_TTHREAD)
EndMacro

Macro lua_isnone(l, n)
    (lua_type(l, (n)) = #LUA_TNONE)
EndMacro

Macro lua_isnoneornil(l, n)	
    (lua_type(l, (n)) <= 0)
EndMacro

Macro lua_pushliteral(l, s)	
	lua_pushlstring(l, s, Len(s)-1)
EndMacro

Macro lua_setglobal(l, s)	
    lua_setfield(l, #LUA_GLOBALSINDEX, (s))
EndMacro

Macro lua_getglobal(l,s)
    lua_getfield(l, #LUA_GLOBALSINDEX, (s))
EndMacro

Macro lua_tostring(l,i)	
    lua_tolstring(l, (i), #Null)
EndMacro

; /*
; ** compatibility macros and functions
; */
Macro lua_open
    luaL_newstate
EndMacro

Macro lua_getregistry(l)
    lua_pushvalue(l, #LUA_REGISTRYINDEX)
EndMacro

Macro lua_getgccount(l)	
    lua_gc(l, #LUA_GCCOUNT, 0)
EndMacro

Macro luaL_getn(l,i)
    lua_objlen(l, i)
EndMacro

Macro luaL_setn(l,i,j)
    ;/* no op! */
EndMacro

Macro luaL_argcheck(l, cond, numarg, extramsg)
    ((cond) Or luaL_argerror(l, numarg, extramsg))
EndMacro

Macro luaL_checkstring(l, n)
    (luaL_checklstring(l, (n), #Null))
EndMacro

Macro luaL_optstring(l, n, d)
    (luaL_optlstring(l, (n), (d), #Null))
EndMacro

Macro luaL_checkint(l, n)	
    (luaL_checkinteger(l, (n)))
EndMacro

Macro luaL_optint(l, n, d)
    (luaL_optinteger(l, (n), (d)))
EndMacro

Macro luaL_checklong(l, n)	
    (luaL_checkinteger(l, (n)))
EndMacro

Macro luaL_optlong(l, n, d)
    (luaL_optinteger(l, (n), (d)))
EndMacro

Macro luaL_typename(l, i)	
    lua_typename(l, lua_type(l,(i)))
EndMacro

Macro luaL_dofile(l, fn)	
    luaL_loadfile(l, fn)
    lua_pcall(l, 0, 0, 0)
EndMacro

Macro luaL_dostring(l, s)	 
    luaL_loadstring(l, s) 
    lua_pcall(l, 0, 0, 0)
EndMacro

Macro luaL_getmetatable(l, n)	
    (lua_getfield(l, #LUA_REGISTRYINDEX, (n)))
EndMacro

; Macro luaL_opt(L, f, n, d)	
;   (lua_isnoneornil(L, (n)) ? (d) : f(L,(n)))
; EndMacro

Macro lua_baselibopen
    luaopen_base
EndMacro

Macro lua_tablelibopen
    luaopen_table
EndMacro
        
Macro lua_iolibopen
    luaopen_io
EndMacro
        
Macro lua_oslibopen
    luaopen_os
EndMacro
        
Macro lua_stinglibopen
    luaopen_string
EndMacro
        
Macro lua_mathlibopen
    luaopen_math
EndMacro
        
Macro lua_debuglibopen
    luaopen_debug
EndMacro
        
Macro lua_packagelibopen
    luaopen_package
EndMacro
; END OF LUA_PBI
example.pb:

Code: Select all

; EXAMPLE:
XIncludeFile "lua.pbi"

OpenConsole()

*l = lua_open() 
luaL_openlibs(*l)

            ;*-- lua state 
            ;|   *-- pointer to buffer with compiled or uncompiled chunk
            ;|   |          *------ size of buffer containing the chunk
            ;|   |          |            *-- string name / optional label (?) of chunk
            ;|   |          |            |       
            ;v   v          v            v
luaL_loadbuffer(*l, ?foo, ?endfoo-?foo, "foo")
lua_pcall(*l, 0, 0, 0)

;luaL_dofile(*l, "dobuffer.lua")
lua_close(*l)

Input()
CloseConsole()
End


DataSection
foo:
  ;IncludeBinary "dobuffer.luac"
IncludeBinary "dobuffer.lua"
endfoo:
EndDataSection 
Tested* with lua51.dll & import lib lua51.lib from luaforge


*not extensively, so expect some trouble.
My favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

"!!! This Library is deprecated. You may contact me for further information !!!"
What's that all about?
Post Reply