lua.pas 31 KB

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