LUA in PureBasic

Just starting out? Need help? Post your questions and find answers here.
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

LUA in PureBasic

Post by Xanos »

Hello guys,
for my current project I (unfortunately) need to be able to parse simple lua scripts on Win, Lin and Mac
I already searched the web and this forum but only found really old posts regarding LUA in PureBasic. I also never used LUA in my life before, thus I am not really a specialist in what functions to use and how ;)

I currently try to get it running under Windows, and downloaded a precompiled binary (.lib) for LUA 5.2.3 from http://sourceforge.net/projects/luabinaries/files/
I also tried simple Purebasic code like this:

Code: Select all

ImportC "lua52.lib"
  luaL_newstate()
  luaL_openlibs(*L)
  luaL_loadfilex(*L, file$, *mode)
EndImport


L = luaL_newstate()
Debug L
luaL_openlibs(L)

status = luaL_loadfilex(L, "info.lua", 0)
If status
  Debug "error"
EndIf
This really small code is working but this is already as far as I got until now. Unfortunately, I do not really have the time to extensively learn LUA and/or its interface :( However, I only need a really simple implementation with the most basic functions to parse files like the one I pasted below.

So my question to you: Is there a relatively recent version of some lua.pbi file providing the required functions, constants and macros? (The old ones I found throw syntax errors)
Or is there another easy way to parse simple lua files like the following?

Code: Select all

function data()
return {
  minorVersion = 0,
  severity = "WARNING",
  name = _("Name"),
  description = _(""),
  authors = {
    {
      name = "Author_1",
      role = "CREATOR",
      text = "",
    },
    {
      name = "Author_2",
      role = "CO_CREATOR",
      text = "",
    },
  },
  tags = {"tag_1", "tag_2", },
  id = 7,
  dependencies = { },
  url = "",
}
end
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
User avatar
fiver
User
User
Posts: 36
Joined: Wed May 05, 2004 8:21 pm
Location: An outer spiral arm of the Milky Way

Re: LUA in PureBasic

Post by fiver »

Hello Xanos,

Here is some tinkering I did a year or so ago which may help you, you don't necessarily need an include - you can just import the functions you want directly from the library. I made some (scant!) notes and the links are worth a read to.

lua_test.pb:


7th March 2015 EDITED the first block of code to include additional steps required on windows

Code: Select all

;-- Based on earlier versions by FloHimself, ts-soft and others see:
;-- http://purebasic.fr/english/viewtopic.php?f=7&t=8056&hilit=lua+pbi&start=30

;-- Refs:
;-- TFM:
;-- http://www.lua.org/manual/5.2/contents.html
;-- Discussion of changes in lua 5.2, covers c api changes.
;-- http://www.corsix.org/content/look-lua-52-work3
;-- Good article about why embedded lua is useful
;-- http://www.grimrock.net/2012/07/25/making-of-grimrock-rapid-programming/
;-- Windows static libs:
;-- http://sourceforge.net/projects/luabinaries/files/5.2/Windows%20Libraries/Static/

;-- Newer binaries for windows and possible the Visual C++ Redistributable Packages for Visual Studio 2013 
;-- are also required
;-- http://luabinaries.sourceforge.net/
;-- http://www.microsoft.com/en-gb/download/details.aspx?id=40784


CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
ImportC "lua52.lib"
  luaL_newstate()
  ;-- Opens all standard Lua libraries into the given state.
  luaL_openlibs (*lua_State)
  luaL_loadstring(*lua_State, *Str)
  luaL_loadbufferx(*lua_State, *buffer, sz.i, *name, *mode=0)
  luaL_loadfilex(*lua_State, *filename, *mode=0);
  lua_pcallk(*lua_State, nargs.i, nresults.i, errfunc.i, ctx.i=0, lua_CFunction.i=0)
  lua_getglobal(*lua_State, *name)
  lua_pushlstring(*lua_State, *Str, *len=0)
  ;-- lua_Number is a double
  lua_pushnumber(*lua_State, number.d)
  ;-- returns lua_Number
  lua_tonumberx(*lua_State, index.i, *isnum=0)
  lua_tolstring(*lua_State, index.i, *len=0)
  lua_settop(*lua_State, index.i)
  lua_close(*lua_State)
EndImport
CompilerCase #PB_OS_Linux
ImportC "liblua.a"
  luaL_newstate()
  ;-- Opens all standard Lua libraries into the given state.
  luaL_openlibs (*lua_State)
  luaL_loadstring(*lua_State, *Str)
  luaL_loadbufferx(*lua_State, *buffer, sz.i, *name, *mode=0)
  luaL_loadfilex(*lua_State, *filename, *mode=0);
  lua_pcallk(*lua_State, nargs.i, nresults.i, errfunc.i, ctx.i=0, lua_CFunction.i=0)
  lua_getglobal(*lua_State, *name)
  lua_pushlstring(*lua_State, *Str, *len=0)
  ;-- lua_Number is double
  lua_pushnumber(*lua_State, n.d)
  ;-- returns lua_Number
  lua_tonumberx(*lua_State, index.i, *isnum=0)
  lua_tolstring(*lua_State, index.i, *len=0)
  lua_settop(*lua_State, index.i)
  lua_close(*lua_State)
EndImport

;-- Make sure the linker can find the math lib, this import must be done last.
;--ImportC "-lm" : EndImport

CompilerEndSelect

;-- Many of the C api functions are actually Macros, for instance lua_pop:
;-- void lua_pop (lua_State *L, int n);
;-- #define lua_pop(L,n) lua_settop(L, -(n)-1)
;-- Pops n elements from the stack.
Macro lua_pop(l, n)
  lua_settop(l, -(n)-1)
EndMacro

Procedure luaError(*lua_State, msg$)
  Error$ = PeekS(lua_tolstring(*lua_State, 1))
  MessageRequester("lua error:", msg$+ " " + Error$)
  End
EndProcedure

OpenConsole()

*lua_State = luaL_newstate()
luaL_openlibs (*lua_State)

luaL_loadstring(*lua_State, @"io.write('He\'s not the Messiah, he\'s a very naughty boy.\n\n')")
lua_pcallk(*lua_State, 0, -1, 0)

luaL_loadstring(*lua_State, @"print('lua version : ',_VERSION,'\n\n')")
lua_pcallk(*lua_State, 0, -1, 0)

luaL_loadstring(*lua_State, @"io.write(math.sqrt(9),'\n\n')")
lua_pcallk(*lua_State, 0, -1, 0)

;-- Loading and processing a lua file as a buffer from the data section
;luaL_loadbufferx(*lua_State, ?foo, ?endfoo-?foo, @"foo")
;lua_pcallk(*lua_State, 0, -1, 0)

;-- Same script, but loading the file directly this time
luaL_loadfilex(*lua_State, @"script.lua")
If lua_pcallk(*lua_State, 0, -1, 0)
  luaError(*lua_State, msg$)
EndIf
;-- Once the file is loaded functions from it can be called directly
lua_getglobal(*lua_State, @"tellme")
lua_pcallk(*lua_State, 0, -1, 0)

lua_getglobal(*lua_State, @"sayhello")
Name$ = "Dave"
lua_pushlstring(*lua_State, @Name$, Len(Name$))
lua_pcallk(*lua_State, 1, 1, 0)
RetString$ = PeekS(lua_tolstring(*lua_State, 1))

;-- No dice on windows now, I'm not sure why ;-)
;-- need to pop here, so index 1 is available again
;lua_pop(*lua_State, 1)
;lua_getglobal(*lua_State, @"square")
;lua_pushnumber(*lua_State, 7.35)
;-- 1 arg, 1 result expected
;lua_pcallk(*lua_State, 1, 1, 0)
;RetNumber.d = PeekD(lua_tonumberx(*lua_State, 1))
;Debug RetNumber 

;-- need to pop here, so index 1 is available again
;--lua_pop(*lua_State, 1)
;lua_getglobal(*lua_State, @"square")
;lua_pushnumber(*lua_State, 5)
;-- 1 arg, 1 result expected
;lua_pcallk(*lua_State, 1, 1, 0)
;RetNumber.d = PeekD(lua_tonumberx(*lua_State, 1))
;Debug RetNumber

lua_close(*lua_State)

Input()
CloseConsole()

DataSection
  foo:
  IncludeBinary "script.lua"
  endfoo:
EndDataSection

; IDE Options = PureBasic 5.31 (Windows - x86)
; ExecutableFormat = Console
; CursorPosition = 112
; Folding = -
; EnableXP
; Executable = ..\Documents\PB\lua\test.exe

An example script

script.lua

Code: Select all

a = 7

print("Rule ",a*6,"All persons more than a mile high to leave the court.\n\n")

print("I am using Lua from within PB, Hazah!\n\n")


function foo(a,b)
	print(a*b,"\n\n")
end

foo(7,10)

print(string.sub("123456789", 4, 7),"\n\n")     --should print 4567

function tellme()
	io.write("This is coming from lua.tellme.\n\n")
end

function sayhello(name)
	io.write("Howdy ")
	io.write(tostring(name), ".\n\n")
	return "Howdy " .. tostring(name) .. ".\n\n"
end

function square(n)
	io.write("This is coming from lua.square, arg=")
	io.write(tostring(n))
	n = n * n
	io.write(", square=")
	io.write(tostring(n))
	print(".")
	return(n)
end

Last edited by fiver on Sat Mar 07, 2015 3:13 pm, edited 1 time in total.
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Re: LUA in PureBasic

Post by Xanos »

Nice, thanks a lot! I will check out the link and take a detailed look at the code :)
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
GuideBot
New User
New User
Posts: 8
Joined: Sun Feb 03, 2013 6:53 am

Re: LUA in PureBasic

Post by GuideBot »

Hi Fiver,

Apologies if this is a dumb question, but which file do you download to work with your example, fiver? I have tried lua-5.2.3_Win32_mingw4_lib.zip and lua-5.2.3_Win32_dll12_lib.zip from the link in the OP (mingw as that what was suggested in your linked thread) and extracted them to the same directory as the example code you provided, but it errors saying lua52.lib cannot be found (obviously, as such a file does not exist in the archive I downloaded). Which file is your example designed to work with? Thanks.

Is there an explanation anywhere as to how all this stuff works? I've attempted to use LUA on a number of different occasions and it seems every download I try has a bunch of different files all named in a different fashion. This may just be my unfamiliarity with the underlying technology talking - I find the whole concept of .lib and .a files somewhat inscrutable, to say the least - .dlls at least make sense to me conceptually, and there only tends to be one version of those! But I'd like to not have to include a LUA .dll with my projects if I don't have to.
User avatar
fiver
User
User
Posts: 36
Joined: Wed May 05, 2004 8:21 pm
Location: An outer spiral arm of the Milky Way

Re: LUA in PureBasic

Post by fiver »

Hello Guidebot,

That's not dumb at all, I tried myself on windows just now and the code as was didn't work (see changes above), for my vista windows box newer binaries were required and also the Visual C++ re-distributable packages for Visual Studio 2013. Even then I couldn't get the lua_tonumberx functions working although I'm pretty sure they did when I used this code a while a go.

When I tested today I had lua52.dll, lua52.lib from http://luabinaries.sourceforge.net/, lua_test.pb and script.lua all in the same directory. As I understand it, which is not particularly well ;-), the lib file is like a header file for the dll. On my linux box I have a fully static library which is included in the binary at compile time, I am fairly sure I was able to compile it myself easily on lInux but that it proved impossible on windows, which is why I was using the static lib linked in my first post. I think the reason it no longer works is because of a compatibility problem between the Pelles C compiler PB uses and whatever the static lib was built with, probably an older version of Visual C++.

Don't give up hope though, I'm sure it's possible to get it working statically on windows, I just found the link below which may help:
https://github.com/stevedonovan/luabuild

Cheers, fiver
User avatar
fiver
User
User
Posts: 36
Joined: Wed May 05, 2004 8:21 pm
Location: An outer spiral arm of the Milky Way

Re: LUA in PureBasic

Post by fiver »

Well, because I'm like a dog with a stick I tried a bit longer with this as it would be useful to get it working on windows.
With the stripped down code below I got closer but still no cigar, I suspect someone with a better knowledge of windows than I could get this going.

I tried with various binaries from http://sourceforge.net/projects/luabina ... les/5.2.3/
the vc6 binaries gave the least errors:

POLINK: error: Unresolved external symbol '_errno'.
POLINK: error: Unresolved external symbol '___mb_cur_max'.
POLINK: error: Unresolved external symbol '__pctype'.
POLINK: error: Unresolved external symbol '__HUGE'.
POLINK: fatal error: 4 unresolved external(s).

I may have another go when I have more time, there's a certain masochistic fun working on these kinds of things :)

Code: Select all

ImportC "vc6\lua52.lib"
  luaL_newstate()
  ;-- Opens all standard Lua libraries into the given state.
  luaL_openlibs (*lua_State)
  luaL_loadstring(*lua_State, *Str)
  lua_pcallk(*lua_State, nargs.i, nresults.i, errfunc.i, ctx.i=0, lua_CFunction.i=0)
  ;-- Converts the Lua value at the given index to a C string.
  lua_tolstring(*lua_State, index.i, *len=0)
EndImport

*lua_State = luaL_newstate()
luaL_openlibs(*lua_State)

luaL_loadstring(*lua_State, @"print('lua version : ',_VERSION,'\n\n')")
lua_pcallk(*lua_State, 0, -1, 0)
RetString$ = PeekS(lua_tolstring(*lua_State, 1))
Debug RetString$
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: LUA in PureBasic

Post by jack »

hello fiver
I just downloaded and tested the examples included in this package http://www.realsource.de/downloads/doc_ ... pb-include
the two included examples worked OK
User avatar
fiver
User
User
Posts: 36
Joined: Wed May 05, 2004 8:21 pm
Location: An outer spiral arm of the Milky Way

Re: LUA in PureBasic

Post by fiver »

Thanks jack, that looks useful, what's really bugging me is that I swear I had it working before although looking at the file dates that was early 2013 so I guess a lot has changed since then :)

I've become interested by lua again having worked on some embedded wifi stuff lately with nodemcu that targets the ESP8266 which is a lot of fun for £2 or so :wink:
Henry00
User
User
Posts: 88
Joined: Thu Jul 12, 2012 7:00 pm
Location: Germany
Contact:

Re: LUA in PureBasic

Post by Henry00 »

This is the lua header I made for a game engine. Just download Lua52.dll and you are ready to go. It may have some "Prt" functions (print) you can remove those and it works fine.

Code: Select all

;==============================================================================
; ** Game_Lua52.pb
;------------------------------------------------------------------------------
;  This module represents a Lua5.2 header to import Lua for game scripting.
;==============================================================================
XIncludeFile #PB_Compiler_File + "i" ;- PBHGEN

;--------------------------------------------------------------------------
Global DLL_LUA.i ; DLL Handle
;--------------------------------------------------------------------------
; * Lua Version Information
;--------------------------------------------------------------------------
#LUA_VERSION_MAJOR    = "5"
#LUA_VERSION_MINOR    = "2"
#LUA_VERSION_NUM      = 502
#LUA_VERSION_RELEASE  = "1"
#LUA_VERSION          = "Lua "+#LUA_VERSION_MAJOR+"."+#LUA_VERSION_MINOR
#LUA_RELEASE          = #LUA_VERSION+"."+#LUA_VERSION_RELEASE
#LUA_COPYRIGHT        = #LUA_RELEASE+"  Copyright (C) 1994-2012 Lua.org, PUC-Rio"
#LUA_AUTHORS          = "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
;--------------------------------------------------------------------------
; * PreCompiled Code
;--------------------------------------------------------------------------
#LUA_SIGNATURE        = "\033Lua"
;--------------------------------------------------------------------------
; * Lua Calling Options
;--------------------------------------------------------------------------
#LUA_MULTRET          = -1  ; Option To Return Multiple Values
;--------------------------------------------------------------------------
; * Lua Threading
;--------------------------------------------------------------------------
#LUA_OK               = 0   ; Thread Status Ok
#LUA_YIELD            = 1   ; Thread Status Yield
#LUA_ERRRUN           = 2   ; Thread Status Runtime Error
#LUA_ERRSYNTAX        = 3   ; Thread Status Syntax Error
#LUA_ERRMEM           = 4   ; Thread Status Memory Allocation Error
#LUA_ERRGCMM          = 5   ; Thread Status Garbage Collector?
#LUA_ERRERR           = 6   ; Thread Status Double Error Attempt to Print
;--------------------------------------------------------------------------
; * Lua Basic Types
;--------------------------------------------------------------------------
#LUA_TNONE            = -1  ; Nothing
#LUA_TNIL             = 0   ; Nil
#LUA_TBOOLEAN         = 1   ; Boolean
#LUA_TLIGHTUSERDATA   = 2   ; Light User Data
#LUA_TNUMBER          = 3   ; Number
#LUA_TSTRING          = 4   ; String
#LUA_TTABLE           = 5   ; Table
#LUA_TFUNCTION        = 6   ; Function
#LUA_TUSERDATA        = 7   ; Heavy User Data
#LUA_TTHREAD          = 8   ; Thread
#LUA_NUMTAGS          = 9   ; Total Types
;--------------------------------------------------------------------------
; * Lua Stack
;--------------------------------------------------------------------------
#LUA_MINSTACK         = 20  ; Minimum Stack Available for a C Function
;--------------------------------------------------------------------------
; * Lua Registry
;--------------------------------------------------------------------------
#LUA_RIDX_MAINTHREAD  = 1   ; Main Thread
#LUA_RIDX_GLOBALS     = 2   ; Global
#LUA_RIDX_LAST        = #LUA_RIDX_GLOBALS
;--------------------------------------------------------------------------
; * Lua Operators
;--------------------------------------------------------------------------
#LUA_OPADD            = 0   ; +
#LUA_OPSUB            = 1   ; -
#LUA_OPMUL            = 2   ; *
#LUA_OPDIV            = 3   ; /
#LUA_OPMOD            = 4   ; %
#LUA_OPPOW            = 5   ; ^
#LUA_OPUNM            = 6   ; -v
#LUA_OPEQ             = 0   ; ==
#LUA_OPLT             = 1   ; <
#LUA_OPLE             = 2   ; <=
;--------------------------------------------------------------------------
; * Lua Garbage Collector
;--------------------------------------------------------------------------
#LUA_GCSTOP           = 0   ; Stop
#LUA_GCRESTART        = 1   ; Restart
#LUA_GCCOLLECT        = 2   ; Collect
#LUA_GCCOUNT          = 3   ; Amount of Memory Kilobytes Used by Lua
#LUA_GCCOUNTB         = 4   ; Remainder of Dividing Bytes of Memory by 1024
#LUA_GCSTEP           = 5   ; Step
#LUA_GCSETPAUSE       = 6   ; Set Pause
#LUA_GCSETSTEPMUL     = 7   ; Set Step Mul
#LUA_GCSETMAJORINC    = 8   ; Set Major Increase
#LUA_GCISRUNNING      = 9   ; Is Running
#LUA_GCGEN            = 10  ; Gen
#LUA_GCINC            = 11  ; Inc
;--------------------------------------------------------------------------
; * Lua Pseudo Indices
;--------------------------------------------------------------------------
#LUA_REGISTRYINDEX    = (-10000)
#LUA_ENVIRONINDEX     = (-10001)
#LUA_GLOBALSINDEX     = (-10002)
;--------------------------------------------------------------------------
; * Lua Event Codes
;--------------------------------------------------------------------------
#LUA_HOOKCALL         = 0
#LUA_HOOKRET          = 1
#LUA_HOOKLINE         = 2
#LUA_HOOKCOUNT        = 3
#LUA_HOOKTAILRET      = 4
;--------------------------------------------------------------------------
; * Lua Event Masks
;--------------------------------------------------------------------------
#LUA_MASKCALL         = 1 << #LUA_HOOKCALL
#LUA_MASKRET          = 1 << #LUA_HOOKRET
#LUA_MASKLINE         = 1 << #LUA_HOOKLINE
#LUA_MASKCOUNT        = 1 << #LUA_HOOKCOUNT
;--------------------------------------------------------------------------
; * Lua Prototypes: State
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_newstate               (*f, *ud)
PrototypeC.l __proto_lua_close                  (lua_State.i)
PrototypeC.l __proto_lua_newthread              (lua_State.i)
PrototypeC.l __proto_lua_atpanic                (lua_State.i, *panicf)
;--------------------------------------------------------------------------
; * Lua Prototypes: Basic Stack Manipulation
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_absindex               (lua_State.i, idx.l)
PrototypeC.l __proto_lua_gettop                 (lua_State.i)
PrototypeC.l __proto_lua_settop                 (lua_State.i, idx.l)
PrototypeC.l __proto_lua_pushvalue              (lua_State.i, idx.l)
PrototypeC.l __proto_lua_remove                 (lua_State.i, idx.l)
PrototypeC.l __proto_lua_insert                 (lua_State.i, idx.l)
PrototypeC.l __proto_lua_replace                (lua_State.i, idx.l)
PrototypeC.l __proto_lua_copy                   (lua_State.i, fromidx.l, toidx.l)
PrototypeC.l __proto_lua_checkstack             (lua_State.i, sz.l)
PrototypeC.l __proto_lua_xmove                  (lua_State_from.i, lua_State_to.i, n.l)
;--------------------------------------------------------------------------
; * Lua Prototypes: Access Functions (Stack->C)
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_isnumber               (lua_State.i, idx.l)
PrototypeC.l __proto_lua_isstring               (lua_State.i, idx.l)
PrototypeC.l __proto_lua_iscfunction            (lua_State.i, idx.l)
PrototypeC.l __proto_lua_isuserdata             (lua_State.i, idx.l)
PrototypeC.l __proto_lua_type                   (lua_State.i, idx.l)
PrototypeC.l __proto_lua_typename               (lua_State.i, tp.l)
PrototypeC.d __proto_lua_tonumberx              (lua_State.i, idx.l, *isnum)
PrototypeC.l __proto_lua_tointegerx             (lua_State.i, idx.l, *isnum)
PrototypeC.l __proto_lua_tounsignedx            (lua_State.i, idx.l, *isnum)
PrototypeC.l __proto_lua_toboolean              (lua_State.i, idx.l)
PrototypeC.l __proto_lua_tolstring              (lua_State.i, idx.l, *len)
PrototypeC.l __proto_lua_rawlen                 (lua_State.i, idx.l)
PrototypeC.l __proto_lua_tocfunction            (lua_State.i, idx.l)
PrototypeC.l __proto_lua_touserdata             (lua_State.i, idx.l)
PrototypeC.l __proto_lua_tothread               (lua_State.i, idx.l)
PrototypeC.l __proto_lua_topointer              (lua_State.i, idx.l)
;--------------------------------------------------------------------------
; * Lua Prototypes: Comparison And Arithmetic
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_arith                  (lua_State.i, op.l)
PrototypeC.l __proto_lua_rawequal               (lua_State.i, idx1.l, idx2.l)
PrototypeC.l __proto_lua_compare                (lua_State.i, idx1.l, idx2.l, op.l)
;--------------------------------------------------------------------------
; * Lua Prototypes: Push Functions (C->Stack)
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_pushnil                (lua_State.i)
PrototypeC.l __proto_lua_pushnumber             (lua_State.i, n.d)
PrototypeC.l __proto_lua_pushinteger            (lua_State.i, n.q)
PrototypeC.l __proto_lua_pushunsigned           (lua_State.i, n.q)
PrototypeC.l __proto_lua_pushlstring            (lua_State.i, string.p-ascii, size.i)
PrototypeC.l __proto_lua_pushstring             (lua_State.i, string.p-ascii)
PrototypeC.l __proto_lua_pushcclosure           (lua_State.i, *fn, n.l)
PrototypeC.l __proto_lua_pushboolean            (lua_State.i, b.l)
PrototypeC.l __proto_lua_pushlightuserdata      (lua_State.i, *p)
PrototypeC.l __proto_lua_pushthread             (lua_State.i)
;--------------------------------------------------------------------------
; * Lua Prototypes: Get Functions (Lua->Stack)
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_getglobal              (lua_State.i, *string_var) ; .p-ascii causes memory leak
PrototypeC.l __proto_lua_gettable               (lua_State.i, idx.l)
PrototypeC.l __proto_lua_getfield               (lua_State.i, idx.l, k.p-ascii)
PrototypeC.l __proto_lua_rawget                 (lua_State.i, idx.l)
PrototypeC.l __proto_lua_rawgeti                (lua_State.i, idx.l, n.l)
PrototypeC.l __proto_lua_rawgetp                (lua_State.i, idx.l, p.p-ascii)
PrototypeC.l __proto_lua_createtable            (lua_State.i, narr.l, nrec.l)
PrototypeC.l __proto_lua_newuserdata            (lua_State.i, sz.i)
PrototypeC.l __proto_lua_getmetatable           (lua_State.i, objindex.l)
PrototypeC.l __proto_lua_getuservalue           (lua_State.i, idx.l)
;--------------------------------------------------------------------------
; * Lua Prototypes: Set Functions (Lua->Stack)
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_setglobal              (lua_State.i, var.p-ascii)
PrototypeC.l __proto_lua_settable               (lua_State.i, idx.l)
PrototypeC.l __proto_lua_setfield               (lua_State.i, idx.l, k.p-ascii)
PrototypeC.l __proto_lua_rawset                 (lua_State.i, idx.l)
PrototypeC.l __proto_lua_rawseti                (lua_State.i, idx.l, n.l)
PrototypeC.l __proto_lua_rawsetp                (lua_State.i, idx.l, p.p-ascii)
PrototypeC.l __proto_lua_setmetatable           (lua_State.i, objindex.l)
PrototypeC.l __proto_lua_setuservalue           (lua_State.i, idx.l)
;--------------------------------------------------------------------------
; * Lua Prototypes: Load, Call
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_callk                  (lua_State.i, nargs.l, nresults.l, ctx.l, *k)
PrototypeC.l __proto_lua_getctx                 (lua_State.i, *ctx)
PrototypeC.l __proto_lua_pcallk                 (lua_State.i, nargs.l, nresults.l, *errfunc, ctx.l, *k)
PrototypeC.l __proto_lua_load                   (lua_State.i, reader.i, *dt, chunkname.p-ascii, mode.p-ascii)
PrototypeC.l __proto_lua_dump                   (lua_State.i, writer.i, *data_)
;--------------------------------------------------------------------------
; * Lua Prototypes: Coroutine Functions
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_yieldk                 (lua_State.i, nresults.l, ctx.l, *k)
PrototypeC.l __proto_lua_resume                 (lua_State.i, lua_State_from.i, narg.l)
PrototypeC.l __proto_lua_status                 (lua_State.i)
;--------------------------------------------------------------------------
; * Lua Prototypes: Garbage Collection
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_gc                     (lua_State.i, what.l, data_.i)
;--------------------------------------------------------------------------
; * Lua Prototypes: Miscellaneous Functions
;--------------------------------------------------------------------------
PrototypeC.l __proto_lua_error                  (lua_State.i)
PrototypeC.l __proto_lua_next                   (lua_State.i, idx.l)
PrototypeC.l __proto_lua_concat                 (lua_State.i, n.l)
PrototypeC.l __proto_lua_len                    (lua_State.i, idx.l)
PrototypeC.l __proto_lua_getallocf              (lua_State.i, *ud)
PrototypeC.l __proto_lua_setallocf              (lua_State.i, *f, *ud)
;--------------------------------------------------------------------------
PrototypeC.l __proto_luaopen_base               (lua_State.i)
PrototypeC.l __proto_luaopen_coroutine          (lua_State.i)
PrototypeC.l __proto_luaopen_table              (lua_State.i)
PrototypeC.l __proto_luaopen_io                 (lua_State.i)
PrototypeC.l __proto_luaopen_os                 (lua_State.i)
PrototypeC.l __proto_luaopen_string             (lua_State.i)
PrototypeC.l __proto_luaopen_bit32              (lua_State.i)
PrototypeC.l __proto_luaopen_math               (lua_State.i)
PrototypeC.l __proto_luaopen_debug              (lua_State.i)
PrototypeC.l __proto_luaopen_package            (lua_State.i)
;--------------------------------------------------------------------------
; * Lua Prototypes: Macro Functions
;--------------------------------------------------------------------------
PrototypeC.l __proto_luaL_openlibs              (lua_State.i)
PrototypeC.l __proto_luaL_openlib               (lua_State.i, libname.p-ascii, *luaL_Reg, nup.l)
PrototypeC.l __proto_luaL_register              (lua_State.i, libname.p-ascii, *luaL_Reg)
PrototypeC.l __proto_luaL_getmetafield          (lua_State.i, obj.l, e.p-ascii)
PrototypeC.l __proto_luaL_callmeta              (lua_State.i, obj.l, e.p-ascii)
PrototypeC.l __proto_luaL_typerror              (lua_State.i, narg.l, tname.p-ascii)
PrototypeC.l __proto_luaL_argerror              (lua_State.i, numarg.l, extramsg.p-ascii)
PrototypeC.l __proto_luaL_checklstring          (lua_State.i, numarg.l, size.l)
PrototypeC.l __proto_luaL_optlstring            (lua_State.i, numarg.l, def.p-ascii, size.l)
PrototypeC.l __proto_luaL_checknumber           (lua_State.i, numarg.l)
PrototypeC.l __proto_luaL_optnumber             (lua_State.i, narg, LUA_NUMBER.d)
PrototypeC.l __proto_luaL_checkinteger          (lua_State.i, numarg.l)
PrototypeC.l __proto_luaL_optinteger            (lua_State.i, narg.l, LUA_INTEGER.l)
PrototypeC.l __proto_luaL_checkstack            (lua_State.i, sz.l, msg.p-ascii)
PrototypeC.l __proto_luaL_checktype             (lua_State.i, narg.l, t.l)
PrototypeC.l __proto_luaL_checkany              (lua_State.i, narg.l)
PrototypeC.l __proto_luaL_newmetatable          (lua_State.i, tname.p-ascii)
PrototypeC.l __proto_luaL_checkudata            (lua_State.i, ud.l, tname.p-ascii)
PrototypeC.l __proto_luaL_where                 (lua_State.i, lvl.l)
PrototypeC.l __proto_luaL_checkoption           (lua_State.i, narg.l, def.p-ascii, *string_array)
PrototypeC.l __proto_luaL_ref                   (lua_State.i, t.l)
PrototypeC.l __proto_luaL_unref                 (lua_State.i, t.l, ref.l)
PrototypeC.l __proto_luaL_loadfilex             (lua_State.i, filename.p-ascii, *mode)
PrototypeC.l __proto_luaL_loadbuffer            (lua_State.i, buff.l, size.l, name.p-ascii)
PrototypeC.l __proto_luaL_loadstring            (lua_State.i, string.p-ascii)
;luaL_loadfile         (lua_State.i, filename.p-ascii)
PrototypeC.l __proto_luaL_gsub                  (lua_State.i, s.p-ascii, p.p-ascii, r.p-ascii)
PrototypeC.l __proto_luaL_newstate              ()
PrototypeC.l __proto_luaL_findtable             (lua_State.i, Index.l, fname.p-ascii)
PrototypeC.l __proto_luaL_buffinit              (lua_State.i, *luaL_Buffer)
PrototypeC.l __proto_luaL_prepbuffer            (*luaL_Buffer)
PrototypeC.l __proto_luaL_addlstring            (*luaL_Buffer, s.p-ascii, size.l)
PrototypeC.l __proto_luaL_addstring             (*luaL_Buffer, s.p-ascii)
PrototypeC.l __proto_luaL_addvalue              (*luaL_Buffer)
PrototypeC.l __proto_luaL_pushresult            (*luaL_Buffer)
;--------------------------------------------------------------------------
; * Initialize and Import Lua Library
;--------------------------------------------------------------------------
Procedure Lua_Initialize()
  Prt(Vocab\Lua\InfoInitializing$)
  DLL_LUA = OpenLibrary(#PB_Any, "Lua52.dll")
  ;--------------------------------------------------------------------------
  ; * Lua Library: State
  ;--------------------------------------------------------------------------
  Global lua_newstate.__proto_lua_newstate                   = GetFunction(DLL_LUA, "lua_newstate")
  Global lua_close.__proto_lua_close                         = GetFunction(DLL_LUA, "lua_close")
  Global lua_newthread.__proto_lua_newthread                 = GetFunction(DLL_LUA, "lua_newthread")
  Global lua_atpanic.__proto_lua_atpanic                     = GetFunction(DLL_LUA, "lua_atpanic")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Basic Stack Manipulation
  ;--------------------------------------------------------------------------
  Global lua_absindex.__proto_lua_absindex                   = GetFunction(DLL_LUA, "lua_absindex")
  Global lua_gettop.__proto_lua_gettop                       = GetFunction(DLL_LUA, "lua_gettop")
  Global lua_settop.__proto_lua_settop                       = GetFunction(DLL_LUA, "lua_settop")
  Global lua_pushvalue.__proto_lua_pushvalue                 = GetFunction(DLL_LUA, "lua_pushvalue")
  Global lua_remove.__proto_lua_remove                       = GetFunction(DLL_LUA, "lua_remove")
  Global lua_insert.__proto_lua_insert                       = GetFunction(DLL_LUA, "lua_insert")
  Global lua_replace.__proto_lua_replace                     = GetFunction(DLL_LUA, "lua_replace")
  Global lua_copy.__proto_lua_copy                           = GetFunction(DLL_LUA, "lua_copy")
  Global lua_checkstack.__proto_lua_checkstack               = GetFunction(DLL_LUA, "lua_checkstack")
  Global lua_xmove.__proto_lua_xmove                         = GetFunction(DLL_LUA, "lua_xmove")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Access Functions (Stack->C)
  ;--------------------------------------------------------------------------
  Global lua_isnumber.__proto_lua_isnumber                   = GetFunction(DLL_LUA, "lua_isnumber")
  Global lua_isstring.__proto_lua_isstring                   = GetFunction(DLL_LUA, "lua_isstring")
  Global lua_iscfunction.__proto_lua_iscfunction             = GetFunction(DLL_LUA, "lua_iscfunction")
  Global lua_isuserdata.__proto_lua_isuserdata               = GetFunction(DLL_LUA, "lua_isuserdata")
  Global lua_type.__proto_lua_type                           = GetFunction(DLL_LUA, "lua_type")
  Global lua_typename.__proto_lua_typename                   = GetFunction(DLL_LUA, "lua_typename")
  Global lua_tonumberx.__proto_lua_tonumberx                 = GetFunction(DLL_LUA, "lua_tonumberx")
  Global lua_tointegerx.__proto_lua_tointegerx               = GetFunction(DLL_LUA, "lua_tointegerx")
  Global lua_tounsignedx.__proto_lua_tounsignedx             = GetFunction(DLL_LUA, "lua_tounsignedx")
  Global lua_toboolean.__proto_lua_toboolean                 = GetFunction(DLL_LUA, "lua_toboolean")
  Global lua_tolstring.__proto_lua_tolstring                 = GetFunction(DLL_LUA, "lua_tolstring")
  Global lua_rawlen.__proto_lua_rawlen                       = GetFunction(DLL_LUA, "lua_rawlen")
  Global lua_tocfunction.__proto_lua_tocfunction             = GetFunction(DLL_LUA, "lua_tocfunction")
  Global lua_touserdata.__proto_lua_touserdata               = GetFunction(DLL_LUA, "lua_touserdata")
  Global lua_tothread.__proto_lua_tothread                   = GetFunction(DLL_LUA, "lua_tothread")
  Global lua_topointer.__proto_lua_topointer                 = GetFunction(DLL_LUA, "lua_topointer")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Comparison And Arithmetic
  ;--------------------------------------------------------------------------
  Global lua_arith.__proto_lua_arith                         = GetFunction(DLL_LUA, "lua_arith")
  Global lua_rawequal.__proto_lua_rawequal                   = GetFunction(DLL_LUA, "lua_rawequal")
  Global lua_compare.__proto_lua_compare                     = GetFunction(DLL_LUA, "lua_compare")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Push Functions (C->Stack)
  ;--------------------------------------------------------------------------
  Global lua_pushnil.__proto_lua_pushnil                     = GetFunction(DLL_LUA, "lua_pushnil")
  Global lua_pushnumber.__proto_lua_pushnumber               = GetFunction(DLL_LUA, "lua_pushnumber")
  Global lua_pushinteger.__proto_lua_pushinteger             = GetFunction(DLL_LUA, "lua_pushinteger")
  Global lua_pushunsigned.__proto_lua_pushunsigned           = GetFunction(DLL_LUA, "lua_pushunsigned")
  Global lua_pushlstring.__proto_lua_pushlstring             = GetFunction(DLL_LUA, "lua_pushlstring")
  Global lua_pushstring.__proto_lua_pushstring               = GetFunction(DLL_LUA, "lua_pushstring")
  Global lua_pushcclosure.__proto_lua_pushcclosure           = GetFunction(DLL_LUA, "lua_pushcclosure")
  Global lua_pushboolean.__proto_lua_pushboolean             = GetFunction(DLL_LUA, "lua_pushboolean")
  Global lua_pushlightuserdata.__proto_lua_pushlightuserdata = GetFunction(DLL_LUA, "lua_pushlightuserdata")
  Global lua_pushthread.__proto_lua_pushthread               = GetFunction(DLL_LUA, "lua_pushthread")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Get Functions (Lua->Stack)
  ;--------------------------------------------------------------------------
  Global lua_getglobal.__proto_lua_getglobal                 = GetFunction(DLL_LUA, "lua_getglobal")
  Global lua_gettable.__proto_lua_gettable                   = GetFunction(DLL_LUA, "lua_gettable")
  Global lua_getfield.__proto_lua_getfield                   = GetFunction(DLL_LUA, "lua_getfield")
  Global lua_rawget.__proto_lua_rawget                       = GetFunction(DLL_LUA, "lua_rawget")
  Global lua_rawgeti.__proto_lua_rawgeti                     = GetFunction(DLL_LUA, "lua_rawgeti")
  Global lua_rawgetp.__proto_lua_rawgetp                     = GetFunction(DLL_LUA, "lua_rawgetp")
  Global lua_createtable.__proto_lua_createtable             = GetFunction(DLL_LUA, "lua_createtable")
  Global lua_newuserdata.__proto_lua_newuserdata             = GetFunction(DLL_LUA, "lua_newuserdata")
  Global lua_getmetatable.__proto_lua_getmetatable           = GetFunction(DLL_LUA, "lua_getmetatable")
  Global lua_getuservalue.__proto_lua_getuservalue           = GetFunction(DLL_LUA, "lua_getuservalue")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Set Functions (Lua->Stack)
  ;--------------------------------------------------------------------------
  Global lua_setglobal.__proto_lua_setglobal                 = GetFunction(DLL_LUA, "lua_setglobal")
  Global lua_settable.__proto_lua_settable                   = GetFunction(DLL_LUA, "lua_settable")
  Global lua_setfield.__proto_lua_setfield                   = GetFunction(DLL_LUA, "lua_setfield")
  Global lua_rawset.__proto_lua_rawset                       = GetFunction(DLL_LUA, "lua_rawset")
  Global lua_rawseti.__proto_lua_rawseti                     = GetFunction(DLL_LUA, "lua_rawseti")
  Global lua_rawsetp.__proto_lua_rawsetp                     = GetFunction(DLL_LUA, "lua_rawsetp")
  Global lua_setmetatable.__proto_lua_setmetatable           = GetFunction(DLL_LUA, "lua_setmetatable")
  Global lua_setuservalue.__proto_lua_setuservalue           = GetFunction(DLL_LUA, "lua_setuservalue")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Load, Call
  ;--------------------------------------------------------------------------
  Global lua_callk.__proto_lua_callk                         = GetFunction(DLL_LUA, "lua_callk")
  Global lua_getctx.__proto_lua_getctx                       = GetFunction(DLL_LUA, "lua_getctx")
  Global lua_pcallk.__proto_lua_pcallk                       = GetFunction(DLL_LUA, "lua_pcallk")
  Global lua_load.__proto_lua_load                           = GetFunction(DLL_LUA, "lua_load")
  Global lua_dump.__proto_lua_dump                           = GetFunction(DLL_LUA, "lua_dump")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Coroutine Functions
  ;--------------------------------------------------------------------------
  Global lua_yieldk.__proto_lua_yieldk                       = GetFunction(DLL_LUA, "lua_yieldk")
  Global lua_resume.__proto_lua_resume                       = GetFunction(DLL_LUA, "lua_resume")
  Global lua_status.__proto_lua_status                       = GetFunction(DLL_LUA, "lua_status")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Garbage Collection
  ;--------------------------------------------------------------------------
  Global lua_gc.__proto_lua_gc                               = GetFunction(DLL_LUA, "lua_gc")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Miscellaneous Functions
  ;--------------------------------------------------------------------------
  Global lua_error.__proto_lua_error                         = GetFunction(DLL_LUA, "lua_error")
  Global lua_next.__proto_lua_next                           = GetFunction(DLL_LUA, "lua_next")
  Global lua_concat.__proto_lua_concat                       = GetFunction(DLL_LUA, "lua_concat")
  Global lua_len.__proto_lua_len                             = GetFunction(DLL_LUA, "lua_len")
  Global lua_getallocf.__proto_lua_getallocf                 = GetFunction(DLL_LUA, "lua_getallocf")
  Global lua_setallocf.__proto_lua_setallocf                 = GetFunction(DLL_LUA, "lua_setallocf")
  ;--------------------------------------------------------------------------
  Global luaopen_base.__proto_luaopen_base                   = GetFunction(DLL_LUA, "luaopen_base")
  Global luaopen_coroutine.__proto_luaopen_coroutine         = GetFunction(DLL_LUA, "luaopen_coroutine")
  Global luaopen_table.__proto_luaopen_table                 = GetFunction(DLL_LUA, "luaopen_table")
  Global luaopen_io.__proto_luaopen_io                       = GetFunction(DLL_LUA, "luaopen_io")
  Global luaopen_os.__proto_luaopen_os                       = GetFunction(DLL_LUA, "luaopen_os")
  Global luaopen_string.__proto_luaopen_string               = GetFunction(DLL_LUA, "luaopen_string")
  Global luaopen_bit32.__proto_luaopen_bit32                 = GetFunction(DLL_LUA, "luaopen_bit32")
  Global luaopen_math.__proto_luaopen_math                   = GetFunction(DLL_LUA, "luaopen_math")
  Global luaopen_debug.__proto_luaopen_debug                 = GetFunction(DLL_LUA, "luaopen_debug")
  Global luaopen_package.__proto_luaopen_package             = GetFunction(DLL_LUA, "luaopen_package")
  ;--------------------------------------------------------------------------
  ; * Lua Library: Macro Functions
  ;--------------------------------------------------------------------------
  Global luaL_openlibs.__proto_luaL_openlibs                 = GetFunction(DLL_LUA, "luaL_openlibs")
  Global luaL_openlib.__proto_luaL_openlib                   = GetFunction(DLL_LUA, "luaL_openlib")
  Global luaL_register.__proto_luaL_register                 = GetFunction(DLL_LUA, "luaL_register")
  Global luaL_getmetafield.__proto_luaL_getmetafield         = GetFunction(DLL_LUA, "luaL_getmetafield")
  Global luaL_callmeta.__proto_luaL_callmeta                 = GetFunction(DLL_LUA, "luaL_callmeta")
  Global luaL_typerror.__proto_luaL_typerror                 = GetFunction(DLL_LUA, "luaL_typerror")
  Global luaL_argerror.__proto_luaL_argerror                 = GetFunction(DLL_LUA, "luaL_argerror")
  Global luaL_checklstring.__proto_luaL_checklstring         = GetFunction(DLL_LUA, "luaL_checklstring")
  Global luaL_optlstring.__proto_luaL_optlstring             = GetFunction(DLL_LUA, "luaL_optlstring")
  Global luaL_checknumber.__proto_luaL_checknumber           = GetFunction(DLL_LUA, "luaL_checknumber")
  Global luaL_optnumber.__proto_luaL_optnumber               = GetFunction(DLL_LUA, "luaL_optnumber")
  Global luaL_checkinteger.__proto_luaL_checkinteger         = GetFunction(DLL_LUA, "luaL_checkinteger")
  Global luaL_optinteger.__proto_luaL_optinteger             = GetFunction(DLL_LUA, "luaL_optinteger")
  Global luaL_checkstack.__proto_luaL_checkstack             = GetFunction(DLL_LUA, "luaL_checkstack")
  Global luaL_checktype.__proto_luaL_checktype               = GetFunction(DLL_LUA, "luaL_checktype")
  Global luaL_checkany.__proto_luaL_checkany                 = GetFunction(DLL_LUA, "luaL_checkany")
  Global luaL_newmetatable.__proto_luaL_newmetatable         = GetFunction(DLL_LUA, "luaL_newmetatable")
  Global luaL_checkudata.__proto_luaL_checkudata             = GetFunction(DLL_LUA, "luaL_checkudata")
  Global luaL_where.__proto_luaL_where                       = GetFunction(DLL_LUA, "luaL_where")
  Global luaL_checkoption.__proto_luaL_checkoption           = GetFunction(DLL_LUA, "luaL_checkoption")
  Global luaL_ref.__proto_luaL_ref                           = GetFunction(DLL_LUA, "luaL_ref")
  Global luaL_unref.__proto_luaL_unref                       = GetFunction(DLL_LUA, "luaL_unref")
  Global luaL_loadfilex.__proto_luaL_loadfilex               = GetFunction(DLL_LUA, "luaL_loadfilex")
  Global luaL_loadbuffer.__proto_luaL_loadbuffer             = GetFunction(DLL_LUA, "luaL_loadbuffer")
  Global luaL_loadstring.__proto_luaL_loadstring             = GetFunction(DLL_LUA, "luaL_loadstring")
  Global luaL_gsub.__proto_luaL_gsub                         = GetFunction(DLL_LUA, "luaL_gsub")
  Global luaL_newstate.__proto_luaL_newstate                 = GetFunction(DLL_LUA, "luaL_newstate")
  Global luaL_findtable.__proto_luaL_findtable               = GetFunction(DLL_LUA, "luaL_findtable")
  Global luaL_buffinit.__proto_luaL_buffinit                 = GetFunction(DLL_LUA, "luaL_buffinit")
  Global luaL_prepbuffer.__proto_luaL_prepbuffer             = GetFunction(DLL_LUA, "luaL_prepbuffer")
  Global luaL_addlstring.__proto_luaL_addlstring             = GetFunction(DLL_LUA, "luaL_addlstring")
  Global luaL_addstring.__proto_luaL_addstring               = GetFunction(DLL_LUA, "luaL_addstring")
  Global luaL_addvalue.__proto_luaL_addvalue                 = GetFunction(DLL_LUA, "luaL_addvalue")
  Global luaL_pushresult.__proto_luaL_pushresult             = GetFunction(DLL_LUA, "luaL_pushresult")
EndProcedure
;--------------------------------------------------------------------------
; * Dispose Lua Library
;--------------------------------------------------------------------------
Procedure Lua_Dispose()
  Prt(Vocab\Lua\InfoDisposing$)
  CloseLibrary(DLL_LUA)
EndProcedure
;--------------------------------------------------------------------------
; * IDX To String
;--------------------------------------------------------------------------
Procedure.s lua_tostring(Lua_State, idx)
  Protected *Temp_String = lua_tolstring(Lua_State, idx, #Null)
  If *Temp_String : Protected Result_String$ = PeekS(*Temp_String, -1, #PB_Ascii) : Else : Result_String$ = "" : EndIf
  ProcedureReturn Result_String$
EndProcedure

Macro lua_call(L, n, r)
  lua_callk((L), (n), (r), 0, #Null)
EndMacro

Macro lua_pcall(L, n, r, f)
  lua_pcallk((L), (n), (r), (f), 0, #Null)
EndMacro

Macro lua_yield(L, n)
  lua_yieldk((L), (n), 0, #Null)
EndMacro

Procedure.d lua_tonumber(L, i)
  Protected Value.d = lua_tonumberx((L), (i), #Null)
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows And #PB_Compiler_Processor = #PB_Processor_x64
    EnableASM
    MOVQ Value, XMM0
    DisableASM
  CompilerEndIf
  ProcedureReturn Value
EndProcedure

Macro lua_tointeger(L, i)
  lua_tointegerx((L), (i), #Null)
EndMacro

Macro lua_tounsigned(L, i)
  lua_tounsignedx((L), (i), #Null)
EndMacro

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_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 luaL_dofile(Lua_State, Filename)
	luaL_loadfile(Lua_State, Filename)
	lua_pcall(Lua_State, 0, #LUA_MULTRET, 0)
EndMacro

Macro luaL_dostring(Lua_State, String)
	luaL_loadstring(Lua_State, String)
	lua_pcall(Lua_State, 0, #LUA_MULTRET, 0)
EndMacro

Macro luaL_loadfile(L, f)
  luaL_loadfilex((L),f,#Null)
EndMacro

Procedure lua_getglobal_fixed(L, String$) ; Because of a memory leak with .p-ascii
  Protected *String_Buffer = AllocateMemory(StringByteLength(String$, #PB_Ascii)+1)
  If *String_Buffer
    PokeS(*String_Buffer, String$, -1, #PB_Ascii)
  EndIf
  lua_getglobal(L, *String_Buffer)
  FreeMemory(*String_Buffer)
EndProcedure
Post Reply