lua.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. (*
  2. ** $Id: lua.pas,v 1.1 2005-04-24 19:31:20 jfgoulet Exp $
  3. ** Lua - An Extensible Extension Language
  4. ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
  5. ** http://www.lua.org mailto:[email protected]
  6. ** See Copyright Notice at the end of this file
  7. *)
  8. unit lua;
  9. {$IFNDEF lua_h}
  10. {$DEFINE lua_h}
  11. {$ENDIF}
  12. interface
  13. const
  14. CONST_LUA_VERSION = 'Lua 5.0';
  15. CONST_LUA_COPYRIGHT = 'Copyright (C) 1994-2003 Tecgraf, PUC-Rio';
  16. CONST_LUA_AUTHORS = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes';
  17. (* option for multiple returns in `lua_pcall' and `lua_call' *)
  18. LUA_MULTRET = (-1);
  19. (*
  20. ** pseudo-indices
  21. *)
  22. LUA_REGISTRYINDEX = (-10000);
  23. LUA_GLOBALSINDEX = (-10001);
  24. function lua_upvalueindex(I: Integer): Integer;
  25. const
  26. (* error codes for `lua_load' and `lua_pcall' *)
  27. LUA_ERRRUN = 1;
  28. LUA_ERRFILE = 2;
  29. LUA_ERRSYNTAX = 3;
  30. LUA_ERRMEM = 4;
  31. LUA_ERRERR = 5;
  32. type
  33. lua_State = record
  34. end;
  35. Plua_State = ^lua_State;
  36. lua_CFunction = function (L: Plua_State): Integer; cdecl;
  37. size_t = Integer;
  38. Psize_t = ^size_t;
  39. (*
  40. ** functions that read/write blocks when loading/dumping Lua chunks
  41. *)
  42. lua_Chunkreader = function (L: Plua_State; UD: Pointer; var SZ: size_t): PChar;
  43. lua_Chunkwriter = function (L: Plua_State; const P: Pointer; SZ: size_t; UD: Pointer): Integer;
  44. const
  45. (*
  46. ** basic types
  47. *)
  48. LUA_TNONE = (-1);
  49. LUA_TNIL = 0;
  50. LUA_TBOOLEAN = 1;
  51. LUA_TLIGHTUSERDATA = 2;
  52. LUA_TNUMBER = 3;
  53. LUA_TSTRING = 4;
  54. LUA_TTABLE = 5;
  55. LUA_TFUNCTION = 6;
  56. LUA_TUSERDATA = 7;
  57. LUA_TTHREAD = 8;
  58. (* minimum Lua stack available to a C function *)
  59. LUA_MINSTACK = 20;
  60. (*
  61. ** generic extra include file
  62. *)
  63. {$IFDEF LUA_USER_H}
  64. {$INCLUDE} LUA_USER_H
  65. {$ENDIF}
  66. (* type of numbers in Lua *)
  67. type
  68. {$IFNDEF LUA_NUMBER}
  69. lua_Number = Double;
  70. {$ELSE}
  71. lua_Number = LUA_NUMBER;
  72. {$ENDIF}
  73. (* mark for all API functions *)
  74. (*
  75. ** state manipulation
  76. *)
  77. function lua_open: Plua_State;
  78. cdecl external 'lua.dll';
  79. procedure lua_close(L: Plua_State);
  80. cdecl external 'lua.dll';
  81. function lua_newthread(L: Plua_State): Plua_State;
  82. cdecl external 'lua.dll';
  83. function lua_atpanic(L: Plua_State; Panicf: lua_CFunction): lua_CFunction;
  84. cdecl external 'lua.dll';
  85. (*
  86. ** basic stack manipulation
  87. *)
  88. function lua_gettop(L: Plua_State): Integer;
  89. cdecl external 'lua.dll';
  90. procedure lua_settop(L: Plua_State; Idx: Integer);
  91. cdecl external 'lua.dll';
  92. procedure lua_pushvalue(L: Plua_State; Idx: Integer);
  93. cdecl external 'lua.dll';
  94. procedure lua_remove(L: Plua_State; Idx: Integer);
  95. cdecl external 'lua.dll';
  96. procedure lua_insert(L: Plua_State; Idx: Integer);
  97. cdecl external 'lua.dll';
  98. procedure lua_replace(L: Plua_State; Idx: Integer);
  99. cdecl external 'lua.dll';
  100. function lua_checkstack(L: Plua_State; SZ: Integer): Integer;
  101. cdecl external 'lua.dll';
  102. procedure lua_xmove (Src, Dst: Plua_State; N: Integer);
  103. cdecl external 'lua.dll';
  104. (*
  105. ** access functions (stack -> C)
  106. *)
  107. function lua_isnumber(L: Plua_State; Idx: Integer): Integer;
  108. cdecl external 'lua.dll';
  109. function lua_isstring(L: Plua_State; Idx: Integer): Integer;
  110. cdecl external 'lua.dll';
  111. function lua_iscfunction(L: Plua_State; Idx: Integer): Integer;
  112. cdecl external 'lua.dll';
  113. function lua_isuserdata(L: Plua_State; Idx: Integer): Integer;
  114. cdecl external 'lua.dll';
  115. function lua_type(L: Plua_State; Idx: Integer): Integer;
  116. cdecl external 'lua.dll';
  117. function lua_typename(L: Plua_State; TP: Integer): PChar;
  118. cdecl external 'lua.dll';
  119. function lua_equal(L: Plua_State; Idx1: Integer; Idx2: Integer): Integer;
  120. cdecl external 'lua.dll';
  121. function lua_rawequal(L: Plua_State; Idx1: Integer; Idx2: Integer): Integer;
  122. cdecl external 'lua.dll';
  123. function lua_lessthan(L: Plua_State; Idx1: Integer; Idx2: Integer): Integer;
  124. cdecl external 'lua.dll';
  125. function lua_tonumber(L: Plua_State; Idx: Integer): lua_Number;
  126. cdecl external 'lua.dll';
  127. function lua_toboolean(L: Plua_State; Idx: Integer): Integer;
  128. cdecl external 'lua.dll';
  129. function lua_tostring(L: Plua_State; Idx: Integer): PChar;
  130. cdecl external 'lua.dll';
  131. function lua_strlen(L: Plua_State; Idx: Integer): size_t;
  132. cdecl external 'lua.dll';
  133. function lua_tocfunction(L: Plua_State; Idx: Integer): lua_CFunction;
  134. cdecl external 'lua.dll';
  135. function lua_touserdata(L: Plua_State; Idx: Integer): Pointer;
  136. cdecl external 'lua.dll';
  137. function lua_tothread(L: Plua_State; Idx: Integer): Plua_State;
  138. cdecl external 'lua.dll';
  139. function lua_topointer(L: Plua_State; Idx: Integer): Pointer;
  140. cdecl external 'lua.dll';
  141. (*
  142. ** push functions (C -> stack)
  143. *)
  144. procedure lua_pushnil(L: Plua_State);
  145. cdecl external 'lua.dll';
  146. procedure lua_pushnumber(L: Plua_State; N: lua_Number);
  147. cdecl external 'lua.dll';
  148. procedure lua_pushlstring(L: Plua_State; const S: PChar; N: size_t);
  149. cdecl external 'lua.dll';
  150. procedure lua_pushstring(L: Plua_State; const S: PChar);
  151. cdecl external 'lua.dll';
  152. function lua_pushvfstring(L: Plua_State; const Fmt: PChar; Argp: Pointer): PChar;
  153. cdecl external 'lua.dll';
  154. function lua_pushfstring(L: Plua_State; const Fmt: PChar): PChar; varargs;
  155. cdecl external 'lua.dll';
  156. procedure lua_pushcclosure(L: Plua_State; Fn: lua_CFunction; N: Integer);
  157. cdecl external 'lua.dll';
  158. procedure lua_pushboolean(L: Plua_State; B: Integer);
  159. cdecl external 'lua.dll';
  160. procedure lua_pushlightuserdata(L: Plua_State; P: Pointer);
  161. cdecl external 'lua.dll';
  162. (*
  163. ** get functions (Lua -> stack)
  164. *)
  165. procedure lua_gettable(L: Plua_State; Idx: Integer);
  166. cdecl external 'lua.dll';
  167. procedure lua_rawget(L: Plua_State; Idx: Integer);
  168. cdecl external 'lua.dll';
  169. procedure lua_rawgeti(L: Plua_State; Idx: Integer; N: Integer);
  170. cdecl external 'lua.dll';
  171. procedure lua_newtable(L: Plua_State);
  172. cdecl external 'lua.dll';
  173. function lua_newuserdata(L: Plua_State; SZ: size_t): Pointer;
  174. cdecl external 'lua.dll';
  175. function lua_getmetatable(L: Plua_State; ObjIndex: Integer): Integer;
  176. cdecl external 'lua.dll';
  177. procedure lua_getfenv(L: Plua_State; Idx: Integer);
  178. cdecl external 'lua.dll';
  179. (*
  180. ** set functions (stack -> Lua)
  181. *)
  182. procedure lua_settable(L: Plua_State; Idx: Integer);
  183. cdecl external 'lua.dll';
  184. procedure lua_rawset(L: Plua_State; Idx: Integer);
  185. cdecl external 'lua.dll';
  186. procedure lua_rawseti(L: Plua_State; Idx: Integer; N: Integer);
  187. cdecl external 'lua.dll';
  188. function lua_setmetatable(L: Plua_State; ObjIndex: Integer): Integer;
  189. cdecl external 'lua.dll';
  190. function lua_setfenv(L: Plua_State; Idx: Integer): Integer;
  191. cdecl external 'lua.dll';
  192. (*
  193. ** `load' and `call' functions (load and run Lua code)
  194. *)
  195. procedure lua_call(L: Plua_State; NArgs: Integer; NResults: Integer);
  196. cdecl external 'lua.dll';
  197. function lua_pcall(L: Plua_State; NArgs: Integer; NResults: Integer; ErrFunc: Integer): Integer;
  198. cdecl external 'lua.dll';
  199. function lua_cpcall(L: Plua_State; Func: lua_CFunction; UD: Pointer): Integer;
  200. cdecl external 'lua.dll';
  201. function lua_load(L: Plua_State; Reader: lua_Chunkreader; DT: Pointer;
  202. const ChunkName: PChar): Integer;
  203. cdecl external 'lua.dll';
  204. function lua_dump(L: Plua_State; Writer: lua_Chunkwriter; Data: Pointer): Integer;
  205. cdecl external 'lua.dll';
  206. (*
  207. ** coroutine functions
  208. *)
  209. function lua_yield(L: Plua_State; NResults: Integer): Integer;
  210. cdecl external 'lua.dll';
  211. function lua_resume(L: Plua_State; NArg: Integer): Integer;
  212. cdecl external 'lua.dll';
  213. (*
  214. ** garbage-collection functions
  215. *)
  216. function lua_getgcthreshold(L: Plua_State): Integer;
  217. cdecl external 'lua.dll';
  218. function lua_getgccount(L: Plua_State): Integer;
  219. cdecl external 'lua.dll';
  220. procedure lua_setgcthreshold(L: Plua_State; NewThreshold: Integer);
  221. cdecl external 'lua.dll';
  222. (*
  223. ** miscellaneous functions
  224. *)
  225. function lua_version: PChar;
  226. cdecl external 'lua.dll';
  227. function lua_error(L: Plua_State): Integer;
  228. cdecl external 'lua.dll';
  229. function lua_next(L: Plua_State; Idx: Integer): Integer;
  230. cdecl external 'lua.dll';
  231. procedure lua_concat(L: Plua_State; N: Integer);
  232. cdecl external 'lua.dll';
  233. (*
  234. ** ===============================================================
  235. ** some useful macros
  236. ** ===============================================================
  237. *)
  238. function lua_boxpointer(L: Plua_State; U: Pointer): Pointer;
  239. function lua_unboxpointer(L: Plua_State; I: Integer): Pointer;
  240. procedure lua_pop(L: Plua_State; N: Integer);
  241. procedure lua_register(L: Plua_State; const N: PChar; F: lua_CFunction);
  242. procedure lua_pushcfunction(L: Plua_State; F: lua_CFunction);
  243. function lua_isfunction(L: Plua_State; N: Integer): Boolean;
  244. function lua_istable(L: Plua_State; N: Integer): Boolean;
  245. function lua_islightuserdata(L: Plua_State; N: Integer): Boolean;
  246. function lua_isnil(L: Plua_State; N: Integer): Boolean;
  247. function lua_isboolean(L: Plua_State; N: Integer): Boolean;
  248. function lua_isnone(L: Plua_State; N: Integer): Boolean;
  249. function lua_isnoneornil(L: Plua_State; N: Integer): Boolean;
  250. procedure lua_pushliteral(L: Plua_State; const S: PChar);
  251. (*
  252. ** compatibility macros and functions
  253. *)
  254. function lua_pushupvalues(L: Plua_State): Integer;
  255. cdecl; external 'lua.dll';
  256. procedure lua_getregistry(L: Plua_State);
  257. procedure lua_setglobal(L: Plua_State; const S: PChar);
  258. procedure lua_getglobal(L: Plua_State; const S: PChar);
  259. (* compatibility with ref system *)
  260. (* pre-defined references *)
  261. const
  262. LUA_NOREF = (-2);
  263. LUA_REFNIL = (-1);
  264. procedure lua_ref(L: Plua_State; Lock: Integer);
  265. procedure lua_unref(L: Plua_State; Ref: Integer);
  266. procedure lua_getref(L: Plua_State; Ref: Integer);
  267. (*
  268. ** {======================================================================
  269. ** useful definitions for Lua kernel and libraries
  270. ** =======================================================================
  271. *)
  272. (* formats for Lua numbers *)
  273. {$IFNDEF LUA_NUMBER_SCAN}
  274. const
  275. LUA_NUMBER_SCAN = '%lf';
  276. {$ENDIF}
  277. {$IFNDEF LUA_NUMBER_FMT}
  278. const
  279. LUA_NUMBER_FMT = '%.14g';
  280. {$ENDIF}
  281. (* }====================================================================== *)
  282. (*
  283. ** {======================================================================
  284. ** Debug API
  285. ** =======================================================================
  286. *)
  287. const
  288. (*
  289. ** Event codes
  290. *)
  291. LUA_HOOKCALL = 0;
  292. LUA_HOOKRET = 1;
  293. LUA_HOOKLINE = 2;
  294. LUA_HOOKCOUNT = 3;
  295. LUA_HOOKTAILRET = 4;
  296. (*
  297. ** Event masks
  298. *)
  299. LUA_MASKCALL = (1 shl LUA_HOOKCALL);
  300. LUA_MASKRET = (1 shl LUA_HOOKRET);
  301. LUA_MASKLINE = (1 shl LUA_HOOKLINE);
  302. LUA_MASKCOUNT = (1 shl LUA_HOOKCOUNT);
  303. const
  304. LUA_IDSIZE = 60;
  305. type
  306. lua_Debug = record
  307. event: Integer;
  308. name: PChar; (* (n) *)
  309. namewhat: PChar; (* (n) `global', `local', `field', `method' *)
  310. what: PChar; (* (S) `Lua', `C', `main', `tail' *)
  311. source: PChar; (* (S) *)
  312. currentline: Integer; (* (l) *)
  313. nups: Integer; (* (u) number of upvalues *)
  314. linedefined: Integer; (* (S) *)
  315. short_src: array [0..LUA_IDSIZE - 1] of Char; (* (S) *)
  316. (* private part *)
  317. i_ci: Integer; (* active function *)
  318. end;
  319. Plua_Debug = ^lua_Debug;
  320. lua_Hook = procedure (L: Plua_State; AR: Plua_Debug); cdecl;
  321. function lua_getstack(L: Plua_State; Level: Integer; AR: Plua_Debug): Integer;
  322. cdecl external 'lua.dll';
  323. function lua_getinfo(L: Plua_State; const What: PChar; AR: Plua_Debug): Integer;
  324. cdecl external 'lua.dll';
  325. function lua_getlocal(L: Plua_State; const AR: Plua_Debug; N: Integer): PChar;
  326. cdecl external 'lua.dll';
  327. function lua_setlocal(L: Plua_State; const AR: Plua_Debug; N: Integer): PChar;
  328. cdecl external 'lua.dll';
  329. function lua_getupvalue(L: Plua_State; FuncIndex: Integer; N: Integer): PChar;
  330. cdecl external 'lua.dll';
  331. function lua_setupvalue(L: Plua_State; FuncIndex: Integer; N: Integer): PChar;
  332. cdecl external 'lua.dll';
  333. function lua_sethook(L: Plua_State; Func: lua_Hook; Mask: Integer; Count: Integer): Integer;
  334. cdecl external 'lua.dll';
  335. function lua_gethook(L: Plua_State): lua_Hook;
  336. cdecl external 'lua.dll';
  337. function lua_gethookmask(L: Plua_State): Integer;
  338. cdecl external 'lua.dll';
  339. function lua_gethookcount(L: Plua_State): Integer;
  340. cdecl external 'lua.dll';
  341. (* }====================================================================== *)
  342. (******************************************************************************
  343. * Copyright (C) 1994-2003 Tecgraf, PUC-Rio. All rights reserved.
  344. *
  345. * Permission is hereby granted, free of charge, to any person obtaining
  346. * a copy of this software and associated documentation files (the
  347. * "Software"), to deal in the Software without restriction, including
  348. * without limitation the rights to use, copy, modify, merge, publish,
  349. * distribute, sublicense, and/or sell copies of the Software, and to
  350. * permit persons to whom the Software is furnished to do so, subject to
  351. * the following conditions:
  352. *
  353. * The above copyright notice and this permission notice shall be
  354. * included in all copies or substantial portions of the Software.
  355. *
  356. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  357. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  358. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  359. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  360. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  361. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  362. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  363. ******************************************************************************)
  364. implementation
  365. uses
  366. SysUtils, lauxlib;
  367. const
  368. MAX_SIZE = High(Integer);
  369. MAX_POINTER_ARRAY = MAX_SIZE div SizeOf(Pointer);
  370. type
  371. TPointerArray = array [0..MAX_POINTER_ARRAY - 1] of Pointer;
  372. PPointerArray = ^TPointerArray;
  373. function lua_upvalueindex(I: Integer): Integer;
  374. begin
  375. Result := LUA_GLOBALSINDEX - I;
  376. end;
  377. function lua_boxpointer(L: Plua_State; U: Pointer): Pointer;
  378. begin
  379. PPointerArray(lua_newuserdata(L, sizeof(U)))^[0] := U;
  380. Result := U;
  381. end;
  382. function lua_unboxpointer(L: Plua_State; I: Integer): Pointer;
  383. begin
  384. Result := PPointerArray(lua_touserdata(L, I))^[0];
  385. end;
  386. procedure lua_pop(L: Plua_State; N: Integer);
  387. begin
  388. lua_settop(L, -(N)-1);
  389. end;
  390. procedure lua_register(L: Plua_State; const N: PChar; F: lua_CFunction);
  391. begin
  392. lua_pushstring(L, N);
  393. lua_pushcfunction(L, F);
  394. lua_settable(L, LUA_GLOBALSINDEX);
  395. end;
  396. procedure lua_pushcfunction(L: Plua_State; F: lua_CFunction);
  397. begin
  398. lua_pushcclosure(L, F, 0);
  399. end;
  400. function lua_isfunction(L: Plua_State; N: Integer): Boolean;
  401. begin
  402. Result := lua_type(L, n) = LUA_TFUNCTION;
  403. end;
  404. function lua_istable(L: Plua_State; N: Integer): Boolean;
  405. begin
  406. Result := lua_type(L, n) = LUA_TTABLE;
  407. end;
  408. function lua_islightuserdata(L: Plua_State; N: Integer): Boolean;
  409. begin
  410. Result := lua_type(L, n) = LUA_TLIGHTUSERDATA;
  411. end;
  412. function lua_isnil(L: Plua_State; N: Integer): Boolean;
  413. begin
  414. Result := lua_type(L, n) = LUA_TNIL;
  415. end;
  416. function lua_isboolean(L: Plua_State; N: Integer): Boolean;
  417. begin
  418. Result := lua_type(L, n) = LUA_TBOOLEAN;
  419. end;
  420. function lua_isnone(L: Plua_State; N: Integer): Boolean;
  421. begin
  422. Result := lua_type(L, n) = LUA_TNONE;
  423. end;
  424. function lua_isnoneornil(L: Plua_State; N: Integer): Boolean;
  425. begin
  426. Result := lua_type(L, n) <= 0;
  427. end;
  428. procedure lua_pushliteral(L: Plua_State; const S: PChar);
  429. begin
  430. lua_pushlstring(L, S, StrLen(S));
  431. end;
  432. procedure lua_getregistry(L: Plua_State);
  433. begin
  434. lua_pushvalue(L, LUA_REGISTRYINDEX);
  435. end;
  436. procedure lua_setglobal(L: Plua_State; const S: PChar);
  437. begin
  438. lua_pushstring(L, S);
  439. lua_insert(L, -2);
  440. lua_settable(L, LUA_GLOBALSINDEX);
  441. end;
  442. procedure lua_getglobal(L: Plua_State; const S: PChar);
  443. begin
  444. lua_pushstring(L, S);
  445. lua_gettable(L, LUA_GLOBALSINDEX);
  446. end;
  447. procedure lua_ref(L: Plua_State; Lock: Integer);
  448. begin
  449. if (Lock <> 0) then
  450. begin
  451. luaL_ref(L, LUA_REGISTRYINDEX);
  452. end else
  453. begin
  454. lua_pushstring(L, 'unlocked references are obsolete');
  455. lua_error(L);
  456. end;
  457. end;
  458. procedure lua_unref(L: Plua_State; Ref: Integer);
  459. begin
  460. luaL_unref(L, LUA_REGISTRYINDEX, Ref);
  461. end;
  462. procedure lua_getref(L: Plua_State; Ref: Integer);
  463. begin
  464. lua_rawgeti(L, LUA_REGISTRYINDEX, Ref);
  465. end;
  466. end.