|
1 unit LuaPas; |
|
2 |
|
3 (* |
|
4 * A complete Pascal wrapper for Lua 5.1 DLL module. |
|
5 * |
|
6 * Created by Geo Massar, 2006 |
|
7 * Distributed as free/open source. |
|
8 *) |
|
9 |
|
10 interface |
|
11 |
|
12 {.$DEFINE LUA_GETHOOK} |
|
13 |
|
14 type |
|
15 size_t = type Cardinal; |
|
16 Psize_t = ^size_t; |
|
17 PPointer = ^Pointer; |
|
18 |
|
19 lua_State = record end; |
|
20 Plua_State = ^lua_State; |
|
21 |
|
22 const |
|
23 {$IFDEF UNIX} |
|
24 LuaDLL = 'lua5.1.so'; |
|
25 // LuaDLL = 'lua5.1.a'; |
|
26 {$ELSE} |
|
27 LuaDLL = 'lua5.1.dll'; |
|
28 {$ENDIF} |
|
29 |
|
30 |
|
31 (*****************************************************************************) |
|
32 (* luaconfig.h *) |
|
33 (*****************************************************************************) |
|
34 |
|
35 (* |
|
36 ** $Id: luaconf.h,v 1.81 2006/02/10 17:44:06 roberto Exp $ |
|
37 ** Configuration file for Lua |
|
38 ** See Copyright Notice in lua.h |
|
39 *) |
|
40 |
|
41 (* |
|
42 ** {================================================================== |
|
43 @@ LUA_NUMBER is the type of numbers in Lua. |
|
44 ** CHANGE the following definitions only if you want to build Lua |
|
45 ** with a number type different from double. You may also need to |
|
46 ** change lua_number2int & lua_number2integer. |
|
47 ** =================================================================== |
|
48 *) |
|
49 type |
|
50 LUA_NUMBER_ = type Double; // ending underscore is needed in Pascal |
|
51 LUA_INTEGER_ = type LongInt; |
|
52 |
|
53 (* |
|
54 @@ LUA_IDSIZE gives the maximum size for the description of the source |
|
55 @* of a function in debug information. |
|
56 ** CHANGE it if you want a different size. |
|
57 *) |
|
58 const |
|
59 LUA_IDSIZE = 60; |
|
60 |
|
61 (* |
|
62 @@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. |
|
63 *) |
|
64 const |
|
65 LUAL_BUFFERSIZE = 1024; |
|
66 |
|
67 (* |
|
68 @@ LUA_PROMPT is the default prompt used by stand-alone Lua. |
|
69 @@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua. |
|
70 ** CHANGE them if you want different prompts. (You can also change the |
|
71 ** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.) |
|
72 *) |
|
73 const |
|
74 LUA_PROMPT = '> '; |
|
75 LUA_PROMPT2 = '>> '; |
|
76 |
|
77 (* |
|
78 @@ lua_readline defines how to show a prompt and then read a line from |
|
79 @* the standard input. |
|
80 @@ lua_saveline defines how to "save" a read line in a "history". |
|
81 @@ lua_freeline defines how to free a line read by lua_readline. |
|
82 ** CHANGE them if you want to improve this functionality (e.g., by using |
|
83 ** GNU readline and history facilities). |
|
84 *) |
|
85 function lua_readline(L : Plua_State; var b : PChar; p : PChar): Boolean; |
|
86 procedure lua_saveline(L : Plua_State; idx : LongInt); |
|
87 procedure lua_freeline(L : Plua_State; b : PChar); |
|
88 |
|
89 (* |
|
90 @@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that |
|
91 @* is, whether we're running lua interactively). |
|
92 ** CHANGE it if you have a better definition for non-POSIX/non-Windows |
|
93 ** systems. |
|
94 */ |
|
95 #include <io.h> |
|
96 #include <stdio.h> |
|
97 #define lua_stdin_is_tty() _isatty(_fileno(stdin)) |
|
98 *) |
|
99 const |
|
100 lua_stdin_is_tty = TRUE; |
|
101 |
|
102 (*****************************************************************************) |
|
103 (* lua.h *) |
|
104 (*****************************************************************************) |
|
105 |
|
106 (* |
|
107 ** $Id: lua.h,v 1.216 2006/01/10 12:50:13 roberto Exp $ |
|
108 ** Lua - An Extensible Extension Language |
|
109 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) |
|
110 ** See Copyright Notice at the end of this file |
|
111 *) |
|
112 |
|
113 const |
|
114 LUA_VERSION = 'Lua 5.1'; |
|
115 LUA_VERSION_NUM = 501; |
|
116 LUA_COPYRIGHT = 'Copyright (C) 1994-2006 Tecgraf, PUC-Rio'; |
|
117 LUA_AUTHORS = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes'; |
|
118 |
|
119 (* mark for precompiled code (`<esc>Lua') *) |
|
120 LUA_SIGNATURE = #27'Lua'; |
|
121 |
|
122 (* option for multiple returns in `lua_pcall' and `lua_call' *) |
|
123 LUA_MULTRET = -1; |
|
124 |
|
125 (* |
|
126 ** pseudo-indices |
|
127 *) |
|
128 LUA_REGISTRYINDEX = -10000; |
|
129 LUA_ENVIRONINDEX = -10001; |
|
130 LUA_GLOBALSINDEX = -10002; |
|
131 |
|
132 function lua_upvalueindex(idx : LongInt) : LongInt; // a marco |
|
133 |
|
134 const |
|
135 (* thread status; 0 is OK *) |
|
136 LUA_YIELD_ = 1; // Note: the ending underscore is needed in Pascal |
|
137 LUA_ERRRUN = 2; |
|
138 LUA_ERRSYNTAX = 3; |
|
139 LUA_ERRMEM = 4; |
|
140 LUA_ERRERR = 5; |
|
141 |
|
142 type |
|
143 lua_CFunction = function(L : Plua_State) : LongInt; cdecl; |
|
144 |
|
145 (* |
|
146 ** functions that read/write blocks when loading/dumping Lua chunks |
|
147 *) |
|
148 lua_Reader = function (L : Plua_State; ud : Pointer; |
|
149 sz : Psize_t) : PChar; cdecl; |
|
150 lua_Writer = function (L : Plua_State; const p : Pointer; sz : size_t; |
|
151 ud : Pointer) : LongInt; cdecl; |
|
152 |
|
153 (* |
|
154 ** prototype for memory-allocation functions |
|
155 *) |
|
156 lua_Alloc = function (ud, ptr : Pointer; |
|
157 osize, nsize : size_t) : Pointer; cdecl; |
|
158 |
|
159 const |
|
160 (* |
|
161 ** basic types |
|
162 *) |
|
163 LUA_TNONE = -1; |
|
164 |
|
165 LUA_TNIL = 0; |
|
166 LUA_TBOOLEAN = 1; |
|
167 LUA_TLIGHTUSERDATA = 2; |
|
168 LUA_TNUMBER = 3; |
|
169 LUA_TSTRING = 4; |
|
170 LUA_TTABLE = 5; |
|
171 LUA_TFUNCTION = 6; |
|
172 LUA_TUSERDATA = 7; |
|
173 LUA_TTHREAD = 8; |
|
174 |
|
175 (* minimum Lua stack available to a C function *) |
|
176 LUA_MINSTACK = 20; |
|
177 |
|
178 type |
|
179 (* type of numbers in Lua *) |
|
180 lua_Number = LUA_NUMBER_; |
|
181 |
|
182 (* type for integer functions *) |
|
183 lua_Integer = LUA_INTEGER_; |
|
184 |
|
185 (* |
|
186 ** state manipulation |
|
187 *) |
|
188 function lua_newstate(f : lua_Alloc; ud : Pointer) : Plua_State; |
|
189 cdecl; external LuaDLL; |
|
190 procedure lua_close(L: Plua_State); |
|
191 cdecl; external LuaDLL; |
|
192 function lua_newthread(L : Plua_State) : Plua_State; |
|
193 cdecl; external LuaDLL; |
|
194 |
|
195 function lua_atpanic(L : Plua_State; panicf : lua_CFunction) : lua_CFunction; |
|
196 cdecl; external LuaDLL; |
|
197 |
|
198 |
|
199 (* |
|
200 ** basic stack manipulation |
|
201 *) |
|
202 function lua_gettop(L : Plua_State) : LongInt; |
|
203 cdecl; external LuaDLL; |
|
204 procedure lua_settop(L : Plua_State; idx : LongInt); |
|
205 cdecl; external LuaDLL; |
|
206 procedure lua_pushvalue(L : Plua_State; idx : LongInt); |
|
207 cdecl; external LuaDLL; |
|
208 procedure lua_remove(L : Plua_State; idx : LongInt); |
|
209 cdecl; external LuaDLL; |
|
210 procedure lua_insert(L : Plua_State; idx : LongInt); |
|
211 cdecl; external LuaDLL; |
|
212 procedure lua_replace(L : Plua_State; idx : LongInt); |
|
213 cdecl; external LuaDLL; |
|
214 function lua_checkstack(L : Plua_State; sz : LongInt) : LongBool; |
|
215 cdecl; external LuaDLL; |
|
216 |
|
217 procedure lua_xmove(src, dest : Plua_State; n : LongInt); |
|
218 cdecl; external LuaDLL; |
|
219 |
|
220 |
|
221 (* |
|
222 ** access functions (stack -> C) |
|
223 *) |
|
224 function lua_isnumber(L : Plua_State; idx : LongInt) : LongBool; |
|
225 cdecl; external LuaDLL; |
|
226 function lua_isstring(L : Plua_State; idx : LongInt) : LongBool; |
|
227 cdecl; external LuaDLL; |
|
228 function lua_iscfunction(L : Plua_State; idx : LongInt) : LongBool; |
|
229 cdecl; external LuaDLL; |
|
230 function lua_isuserdata(L : Plua_State; idx : LongInt) : LongBool; |
|
231 cdecl; external LuaDLL; |
|
232 function lua_type(L : Plua_State; idx : LongInt) : LongInt; |
|
233 cdecl; external LuaDLL; |
|
234 function lua_typename(L : Plua_State; tp : LongInt) : PChar; |
|
235 cdecl; external LuaDLL; |
|
236 |
|
237 function lua_equal(L : Plua_State; idx1, idx2 : LongInt) : LongBool; |
|
238 cdecl; external LuaDLL; |
|
239 function lua_rawequal(L : Plua_State; idx1, idx2 : LongInt) : LongBool; |
|
240 cdecl; external LuaDLL; |
|
241 function lua_lessthan(L : Plua_State; idx1, idx2 : LongInt) : LongBool; |
|
242 cdecl; external LuaDLL; |
|
243 |
|
244 function lua_tonumber(L : Plua_State; idx : LongInt) : lua_Number; |
|
245 cdecl; external LuaDLL; |
|
246 function lua_tointeger(L : Plua_State; idx : LongInt) : lua_Integer; |
|
247 cdecl; external LuaDLL; |
|
248 function lua_toboolean(L : Plua_State; idx : LongInt) : LongBool; |
|
249 cdecl; external LuaDLL; |
|
250 function lua_tolstring(L : Plua_State; idx : LongInt; |
|
251 len : Psize_t) : PChar; |
|
252 cdecl; external LuaDLL; |
|
253 function lua_objlen(L : Plua_State; idx : LongInt) : size_t; |
|
254 cdecl; external LuaDLL; |
|
255 function lua_tocfunction(L : Plua_State; idx : LongInt) : lua_CFunction; |
|
256 cdecl; external LuaDLL; |
|
257 function lua_touserdata(L : Plua_State; idx : LongInt) : Pointer; |
|
258 cdecl; external LuaDLL; |
|
259 function lua_tothread(L : Plua_State; idx : LongInt) : Plua_State; |
|
260 cdecl; external LuaDLL; |
|
261 function lua_topointer(L : Plua_State; idx : LongInt) : Pointer; |
|
262 cdecl; external LuaDLL; |
|
263 |
|
264 |
|
265 (* |
|
266 ** push functions (C -> stack) |
|
267 *) |
|
268 procedure lua_pushnil(L : Plua_State); |
|
269 cdecl; external LuaDLL; |
|
270 procedure lua_pushnumber(L : Plua_State; n : lua_Number); |
|
271 cdecl; external LuaDLL; |
|
272 procedure lua_pushinteger(L : Plua_State; n : lua_Integer); |
|
273 cdecl; external LuaDLL; |
|
274 procedure lua_pushlstring(L : Plua_State; const s : PChar; ls : size_t); |
|
275 cdecl; external LuaDLL; |
|
276 procedure lua_pushstring(L : Plua_State; const s : PChar); |
|
277 cdecl; external LuaDLL; |
|
278 function lua_pushvfstring(L : Plua_State; |
|
279 const fmt : PChar; argp : Pointer) : PChar; |
|
280 cdecl; external LuaDLL; |
|
281 function lua_pushfstring(L : Plua_State; const fmt : PChar) : PChar; varargs; |
|
282 cdecl; external LuaDLL; |
|
283 procedure lua_pushcclosure(L : Plua_State; fn : lua_CFunction; n : LongInt); |
|
284 cdecl; external LuaDLL; |
|
285 procedure lua_pushboolean(L : Plua_State; b : LongBool); |
|
286 cdecl; external LuaDLL; |
|
287 procedure lua_pushlightuserdata(L : Plua_State; p : Pointer); |
|
288 cdecl; external LuaDLL; |
|
289 function lua_pushthread(L : Plua_state) : Cardinal; |
|
290 cdecl; external LuaDLL; |
|
291 |
|
292 |
|
293 (* |
|
294 ** get functions (Lua -> stack) |
|
295 *) |
|
296 procedure lua_gettable(L : Plua_State ; idx : LongInt); |
|
297 cdecl; external LuaDLL; |
|
298 procedure lua_getfield(L : Plua_State; idx : LongInt; k : PChar); |
|
299 cdecl; external LuaDLL; |
|
300 procedure lua_rawget(L : Plua_State; idx : LongInt); |
|
301 cdecl; external LuaDLL; |
|
302 procedure lua_rawgeti(L : Plua_State; idx, n : LongInt); |
|
303 cdecl; external LuaDLL; |
|
304 procedure lua_createtable(L : Plua_State; narr, nrec : LongInt); |
|
305 cdecl; external LuaDLL; |
|
306 function lua_newuserdata(L : Plua_State; sz : size_t) : Pointer; |
|
307 cdecl; external LuaDLL; |
|
308 function lua_getmetatable(L : Plua_State; objindex : LongInt) : LongBool; |
|
309 cdecl; external LuaDLL; |
|
310 procedure lua_getfenv(L : Plua_State; idx : LongInt); |
|
311 cdecl; external LuaDLL; |
|
312 |
|
313 |
|
314 (* |
|
315 ** set functions (stack -> Lua) |
|
316 *) |
|
317 procedure lua_settable(L : Plua_State; idx : LongInt); |
|
318 cdecl; external LuaDLL; |
|
319 procedure lua_setfield(L : Plua_State; idx : LongInt; const k : PChar); |
|
320 cdecl; external LuaDLL; |
|
321 procedure lua_rawset(L : Plua_State; idx : LongInt); |
|
322 cdecl; external LuaDLL; |
|
323 procedure lua_rawseti(L : Plua_State; idx , n: LongInt); |
|
324 cdecl; external LuaDLL; |
|
325 function lua_setmetatable(L : Plua_State; objindex : LongInt): LongBool; |
|
326 cdecl; external LuaDLL; |
|
327 function lua_setfenv(L : Plua_State; idx : LongInt): LongBool; |
|
328 cdecl; external LuaDLL; |
|
329 |
|
330 (* |
|
331 ** `load' and `call' functions (load and run Lua code) |
|
332 *) |
|
333 procedure lua_call(L : Plua_State; nargs, nresults : LongInt); |
|
334 cdecl; external LuaDLL; |
|
335 function lua_pcall(L : Plua_State; |
|
336 nargs, nresults, errfunc : LongInt) : LongInt; |
|
337 cdecl; external LuaDLL; |
|
338 function lua_cpcall(L : Plua_State; |
|
339 func : lua_CFunction; ud : Pointer) : LongInt; |
|
340 cdecl; external LuaDLL; |
|
341 function lua_load(L : Plua_State; reader : lua_Reader; |
|
342 dt : Pointer; const chunkname : PChar) : LongInt; |
|
343 cdecl; external LuaDLL; |
|
344 |
|
345 function lua_dump(L : Plua_State; writer : lua_Writer; data: Pointer) : LongInt; |
|
346 cdecl; external LuaDLL; |
|
347 |
|
348 |
|
349 (* |
|
350 ** coroutine functions |
|
351 *) |
|
352 function lua_yield(L : Plua_State; nresults : LongInt) : LongInt; |
|
353 cdecl; external LuaDLL; |
|
354 function lua_resume(L : Plua_State; narg : LongInt) : LongInt; |
|
355 cdecl; external LuaDLL; |
|
356 function lua_status(L : Plua_State) : LongInt; |
|
357 cdecl; external LuaDLL; |
|
358 |
|
359 (* |
|
360 ** garbage-collection functions and options |
|
361 *) |
|
362 const |
|
363 LUA_GCSTOP = 0; |
|
364 LUA_GCRESTART = 1; |
|
365 LUA_GCCOLLECT = 2; |
|
366 LUA_GCCOUNT = 3; |
|
367 LUA_GCCOUNTB = 4; |
|
368 LUA_GCSTEP = 5; |
|
369 LUA_GCSETPAUSE = 6; |
|
370 LUA_GCSETSTEPMUL = 7; |
|
371 |
|
372 function lua_gc(L : Plua_State; what, data : LongInt) : LongInt; |
|
373 cdecl; external LuaDLL; |
|
374 |
|
375 (* |
|
376 ** miscellaneous functions |
|
377 *) |
|
378 function lua_error(L : Plua_State) : LongInt; |
|
379 cdecl; external LuaDLL; |
|
380 |
|
381 function lua_next(L : Plua_State; idx : LongInt) : LongInt; |
|
382 cdecl; external LuaDLL; |
|
383 |
|
384 procedure lua_concat(L : Plua_State; n : LongInt); |
|
385 cdecl; external LuaDLL; |
|
386 |
|
387 function lua_getallocf(L : Plua_State; ud : PPointer) : lua_Alloc; |
|
388 cdecl; external LuaDLL; |
|
389 procedure lua_setallocf(L : Plua_State; f : lua_Alloc; ud : Pointer); |
|
390 cdecl; external LuaDLL; |
|
391 |
|
392 (* |
|
393 ** =============================================================== |
|
394 ** some useful macros |
|
395 ** =============================================================== |
|
396 *) |
|
397 procedure lua_pop(L : Plua_State; n : LongInt); |
|
398 |
|
399 procedure lua_newtable(L : Plua_State); |
|
400 |
|
401 procedure lua_register(L : Plua_State; n : PChar; f : lua_CFunction); |
|
402 |
|
403 procedure lua_pushcfunction(L : Plua_State; f : lua_CFunction); |
|
404 |
|
405 function lua_strlen(L : Plua_State; idx : LongInt) : LongInt; |
|
406 |
|
407 function lua_isfunction(L : Plua_State; n : LongInt) : Boolean; |
|
408 function lua_istable(L : Plua_State; n : LongInt) : Boolean; |
|
409 function lua_islightuserdata(L : Plua_State; n : LongInt) : Boolean; |
|
410 function lua_isnil(L : Plua_State; n : LongInt) : Boolean; |
|
411 function lua_isboolean(L : Plua_State; n : LongInt) : Boolean; |
|
412 function lua_isthread(L : Plua_State; n : LongInt) : Boolean; |
|
413 function lua_isnone(L : Plua_State; n : LongInt) : Boolean; |
|
414 function lua_isnoneornil(L : Plua_State; n : LongInt) : Boolean; |
|
415 |
|
416 procedure lua_pushliteral(L : Plua_State; s : PChar); |
|
417 |
|
418 procedure lua_setglobal(L : Plua_State; s : PChar); |
|
419 procedure lua_getglobal(L : Plua_State; s : PChar); |
|
420 |
|
421 function lua_tostring(L : Plua_State; idx : LongInt) : PChar; |
|
422 |
|
423 |
|
424 (* |
|
425 ** compatibility macros and functions |
|
426 *) |
|
427 function lua_open : Plua_State; |
|
428 |
|
429 procedure lua_getregistry(L : Plua_State); |
|
430 |
|
431 function lua_getgccount(L : Plua_State) : LongInt; |
|
432 |
|
433 type |
|
434 lua_Chuckreader = type lua_Reader; |
|
435 lua_Chuckwriter = type lua_Writer; |
|
436 |
|
437 (* ====================================================================== *) |
|
438 |
|
439 (* |
|
440 ** {====================================================================== |
|
441 ** Debug API |
|
442 ** ======================================================================= |
|
443 *) |
|
444 |
|
445 (* |
|
446 ** Event codes |
|
447 *) |
|
448 const |
|
449 LUA_HOOKCALL = 0; |
|
450 LUA_HOOKRET = 1; |
|
451 LUA_HOOKLINE = 2; |
|
452 LUA_HOOKCOUNT = 3; |
|
453 LUA_HOOKTAILRET = 4; |
|
454 |
|
455 |
|
456 (* |
|
457 ** Event masks |
|
458 *) |
|
459 LUA_MASKCALL = 1 shl LUA_HOOKCALL; |
|
460 LUA_MASKRET = 1 shl LUA_HOOKRET; |
|
461 LUA_MASKLINE = 1 shl LUA_HOOKLINE; |
|
462 LUA_MASKCOUNT = 1 shl LUA_HOOKCOUNT; |
|
463 |
|
464 type |
|
465 lua_Debug = packed record |
|
466 event : LongInt; |
|
467 name : PChar; (* (n) *) |
|
468 namewhat : PChar; (* (n) `global', `local', `field', `method' *) |
|
469 what : PChar; (* (S) `Lua', `C', `main', `tail' *) |
|
470 source : PChar; (* (S) *) |
|
471 currentline : LongInt; (* (l) *) |
|
472 nups : LongInt; (* (u) number of upvalues *) |
|
473 linedefined : LongInt; (* (S) *) |
|
474 short_src : array [0..LUA_IDSIZE-1] of Char; (* (S) *) |
|
475 (* private part *) |
|
476 i_ci : LongInt; (* active function *) |
|
477 end; |
|
478 Plua_Debug = ^lua_Debug; |
|
479 |
|
480 (* Functions to be called by the debuger in specific events *) |
|
481 lua_Hook = procedure (L : Plua_State; ar : Plua_Debug); cdecl; |
|
482 |
|
483 |
|
484 function lua_getstack(L : Plua_State; level : LongInt; |
|
485 ar : Plua_Debug) : LongInt; |
|
486 cdecl; external LuaDLL; |
|
487 function lua_getinfo(L : Plua_State; const what : PChar; |
|
488 ar: Plua_Debug): LongInt; |
|
489 cdecl; external LuaDLL; |
|
490 function lua_getlocal(L : Plua_State; |
|
491 ar : Plua_Debug; n : LongInt) : PChar; |
|
492 cdecl; external LuaDLL; |
|
493 function lua_setlocal(L : Plua_State; |
|
494 ar : Plua_Debug; n : LongInt) : PChar; |
|
495 cdecl; external LuaDLL; |
|
496 function lua_getupvalue(L : Plua_State; funcindex, n : LongInt) : PChar; |
|
497 cdecl; external LuaDLL; |
|
498 function lua_setupvalue(L : Plua_State; funcindex, n : LongInt) : PChar; |
|
499 cdecl; external LuaDLL; |
|
500 |
|
501 function lua_sethook(L : Plua_State; func : lua_Hook; |
|
502 mask, count: LongInt): LongInt; |
|
503 cdecl; external LuaDLL; |
|
504 {$IFDEF LUA_GETHOOK} |
|
505 function lua_gethook(L : Plua_State) : lua_Hook; |
|
506 cdecl; external LuaDLL; |
|
507 {$ENDIF} |
|
508 |
|
509 function lua_gethookmask(L : Plua_State) : LongInt; |
|
510 cdecl; external LuaDLL; |
|
511 function lua_gethookcount(L : Plua_State) : LongInt; |
|
512 cdecl; external LuaDLL; |
|
513 |
|
514 |
|
515 (*****************************************************************************) |
|
516 (* lualib.h *) |
|
517 (*****************************************************************************) |
|
518 |
|
519 (* |
|
520 ** $Id: lualib.h,v 1.36 2005/12/27 17:12:00 roberto Exp $ |
|
521 ** Lua standard libraries |
|
522 ** See Copyright Notice at the end of this file |
|
523 *) |
|
524 |
|
525 const |
|
526 (* Key to file-handle type *) |
|
527 LUA_FILEHANDLE = 'FILE*'; |
|
528 |
|
529 LUA_COLIBNAME = 'coroutine'; |
|
530 LUA_TABLIBNAME = 'table'; |
|
531 LUA_IOLIBNAME = 'io'; |
|
532 LUA_OSLIBNAME = 'os'; |
|
533 LUA_STRLIBNAME = 'string'; |
|
534 LUA_MATHLIBNAME = 'math'; |
|
535 LUA_DBLIBNAME = 'debug'; |
|
536 LUA_LOADLIBNAME = 'package'; |
|
537 |
|
538 function luaopen_base(L : Plua_State) : LongInt; |
|
539 cdecl; external LuaDLL; |
|
540 |
|
541 function luaopen_table(L : Plua_State) : LongInt; |
|
542 cdecl; external LuaDLL; |
|
543 |
|
544 function luaopen_io(L : Plua_State) : LongInt; |
|
545 cdecl; external LuaDLL; |
|
546 |
|
547 function luaopen_os(L : Plua_State) : LongInt; |
|
548 cdecl; external LuaDLL; |
|
549 |
|
550 function luaopen_string(L : Plua_State) : LongInt; |
|
551 cdecl; external LuaDLL; |
|
552 |
|
553 function luaopen_math(L : Plua_State) : LongInt; |
|
554 cdecl; external LuaDLL; |
|
555 |
|
556 function luaopen_debug(L : Plua_State) : LongInt; |
|
557 cdecl; external LuaDLL; |
|
558 |
|
559 function luaopen_package(L : Plua_State) : LongInt; |
|
560 cdecl; external LuaDLL; |
|
561 |
|
562 procedure luaL_openlibs(L : Plua_State); |
|
563 cdecl; external LuaDLL; |
|
564 |
|
565 procedure lua_assert(x : Boolean); // a macro |
|
566 |
|
567 |
|
568 (*****************************************************************************) |
|
569 (* lauxlib.h *) |
|
570 (*****************************************************************************) |
|
571 |
|
572 (* |
|
573 ** $Id: lauxlib.h,v 1.87 2005/12/29 15:32:11 roberto Exp $ |
|
574 ** Auxiliary functions for building Lua libraries |
|
575 ** See Copyright Notice at the end of this file. |
|
576 *) |
|
577 |
|
578 // not compatibility with the behavior of setn/getn in Lua 5.0 |
|
579 function luaL_getn(L : Plua_State; idx : LongInt) : LongInt; |
|
580 procedure luaL_setn(L : Plua_State; i, j : LongInt); |
|
581 |
|
582 const |
|
583 LUA_ERRFILE = LUA_ERRERR + 1; |
|
584 |
|
585 type |
|
586 luaL_Reg = packed record |
|
587 name : PChar; |
|
588 func : lua_CFunction; |
|
589 end; |
|
590 PluaL_Reg = ^luaL_Reg; |
|
591 |
|
592 |
|
593 procedure luaL_openlib(L : Plua_State; const libname : PChar; |
|
594 const lr : PluaL_Reg; nup : LongInt); |
|
595 cdecl; external LuaDLL; |
|
596 procedure luaL_register(L : Plua_State; const libname : PChar; |
|
597 const lr : PluaL_Reg); |
|
598 cdecl; external LuaDLL; |
|
599 function luaL_getmetafield(L : Plua_State; obj : LongInt; |
|
600 const e : PChar) : LongInt; |
|
601 cdecl; external LuaDLL; |
|
602 function luaL_callmeta(L : Plua_State; obj : LongInt; |
|
603 const e : PChar) : LongInt; |
|
604 cdecl; external LuaDLL; |
|
605 function luaL_typerror(L : Plua_State; narg : LongInt; |
|
606 const tname : PChar) : LongInt; |
|
607 cdecl; external LuaDLL; |
|
608 function luaL_argerror(L : Plua_State; numarg : LongInt; |
|
609 const extramsg : PChar) : LongInt; |
|
610 cdecl; external LuaDLL; |
|
611 function luaL_checklstring(L : Plua_State; numArg : LongInt; |
|
612 ls : Psize_t) : PChar; |
|
613 cdecl; external LuaDLL; |
|
614 function luaL_optlstring(L : Plua_State; numArg : LongInt; |
|
615 const def: PChar; ls: Psize_t) : PChar; |
|
616 cdecl; external LuaDLL; |
|
617 function luaL_checknumber(L : Plua_State; numArg : LongInt) : lua_Number; |
|
618 cdecl; external LuaDLL; |
|
619 function luaL_optnumber(L : Plua_State; nArg : LongInt; |
|
620 def : lua_Number) : lua_Number; |
|
621 cdecl; external LuaDLL; |
|
622 |
|
623 function luaL_checkinteger(L : Plua_State; numArg : LongInt) : lua_Integer; |
|
624 cdecl; external LuaDLL; |
|
625 function luaL_optinteger(L : Plua_State; nArg : LongInt; |
|
626 def : lua_Integer) : lua_Integer; |
|
627 cdecl; external LuaDLL; |
|
628 |
|
629 procedure luaL_checkstack(L : Plua_State; sz : LongInt; const msg : PChar); |
|
630 cdecl; external LuaDLL; |
|
631 procedure luaL_checktype(L : Plua_State; narg, t : LongInt); |
|
632 cdecl; external LuaDLL; |
|
633 procedure luaL_checkany(L : Plua_State; narg : LongInt); |
|
634 cdecl; external LuaDLL; |
|
635 |
|
636 function luaL_newmetatable(L : Plua_State; const tname : PChar) : LongInt; |
|
637 cdecl; external LuaDLL; |
|
638 function luaL_checkudata(L : Plua_State; ud : LongInt; |
|
639 const tname : PChar) : Pointer; |
|
640 cdecl; external LuaDLL; |
|
641 |
|
642 procedure luaL_where(L : Plua_State; lvl : LongInt); |
|
643 cdecl; external LuaDLL; |
|
644 function luaL_error(L : Plua_State; const fmt : PChar) : LongInt; varargs; |
|
645 cdecl; external LuaDLL; |
|
646 |
|
647 function luaL_checkoption(L : Plua_State; narg : LongInt; const def : PChar; |
|
648 const lst : array of PChar) : LongInt; |
|
649 cdecl; external LuaDLL; |
|
650 |
|
651 function luaL_ref(L : Plua_State; t : LongInt) : LongInt; |
|
652 cdecl; external LuaDLL; |
|
653 procedure luaL_unref(L : Plua_State; t, ref : LongInt); |
|
654 cdecl; external LuaDLL; |
|
655 |
|
656 function luaL_loadfile(L : Plua_State; const filename : PChar) : LongInt; |
|
657 cdecl; external LuaDLL; |
|
658 function luaL_loadbuffer(L : Plua_State; const buff : PChar; |
|
659 sz : size_t; const name: PChar) : LongInt; |
|
660 cdecl; external LuaDLL; |
|
661 |
|
662 function luaL_loadstring(L : Plua_State; const s : Pchar) : LongInt; |
|
663 cdecl; external LuaDLL; |
|
664 |
|
665 function luaL_newstate : Plua_State; |
|
666 cdecl; external LuaDLL; |
|
667 |
|
668 function luaL_gsub(L : Plua_State; const s, p, r : PChar) : PChar; |
|
669 cdecl; external LuaDLL; |
|
670 |
|
671 function luaL_findtable(L : Plua_State; idx : LongInt; |
|
672 const fname : PChar; szhint : LongInt) : PChar; |
|
673 cdecl; external LuaDLL; |
|
674 |
|
675 |
|
676 (* |
|
677 ** =============================================================== |
|
678 ** some useful macros |
|
679 ** =============================================================== |
|
680 *) |
|
681 |
|
682 function luaL_argcheck(L : Plua_State; cond : Boolean; numarg : LongInt; |
|
683 extramsg : PChar): LongInt; |
|
684 function luaL_checkstring(L : Plua_State; n : LongInt) : PChar; |
|
685 function luaL_optstring(L : Plua_State; n : LongInt; d : PChar) : PChar; |
|
686 function luaL_checkint(L : Plua_State; n : LongInt) : LongInt; |
|
687 function luaL_optint(L : Plua_State; n, d : LongInt): LongInt; |
|
688 function luaL_checklong(L : Plua_State; n : LongInt) : LongInt; |
|
689 function luaL_optlong(L : Plua_State; n : LongInt; d : LongInt) : LongInt; |
|
690 |
|
691 function luaL_typename(L : Plua_State; idx : LongInt) : PChar; |
|
692 |
|
693 function luaL_dofile(L : Plua_State; fn : PChar) : LongInt; |
|
694 |
|
695 function luaL_dostring(L : Plua_State; s : PChar) : LongInt; |
|
696 |
|
697 procedure luaL_getmetatable(L : Plua_State; n : PChar); |
|
698 |
|
699 (* not implemented yet |
|
700 #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) |
|
701 *) |
|
702 |
|
703 (* |
|
704 ** {====================================================== |
|
705 ** Generic Buffer manipulation |
|
706 ** ======================================================= |
|
707 *) |
|
708 |
|
709 type |
|
710 luaL_Buffer = packed record |
|
711 p : PChar; (* current position in buffer *) |
|
712 lvl : LongInt; (* number of strings in the stack (level) *) |
|
713 L : Plua_State; |
|
714 buffer : array [0..LUAL_BUFFERSIZE-1] of Char; |
|
715 end; |
|
716 PluaL_Buffer = ^luaL_Buffer; |
|
717 |
|
718 procedure luaL_addchar(B : PluaL_Buffer; c : Char); |
|
719 |
|
720 (* compatibility only *) |
|
721 procedure luaL_putchar(B : PluaL_Buffer; c : Char); |
|
722 |
|
723 procedure luaL_addsize(B : PluaL_Buffer; n : LongInt); |
|
724 |
|
725 procedure luaL_buffinit(L : Plua_State; B : PluaL_Buffer); |
|
726 cdecl; external LuaDLL; |
|
727 function luaL_prepbuffer(B : PluaL_Buffer) : PChar; |
|
728 cdecl; external LuaDLL; |
|
729 procedure luaL_addlstring(B : PluaL_Buffer; const s : PChar; ls : size_t); |
|
730 cdecl; external LuaDLL; |
|
731 procedure luaL_addstring(B : PluaL_Buffer; const s : PChar); |
|
732 cdecl; external LuaDLL; |
|
733 procedure luaL_addvalue(B : PluaL_Buffer); |
|
734 cdecl; external LuaDLL; |
|
735 procedure luaL_pushresult(B : PluaL_Buffer); |
|
736 cdecl; external LuaDLL; |
|
737 |
|
738 (* ====================================================== *) |
|
739 |
|
740 |
|
741 (* compatibility with ref system *) |
|
742 |
|
743 (* pre-defined references *) |
|
744 const |
|
745 LUA_NOREF = -2; |
|
746 LUA_REFNIL = -1; |
|
747 |
|
748 function lua_ref(L : Plua_State; lock : Boolean) : LongInt; |
|
749 |
|
750 procedure lua_unref(L : Plua_State; ref : LongInt); |
|
751 |
|
752 procedure lua_getref(L : Plua_State; ref : LongInt); |
|
753 |
|
754 |
|
755 (******************************************************************************) |
|
756 (******************************************************************************) |
|
757 (******************************************************************************) |
|
758 |
|
759 implementation |
|
760 |
|
761 uses |
|
762 SysUtils; |
|
763 |
|
764 (*****************************************************************************) |
|
765 (* luaconfig.h *) |
|
766 (*****************************************************************************) |
|
767 |
|
768 function lua_readline(L : Plua_State; var b : PChar; p : PChar): Boolean; |
|
769 var |
|
770 s : AnsiString; |
|
771 begin |
|
772 Write(p); // show prompt |
|
773 ReadLn(s); // get line |
|
774 b := PChar(s); // and return it |
|
775 lua_readline := (b[0] <> #4); // test for ctrl-D |
|
776 end; |
|
777 |
|
778 procedure lua_saveline(L : Plua_State; idx : LongInt); |
|
779 begin |
|
780 end; |
|
781 |
|
782 procedure lua_freeline(L : Plua_State; b : PChar); |
|
783 begin |
|
784 end; |
|
785 |
|
786 |
|
787 (*****************************************************************************) |
|
788 (* lua.h *) |
|
789 (*****************************************************************************) |
|
790 |
|
791 function lua_upvalueindex(idx : LongInt) : LongInt; |
|
792 begin |
|
793 lua_upvalueindex := LUA_GLOBALSINDEX - idx; |
|
794 end; |
|
795 |
|
796 procedure lua_pop(L : Plua_State; n : LongInt); |
|
797 begin |
|
798 lua_settop(L, -n - 1); |
|
799 end; |
|
800 |
|
801 procedure lua_newtable(L : Plua_State); |
|
802 begin |
|
803 lua_createtable(L, 0, 0); |
|
804 end; |
|
805 |
|
806 procedure lua_register(L : Plua_State; n : PChar; f : lua_CFunction); |
|
807 begin |
|
808 lua_pushcfunction(L, f); |
|
809 lua_setglobal(L, n); |
|
810 end; |
|
811 |
|
812 procedure lua_pushcfunction(L : Plua_State; f : lua_CFunction); |
|
813 begin |
|
814 lua_pushcclosure(L, f, 0); |
|
815 end; |
|
816 |
|
817 function lua_strlen(L : Plua_State; idx : LongInt) : LongInt; |
|
818 begin |
|
819 lua_strlen := lua_objlen(L, idx); |
|
820 end; |
|
821 |
|
822 function lua_isfunction(L : Plua_State; n : LongInt) : Boolean; |
|
823 begin |
|
824 lua_isfunction := lua_type(L, n) = LUA_TFUNCTION; |
|
825 end; |
|
826 |
|
827 function lua_istable(L : Plua_State; n : LongInt) : Boolean; |
|
828 begin |
|
829 lua_istable := lua_type(L, n) = LUA_TTABLE; |
|
830 end; |
|
831 |
|
832 function lua_islightuserdata(L : Plua_State; n : LongInt) : Boolean; |
|
833 begin |
|
834 lua_islightuserdata := lua_type(L, n) = LUA_TLIGHTUSERDATA; |
|
835 end; |
|
836 |
|
837 function lua_isnil(L : Plua_State; n : LongInt) : Boolean; |
|
838 begin |
|
839 lua_isnil := lua_type(L, n) = LUA_TNIL; |
|
840 end; |
|
841 |
|
842 function lua_isboolean(L : Plua_State; n : LongInt) : Boolean; |
|
843 begin |
|
844 lua_isboolean := lua_type(L, n) = LUA_TBOOLEAN; |
|
845 end; |
|
846 |
|
847 function lua_isthread(L : Plua_State; n : LongInt) : Boolean; |
|
848 begin |
|
849 lua_isthread := lua_type(L, n) = LUA_TTHREAD; |
|
850 end; |
|
851 |
|
852 function lua_isnone(L : Plua_State; n : LongInt) : Boolean; |
|
853 begin |
|
854 lua_isnone := lua_type(L, n) = LUA_TNONE; |
|
855 end; |
|
856 |
|
857 function lua_isnoneornil(L : Plua_State; n : LongInt) : Boolean; |
|
858 begin |
|
859 lua_isnoneornil := lua_type(L, n) <= 0; |
|
860 end; |
|
861 |
|
862 procedure lua_pushliteral(L : Plua_State; s : PChar); |
|
863 begin |
|
864 lua_pushlstring(L, s, StrLen(s)); |
|
865 end; |
|
866 |
|
867 procedure lua_setglobal(L : Plua_State; s : PChar); |
|
868 begin |
|
869 lua_setfield(L, LUA_GLOBALSINDEX, s); |
|
870 end; |
|
871 |
|
872 procedure lua_getglobal(L: Plua_State; s: PChar); |
|
873 begin |
|
874 lua_getfield(L, LUA_GLOBALSINDEX, s); |
|
875 end; |
|
876 |
|
877 function lua_tostring(L : Plua_State; idx : LongInt) : PChar; |
|
878 begin |
|
879 lua_tostring := lua_tolstring(L, idx, nil); |
|
880 end; |
|
881 |
|
882 function lua_open : Plua_State; |
|
883 begin |
|
884 lua_open := luaL_newstate; |
|
885 end; |
|
886 |
|
887 procedure lua_getregistry(L : Plua_State); |
|
888 begin |
|
889 lua_pushvalue(L, LUA_REGISTRYINDEX); |
|
890 end; |
|
891 |
|
892 function lua_getgccount(L : Plua_State) : LongInt; |
|
893 begin |
|
894 lua_getgccount := lua_gc(L, LUA_GCCOUNT, 0); |
|
895 end; |
|
896 |
|
897 |
|
898 (*****************************************************************************) |
|
899 (* lualib.h *) |
|
900 (*****************************************************************************) |
|
901 |
|
902 procedure lua_assert(x : Boolean); |
|
903 begin |
|
904 end; |
|
905 |
|
906 |
|
907 (*****************************************************************************) |
|
908 (* lauxlib.h n *) |
|
909 (*****************************************************************************) |
|
910 |
|
911 function luaL_getn(L : Plua_State; idx : LongInt) : LongInt; |
|
912 begin |
|
913 luaL_getn := lua_objlen(L, idx); |
|
914 end; |
|
915 |
|
916 procedure luaL_setn(L : plua_State; i, j : LongInt); |
|
917 begin |
|
918 (* no op *) |
|
919 end; |
|
920 |
|
921 function luaL_argcheck(L : Plua_State; cond : Boolean; numarg : LongInt; |
|
922 extramsg : PChar): LongInt; |
|
923 begin |
|
924 if not cond then |
|
925 luaL_argcheck := luaL_argerror(L, numarg, extramsg) |
|
926 else |
|
927 luaL_argcheck := 0; |
|
928 end; |
|
929 |
|
930 function luaL_checkstring(L : Plua_State; n : LongInt) : PChar; |
|
931 begin |
|
932 luaL_checkstring := luaL_checklstring(L, n, nil); |
|
933 end; |
|
934 |
|
935 function luaL_optstring(L : Plua_State; n : LongInt; d : PChar) : PChar; |
|
936 begin |
|
937 luaL_optstring := luaL_optlstring(L, n, d, nil); |
|
938 end; |
|
939 |
|
940 function luaL_checkint(L : Plua_State; n : LongInt) : LongInt; |
|
941 begin |
|
942 luaL_checkint := luaL_checkinteger(L, n); |
|
943 end; |
|
944 |
|
945 function luaL_optint(L : Plua_State; n, d : LongInt): LongInt; |
|
946 begin |
|
947 luaL_optint := luaL_optinteger(L, n, d); |
|
948 end; |
|
949 |
|
950 function luaL_checklong(L : Plua_State; n : LongInt) : LongInt; |
|
951 begin |
|
952 luaL_checklong := luaL_checkinteger(L, n); |
|
953 end; |
|
954 |
|
955 function luaL_optlong(L : Plua_State; n : LongInt; d : LongInt) : LongInt; |
|
956 begin |
|
957 luaL_optlong := luaL_optinteger(L, n, d); |
|
958 end; |
|
959 |
|
960 function luaL_typename(L : Plua_State; idx : LongInt) : PChar; |
|
961 begin |
|
962 luaL_typename := lua_typename( L, lua_type(L, idx) ); |
|
963 end; |
|
964 |
|
965 function luaL_dofile(L : Plua_State; fn : PChar) : LongInt; |
|
966 begin |
|
967 luaL_dofile := luaL_loadfile(L, fn); |
|
968 if luaL_dofile = 0 then |
|
969 luaL_dofile := lua_pcall(L, 0, 0, 0); |
|
970 end; |
|
971 |
|
972 function luaL_dostring(L : Plua_State; s : PChar) : LongInt; |
|
973 begin |
|
974 luaL_dostring := luaL_loadstring(L, s); |
|
975 if luaL_dostring = 0 then |
|
976 luaL_dostring := lua_pcall(L, 0, 0, 0); |
|
977 end; |
|
978 |
|
979 procedure luaL_getmetatable(L : Plua_State; n : PChar); |
|
980 begin |
|
981 lua_getfield(L, LUA_REGISTRYINDEX, n); |
|
982 end; |
|
983 |
|
984 procedure luaL_addchar(B : PluaL_Buffer; c : Char); |
|
985 begin |
|
986 if not(B^.p < B^.buffer + LUAL_BUFFERSIZE) then |
|
987 luaL_prepbuffer(B); |
|
988 B^.p^ := c; |
|
989 Inc(B^.p); |
|
990 end; |
|
991 |
|
992 procedure luaL_putchar(B : PluaL_Buffer; c : Char); |
|
993 begin |
|
994 luaL_addchar(B, c); |
|
995 end; |
|
996 |
|
997 procedure luaL_addsize(B : PluaL_Buffer; n : LongInt); |
|
998 begin |
|
999 Inc(B^.p, n); |
|
1000 end; |
|
1001 |
|
1002 function lua_ref(L : Plua_State; lock : Boolean) : LongInt; |
|
1003 begin |
|
1004 if lock then |
|
1005 lua_ref := luaL_ref(L, LUA_REGISTRYINDEX) |
|
1006 else begin |
|
1007 lua_pushstring(L, 'unlocked references are obsolete'); |
|
1008 lua_error(L); |
|
1009 lua_ref := 0; |
|
1010 end; |
|
1011 end; |
|
1012 |
|
1013 procedure lua_unref(L : Plua_State; ref : LongInt); |
|
1014 begin |
|
1015 luaL_unref(L, LUA_REGISTRYINDEX, ref); |
|
1016 end; |
|
1017 |
|
1018 procedure lua_getref(L : Plua_State; ref : LongInt); |
|
1019 begin |
|
1020 lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
|
1021 end; |
|
1022 |
|
1023 |
|
1024 (****************************************************************************** |
|
1025 * Original copyright for the lua source and headers: |
|
1026 * 1994-2004 Tecgraf, PUC-Rio. |
|
1027 * www.lua.org. |
|
1028 * |
|
1029 * |
|
1030 * Permission is hereby granted, free of charge, to any person obtaining |
|
1031 * a copy of this software and associated documentation files (the |
|
1032 * "Software"), to deal in the Software without restriction, including |
|
1033 * without limitation the rights to use, copy, modify, merge, publish, |
|
1034 * distribute, sublicense, and/or sell copies of the Software, and to |
|
1035 * permit persons to whom the Software is furnished to do so, subject to |
|
1036 * the following conditions: |
|
1037 * |
|
1038 * The above copyright notice and this permission notice shall be |
|
1039 * included in all copies or substantial portions of the Software. |
|
1040 * |
|
1041 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
1042 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
1043 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
1044 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
1045 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
1046 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
1047 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
1048 ******************************************************************************) |
|
1049 |
|
1050 end. |
|
1051 |