lauxlib.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. (*
  2. ** $Id: lauxlib.pas,v 1.4 2007-01-20 16:40:27 jfgoulet Exp $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. **
  6. ** Translation form C and Delphi adaptation of Code : Massimo Magnano 2006
  7. *)
  8. unit lauxlib;
  9. {$IFNDEF lauxlib_h}
  10. {$DEFINE lauxlib_h}
  11. {$ENDIF}
  12. interface
  13. uses lua, luaconf;
  14. {$ifdef LUA_COMPAT_GETN}
  15. function luaL_getn(L: Plua_State; t: Integer): Integer; cdecl external 'lua5.1.dll';
  16. procedure luaL_setn(L: Plua_State; t: Integer; n: Integer); cdecl external 'lua5.1.dll';
  17. {$else}
  18. function luaL_getn(L: Plua_State; t: Integer): Integer;
  19. procedure luaL_setn(L: Plua_State; t: Integer; n: Integer);
  20. {$endif}
  21. {$ifdef LUA_COMPAT_OPENLIB}
  22. procedure luaL_openlib(L: Plua_State; libname: PChar;
  23. R: PluaL_reg; Nup: Integer); cdecl external 'lua5.1.dll' name 'luaI_openlib';
  24. {$endif}
  25. const
  26. LUA_ERRFILE = (LUA_ERRERR+1);
  27. type
  28. luaL_Reg = record
  29. name: PChar;
  30. func: lua_CFunction;
  31. end;
  32. PluaL_Reg = ^luaL_Reg;
  33. procedure luaI_openlib(L: Plua_State; libname: PChar;
  34. R: PluaL_reg; nup: Integer); cdecl external 'lua5.1.dll';
  35. procedure luaL_register(L: Plua_State; libname: PChar;
  36. const R: PluaL_reg); cdecl external 'lua5.1.dll';
  37. function luaL_getmetafield(L: Plua_State; obj: Integer; const e: PChar): Integer; cdecl external 'lua5.1.dll';
  38. function luaL_callmeta(L: Plua_State; obj: Integer; const e: PChar): Integer; cdecl external 'lua5.1.dll';
  39. function luaL_typerror(L: Plua_State; narg: Integer; const tname: PChar): Integer; cdecl external 'lua5.1.dll';
  40. function luaL_argerror(L: Plua_State; numarg: Integer; const extramsg: PChar): Integer; cdecl external 'lua5.1.dll';
  41. function luaL_checklstring(L: Plua_State; numArg: Integer; s: Psize_t): PChar; cdecl external 'lua5.1.dll';
  42. function luaL_optlstring(L: Plua_State; numArg: Integer;
  43. const def: PChar; s: Psize_t): PChar; cdecl external 'lua5.1.dll';
  44. function luaL_checknumber(L: Plua_State; numArg: Integer): LUA_NUMBER; cdecl external 'lua5.1.dll';
  45. function luaL_optnumber(L: Plua_State; nArg: Integer; def: LUA_NUMBER): LUA_NUMBER; cdecl external 'lua5.1.dll';
  46. function luaL_checkinteger(L: Plua_State; numArg: Integer): LUA_INTEGER; cdecl external 'lua5.1.dll';
  47. function luaL_optinteger(L: Plua_State; nArg: Integer; def: LUA_INTEGER): LUA_INTEGER; cdecl external 'lua5.1.dll';
  48. procedure luaL_checkstack(L: Plua_State; sz: Integer; const msg: PChar); cdecl external 'lua5.1.dll';
  49. procedure luaL_checktype(L: Plua_State; narg, t: Integer); cdecl external 'lua5.1.dll';
  50. procedure luaL_checkany(L: Plua_State; narg: Integer); cdecl external 'lua5.1.dll';
  51. function luaL_newmetatable(L: Plua_State; const tname: PChar): Integer; cdecl external 'lua5.1.dll';
  52. function luaL_checkudata(L: Plua_State; ud: Integer; const tname: PChar): Pointer; cdecl external 'lua5.1.dll';
  53. procedure luaL_where(L: Plua_State; lvl: Integer); cdecl external 'lua5.1.dll';
  54. function luaL_error(L: Plua_State; const fmt: PChar): Integer; varargs; cdecl external 'lua5.1.dll';
  55. function luaL_checkoption(L: Plua_State; narg: Integer; def: PChar; lst: array of PChar): Integer; cdecl external 'lua5.1.dll';
  56. function luaL_ref(L: Plua_State; t: Integer): Integer; cdecl external 'lua5.1.dll';
  57. procedure luaL_unref(L: Plua_State; t: Integer; ref: Integer); cdecl external 'lua5.1.dll';
  58. function luaL_loadfile(L: Plua_State; const fileName: PChar): Integer; cdecl external 'lua5.1.dll';
  59. function luaL_loadbuffer(L: Plua_State; const buff: PChar; sz: size_t;
  60. const name: PChar): Integer; cdecl external 'lua5.1.dll';
  61. function luaL_loadstring(L: Plua_State; const s: PChar): Integer; cdecl external 'lua5.1.dll';
  62. function luaL_newstate: Plua_State; cdecl external 'lua5.1.dll';
  63. function luaL_gsub(L: Plua_State; const s, p, r: PChar): PChar; cdecl external 'lua5.1.dll';
  64. function luaL_findtable(L: Plua_State; idx: Integer; const fname: PChar; szhint: Integer): PChar; cdecl external 'lua5.1.dll';
  65. (*
  66. ** ===============================================================
  67. ** some useful macros
  68. ** ===============================================================
  69. *)
  70. function luaL_argcheck(L: Plua_State; Cond: Boolean; NumArg: Integer; const ExtraMsg: PChar): Integer;
  71. function luaL_checkstring(L: Plua_State; N: Integer): PChar;
  72. function luaL_optstring(L: Plua_State; N: Integer; const D: PChar): PChar;
  73. function luaL_checkint(L: Plua_State; narg: Integer): Integer;
  74. function luaL_optint(L: Plua_State; narg: Integer; D: Integer): Integer;
  75. function luaL_checklong(L: Plua_State; narg: Integer): LongInt;
  76. function luaL_optlong(L: Plua_State; narg: Integer; d: Longint): Longint;
  77. function luaL_typename(L: Plua_State; i: Integer): PChar;
  78. function luaL_dofile(L: Plua_State; fn :PChar): Integer;
  79. function luaL_dostring(L: Plua_State; s :PChar): Integer;
  80. procedure luaL_getmetatable(L: Plua_State; const TName: PChar);
  81. type
  82. luaL_optFunction = function (L: Plua_State; n :Integer): Integer; cdecl;
  83. function luaL_opt(L: Plua_State; f: luaL_optFunction; n, d :Integer): Integer;
  84. (*
  85. ** {======================================================
  86. ** Generic Buffer manipulation
  87. ** =======================================================
  88. *)
  89. type
  90. luaL_Buffer = record
  91. p: PChar; (* current position in buffer *)
  92. lvl: Integer; (* number of strings in the stack(level) *)
  93. L: Plua_State;
  94. buffer: array [0..LUAL_BUFFERSIZE - 1] of Char;
  95. end;
  96. PluaL_Buffer = ^luaL_Buffer;
  97. procedure luaL_addchar(B: PluaL_Buffer; c: Char);
  98. (* compatibility only *)
  99. procedure luaL_putchar(B: PluaL_Buffer; c: Char);
  100. function luaL_addsize(B: PLuaL_Buffer; N: Integer): PChar;
  101. procedure luaL_buffinit(L: Plua_State; B: PluaL_Buffer); cdecl external 'lua5.1.dll';
  102. function luaL_prepbuffer(B: PluaL_Buffer): PChar; cdecl external 'lua5.1.dll';
  103. procedure luaL_addlstring(B: PluaL_Buffer; const s: PChar; l: size_t); cdecl external 'lua5.1.dll';
  104. procedure luaL_addstring(B: PluaL_Buffer; const s: PChar); cdecl external 'lua5.1.dll';
  105. procedure luaL_addvalue(B: PluaL_Buffer); cdecl external 'lua5.1.dll';
  106. procedure luaL_pushresult(B: PluaL_Buffer); cdecl external 'lua5.1.dll';
  107. function lua_dofile(L: Plua_State; const FileName: PChar): Integer;
  108. function lua_dostring(L: Plua_State; const Str: PChar): Integer;
  109. function lua_dobuffer(L: Plua_State; const Buff: PChar; SZ: size_t;
  110. const N: PChar): Integer;
  111. (* }====================================================== *)
  112. (* Compatibility with ref system *)
  113. const
  114. LUA_NOREF = -2;
  115. LUA_REFNIL = -1;
  116. function lua_ref(L: Plua_State; lock: Boolean): Integer;
  117. procedure lua_unref(L: Plua_State; ref: Integer);
  118. procedure lua_getref(L: Plua_State; ref: Integer);
  119. implementation
  120. {$ifndef LUA_COMPAT_GETN}
  121. function luaL_getn(L: Plua_State; t: Integer): Integer;
  122. begin
  123. Result :=lua_objlen(L, t)
  124. end;
  125. procedure luaL_setn(L: Plua_State; t: Integer; n: Integer);
  126. begin
  127. end;
  128. {$endif}
  129. (*
  130. ** ===============================================================
  131. ** some useful macros
  132. ** ===============================================================
  133. *)
  134. function luaL_argcheck(L: Plua_State; Cond: Boolean; NumArg: Integer; const ExtraMsg: PChar): Integer;
  135. begin
  136. Result := 0;
  137. if not(Cond)
  138. then Result := luaL_argerror(L, NumArg, ExtraMsg)
  139. end;
  140. function luaL_checkstring(L: Plua_State; N: Integer): PChar;
  141. begin
  142. Result := luaL_checklstring(L, N, nil);
  143. end;
  144. function luaL_optstring(L: Plua_State; N: Integer; const D: PChar): PChar;
  145. begin
  146. Result := luaL_optlstring(L, N, D, nil);
  147. end;
  148. function luaL_checkint(L: Plua_State; narg: Integer): Integer;
  149. begin
  150. Result := luaL_checkinteger(L, narg);
  151. end;
  152. function luaL_optint(L: Plua_State; narg: Integer; D: Integer): Integer;
  153. begin
  154. Result := luaL_optinteger(L, narg, D);
  155. end;
  156. function luaL_checklong(L: Plua_State; narg: Integer): Longint;
  157. begin
  158. Result := LongInt(luaL_checkinteger(L, narg));
  159. //old code : Result := Trunc(luaL_checknumber(L, narg));
  160. end;
  161. function luaL_optlong(L: Plua_State; narg: Integer; d: Longint): Longint;
  162. begin
  163. Result := LongInt(luaL_optinteger(L, narg, D));
  164. //old code : Result := Trunc(luaL_optnumber(L, narg, D));
  165. end;
  166. function luaL_typename(L: Plua_State; i: Integer): PChar;
  167. begin
  168. Result :=lua_typename(L, lua_type(L, i));
  169. end;
  170. function luaL_dofile(L: Plua_State; fn :PChar): Integer;
  171. begin
  172. Result := luaL_loadfile(L, fn);
  173. if (Result=0)
  174. then Result := lua_pcall(L, 0, LUA_MULTRET, 0);
  175. end;
  176. function luaL_dostring(L: Plua_State; s :PChar): Integer;
  177. begin
  178. Result := luaL_loadstring(L, s);
  179. if (Result=0)
  180. then Result := lua_pcall(L, 0, LUA_MULTRET, 0);
  181. end;
  182. procedure luaL_getmetatable(L: Plua_State; const TName: PChar);
  183. begin
  184. lua_getfield(L, LUA_REGISTRYINDEX, TName);
  185. end;
  186. function luaL_opt(L: Plua_State; f: luaL_optFunction; n, d :Integer): Integer;
  187. begin
  188. if lua_isnoneornil(L, n)
  189. then Result := d
  190. else Result := f(L, n);
  191. end;
  192. (*
  193. ** {======================================================
  194. ** Generic Buffer manipulation
  195. ** =======================================================
  196. *)
  197. procedure luaL_addchar(B: PluaL_Buffer; C: Char);
  198. begin
  199. if ((B.P <= @B.Buffer[High(B.Buffer)]) or (luaL_prepbuffer(B) <> #0)) then
  200. begin
  201. B.P^ := C;
  202. Inc(B.P);
  203. end;
  204. end;
  205. procedure luaL_putchar(B: PluaL_Buffer; c: Char);
  206. begin
  207. luaL_addchar(B, C);
  208. end;
  209. function luaL_addsize(B: PLuaL_Buffer; N: Integer): PChar;
  210. begin
  211. Inc(B.P, N);
  212. Result := B.P;
  213. end;
  214. function lua_dofile(L: Plua_State; const FileName: PChar): Integer;
  215. begin
  216. Result :=luaL_dofile(L, FileName);
  217. end;
  218. function lua_dostring(L: Plua_State; const Str: PChar): Integer;
  219. begin
  220. Result :=luaL_dostring(L, Str);
  221. end;
  222. function lua_dobuffer(L: Plua_State; const Buff: PChar; SZ: size_t;
  223. const N: PChar): Integer;
  224. begin
  225. Result := luaL_loadbuffer(L, Buff, SZ, N);
  226. if (Result=0)
  227. then Result := lua_pcall(L, 0, LUA_MULTRET, 0);
  228. end;
  229. (* compatibility with ref system *)
  230. function lua_ref(L: Plua_State; lock: Boolean): Integer;
  231. begin
  232. if lock
  233. then Result :=luaL_ref(L, LUA_REGISTRYINDEX)
  234. else begin
  235. lua_pushstring(L, 'unlocked references are obsolete');
  236. lua_error(L);
  237. Result :=0;
  238. end;
  239. end;
  240. procedure lua_unref(L: Plua_State; ref: Integer);
  241. begin
  242. luaL_unref(L, LUA_REGISTRYINDEX, ref);
  243. end;
  244. procedure lua_getref(L: Plua_State; ref: Integer);
  245. begin
  246. lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
  247. end;
  248. end.