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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
(******************************************************************************
* *
* 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
*)
{$IFDEF FPC}{$MODE OBJFPC}{$H+}{$ENDIF}
unit lauxlib;
interface
uses
Lua;
// functions added for Pascal
procedure lua_pushstring(L: Plua_State; const s: string);
// compatibilty macros
function luaL_getn(L: Plua_State; n: Integer): Integer; // calls lua_objlen
procedure luaL_setn(L: Plua_State; t, n: Integer); // does nothing!
type
luaL_reg = record
name: PChar;
func: lua_CFunction;
end;
PluaL_reg = ^luaL_reg;
procedure luaL_openlib(L: Plua_State; const libname: PChar; const lr: PluaL_reg; nup: Integer); cdecl;
procedure luaL_register(L: Plua_State; const libname: PChar; const lr: PluaL_reg); cdecl;
function luaL_getmetafield(L: Plua_State; obj: Integer; const e: PChar): Integer; cdecl;
function luaL_callmeta(L: Plua_State; obj: Integer; const e: PChar): Integer; cdecl;
function luaL_typerror(L: Plua_State; narg: Integer; const tname: PChar): Integer; cdecl;
function luaL_argerror(L: Plua_State; numarg: Integer; const extramsg: PChar): Integer; cdecl;
function luaL_checklstring(L: Plua_State; numArg: Integer; l_: Psize_t): PChar; cdecl;
function luaL_optlstring(L: Plua_State; numArg: Integer; const def: PChar; l_: Psize_t): PChar; cdecl;
function luaL_checknumber(L: Plua_State; numArg: Integer): lua_Number; cdecl;
function luaL_optnumber(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number; cdecl;
function luaL_checkinteger(L: Plua_State; numArg: Integer): lua_Integer; cdecl;
function luaL_optinteger(L: Plua_State; nArg: Integer; def: lua_Integer): lua_Integer; cdecl;
procedure luaL_checkstack(L: Plua_State; sz: Integer; const msg: PChar); cdecl;
procedure luaL_checktype(L: Plua_State; narg, t: Integer); cdecl;
procedure luaL_checkany(L: Plua_State; narg: Integer); cdecl;
function luaL_newmetatable(L: Plua_State; const tname: PChar): Integer; cdecl;
function luaL_checkudata(L: Plua_State; ud: Integer; const tname: PChar): Pointer; cdecl;
procedure luaL_where(L: Plua_State; lvl: Integer); cdecl;
function luaL_error(L: Plua_State; const fmt: PChar; args: array of const): Integer; cdecl; external LUA_LIB_NAME; // note: C's ... to array of const conversion is not portable to Delphi
function luaL_checkoption(L: Plua_State; narg: Integer; def: PChar; lst: PPChar): Integer; cdecl;
function luaL_ref(L: Plua_State; t: Integer): Integer; cdecl;
procedure luaL_unref(L: Plua_State; t, ref: Integer); cdecl;
function luaL_loadfile(L: Plua_State; const filename: PChar): Integer; cdecl;
function luaL_loadbuffer(L: Plua_State; const buff: PChar; size: size_t; const name: PChar): Integer; cdecl;
function luaL_loadstring(L: Plua_State; const s: PChar): Integer; cdecl;
function luaL_newstate: Plua_State; cdecl;
function lua_open: Plua_State; // compatibility; moved from unit lua to lauxlib because it needs luaL_newstate
function luaL_gsub(L: Plua_State; const s, p, r: PChar): PChar; cdecl;
function luaL_findtable(L: Plua_State; idx: Integer; const fname: PChar; szhint: Integer): PChar; cdecl;
(*
** ===============================================================
** some useful macros
** ===============================================================
*)
procedure luaL_argcheck(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PChar);
function luaL_checkstring(L: Plua_State; n: Integer): PChar;
function luaL_optstring(L: Plua_State; n: Integer; d: PChar): PChar;
function luaL_checkint(L: Plua_State; n: Integer): Integer;
function luaL_checklong(L: Plua_State; n: Integer): LongInt;
function luaL_optint(L: Plua_State; n: Integer; d: Double): Integer;
function luaL_optlong(L: Plua_State; n: Integer; d: Double): LongInt;
function luaL_typename(L: Plua_State; i: Integer): PChar;
function lua_dofile(L: Plua_State; const filename: PChar): Integer;
function lua_dostring(L: Plua_State; const str: PChar): Integer;
procedure lua_Lgetmetatable(L: Plua_State; tname: PChar);
// 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 = record
p: PChar; (* current position in buffer *)
lvl: Integer; (* number of strings in the stack (level) *)
L: Plua_State;
buffer: array [0..LUAL_BUFFERSIZE - 1] of Char; // warning: see note above about LUAL_BUFFERSIZE
end;
PluaL_Buffer = ^luaL_Buffer;
procedure luaL_addchar(B: PluaL_Buffer; c: Char); // warning: see note above about LUAL_BUFFERSIZE
(* compatibility only (alias for luaL_addchar) *)
procedure luaL_putchar(B: PluaL_Buffer; c: Char); // warning: see note above about LUAL_BUFFERSIZE
procedure luaL_addsize(B: PluaL_Buffer; n: Integer);
procedure luaL_buffinit(L: Plua_State; B: PluaL_Buffer); cdecl;
function luaL_prepbuffer(B: PluaL_Buffer): PChar; cdecl;
procedure luaL_addlstring(B: PluaL_Buffer; const s: PChar; l: size_t); cdecl;
procedure luaL_addstring(B: PluaL_Buffer; const s: PChar); cdecl;
procedure luaL_addvalue(B: PluaL_Buffer); cdecl;
procedure luaL_pushresult(B: PluaL_Buffer); cdecl;
(* compatibility with ref system *)
(* pre-defined references *)
const
LUA_NOREF = -2;
LUA_REFNIL = -1;
procedure lua_unref(L: Plua_State; ref: Integer);
procedure lua_getref(L: Plua_State; ref: Integer);
(*
** Compatibility macros and functions
*)
function luaL_check_lstr(L: Plua_State; numArg: Integer; len: Psize_t): PChar;
function luaL_opt_lstr(L: Plua_State; numArg: Integer; const def: PChar; len: Psize_t): PChar;
function luaL_check_number(L: Plua_State; numArg: Integer): lua_Number;
function luaL_opt_number(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number;
procedure luaL_arg_check(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PChar);
function luaL_check_string(L: Plua_State; n: Integer): PChar;
function luaL_opt_string(L: Plua_State; n: Integer; d: PChar): PChar;
function luaL_check_int(L: Plua_State; n: Integer): Integer;
function luaL_check_long(L: Plua_State; n: Integer): LongInt;
function luaL_opt_int(L: Plua_State; n: Integer; d: Double): Integer;
function luaL_opt_long(L: Plua_State; n: Integer; d: Double): LongInt;
implementation
procedure lua_pushstring(L: Plua_State; const s: string);
begin
lua_pushlstring(L, PChar(s), Length(s));
end;
function luaL_getn(L: Plua_State; n: Integer): Integer;
begin
Result := lua_objlen(L, n);
end;
procedure luaL_setn(L: Plua_State; t, n: Integer);
begin
// does nothing as this operation is deprecated
end;
procedure luaL_openlib(L: Plua_State; const libname: PChar; const lr: PluaL_reg; nup: Integer); cdecl; external LUA_LIB_NAME;
procedure luaL_register(L: Plua_State; const libname: PChar; const lr: PluaL_reg); cdecl; external LUA_LIB_NAME;
function luaL_getmetafield(L: Plua_State; obj: Integer; const e: PChar): Integer; cdecl; external LUA_LIB_NAME;
function luaL_callmeta(L: Plua_State; obj: Integer; const e: PChar): Integer; cdecl; external LUA_LIB_NAME;
function luaL_typerror(L: Plua_State; narg: Integer; const tname: PChar): Integer; cdecl; external LUA_LIB_NAME;
function luaL_argerror(L: Plua_State; numarg: Integer; const extramsg: PChar): Integer; cdecl; external LUA_LIB_NAME;
function luaL_checklstring(L: Plua_State; numArg: Integer; l_: Psize_t): PChar; cdecl; external LUA_LIB_NAME;
function luaL_optlstring(L: Plua_State; numArg: Integer; const def: PChar; l_: Psize_t): PChar; cdecl; external LUA_LIB_NAME;
function luaL_checknumber(L: Plua_State; numArg: Integer): lua_Number; cdecl; external LUA_LIB_NAME;
function luaL_optnumber(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number; cdecl; external LUA_LIB_NAME;
function luaL_checkinteger(L: Plua_State; numArg: Integer): lua_Integer; cdecl; external LUA_LIB_NAME;
function luaL_optinteger(L: Plua_State; nArg: Integer; def: lua_Integer): lua_Integer; cdecl; external LUA_LIB_NAME;
procedure luaL_checkstack(L: Plua_State; sz: Integer; const msg: PChar); cdecl; external LUA_LIB_NAME;
procedure luaL_checktype(L: Plua_State; narg, t: Integer); cdecl; external LUA_LIB_NAME;
procedure luaL_checkany(L: Plua_State; narg: Integer); cdecl; external LUA_LIB_NAME;
function luaL_newmetatable(L: Plua_State; const tname: PChar): Integer; cdecl; external LUA_LIB_NAME;
function luaL_checkudata(L: Plua_State; ud: Integer; const tname: PChar): Pointer; cdecl; external LUA_LIB_NAME;
procedure luaL_where(L: Plua_State; lvl: Integer); cdecl; external LUA_LIB_NAME;
// function luaL_error(L: Plua_State; const fmt: PChar; args: array of const): Integer; cdecl; external LUA_LIB_NAME;
function luaL_checkoption(L: Plua_State; narg: Integer; def: PChar; lst: PPChar): Integer; cdecl; external LUA_LIB_NAME;
function luaL_ref(L: Plua_State; t: Integer): Integer; cdecl; external LUA_LIB_NAME;
procedure luaL_unref(L: Plua_State; t, ref: Integer); cdecl; external LUA_LIB_NAME;
function luaL_loadfile(L: Plua_State; const filename: PChar): Integer; cdecl; external LUA_LIB_NAME;
function luaL_loadbuffer(L: Plua_State; const buff: PChar; size: size_t; const name: PChar): Integer; cdecl; external LUA_LIB_NAME;
function luaL_loadstring(L: Plua_State; const s: PChar): Integer; cdecl; external LUA_LIB_NAME;
function luaL_newstate: Plua_State; cdecl; external LUA_LIB_NAME;
function lua_open: Plua_State;
begin
Result := luaL_newstate;
end;
function luaL_gsub(L: Plua_State; const s, p, r: PChar): PChar; cdecl; external LUA_LIB_NAME;
function luaL_findtable(L: Plua_State; idx: Integer; const fname: PChar; szhint: Integer): PChar; cdecl; external LUA_LIB_NAME;
function luaL_typename(L: Plua_State; i: Integer): PChar;
begin
Result := lua_typename(L, lua_type(L, i));
end;
function lua_dofile(L: Plua_State; const filename: PChar): Integer;
begin
Result := luaL_loadfile(L, filename);
if Result = 0 then
Result := lua_pcall(L, 0, LUA_MULTRET, 0);
end;
function lua_dostring(L: Plua_State; const str: PChar): Integer;
begin
Result := luaL_loadstring(L, str);
if Result = 0 then
Result := lua_pcall(L, 0, LUA_MULTRET, 0);
end;
procedure lua_Lgetmetatable(L: Plua_State; tname: PChar);
begin
lua_getfield(L, LUA_REGISTRYINDEX, tname);
end;
procedure luaL_argcheck(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PChar);
begin
if not cond then
luaL_argerror(L, numarg, extramsg)
end;
function luaL_checkstring(L: Plua_State; n: Integer): PChar;
begin
Result := luaL_checklstring(L, n, nil)
end;
function luaL_optstring(L: Plua_State; n: Integer; d: PChar): PChar;
begin
Result := luaL_optlstring(L, n, d, nil)
end;
function luaL_checkint(L: Plua_State; n: Integer): Integer;
begin
Result := Integer(Trunc(luaL_checknumber(L, n)))
end;
function luaL_checklong(L: Plua_State; n: Integer): LongInt;
begin
Result := LongInt(Trunc(luaL_checknumber(L, n)))
end;
function luaL_optint(L: Plua_State; n: Integer; d: Double): Integer;
begin
Result := Integer(Trunc(luaL_optnumber(L, n, d)))
end;
function luaL_optlong(L: Plua_State; n: Integer; d: Double): LongInt;
begin
Result := LongInt(Trunc(luaL_optnumber(L, n, d)))
end;
procedure luaL_addchar(B: PluaL_Buffer; c: Char);
begin
if Cardinal(@(B^.p)) < (Cardinal(@(B^.buffer[0])) + LUAL_BUFFERSIZE) then
luaL_prepbuffer(B);
B^.p[1] := c;
B^.p := B^.p + 1;
end;
procedure luaL_putchar(B: PluaL_Buffer; c: Char);
begin
luaL_addchar(B, c);
end;
procedure luaL_addsize(B: PluaL_Buffer; n: Integer);
begin
B^.p := B^.p + n;
end;
procedure luaL_buffinit(L: Plua_State ; B: PluaL_Buffer); cdecl; external LUA_LIB_NAME;
function luaL_prepbuffer(B: PluaL_Buffer): PChar; cdecl; external LUA_LIB_NAME;
procedure luaL_addlstring(B: PluaL_Buffer; const s: PChar; l: size_t); cdecl; external LUA_LIB_NAME;
procedure luaL_addstring(B: PluaL_Buffer; const s: PChar); cdecl; external LUA_LIB_NAME;
procedure luaL_addvalue(B: PluaL_Buffer); cdecl; external LUA_LIB_NAME;
procedure luaL_pushresult(B: PluaL_Buffer); cdecl; external LUA_LIB_NAME;
procedure lua_unref(L: Plua_State; ref: Integer);
begin
luaL_unref(L, LUA_REGISTRYINDEX, ref);
end;
procedure lua_getref(L: Plua_State; ref: Integer);
begin
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
end;
function luaL_check_lstr(L: Plua_State; numArg: Integer; len: Psize_t): PChar;
begin
Result := luaL_checklstring(L, numArg, len);
end;
function luaL_opt_lstr(L: Plua_State; numArg: Integer; const def: PChar; len: Psize_t): PChar;
begin
Result := luaL_optlstring(L, numArg, def, len);
end;
function luaL_check_number(L: Plua_State; numArg: Integer): lua_Number;
begin
Result := luaL_checknumber(L, numArg);
end;
function luaL_opt_number(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number;
begin
Result := luaL_optnumber(L, nArg, def);
end;
procedure luaL_arg_check(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PChar);
begin
luaL_argcheck(L, cond, numarg, extramsg);
end;
function luaL_check_string(L: Plua_State; n: Integer): PChar;
begin
Result := luaL_checkstring(L, n);
end;
function luaL_opt_string(L: Plua_State; n: Integer; d: PChar): PChar;
begin
Result := luaL_optstring(L, n, d);
end;
function luaL_check_int(L: Plua_State; n: Integer): Integer;
begin
Result := luaL_checkint(L, n);
end;
function luaL_check_long(L: Plua_State; n: Integer): LongInt;
begin
Result := luaL_checklong(L, n);
end;
function luaL_opt_int(L: Plua_State; n: Integer; d: Double): Integer;
begin
Result := luaL_optint(L, n, d);
end;
function luaL_opt_long(L: Plua_State; n: Integer; d: Double): LongInt;
begin
Result := luaL_optlong(L, n, d);
end;
end.
|