1 (******************************************************************************
4 * Authors: TeCGraf (C headers + actual Lua libraries) *
5 * Lavergne Thomas (original translation to Pascal) *
6 * Bram Kuijvenhoven (update to Lua 5.1.1 for FreePascal) *
7 * Description: Lua auxiliary library *
9 ******************************************************************************)
12 ** $Id: lauxlib.h,v 1.59 2003/03/18 12:25:32 roberto Exp $
13 ** Auxiliary functions for building Lua libraries
14 ** See Copyright Notice in lua.h
17 ** Translated to pascal by Lavergne Thomas
19 ** - Pointers type was prefixed with 'P'
21 ** - thomas.lavergne@laposte.net
22 ** In french or in english
25 {$IFDEF FPC}{$MODE OBJFPC}{$H+}{$ENDIF}
34 // functions added for Pascal
35 procedure lua_pushstring(L
: Plua_State
; const s
: string);
37 // compatibilty macros
38 function luaL_getn(L
: Plua_State
; n
: Integer): Integer; // calls lua_objlen
39 procedure luaL_setn(L
: Plua_State
; t
, n
: Integer); // does nothing!
46 PluaL_reg
= ^luaL_reg
;
48 procedure luaL_openlib(L
: Plua_State
; const libname
: PChar; const lr
: PluaL_reg
; nup
: Integer); cdecl;
49 procedure luaL_register(L
: Plua_State
; const libname
: PChar; const lr
: PluaL_reg
); cdecl;
50 function luaL_getmetafield(L
: Plua_State
; obj
: Integer; const e
: PChar): Integer; cdecl;
51 function luaL_callmeta(L
: Plua_State
; obj
: Integer; const e
: PChar): Integer; cdecl;
52 function luaL_typerror(L
: Plua_State
; narg
: Integer; const tname
: PChar): Integer; cdecl;
53 function luaL_argerror(L
: Plua_State
; numarg
: Integer; const extramsg
: PChar): Integer; cdecl;
54 function luaL_checklstring(L
: Plua_State
; numArg
: Integer; l_
: Psize_t
): PChar; cdecl;
55 function luaL_optlstring(L
: Plua_State
; numArg
: Integer; const def
: PChar; l_
: Psize_t
): PChar; cdecl;
56 function luaL_checknumber(L
: Plua_State
; numArg
: Integer): lua_Number
; cdecl;
57 function luaL_optnumber(L
: Plua_State
; nArg
: Integer; def
: lua_Number
): lua_Number
; cdecl;
58 function luaL_checkinteger(L
: Plua_State
; numArg
: Integer): lua_Integer
; cdecl;
59 function luaL_optinteger(L
: Plua_State
; nArg
: Integer; def
: lua_Integer
): lua_Integer
; cdecl;
61 procedure luaL_checkstack(L
: Plua_State
; sz
: Integer; const msg
: PChar); cdecl;
62 procedure luaL_checktype(L
: Plua_State
; narg
, t
: Integer); cdecl;
63 procedure luaL_checkany(L
: Plua_State
; narg
: Integer); cdecl;
65 function luaL_newmetatable(L
: Plua_State
; const tname
: PChar): Integer; cdecl;
66 function luaL_checkudata(L
: Plua_State
; ud
: Integer; const tname
: PChar): Pointer; cdecl;
68 procedure luaL_where(L
: Plua_State
; lvl
: Integer); cdecl;
69 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
71 function luaL_checkoption(L
: Plua_State
; narg
: Integer; def
: PChar; lst
: PPChar
): Integer; cdecl;
73 function luaL_ref(L
: Plua_State
; t
: Integer): Integer; cdecl;
74 procedure luaL_unref(L
: Plua_State
; t
, ref
: Integer); cdecl;
76 function luaL_loadfile(L
: Plua_State
; const filename
: PChar): Integer; cdecl;
77 function luaL_loadbuffer(L
: Plua_State
; const buff
: PChar; size
: size_t
; const name
: PChar): Integer; cdecl;
78 function luaL_loadstring(L
: Plua_State
; const s
: PChar): Integer; cdecl;
80 function luaL_newstate
: Plua_State
; cdecl;
81 function lua_open
: Plua_State
; // compatibility; moved from unit lua to lauxlib because it needs luaL_newstate
83 function luaL_gsub(L
: Plua_State
; const s
, p
, r
: PChar): PChar; cdecl;
84 function luaL_findtable(L
: Plua_State
; idx
: Integer; const fname
: PChar; szhint
: Integer): PChar; cdecl;
87 ** ===============================================================
89 ** ===============================================================
92 procedure luaL_argcheck(L
: Plua_State
; cond
: Boolean; numarg
: Integer; extramsg
: PChar);
93 function luaL_checkstring(L
: Plua_State
; n
: Integer): PChar;
94 function luaL_optstring(L
: Plua_State
; n
: Integer; d
: PChar): PChar;
95 function luaL_checkint(L
: Plua_State
; n
: Integer): Integer;
96 function luaL_checklong(L
: Plua_State
; n
: Integer): LongInt;
97 function luaL_optint(L
: Plua_State
; n
: Integer; d
: Double): Integer;
98 function luaL_optlong(L
: Plua_State
; n
: Integer; d
: Double): LongInt;
100 function luaL_typename(L
: Plua_State
; i
: Integer): PChar;
102 function lua_dofile(L
: Plua_State
; const filename
: PChar): Integer;
103 function lua_dostring(L
: Plua_State
; const str
: PChar): Integer;
105 procedure lua_Lgetmetatable(L
: Plua_State
; tname
: PChar);
108 // #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
112 ** =======================================================
113 ** Generic Buffer manipulation
114 ** =======================================================
118 // note: this is just arbitrary, as it related to the BUFSIZ defined in stdio.h ...
119 LUAL_BUFFERSIZE
= 4096;
123 p
: PChar; (* current position in buffer *)
124 lvl
: Integer; (* number of strings in the stack (level) *)
126 buffer
: array [0..LUAL_BUFFERSIZE
- 1] of Char; // warning: see note above about LUAL_BUFFERSIZE
128 PluaL_Buffer
= ^luaL_Buffer
;
130 procedure luaL_addchar(B
: PluaL_Buffer
; c
: Char); // warning: see note above about LUAL_BUFFERSIZE
132 (* compatibility only (alias for luaL_addchar) *)
133 procedure luaL_putchar(B
: PluaL_Buffer
; c
: Char); // warning: see note above about LUAL_BUFFERSIZE
135 procedure luaL_addsize(B
: PluaL_Buffer
; n
: Integer);
137 procedure luaL_buffinit(L
: Plua_State
; B
: PluaL_Buffer
); cdecl;
138 function luaL_prepbuffer(B
: PluaL_Buffer
): PChar; cdecl;
139 procedure luaL_addlstring(B
: PluaL_Buffer
; const s
: PChar; l
: size_t
); cdecl;
140 procedure luaL_addstring(B
: PluaL_Buffer
; const s
: PChar); cdecl;
141 procedure luaL_addvalue(B
: PluaL_Buffer
); cdecl;
142 procedure luaL_pushresult(B
: PluaL_Buffer
); cdecl;
145 (* compatibility with ref system *)
147 (* pre-defined references *)
152 procedure lua_unref(L
: Plua_State
; ref
: Integer);
153 procedure lua_getref(L
: Plua_State
; ref
: Integer);
156 ** Compatibility macros and functions
159 function luaL_check_lstr(L
: Plua_State
; numArg
: Integer; len
: Psize_t
): PChar;
160 function luaL_opt_lstr(L
: Plua_State
; numArg
: Integer; const def
: PChar; len
: Psize_t
): PChar;
161 function luaL_check_number(L
: Plua_State
; numArg
: Integer): lua_Number
;
162 function luaL_opt_number(L
: Plua_State
; nArg
: Integer; def
: lua_Number
): lua_Number
;
163 procedure luaL_arg_check(L
: Plua_State
; cond
: Boolean; numarg
: Integer; extramsg
: PChar);
164 function luaL_check_string(L
: Plua_State
; n
: Integer): PChar;
165 function luaL_opt_string(L
: Plua_State
; n
: Integer; d
: PChar): PChar;
166 function luaL_check_int(L
: Plua_State
; n
: Integer): Integer;
167 function luaL_check_long(L
: Plua_State
; n
: Integer): LongInt;
168 function luaL_opt_int(L
: Plua_State
; n
: Integer; d
: Double): Integer;
169 function luaL_opt_long(L
: Plua_State
; n
: Integer; d
: Double): LongInt;
173 procedure lua_pushstring(L
: Plua_State
; const s
: string);
175 lua_pushlstring(L
, PChar(s
), Length(s
));
178 function luaL_getn(L
: Plua_State
; n
: Integer): Integer;
180 Result
:= lua_objlen(L
, n
);
183 procedure luaL_setn(L
: Plua_State
; t
, n
: Integer);
185 // does nothing as this operation is deprecated
188 procedure luaL_openlib(L
: Plua_State
; const libname
: PChar; const lr
: PluaL_reg
; nup
: Integer); cdecl; external LUA_LIB_NAME
;
189 procedure luaL_register(L
: Plua_State
; const libname
: PChar; const lr
: PluaL_reg
); cdecl; external LUA_LIB_NAME
;
190 function luaL_getmetafield(L
: Plua_State
; obj
: Integer; const e
: PChar): Integer; cdecl; external LUA_LIB_NAME
;
191 function luaL_callmeta(L
: Plua_State
; obj
: Integer; const e
: PChar): Integer; cdecl; external LUA_LIB_NAME
;
192 function luaL_typerror(L
: Plua_State
; narg
: Integer; const tname
: PChar): Integer; cdecl; external LUA_LIB_NAME
;
193 function luaL_argerror(L
: Plua_State
; numarg
: Integer; const extramsg
: PChar): Integer; cdecl; external LUA_LIB_NAME
;
194 function luaL_checklstring(L
: Plua_State
; numArg
: Integer; l_
: Psize_t
): PChar; cdecl; external LUA_LIB_NAME
;
195 function luaL_optlstring(L
: Plua_State
; numArg
: Integer; const def
: PChar; l_
: Psize_t
): PChar; cdecl; external LUA_LIB_NAME
;
196 function luaL_checknumber(L
: Plua_State
; numArg
: Integer): lua_Number
; cdecl; external LUA_LIB_NAME
;
197 function luaL_optnumber(L
: Plua_State
; nArg
: Integer; def
: lua_Number
): lua_Number
; cdecl; external LUA_LIB_NAME
;
198 function luaL_checkinteger(L
: Plua_State
; numArg
: Integer): lua_Integer
; cdecl; external LUA_LIB_NAME
;
199 function luaL_optinteger(L
: Plua_State
; nArg
: Integer; def
: lua_Integer
): lua_Integer
; cdecl; external LUA_LIB_NAME
;
201 procedure luaL_checkstack(L
: Plua_State
; sz
: Integer; const msg
: PChar); cdecl; external LUA_LIB_NAME
;
202 procedure luaL_checktype(L
: Plua_State
; narg
, t
: Integer); cdecl; external LUA_LIB_NAME
;
203 procedure luaL_checkany(L
: Plua_State
; narg
: Integer); cdecl; external LUA_LIB_NAME
;
205 function luaL_newmetatable(L
: Plua_State
; const tname
: PChar): Integer; cdecl; external LUA_LIB_NAME
;
206 function luaL_checkudata(L
: Plua_State
; ud
: Integer; const tname
: PChar): Pointer; cdecl; external LUA_LIB_NAME
;
208 procedure luaL_where(L
: Plua_State
; lvl
: Integer); cdecl; external LUA_LIB_NAME
;
209 // function luaL_error(L: Plua_State; const fmt: PChar; args: array of const): Integer; cdecl; external LUA_LIB_NAME;
211 function luaL_checkoption(L
: Plua_State
; narg
: Integer; def
: PChar; lst
: PPChar
): Integer; cdecl; external LUA_LIB_NAME
;
213 function luaL_ref(L
: Plua_State
; t
: Integer): Integer; cdecl; external LUA_LIB_NAME
;
214 procedure luaL_unref(L
: Plua_State
; t
, ref
: Integer); cdecl; external LUA_LIB_NAME
;
216 function luaL_loadfile(L
: Plua_State
; const filename
: PChar): Integer; cdecl; external LUA_LIB_NAME
;
217 function luaL_loadbuffer(L
: Plua_State
; const buff
: PChar; size
: size_t
; const name
: PChar): Integer; cdecl; external LUA_LIB_NAME
;
218 function luaL_loadstring(L
: Plua_State
; const s
: PChar): Integer; cdecl; external LUA_LIB_NAME
;
220 function luaL_newstate
: Plua_State
; cdecl; external LUA_LIB_NAME
;
222 function lua_open
: Plua_State
;
224 Result
:= luaL_newstate
;
227 function luaL_gsub(L
: Plua_State
; const s
, p
, r
: PChar): PChar; cdecl; external LUA_LIB_NAME
;
228 function luaL_findtable(L
: Plua_State
; idx
: Integer; const fname
: PChar; szhint
: Integer): PChar; cdecl; external LUA_LIB_NAME
;
230 function luaL_typename(L
: Plua_State
; i
: Integer): PChar;
232 Result
:= lua_typename(L
, lua_type(L
, i
));
235 function lua_dofile(L
: Plua_State
; const filename
: PChar): Integer;
237 Result
:= luaL_loadfile(L
, filename
);
239 Result
:= lua_pcall(L
, 0, LUA_MULTRET
, 0);
242 function lua_dostring(L
: Plua_State
; const str
: PChar): Integer;
244 Result
:= luaL_loadstring(L
, str
);
246 Result
:= lua_pcall(L
, 0, LUA_MULTRET
, 0);
249 procedure lua_Lgetmetatable(L
: Plua_State
; tname
: PChar);
251 lua_getfield(L
, LUA_REGISTRYINDEX
, tname
);
254 procedure luaL_argcheck(L
: Plua_State
; cond
: Boolean; numarg
: Integer; extramsg
: PChar);
257 luaL_argerror(L
, numarg
, extramsg
)
260 function luaL_checkstring(L
: Plua_State
; n
: Integer): PChar;
262 Result
:= luaL_checklstring(L
, n
, nil)
265 function luaL_optstring(L
: Plua_State
; n
: Integer; d
: PChar): PChar;
267 Result
:= luaL_optlstring(L
, n
, d
, nil)
270 function luaL_checkint(L
: Plua_State
; n
: Integer): Integer;
272 Result
:= Integer(Trunc(luaL_checknumber(L
, n
)))
275 function luaL_checklong(L
: Plua_State
; n
: Integer): LongInt;
277 Result
:= LongInt(Trunc(luaL_checknumber(L
, n
)))
280 function luaL_optint(L
: Plua_State
; n
: Integer; d
: Double): Integer;
282 Result
:= Integer(Trunc(luaL_optnumber(L
, n
, d
)))
285 function luaL_optlong(L
: Plua_State
; n
: Integer; d
: Double): LongInt;
287 Result
:= LongInt(Trunc(luaL_optnumber(L
, n
, d
)))
290 procedure luaL_addchar(B
: PluaL_Buffer
; c
: Char);
292 if Cardinal(@(B
^.p
)) < (Cardinal(@(B
^.buffer
[0])) + LUAL_BUFFERSIZE
) then
298 procedure luaL_putchar(B
: PluaL_Buffer
; c
: Char);
303 procedure luaL_addsize(B
: PluaL_Buffer
; n
: Integer);
308 procedure luaL_buffinit(L
: Plua_State
; B
: PluaL_Buffer
); cdecl; external LUA_LIB_NAME
;
309 function luaL_prepbuffer(B
: PluaL_Buffer
): PChar; cdecl; external LUA_LIB_NAME
;
310 procedure luaL_addlstring(B
: PluaL_Buffer
; const s
: PChar; l
: size_t
); cdecl; external LUA_LIB_NAME
;
311 procedure luaL_addstring(B
: PluaL_Buffer
; const s
: PChar); cdecl; external LUA_LIB_NAME
;
312 procedure luaL_addvalue(B
: PluaL_Buffer
); cdecl; external LUA_LIB_NAME
;
313 procedure luaL_pushresult(B
: PluaL_Buffer
); cdecl; external LUA_LIB_NAME
;
315 procedure lua_unref(L
: Plua_State
; ref
: Integer);
317 luaL_unref(L
, LUA_REGISTRYINDEX
, ref
);
320 procedure lua_getref(L
: Plua_State
; ref
: Integer);
322 lua_rawgeti(L
, LUA_REGISTRYINDEX
, ref
);
325 function luaL_check_lstr(L
: Plua_State
; numArg
: Integer; len
: Psize_t
): PChar;
327 Result
:= luaL_checklstring(L
, numArg
, len
);
330 function luaL_opt_lstr(L
: Plua_State
; numArg
: Integer; const def
: PChar; len
: Psize_t
): PChar;
332 Result
:= luaL_optlstring(L
, numArg
, def
, len
);
335 function luaL_check_number(L
: Plua_State
; numArg
: Integer): lua_Number
;
337 Result
:= luaL_checknumber(L
, numArg
);
340 function luaL_opt_number(L
: Plua_State
; nArg
: Integer; def
: lua_Number
): lua_Number
;
342 Result
:= luaL_optnumber(L
, nArg
, def
);
345 procedure luaL_arg_check(L
: Plua_State
; cond
: Boolean; numarg
: Integer; extramsg
: PChar);
347 luaL_argcheck(L
, cond
, numarg
, extramsg
);
350 function luaL_check_string(L
: Plua_State
; n
: Integer): PChar;
352 Result
:= luaL_checkstring(L
, n
);
355 function luaL_opt_string(L
: Plua_State
; n
: Integer; d
: PChar): PChar;
357 Result
:= luaL_optstring(L
, n
, d
);
360 function luaL_check_int(L
: Plua_State
; n
: Integer): Integer;
362 Result
:= luaL_checkint(L
, n
);
365 function luaL_check_long(L
: Plua_State
; n
: Integer): LongInt;
367 Result
:= luaL_checklong(L
, n
);
370 function luaL_opt_int(L
: Plua_State
; n
: Integer; d
: Double): Integer;
372 Result
:= luaL_optint(L
, n
, d
);
375 function luaL_opt_long(L
: Plua_State
; n
: Integer; d
: Double): LongInt;
377 Result
:= luaL_optlong(L
, n
, d
);