lauxlib.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. (******************************************************************************
  2. * *
  3. * File: lauxlib.pas *
  4. * Authors: TeCGraf (C headers + actual Lua libraries) *
  5. * Lavergne Thomas (original translation to Pascal) *
  6. * Bram Kuijvenhoven (update to Lua 5.1.1 for FreePascal) *
  7. * Description: Lua auxiliary library *
  8. * *
  9. ******************************************************************************)
  10. (*
  11. ** $Id: lauxlib.h,v 1.59 2003/03/18 12:25:32 roberto Exp $
  12. ** Auxiliary functions for building Lua libraries
  13. ** See Copyright Notice in lua.h
  14. *)
  15. (*
  16. ** Translated to pascal by Lavergne Thomas
  17. ** Notes :
  18. ** - Pointers type was prefixed with 'P'
  19. ** Bug reports :
  20. ** - [email protected]
  21. ** In french or in english
  22. *)
  23. {$IFDEF FPC}{$MODE OBJFPC}{$H+}{$ENDIF}
  24. unit lauxlib;
  25. interface
  26. uses
  27. Lua;
  28. // functions added for Pascal
  29. procedure lua_pushstring(L: Plua_State; const s: string);
  30. // compatibilty macros
  31. function luaL_getn(L: Plua_State; n: Integer): Integer; // calls lua_objlen
  32. procedure luaL_setn(L: Plua_State; t, n: Integer); // does nothing!
  33. type
  34. luaL_reg = record
  35. name: PChar;
  36. func: lua_CFunction;
  37. end;
  38. PluaL_reg = ^luaL_reg;
  39. procedure luaL_openlib(L: Plua_State; const libname: PChar; const lr: PluaL_reg; nup: Integer); cdecl;
  40. procedure luaL_register(L: Plua_State; const libname: PChar; const lr: PluaL_reg); cdecl;
  41. function luaL_getmetafield(L: Plua_State; obj: Integer; const e: PChar): Integer; cdecl;
  42. function luaL_callmeta(L: Plua_State; obj: Integer; const e: PChar): Integer; cdecl;
  43. function luaL_typerror(L: Plua_State; narg: Integer; const tname: PChar): Integer; cdecl;
  44. function luaL_argerror(L: Plua_State; numarg: Integer; const extramsg: PChar): Integer; cdecl;
  45. function luaL_checklstring(L: Plua_State; numArg: Integer; l_: Psize_t): PChar; cdecl;
  46. function luaL_optlstring(L: Plua_State; numArg: Integer; const def: PChar; l_: Psize_t): PChar; cdecl;
  47. function luaL_checknumber(L: Plua_State; numArg: Integer): lua_Number; cdecl;
  48. function luaL_optnumber(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number; cdecl;
  49. function luaL_checkinteger(L: Plua_State; numArg: Integer): lua_Integer; cdecl;
  50. function luaL_optinteger(L: Plua_State; nArg: Integer; def: lua_Integer): lua_Integer; cdecl;
  51. procedure luaL_checkstack(L: Plua_State; sz: Integer; const msg: PChar); cdecl;
  52. procedure luaL_checktype(L: Plua_State; narg, t: Integer); cdecl;
  53. procedure luaL_checkany(L: Plua_State; narg: Integer); cdecl;
  54. function luaL_newmetatable(L: Plua_State; const tname: PChar): Integer; cdecl;
  55. function luaL_checkudata(L: Plua_State; ud: Integer; const tname: PChar): Pointer; cdecl;
  56. procedure luaL_where(L: Plua_State; lvl: Integer); cdecl;
  57. function luaL_error(L: Plua_State; const fmt: PChar; args: array of const): Integer; cdecl; // note: C's ... to array of const conversion is not portable to Delphi
  58. function luaL_checkoption(L: Plua_State; narg: Integer; def: PChar; lst: PPChar): Integer; cdecl;
  59. function luaL_ref(L: Plua_State; t: Integer): Integer; cdecl;
  60. procedure luaL_unref(L: Plua_State; t, ref: Integer); cdecl;
  61. function luaL_loadfile(L: Plua_State; const filename: PChar): Integer; cdecl;
  62. function luaL_loadbuffer(L: Plua_State; const buff: PChar; size: size_t; const name: PChar): Integer; cdecl;
  63. function luaL_loadstring(L: Plua_State; const s: PChar): Integer; cdecl;
  64. function luaL_newstate: Plua_State; cdecl;
  65. function lua_open: Plua_State; // compatibility; moved from unit lua to lauxlib because it needs luaL_newstate
  66. function luaL_gsub(L: Plua_State; const s, p, r: PChar): PChar; cdecl;
  67. function luaL_findtable(L: Plua_State; idx: Integer; const fname: PChar; szhint: Integer): PChar; cdecl;
  68. (*
  69. ** ===============================================================
  70. ** some useful macros
  71. ** ===============================================================
  72. *)
  73. procedure luaL_argcheck(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PChar);
  74. function luaL_checkstring(L: Plua_State; n: Integer): PChar;
  75. function luaL_optstring(L: Plua_State; n: Integer; d: PChar): PChar;
  76. function luaL_checkint(L: Plua_State; n: Integer): Integer;
  77. function luaL_checklong(L: Plua_State; n: Integer): LongInt;
  78. function luaL_optint(L: Plua_State; n: Integer; d: Double): Integer;
  79. function luaL_optlong(L: Plua_State; n: Integer; d: Double): LongInt;
  80. function luaL_typename(L: Plua_State; i: Integer): PChar;
  81. function lua_dofile(L: Plua_State; const filename: PChar): Integer;
  82. function lua_dostring(L: Plua_State; const str: PChar): Integer;
  83. procedure lua_Lgetmetatable(L: Plua_State; tname: PChar);
  84. // not translated:
  85. // #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
  86. (*
  87. ** =======================================================
  88. ** Generic Buffer manipulation
  89. ** =======================================================
  90. *)
  91. const
  92. // note: this is just arbitrary, as it related to the BUFSIZ defined in stdio.h ...
  93. LUAL_BUFFERSIZE = 4096;
  94. type
  95. luaL_Buffer = record
  96. p: PChar; (* current position in buffer *)
  97. lvl: Integer; (* number of strings in the stack (level) *)
  98. L: Plua_State;
  99. buffer: array [0..LUAL_BUFFERSIZE - 1] of Char; // warning: see note above about LUAL_BUFFERSIZE
  100. end;
  101. PluaL_Buffer = ^luaL_Buffer;
  102. procedure luaL_addchar(B: PluaL_Buffer; c: Char); // warning: see note above about LUAL_BUFFERSIZE
  103. (* compatibility only (alias for luaL_addchar) *)
  104. procedure luaL_putchar(B: PluaL_Buffer; c: Char); // warning: see note above about LUAL_BUFFERSIZE
  105. procedure luaL_addsize(B: PluaL_Buffer; n: Integer);
  106. procedure luaL_buffinit(L: Plua_State; B: PluaL_Buffer); cdecl;
  107. function luaL_prepbuffer(B: PluaL_Buffer): PChar; cdecl;
  108. procedure luaL_addlstring(B: PluaL_Buffer; const s: PChar; l: size_t); cdecl;
  109. procedure luaL_addstring(B: PluaL_Buffer; const s: PChar); cdecl;
  110. procedure luaL_addvalue(B: PluaL_Buffer); cdecl;
  111. procedure luaL_pushresult(B: PluaL_Buffer); cdecl;
  112. (* compatibility with ref system *)
  113. (* pre-defined references *)
  114. const
  115. LUA_NOREF = -2;
  116. LUA_REFNIL = -1;
  117. procedure lua_unref(L: Plua_State; ref: Integer);
  118. procedure lua_getref(L: Plua_State; ref: Integer);
  119. (*
  120. ** Compatibility macros and functions
  121. *)
  122. function luaL_check_lstr(L: Plua_State; numArg: Integer; len: Psize_t): PChar;
  123. function luaL_opt_lstr(L: Plua_State; numArg: Integer; const def: PChar; len: Psize_t): PChar;
  124. function luaL_check_number(L: Plua_State; numArg: Integer): lua_Number;
  125. function luaL_opt_number(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number;
  126. procedure luaL_arg_check(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PChar);
  127. function luaL_check_string(L: Plua_State; n: Integer): PChar;
  128. function luaL_opt_string(L: Plua_State; n: Integer; d: PChar): PChar;
  129. function luaL_check_int(L: Plua_State; n: Integer): Integer;
  130. function luaL_check_long(L: Plua_State; n: Integer): LongInt;
  131. function luaL_opt_int(L: Plua_State; n: Integer; d: Double): Integer;
  132. function luaL_opt_long(L: Plua_State; n: Integer; d: Double): LongInt;
  133. implementation
  134. procedure lua_pushstring(L: Plua_State; const s: string);
  135. begin
  136. lua_pushlstring(L, PChar(s), Length(s));
  137. end;
  138. function luaL_getn(L: Plua_State; n: Integer): Integer;
  139. begin
  140. Result := lua_objlen(L, n);
  141. end;
  142. procedure luaL_setn(L: Plua_State; t, n: Integer);
  143. begin
  144. // does nothing as this operation is deprecated
  145. end;
  146. procedure luaL_openlib(L: Plua_State; const libname: PChar; const lr: PluaL_reg; nup: Integer); cdecl; external LUA_LIB_NAME;
  147. procedure luaL_register(L: Plua_State; const libname: PChar; const lr: PluaL_reg); cdecl; external LUA_LIB_NAME;
  148. function luaL_getmetafield(L: Plua_State; obj: Integer; const e: PChar): Integer; cdecl; external LUA_LIB_NAME;
  149. function luaL_callmeta(L: Plua_State; obj: Integer; const e: PChar): Integer; cdecl; external LUA_LIB_NAME;
  150. function luaL_typerror(L: Plua_State; narg: Integer; const tname: PChar): Integer; cdecl; external LUA_LIB_NAME;
  151. function luaL_argerror(L: Plua_State; numarg: Integer; const extramsg: PChar): Integer; cdecl; external LUA_LIB_NAME;
  152. function luaL_checklstring(L: Plua_State; numArg: Integer; l_: Psize_t): PChar; cdecl; external LUA_LIB_NAME;
  153. function luaL_optlstring(L: Plua_State; numArg: Integer; const def: PChar; l_: Psize_t): PChar; cdecl; external LUA_LIB_NAME;
  154. function luaL_checknumber(L: Plua_State; numArg: Integer): lua_Number; cdecl; external LUA_LIB_NAME;
  155. function luaL_optnumber(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number; cdecl; external LUA_LIB_NAME;
  156. function luaL_checkinteger(L: Plua_State; numArg: Integer): lua_Integer; cdecl; external LUA_LIB_NAME;
  157. function luaL_optinteger(L: Plua_State; nArg: Integer; def: lua_Integer): lua_Integer; cdecl; external LUA_LIB_NAME;
  158. procedure luaL_checkstack(L: Plua_State; sz: Integer; const msg: PChar); cdecl; external LUA_LIB_NAME;
  159. procedure luaL_checktype(L: Plua_State; narg, t: Integer); cdecl; external LUA_LIB_NAME;
  160. procedure luaL_checkany(L: Plua_State; narg: Integer); cdecl; external LUA_LIB_NAME;
  161. function luaL_newmetatable(L: Plua_State; const tname: PChar): Integer; cdecl; external LUA_LIB_NAME;
  162. function luaL_checkudata(L: Plua_State; ud: Integer; const tname: PChar): Pointer; cdecl; external LUA_LIB_NAME;
  163. procedure luaL_where(L: Plua_State; lvl: Integer); cdecl; external LUA_LIB_NAME;
  164. function luaL_error(L: Plua_State; const fmt: PChar; args: array of const): Integer; cdecl; external LUA_LIB_NAME;
  165. function luaL_checkoption(L: Plua_State; narg: Integer; def: PChar; lst: PPChar): Integer; cdecl; external LUA_LIB_NAME;
  166. function luaL_ref(L: Plua_State; t: Integer): Integer; cdecl; external LUA_LIB_NAME;
  167. procedure luaL_unref(L: Plua_State; t, ref: Integer); cdecl; external LUA_LIB_NAME;
  168. function luaL_loadfile(L: Plua_State; const filename: PChar): Integer; cdecl; external LUA_LIB_NAME;
  169. function luaL_loadbuffer(L: Plua_State; const buff: PChar; size: size_t; const name: PChar): Integer; cdecl; external LUA_LIB_NAME;
  170. function luaL_loadstring(L: Plua_State; const s: PChar): Integer; cdecl; external LUA_LIB_NAME;
  171. function luaL_newstate: Plua_State; cdecl; external LUA_LIB_NAME;
  172. function lua_open: Plua_State;
  173. begin
  174. Result := luaL_newstate;
  175. end;
  176. function luaL_gsub(L: Plua_State; const s, p, r: PChar): PChar; cdecl; external LUA_LIB_NAME;
  177. function luaL_findtable(L: Plua_State; idx: Integer; const fname: PChar; szhint: Integer): PChar; cdecl; external LUA_LIB_NAME;
  178. function luaL_typename(L: Plua_State; i: Integer): PChar;
  179. begin
  180. Result := lua_typename(L, lua_type(L, i));
  181. end;
  182. function lua_dofile(L: Plua_State; const filename: PChar): Integer;
  183. begin
  184. Result := luaL_loadfile(L, filename);
  185. if Result = 0 then
  186. Result := lua_pcall(L, 0, LUA_MULTRET, 0);
  187. end;
  188. function lua_dostring(L: Plua_State; const str: PChar): Integer;
  189. begin
  190. Result := luaL_loadstring(L, str);
  191. if Result = 0 then
  192. Result := lua_pcall(L, 0, LUA_MULTRET, 0);
  193. end;
  194. procedure lua_Lgetmetatable(L: Plua_State; tname: PChar);
  195. begin
  196. lua_getfield(L, LUA_REGISTRYINDEX, tname);
  197. end;
  198. procedure luaL_argcheck(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PChar);
  199. begin
  200. if not cond then
  201. luaL_argerror(L, numarg, extramsg)
  202. end;
  203. function luaL_checkstring(L: Plua_State; n: Integer): PChar;
  204. begin
  205. Result := luaL_checklstring(L, n, nil)
  206. end;
  207. function luaL_optstring(L: Plua_State; n: Integer; d: PChar): PChar;
  208. begin
  209. Result := luaL_optlstring(L, n, d, nil)
  210. end;
  211. function luaL_checkint(L: Plua_State; n: Integer): Integer;
  212. begin
  213. Result := Integer(Trunc(luaL_checknumber(L, n)))
  214. end;
  215. function luaL_checklong(L: Plua_State; n: Integer): LongInt;
  216. begin
  217. Result := LongInt(Trunc(luaL_checknumber(L, n)))
  218. end;
  219. function luaL_optint(L: Plua_State; n: Integer; d: Double): Integer;
  220. begin
  221. Result := Integer(Trunc(luaL_optnumber(L, n, d)))
  222. end;
  223. function luaL_optlong(L: Plua_State; n: Integer; d: Double): LongInt;
  224. begin
  225. Result := LongInt(Trunc(luaL_optnumber(L, n, d)))
  226. end;
  227. procedure luaL_addchar(B: PluaL_Buffer; c: Char);
  228. begin
  229. if Cardinal(@(B^.p)) < (Cardinal(@(B^.buffer[0])) + LUAL_BUFFERSIZE) then
  230. luaL_prepbuffer(B);
  231. B^.p[1] := c;
  232. B^.p := B^.p + 1;
  233. end;
  234. procedure luaL_putchar(B: PluaL_Buffer; c: Char);
  235. begin
  236. luaL_addchar(B, c);
  237. end;
  238. procedure luaL_addsize(B: PluaL_Buffer; n: Integer);
  239. begin
  240. B^.p := B^.p + n;
  241. end;
  242. procedure luaL_buffinit(L: Plua_State ; B: PluaL_Buffer); cdecl; external LUA_LIB_NAME;
  243. function luaL_prepbuffer(B: PluaL_Buffer): PChar; cdecl; external LUA_LIB_NAME;
  244. procedure luaL_addlstring(B: PluaL_Buffer; const s: PChar; l: size_t); cdecl; external LUA_LIB_NAME;
  245. procedure luaL_addstring(B: PluaL_Buffer; const s: PChar); cdecl; external LUA_LIB_NAME;
  246. procedure luaL_addvalue(B: PluaL_Buffer); cdecl; external LUA_LIB_NAME;
  247. procedure luaL_pushresult(B: PluaL_Buffer); cdecl; external LUA_LIB_NAME;
  248. procedure lua_unref(L: Plua_State; ref: Integer);
  249. begin
  250. luaL_unref(L, LUA_REGISTRYINDEX, ref);
  251. end;
  252. procedure lua_getref(L: Plua_State; ref: Integer);
  253. begin
  254. lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
  255. end;
  256. function luaL_check_lstr(L: Plua_State; numArg: Integer; len: Psize_t): PChar;
  257. begin
  258. Result := luaL_checklstring(L, numArg, len);
  259. end;
  260. function luaL_opt_lstr(L: Plua_State; numArg: Integer; const def: PChar; len: Psize_t): PChar;
  261. begin
  262. Result := luaL_optlstring(L, numArg, def, len);
  263. end;
  264. function luaL_check_number(L: Plua_State; numArg: Integer): lua_Number;
  265. begin
  266. Result := luaL_checknumber(L, numArg);
  267. end;
  268. function luaL_opt_number(L: Plua_State; nArg: Integer; def: lua_Number): lua_Number;
  269. begin
  270. Result := luaL_optnumber(L, nArg, def);
  271. end;
  272. procedure luaL_arg_check(L: Plua_State; cond: Boolean; numarg: Integer; extramsg: PChar);
  273. begin
  274. luaL_argcheck(L, cond, numarg, extramsg);
  275. end;
  276. function luaL_check_string(L: Plua_State; n: Integer): PChar;
  277. begin
  278. Result := luaL_checkstring(L, n);
  279. end;
  280. function luaL_opt_string(L: Plua_State; n: Integer; d: PChar): PChar;
  281. begin
  282. Result := luaL_optstring(L, n, d);
  283. end;
  284. function luaL_check_int(L: Plua_State; n: Integer): Integer;
  285. begin
  286. Result := luaL_checkint(L, n);
  287. end;
  288. function luaL_check_long(L: Plua_State; n: Integer): LongInt;
  289. begin
  290. Result := luaL_checklong(L, n);
  291. end;
  292. function luaL_opt_int(L: Plua_State; n: Integer; d: Double): Integer;
  293. begin
  294. Result := luaL_optint(L, n, d);
  295. end;
  296. function luaL_opt_long(L: Plua_State; n: Integer; d: Double): LongInt;
  297. begin
  298. Result := luaL_optlong(L, n, d);
  299. end;
  300. end.