DEADSOFTWARE

650bd7299321407d2f2f071958930286b1b70e2c
[d2df-sdl.git] / src / lib / lua / lauxlib.pas
1 (******************************************************************************
2 * *
3 * File: lauxlib.pas *
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 *
8 * *
9 ******************************************************************************)
11 (*
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
15 *)
16 (*
17 ** Translated to pascal by Lavergne Thomas
18 ** Notes :
19 ** - Pointers type was prefixed with 'P'
20 ** Bug reports :
21 ** - thomas.lavergne@laposte.net
22 ** In french or in english
23 *)
25 {$IFDEF FPC}{$MODE OBJFPC}{$H+}{$ENDIF}
27 unit lauxlib;
29 interface
31 uses
32 Lua;
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!
41 type
42 luaL_reg = record
43 name: PChar;
44 func: lua_CFunction;
45 end;
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;
86 (*
87 ** ===============================================================
88 ** some useful macros
89 ** ===============================================================
90 *)
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);
107 // not translated:
108 // #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
111 (*
112 ** =======================================================
113 ** Generic Buffer manipulation
114 ** =======================================================
115 *)
117 const
118 // note: this is just arbitrary, as it related to the BUFSIZ defined in stdio.h ...
119 LUAL_BUFFERSIZE = 4096;
121 type
122 luaL_Buffer = record
123 p: PChar; (* current position in buffer *)
124 lvl: Integer; (* number of strings in the stack (level) *)
125 L: Plua_State;
126 buffer: array [0..LUAL_BUFFERSIZE - 1] of Char; // warning: see note above about LUAL_BUFFERSIZE
127 end;
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 *)
148 const
149 LUA_NOREF = -2;
150 LUA_REFNIL = -1;
152 procedure lua_unref(L: Plua_State; ref: Integer);
153 procedure lua_getref(L: Plua_State; ref: Integer);
155 (*
156 ** Compatibility macros and functions
157 *)
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;
171 implementation
173 procedure lua_pushstring(L: Plua_State; const s: string);
174 begin
175 lua_pushlstring(L, PChar(s), Length(s));
176 end;
178 function luaL_getn(L: Plua_State; n: Integer): Integer;
179 begin
180 Result := lua_objlen(L, n);
181 end;
183 procedure luaL_setn(L: Plua_State; t, n: Integer);
184 begin
185 // does nothing as this operation is deprecated
186 end;
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;
223 begin
224 Result := luaL_newstate;
225 end;
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;
231 begin
232 Result := lua_typename(L, lua_type(L, i));
233 end;
235 function lua_dofile(L: Plua_State; const filename: PChar): Integer;
236 begin
237 Result := luaL_loadfile(L, filename);
238 if Result = 0 then
239 Result := lua_pcall(L, 0, LUA_MULTRET, 0);
240 end;
242 function lua_dostring(L: Plua_State; const str: PChar): Integer;
243 begin
244 Result := luaL_loadstring(L, str);
245 if Result = 0 then
246 Result := lua_pcall(L, 0, LUA_MULTRET, 0);
247 end;
249 procedure lua_Lgetmetatable(L: Plua_State; tname: PChar);
250 begin
251 lua_getfield(L, LUA_REGISTRYINDEX, tname);
252 end;
254 procedure luaL_argcheck(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PChar);
255 begin
256 if not cond then
257 luaL_argerror(L, numarg, extramsg)
258 end;
260 function luaL_checkstring(L: Plua_State; n: Integer): PChar;
261 begin
262 Result := luaL_checklstring(L, n, nil)
263 end;
265 function luaL_optstring(L: Plua_State; n: Integer; d: PChar): PChar;
266 begin
267 Result := luaL_optlstring(L, n, d, nil)
268 end;
270 function luaL_checkint(L: Plua_State; n: Integer): Integer;
271 begin
272 Result := Integer(Trunc(luaL_checknumber(L, n)))
273 end;
275 function luaL_checklong(L: Plua_State; n: Integer): LongInt;
276 begin
277 Result := LongInt(Trunc(luaL_checknumber(L, n)))
278 end;
280 function luaL_optint(L: Plua_State; n: Integer; d: Double): Integer;
281 begin
282 Result := Integer(Trunc(luaL_optnumber(L, n, d)))
283 end;
285 function luaL_optlong(L: Plua_State; n: Integer; d: Double): LongInt;
286 begin
287 Result := LongInt(Trunc(luaL_optnumber(L, n, d)))
288 end;
290 procedure luaL_addchar(B: PluaL_Buffer; c: Char);
291 begin
292 if Cardinal(@(B^.p)) < (Cardinal(@(B^.buffer[0])) + LUAL_BUFFERSIZE) then
293 luaL_prepbuffer(B);
294 B^.p[1] := c;
295 B^.p := B^.p + 1;
296 end;
298 procedure luaL_putchar(B: PluaL_Buffer; c: Char);
299 begin
300 luaL_addchar(B, c);
301 end;
303 procedure luaL_addsize(B: PluaL_Buffer; n: Integer);
304 begin
305 B^.p := B^.p + n;
306 end;
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);
316 begin
317 luaL_unref(L, LUA_REGISTRYINDEX, ref);
318 end;
320 procedure lua_getref(L: Plua_State; ref: Integer);
321 begin
322 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
323 end;
325 function luaL_check_lstr(L: Plua_State; numArg: Integer; len: Psize_t): PChar;
326 begin
327 Result := luaL_checklstring(L, numArg, len);
328 end;
330 function luaL_opt_lstr(L: Plua_State; numArg: Integer; const def: PChar; len: Psize_t): PChar;
331 begin
332 Result := luaL_optlstring(L, numArg, def, len);
333 end;
335 function luaL_check_number(L: Plua_State; numArg: Integer): lua_Number;
336 begin
337 Result := luaL_checknumber(L, numArg);
338 end;
340 function luaL_opt_number(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number;
341 begin
342 Result := luaL_optnumber(L, nArg, def);
343 end;
345 procedure luaL_arg_check(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PChar);
346 begin
347 luaL_argcheck(L, cond, numarg, extramsg);
348 end;
350 function luaL_check_string(L: Plua_State; n: Integer): PChar;
351 begin
352 Result := luaL_checkstring(L, n);
353 end;
355 function luaL_opt_string(L: Plua_State; n: Integer; d: PChar): PChar;
356 begin
357 Result := luaL_optstring(L, n, d);
358 end;
360 function luaL_check_int(L: Plua_State; n: Integer): Integer;
361 begin
362 Result := luaL_checkint(L, n);
363 end;
365 function luaL_check_long(L: Plua_State; n: Integer): LongInt;
366 begin
367 Result := luaL_checklong(L, n);
368 end;
370 function luaL_opt_int(L: Plua_State; n: Integer; d: Double): Integer;
371 begin
372 Result := luaL_optint(L, n, d);
373 end;
375 function luaL_opt_long(L: Plua_State; n: Integer; d: Double): LongInt;
376 begin
377 Result := luaL_optlong(L, n, d);
378 end;
380 end.