1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
#*****************************************************************************
# * *
# * File: lauxlib.pas *
# * Authors: TeCGraf (C headers + actual Lua libraries) *
# * Lavergne Thomas (original translation to Pascal) *
# * Bram Kuijvenhoven (update to Lua 5.1.1 for FreePascal) *
# * Description: Lua auxiliary library *
# * *
# *****************************************************************************
#
#** $Id: lauxlib.h,v 1.59 2003/03/18 12:25:32 roberto Exp $
#** Auxiliary functions for building Lua libraries
#** See Copyright Notice in lua.h
#
#
#** Translated to pascal by Lavergne Thomas
#** Notes :
#** - Pointers type was prefixed with 'P'
#** Bug reports :
#** - thomas.lavergne@laposte.net
#** In french or in english
#
import lua
proc lua_pushstring*(L: Plua_State, s: string)
# compatibilty macros
proc luaL_getn*(L: Plua_State, n: int): int
# calls lua_objlen
proc luaL_setn*(L: Plua_State, t, n: int)
# does nothing!
type
TLuaL_reg*{.final.} = object
name*: cstring
func*: lua_CFunction
PluaL_reg* = ptr TLuaL_reg
proc luaL_openlib*(L: Plua_State, libname: cstring, lr: PluaL_reg, nup: int){.
cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_register*(L: Plua_State, libname: cstring, lr: PluaL_reg){.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_getmetafield*(L: Plua_State, obj: int, e: cstring): int{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_callmeta*(L: Plua_State, obj: int, e: cstring): int{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_typerror*(L: Plua_State, narg: int, tname: cstring): int{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_argerror*(L: Plua_State, numarg: int, extramsg: cstring): int{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_checklstring*(L: Plua_State, numArg: int, l_: Psize_t): cstring{.
cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_optlstring*(L: Plua_State, numArg: int, def: cstring, l_: Psize_t): cstring{.
cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_checknumber*(L: Plua_State, numArg: int): lua_Number{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_optnumber*(L: Plua_State, nArg: int, def: lua_Number): lua_Number{.
cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_checkinteger*(L: Plua_State, numArg: int): lua_Integer{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_optinteger*(L: Plua_State, nArg: int, def: lua_Integer): lua_Integer{.
cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_checkstack*(L: Plua_State, sz: int, msg: cstring){.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_checktype*(L: Plua_State, narg, t: int){.cdecl, dynlib: LUA_LIB_NAME,
importc.}
proc luaL_checkany*(L: Plua_State, narg: int){.cdecl, dynlib: LUA_LIB_NAME,
importc.}
proc luaL_newmetatable*(L: Plua_State, tname: cstring): int{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_checkudata*(L: Plua_State, ud: int, tname: cstring): Pointer{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_where*(L: Plua_State, lvl: int){.cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_error*(L: Plua_State, fmt: cstring): int{.cdecl, varargs,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_checkoption*(L: Plua_State, narg: int, def: cstring, lst: cstringArray): int{.
cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_ref*(L: Plua_State, t: int): int{.cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_unref*(L: Plua_State, t, theref: int){.cdecl, dynlib: LUA_LIB_NAME,
importc.}
proc luaL_loadfile*(L: Plua_State, filename: cstring): int{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_loadbuffer*(L: Plua_State, buff: cstring, size: size_t, name: cstring): int{.
cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_loadstring*(L: Plua_State, s: cstring): int{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_newstate*(): Plua_State{.cdecl, dynlib: LUA_LIB_NAME, importc.}
proc lua_open*(): Plua_State
# compatibility; moved from unit lua to lauxlib because it needs luaL_newstate
#
#** ===============================================================
#** some useful macros
#** ===============================================================
#
proc luaL_argcheck*(L: Plua_State, cond: bool, numarg: int, extramsg: cstring)
proc luaL_checkstring*(L: Plua_State, n: int): cstring
proc luaL_optstring*(L: Plua_State, n: int, d: cstring): cstring
proc luaL_checkint*(L: Plua_State, n: int): int
proc luaL_checklong*(L: Plua_State, n: int): int32
proc luaL_optint*(L: Plua_State, n: int, d: float64): int
proc luaL_optlong*(L: Plua_State, n: int, d: float64): int32
proc luaL_typename*(L: Plua_State, i: int): cstring
proc lua_dofile*(L: Plua_State, filename: cstring): int
proc lua_dostring*(L: Plua_State, str: cstring): int
proc lua_Lgetmetatable*(L: Plua_State, tname: cstring)
# not translated:
# #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
#
#** =======================================================
#** Generic Buffer manipulation
#** =======================================================
#
const # note: this is just arbitrary, as it related to the BUFSIZ defined in stdio.h ...
LUAL_BUFFERSIZE* = 4096
type
luaL_Buffer*{.final.} = object
p*: cstring # current position in buffer
lvl*: int # number of strings in the stack (level)
L*: Plua_State
buffer*: array[0..LUAL_BUFFERSIZE - 1, Char] # warning: see note above about LUAL_BUFFERSIZE
PluaL_Buffer* = ptr luaL_Buffer
proc luaL_addchar*(B: PluaL_Buffer, c: Char)
# warning: see note above about LUAL_BUFFERSIZE
# compatibility only (alias for luaL_addchar)
proc luaL_putchar*(B: PluaL_Buffer, c: Char)
# warning: see note above about LUAL_BUFFERSIZE
proc luaL_addsize*(B: PluaL_Buffer, n: int)
proc luaL_buffinit*(L: Plua_State, B: PluaL_Buffer){.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_prepbuffer*(B: PluaL_Buffer): cstring{.cdecl, dynlib: LUA_LIB_NAME,
importc.}
proc luaL_addlstring*(B: PluaL_Buffer, s: cstring, L: size_t){.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_addstring*(B: PluaL_Buffer, s: cstring){.cdecl, dynlib: LUA_LIB_NAME,
importc.}
proc luaL_addvalue*(B: PluaL_Buffer){.cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_pushresult*(B: PluaL_Buffer){.cdecl, dynlib: LUA_LIB_NAME, importc.}
proc luaL_gsub*(L: Plua_State, s, p, r: cstring): cstring{.cdecl,
dynlib: LUA_LIB_NAME, importc.}
proc luaL_findtable*(L: Plua_State, idx: int, fname: cstring, szhint: int): cstring{.
cdecl, dynlib: LUA_LIB_NAME, importc.}
# compatibility with ref system
# pre-defined references
const
LUA_NOREF* = - 2
LUA_REFNIL* = - 1
proc lua_unref*(L: Plua_State, theref: int)
proc lua_getref*(L: Plua_State, theref: int)
#
#** Compatibility macros and functions
#
# implementation
proc lua_pushstring(L: Plua_State, s: string) =
lua_pushlstring(L, cstring(s), len(s))
proc luaL_getn(L: Plua_State, n: int): int =
Result = lua_objlen(L, n)
proc luaL_setn(L: Plua_State, t, n: int) =
# does nothing as this operation is deprecated
nil
proc lua_open(): Plua_State =
Result = luaL_newstate()
proc luaL_typename(L: Plua_State, i: int): cstring =
Result = lua_typename(L, lua_type(L, i))
proc lua_dofile(L: Plua_State, filename: cstring): int =
Result = luaL_loadfile(L, filename)
if Result == 0: Result = lua_pcall(L, 0, LUA_MULTRET, 0)
proc lua_dostring(L: Plua_State, str: cstring): int =
Result = luaL_loadstring(L, str)
if Result == 0: Result = lua_pcall(L, 0, LUA_MULTRET, 0)
proc lua_Lgetmetatable(L: Plua_State, tname: cstring) =
lua_getfield(L, LUA_REGISTRYINDEX, tname)
proc luaL_argcheck(L: Plua_State, cond: bool, numarg: int, extramsg: cstring) =
if not cond:
discard luaL_argerror(L, numarg, extramsg)
proc luaL_checkstring(L: Plua_State, n: int): cstring =
Result = luaL_checklstring(L, n, nil)
proc luaL_optstring(L: Plua_State, n: int, d: cstring): cstring =
Result = luaL_optlstring(L, n, d, nil)
proc luaL_checkint(L: Plua_State, n: int): int =
Result = toInt(luaL_checknumber(L, n))
proc luaL_checklong(L: Plua_State, n: int): int32 =
Result = int32(ToInt(luaL_checknumber(L, n)))
proc luaL_optint(L: Plua_State, n: int, d: float64): int =
Result = int(ToInt(luaL_optnumber(L, n, d)))
proc luaL_optlong(L: Plua_State, n: int, d: float64): int32 =
Result = int32(ToInt(luaL_optnumber(L, n, d)))
proc luaL_addchar(B: PluaL_Buffer, c: Char) =
if cast[int](addr((B.p))) < (cast[int](addr((B.buffer[0]))) + LUAL_BUFFERSIZE):
discard luaL_prepbuffer(B)
B.p[1] = c
B.p = cast[cstring](cast[int](B.p) + 1)
proc luaL_putchar(B: PluaL_Buffer, c: Char) =
luaL_addchar(B, c)
proc luaL_addsize(B: PluaL_Buffer, n: int) =
B.p = cast[cstring](cast[int](B.p) + n)
proc lua_unref(L: Plua_State, theref: int) =
luaL_unref(L, LUA_REGISTRYINDEX, theref)
proc lua_getref(L: Plua_State, theref: int) =
lua_rawgeti(L, LUA_REGISTRYINDEX, theref)
|