lua.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. (*
  2. ** $Id: lua.pas,v 1.4 2007-01-20 16:40:27 jfgoulet Exp $
  3. ** Lua - An Extensible Extension Language
  4. ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
  5. ** See Copyright Notice at the end of this file
  6. **
  7. ** Translation form C and Delphi adaptation of Code :
  8. ** Massimo Magnano, Jean-Francois Goulet 2006
  9. *)
  10. unit lua;
  11. {$IFNDEF lua_h}
  12. {$DEFINE lua_h}
  13. {$ENDIF}
  14. interface
  15. uses luaconf;
  16. const
  17. LUA_VERSION = 'Lua 5.1';
  18. LUA_RELEASE = 'Lua 5.1.1';
  19. LUA_VERSION_NUM = 501;
  20. LUA_COPYRIGHT = 'Copyright (C) 1994-2006 Lua.org, PUC-Rio';
  21. LUA_AUTHORS = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes';
  22. // mark for precompiled code (`<esc>Lua')
  23. LUA_SIGNATURE =#33'Lua';
  24. // option for multiple returns in `lua_pcall' and `lua_call'
  25. LUA_MULTRET = -1;
  26. //
  27. // pseudo-indices
  28. //
  29. LUA_REGISTRYINDEX = -10000;
  30. LUA_ENVIRONINDEX = -10001;
  31. LUA_GLOBALSINDEX = -10002;
  32. function lua_upvalueindex(I: Integer): Integer;
  33. const
  34. // thread status; 0 is OK
  35. LUA_YIELD_ = 1; //lua_yield is a function
  36. // error codes for `lua_load' and `lua_pcall'
  37. LUA_ERRRUN = 2;
  38. LUA_ERRSYNTAX = 3;
  39. LUA_ERRMEM = 4;
  40. LUA_ERRERR = 5;
  41. type
  42. //C Types to Delphi Types
  43. size_t = Integer;
  44. Psize_t = ^size_t;
  45. lua_State = record
  46. end;
  47. Plua_State = ^lua_State;
  48. lua_CFunction = function (L: Plua_State): Integer; cdecl;
  49. (*
  50. ** functions that read/write blocks when loading/dumping Lua chunks
  51. *)
  52. lua_Reader = function (L: Plua_State; ud: Pointer; sz: Psize_t): PChar;
  53. lua_Writer = function (L: Plua_State; const p: Pointer; sz: size_t; ud: Pointer): Integer;
  54. (*
  55. ** prototype for memory-allocation functions
  56. *)
  57. lua_Alloc = function (ud: Pointer; ptr: Pointer; osize: size_t; nsize: size_t): Pointer;
  58. const
  59. (*
  60. ** basic types
  61. *)
  62. LUA_TNONE = -1;
  63. LUA_TNIL = 0;
  64. LUA_TBOOLEAN = 1;
  65. LUA_TLIGHTUSERDATA = 2;
  66. LUA_TNUMBER = 3;
  67. LUA_TSTRING = 4;
  68. LUA_TTABLE = 5;
  69. LUA_TFUNCTION = 6;
  70. LUA_TUSERDATA = 7;
  71. LUA_TTHREAD = 8;
  72. // minimum Lua stack available to a C function
  73. LUA_MINSTACK = 20;
  74. (*
  75. ** generic extra include file
  76. *)
  77. {$IFDEF LUA_USER_H}
  78. {$INCLUDE} LUA_USER_H
  79. {$ENDIF}
  80. //type
  81. //type of numbers in Lua
  82. //lua_Number = LUA_NUMBER; see LuaConf.pas
  83. // type for integer functions
  84. //lua_Integer = LUA_INTEGER; see LuaConf.pas
  85. (*
  86. ** state manipulation
  87. *)
  88. function lua_newstate(f: lua_Alloc; ud :Pointer): Plua_State; cdecl external 'lua5.1.dll';
  89. procedure lua_close(L: Plua_State); cdecl external 'lua5.1.dll';
  90. function lua_newthread(L: Plua_State): Plua_State; cdecl external 'lua5.1.dll';
  91. function lua_atpanic(L: Plua_State; panicf: lua_CFunction): lua_CFunction; cdecl external 'lua5.1.dll';
  92. (*
  93. ** basic stack manipulation
  94. *)
  95. function lua_gettop(L: Plua_State): Integer; cdecl external 'lua5.1.dll';
  96. procedure lua_settop(L: Plua_State; idx: Integer); cdecl external 'lua5.1.dll';
  97. procedure lua_pushvalue(L: Plua_State; idx: Integer); cdecl external 'lua5.1.dll';
  98. procedure lua_remove(L: Plua_State; idx: Integer); cdecl external 'lua5.1.dll';
  99. procedure lua_insert(L: Plua_State; idx: Integer); cdecl external 'lua5.1.dll';
  100. procedure lua_replace(L: Plua_State; idx: Integer); cdecl external 'lua5.1.dll';
  101. function lua_checkstack(L: Plua_State; sz: Integer): Integer; cdecl external 'lua5.1.dll';
  102. procedure lua_xmove(_from, _to: Plua_State; n: Integer); cdecl external 'lua5.1.dll';
  103. (*
  104. ** access functions (stack -> C)
  105. *)
  106. function lua_isnumber(L: Plua_State; idx: Integer): Integer; cdecl external 'lua5.1.dll';
  107. function lua_isstring(L: Plua_State; idx: Integer): Integer; cdecl external 'lua5.1.dll';
  108. function lua_iscfunction(L: Plua_State; idx: Integer): Integer; cdecl external 'lua5.1.dll';
  109. function lua_isuserdata(L: Plua_State; idx: Integer): Integer; cdecl external 'lua5.1.dll';
  110. function lua_type(L: Plua_State; idx: Integer): Integer; cdecl external 'lua5.1.dll';
  111. function lua_typename(L: Plua_State; tp: Integer): PChar; cdecl external 'lua5.1.dll';
  112. function lua_equal(L: Plua_State; idx1, idx2: Integer): Integer; cdecl external 'lua5.1.dll';
  113. function lua_rawequal(L: Plua_State; idx1, idx2: Integer): Integer; cdecl external 'lua5.1.dll';
  114. function lua_lessthan(L: Plua_State; Idx1: Integer; Idx2: Integer): Integer; cdecl external 'lua5.1.dll';
  115. function lua_tonumber(L: Plua_State; idx: Integer): LUA_NUMBER; cdecl external 'lua5.1.dll';
  116. function lua_tointeger(L: Plua_State; idx: Integer): lua_Integer; cdecl external 'lua5.1.dll';
  117. function lua_toboolean(L: Plua_State; idx: Integer): Integer; cdecl external 'lua5.1.dll';
  118. function lua_tolstring(L: Plua_State; idx: Integer; len: Psize_t): PChar; cdecl external 'lua5.1.dll';
  119. function lua_objlen(L: Plua_State; idx: Integer): size_t; cdecl external 'lua5.1.dll';
  120. function lua_tocfunction(L: Plua_State; idx: Integer): lua_CFunction; cdecl external 'lua5.1.dll';
  121. function lua_touserdata(L: Plua_State; idx: Integer): Pointer; cdecl external 'lua5.1.dll';
  122. function lua_tothread(L: Plua_State; idx: Integer): Plua_State; cdecl external 'lua5.1.dll';
  123. function lua_topointer(L: Plua_State; idx: Integer): Pointer; cdecl external 'lua5.1.dll';
  124. (*
  125. ** push functions (C -> stack)
  126. *)
  127. procedure lua_pushnil(L: Plua_State); cdecl external 'lua5.1.dll';
  128. procedure lua_pushnumber(L: Plua_State; n: LUA_NUMBER); cdecl external 'lua5.1.dll';
  129. procedure lua_pushinteger(L: Plua_State; n: lua_Integer); cdecl external 'lua5.1.dll';
  130. procedure lua_pushlstring(L: Plua_State; const s: PChar; n: size_t); cdecl external 'lua5.1.dll';
  131. procedure lua_pushstring(L: Plua_State; const s: PChar); cdecl external 'lua5.1.dll';
  132. function lua_pushvfstring(L: Plua_State; const fmt: PChar; Argp: Pointer): PChar; cdecl external 'lua5.1.dll';
  133. function lua_pushfstring(L: Plua_State; const Fmt: PChar): PChar; varargs; cdecl external 'lua5.1.dll';
  134. procedure lua_pushcclosure(L: Plua_State; fn: lua_CFunction; n: Integer); cdecl external 'lua5.1.dll';
  135. procedure lua_pushboolean(L: Plua_State; b: Integer); cdecl external 'lua5.1.dll';
  136. procedure lua_pushlightuserdata(L: Plua_State; p: Pointer); cdecl external 'lua5.1.dll';
  137. function lua_pushthread(L: Plua_State): Integer; cdecl external 'lua5.1.dll';
  138. (*
  139. ** get functions (Lua -> stack)
  140. *)
  141. procedure lua_gettable(L: Plua_State; idx: Integer); cdecl external 'lua5.1.dll';
  142. procedure lua_getfield(L: Plua_State; idx: Integer; k: PChar); cdecl external 'lua5.1.dll';
  143. procedure lua_rawget(L: Plua_State; idx: Integer); cdecl external 'lua5.1.dll';
  144. procedure lua_rawgeti(L: Plua_State; idx, n: Integer); cdecl external 'lua5.1.dll';
  145. procedure lua_createtable(L: Plua_State; narr, nrec: Integer); cdecl external 'lua5.1.dll';
  146. function lua_newuserdata(L: Plua_State; sz: size_t): Pointer; cdecl external 'lua5.1.dll';
  147. function lua_getmetatable(L: Plua_State; objindex: Integer): Integer; cdecl external 'lua5.1.dll';
  148. procedure lua_getfenv(L: Plua_State; idx: Integer); cdecl external 'lua5.1.dll';
  149. (*
  150. ** set functions (stack -> Lua)
  151. *)
  152. procedure lua_settable(L: Plua_State; idx: Integer); cdecl external 'lua5.1.dll';
  153. procedure lua_setfield(L: Plua_State; idx: Integer; k: PChar); cdecl external 'lua5.1.dll';
  154. procedure lua_rawset(L: Plua_State; idx: Integer); cdecl external 'lua5.1.dll';
  155. procedure lua_rawseti(L: Plua_State; idx, n: Integer); cdecl external 'lua5.1.dll';
  156. function lua_setmetatable(L: Plua_State; objindex: Integer): Integer; cdecl external 'lua5.1.dll';
  157. function lua_setfenv(L: Plua_State; idx: Integer): Integer; cdecl external 'lua5.1.dll';
  158. (*
  159. ** `load' and `call' functions (load and run Lua code)
  160. *)
  161. procedure lua_call(L: Plua_State; nargs, nresults: Integer); cdecl external 'lua5.1.dll';
  162. function lua_pcall(L: Plua_State; nargs, nresults, errfunc: Integer): Integer; cdecl external 'lua5.1.dll';
  163. function lua_cpcall(L: Plua_State; func: lua_CFunction; ud: Pointer): Integer; cdecl external 'lua5.1.dll';
  164. function lua_load(L: Plua_State; reader: lua_Reader; dt: Pointer;
  165. const chunkname: PChar): Integer; cdecl external 'lua5.1.dll';
  166. function lua_dump(L: Plua_State; writer: lua_Writer; data: Pointer): Integer; cdecl external 'lua5.1.dll';
  167. (*
  168. ** coroutine functions
  169. *)
  170. function lua_yield(L: Plua_State; nresults: Integer): Integer; cdecl external 'lua5.1.dll';
  171. function lua_resume(L: Plua_State; narg: Integer): Integer; cdecl external 'lua5.1.dll';
  172. function lua_status(L: Plua_State): Integer; cdecl external 'lua5.1.dll';
  173. (*
  174. ** garbage-collection functions and options
  175. *)
  176. const
  177. LUA_GCSTOP =0;
  178. LUA_GCRESTART =1;
  179. LUA_GCCOLLECT =2;
  180. LUA_GCCOUNT =3;
  181. LUA_GCCOUNTB =4;
  182. LUA_GCSTEP =5;
  183. LUA_GCSETPAUSE =6;
  184. LUA_GCSETSTEPMUL =7;
  185. function lua_gc(L: Plua_State; what, data: Integer): Integer; cdecl external 'lua5.1.dll';
  186. (*
  187. ** miscellaneous functions
  188. *)
  189. function lua_error(L: Plua_State): Integer; cdecl external 'lua5.1.dll';
  190. function lua_next(L: Plua_State; idx: Integer): Integer; cdecl external 'lua5.1.dll';
  191. procedure lua_concat(L: Plua_State; n: Integer); cdecl external 'lua5.1.dll';
  192. function lua_getallocf(L: Plua_State; ud: PPointer): lua_Alloc; cdecl external 'lua5.1.dll';
  193. procedure lua_setallocf(L: Plua_State; f: lua_Alloc; ud: Pointer); cdecl external 'lua5.1.dll';
  194. (*
  195. ** ===============================================================
  196. ** some useful macros
  197. ** ===============================================================
  198. *)
  199. procedure lua_pop(L: Plua_State; n: Integer);
  200. procedure lua_newtable(L: Plua_State);
  201. procedure lua_register(L: Plua_State; const n: PChar; f: lua_CFunction);
  202. procedure lua_pushcfunction(L: Plua_State; f: lua_CFunction);
  203. function lua_strlen(L: Plua_State; i: Integer): Integer;
  204. function lua_isfunction(L: Plua_State; n: Integer): Boolean;
  205. function lua_istable(L: Plua_State; n: Integer): Boolean;
  206. function lua_islightuserdata(L: Plua_State; n: Integer): Boolean;
  207. function lua_isnil(L: Plua_State; n: Integer): Boolean;
  208. function lua_isboolean(L: Plua_State; n: Integer): Boolean;
  209. function lua_isthread(L: Plua_State; n: Integer): Boolean;
  210. function lua_isnone(L: Plua_State; n: Integer): Boolean;
  211. function lua_isnoneornil(L: Plua_State; n: Integer): Boolean;
  212. procedure lua_pushliteral(L: Plua_State; const s: PChar);
  213. procedure lua_setglobal(L: Plua_State; s: PChar);
  214. procedure lua_getglobal(L: Plua_State; s: PChar);
  215. function lua_tostring(L: Plua_State; i: Integer): PChar;
  216. (*
  217. ** compatibility macros and functions
  218. *)
  219. function lua_open : Plua_State;
  220. procedure lua_getregistry(L: Plua_State);
  221. function lua_getgccount(L: Plua_State): Integer;
  222. type
  223. lua_Chunkreader = lua_Reader;
  224. lua_Chunkwriter = lua_Writer;
  225. (*
  226. ** {======================================================================
  227. ** Debug API
  228. ** =======================================================================
  229. *)
  230. const
  231. (*
  232. ** Event codes
  233. *)
  234. LUA_HOOKCALL = 0;
  235. LUA_HOOKRET = 1;
  236. LUA_HOOKLINE = 2;
  237. LUA_HOOKCOUNT = 3;
  238. LUA_HOOKTAILRET = 4;
  239. (*
  240. ** Event masks
  241. *)
  242. LUA_MASKCALL = (1 shl LUA_HOOKCALL);
  243. LUA_MASKRET = (1 shl LUA_HOOKRET);
  244. LUA_MASKLINE = (1 shl LUA_HOOKLINE);
  245. LUA_MASKCOUNT = (1 shl LUA_HOOKCOUNT);
  246. type
  247. lua_Debug = record
  248. event: Integer;
  249. name: PChar; (* (n) *)
  250. namewhat: PChar; (* (n) `global', `local', `field', `method' *)
  251. what: PChar; (* (S) `Lua', `C', `main', `tail' *)
  252. source: PChar; (* (S) *)
  253. currentline: Integer; (* (l) *)
  254. nups: Integer; (* (u) number of upvalues *)
  255. linedefined: Integer; (* (S) *)
  256. lastlinedefined: Integer;
  257. short_src: array [0..LUA_IDSIZE - 1] of Char; (* (S) *)
  258. (* private part *)
  259. i_ci: Integer; (* active function *)
  260. end;
  261. Plua_Debug = ^lua_Debug;
  262. // Functions to be called by the debuger in specific events
  263. lua_Hook = procedure (L: Plua_State; ar: Plua_Debug); cdecl;
  264. function lua_getstack(L: Plua_State; level: Integer; ar: Plua_Debug): Integer; cdecl external 'lua5.1.dll';
  265. function lua_getinfo(L: Plua_State; const what: PChar; ar: Plua_Debug): Integer; cdecl external 'lua5.1.dll';
  266. function lua_getlocal(L: Plua_State; const ar: Plua_Debug; n: Integer): PChar; cdecl external 'lua5.1.dll';
  267. function lua_setlocal(L: Plua_State; const ar: Plua_Debug; n: Integer): PChar; cdecl external 'lua5.1.dll';
  268. function lua_getupvalue(L: Plua_State; funcindex: Integer; n: Integer): PChar; cdecl external 'lua5.1.dll';
  269. function lua_setupvalue(L: Plua_State; funcindex: Integer; n: Integer): PChar; cdecl external 'lua5.1.dll';
  270. function lua_sethook(L: Plua_State; func: lua_Hook; mask, count: Integer): Integer; cdecl external 'lua5.1.dll';
  271. function lua_gethook(L: Plua_State): lua_Hook; cdecl external 'lua5.1.dll';
  272. function lua_gethookmask(L: Plua_State): Integer; cdecl external 'lua5.1.dll';
  273. function lua_gethookcount(L: Plua_State): Integer; cdecl external 'lua5.1.dll';
  274. implementation
  275. uses SysUtils, lauxlib;
  276. (*
  277. ** pseudo-indices
  278. *)
  279. function lua_upvalueindex(I: Integer): Integer;
  280. begin
  281. Result := (LUA_GLOBALSINDEX-i);
  282. end;
  283. (*
  284. ** ===============================================================
  285. ** some useful macros
  286. ** ===============================================================
  287. *)
  288. procedure lua_pop(L: Plua_State; n: Integer);
  289. begin
  290. lua_settop(L, -(n)-1);
  291. end;
  292. procedure lua_newtable(L: Plua_State);
  293. begin
  294. lua_createtable(L, 0, 0);
  295. end;
  296. procedure lua_register(L: Plua_State; const n: PChar; f: lua_CFunction);
  297. begin
  298. lua_pushcfunction(L, f);
  299. lua_setglobal(L, n);
  300. end;
  301. procedure lua_pushcfunction(L: Plua_State; f: lua_CFunction);
  302. begin
  303. lua_pushcclosure(L, f, 0);
  304. end;
  305. function lua_strlen(L: Plua_State; i: Integer): Integer;
  306. begin
  307. Result := lua_objlen(L, i);
  308. end;
  309. function lua_isfunction(L: Plua_State; n: Integer): Boolean;
  310. begin
  311. Result := (lua_type(L, n) = LUA_TFUNCTION);
  312. end;
  313. function lua_istable(L: Plua_State; n: Integer): Boolean;
  314. begin
  315. Result := (lua_type(L, n) = LUA_TTABLE);
  316. end;
  317. function lua_islightuserdata(L: Plua_State; n: Integer): Boolean;
  318. begin
  319. Result := (lua_type(L, n) = LUA_TLIGHTUSERDATA);
  320. end;
  321. function lua_isnil(L: Plua_State; n: Integer): Boolean;
  322. begin
  323. Result := (lua_type(L, n) = LUA_TNIL);
  324. end;
  325. function lua_isboolean(L: Plua_State; n: Integer): Boolean;
  326. begin
  327. Result := (lua_type(L, n) = LUA_TBOOLEAN);
  328. end;
  329. function lua_isthread(L: Plua_State; n: Integer): Boolean;
  330. begin
  331. Result := (lua_type(L, n) = LUA_TTHREAD);
  332. end;
  333. function lua_isnone(L: Plua_State; n: Integer): Boolean;
  334. begin
  335. Result := (lua_type(L, n) = LUA_TNONE);
  336. end;
  337. function lua_isnoneornil(L: Plua_State; n: Integer): Boolean;
  338. begin
  339. Result := (lua_type(L, n) <= 0);
  340. end;
  341. procedure lua_pushliteral(L: Plua_State; const s: PChar);
  342. begin
  343. lua_pushlstring(L, s, StrLen(s)(* / sizeof(char) *));
  344. end;
  345. procedure lua_setglobal(L: Plua_State; s: PChar);
  346. begin
  347. lua_setfield(L, LUA_GLOBALSINDEX, s);
  348. end;
  349. procedure lua_getglobal(L: Plua_State; s: PChar);
  350. begin
  351. lua_getfield(L, LUA_GLOBALSINDEX, s);
  352. end;
  353. function lua_tostring(L: Plua_State; i: Integer): PChar;
  354. begin
  355. Result :=lua_tolstring(L, i, nil);
  356. end;
  357. (*
  358. ** compatibility macros and functions
  359. *)
  360. function lua_open: Plua_State;
  361. begin
  362. Result :=luaL_newstate;
  363. end;
  364. procedure lua_getregistry(L: Plua_State);
  365. begin
  366. lua_pushvalue(L, LUA_REGISTRYINDEX);
  367. end;
  368. function lua_getgccount(L: Plua_State): Integer;
  369. begin
  370. Result := lua_gc(L, LUA_GCCOUNT, 0);
  371. end;
  372. (* }====================================================================== *)
  373. (******************************************************************************
  374. * Copyright (C) 1994-2006 Lua.org, PUC-Rio. All rights reserved.
  375. *
  376. * Permission is hereby granted, free of charge, to any person obtaining
  377. * a copy of this software and associated documentation files (the
  378. * "Software"), to deal in the Software without restriction, including
  379. * without limitation the rights to use, copy, modify, merge, publish,
  380. * distribute, sublicense, and/or sell copies of the Software, and to
  381. * permit persons to whom the Software is furnished to do so, subject to
  382. * the following conditions:
  383. *
  384. * The above copyright notice and this permission notice shall be
  385. * included in all copies or substantial portions of the Software.
  386. *
  387. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  388. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  389. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  390. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  391. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  392. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  393. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  394. ******************************************************************************)
  395. end.