You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

440 lines
13 KiB

  1. /*
  2. ** $Id: lua.h,v 1.283 2012/04/20 13:18:26 roberto Exp $
  3. ** Lua - A Scripting Language
  4. ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
  5. ** See Copyright Notice at the end of this file
  6. */
  7. #ifndef lua_h
  8. #define lua_h
  9. #include <stdarg.h>
  10. #include <stddef.h>
  11. #include "luaconf.h"
  12. #define LUA_VERSION_MAJOR "5"
  13. #define LUA_VERSION_MINOR "2"
  14. #define LUA_VERSION_NUM 502
  15. #define LUA_VERSION_RELEASE "1"
  16. #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
  17. #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
  18. #define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2012 Lua.org, PUC-Rio"
  19. #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
  20. /* mark for precompiled code ('<esc>Lua') */
  21. #define LUA_SIGNATURE "\033Lua"
  22. /* option for multiple returns in 'lua_pcall' and 'lua_call' */
  23. #define LUA_MULTRET (-1)
  24. /*
  25. ** pseudo-indices
  26. */
  27. #define LUA_REGISTRYINDEX LUAI_FIRSTPSEUDOIDX
  28. #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i))
  29. /* thread status */
  30. #define LUA_OK 0
  31. #define LUA_YIELD 1
  32. #define LUA_ERRRUN 2
  33. #define LUA_ERRSYNTAX 3
  34. #define LUA_ERRMEM 4
  35. #define LUA_ERRGCMM 5
  36. #define LUA_ERRERR 6
  37. typedef struct lua_State lua_State;
  38. typedef int (*lua_CFunction) (lua_State *L);
  39. /*
  40. ** functions that read/write blocks when loading/dumping Lua chunks
  41. */
  42. typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
  43. typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);
  44. /*
  45. ** prototype for memory-allocation functions
  46. */
  47. typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);
  48. /*
  49. ** basic types
  50. */
  51. #define LUA_TNONE (-1)
  52. #define LUA_TNIL 0
  53. #define LUA_TBOOLEAN 1
  54. #define LUA_TLIGHTUSERDATA 2
  55. #define LUA_TNUMBER 3
  56. #define LUA_TSTRING 4
  57. #define LUA_TTABLE 5
  58. #define LUA_TFUNCTION 6
  59. #define LUA_TUSERDATA 7
  60. #define LUA_TTHREAD 8
  61. #define LUA_NUMTAGS 9
  62. /* minimum Lua stack available to a C function */
  63. #define LUA_MINSTACK 20
  64. /* predefined values in the registry */
  65. #define LUA_RIDX_MAINTHREAD 1
  66. #define LUA_RIDX_GLOBALS 2
  67. #define LUA_RIDX_LAST LUA_RIDX_GLOBALS
  68. /* type of numbers in Lua */
  69. typedef LUA_NUMBER lua_Number;
  70. /* type for integer functions */
  71. typedef LUA_INTEGER lua_Integer;
  72. /* unsigned integer type */
  73. typedef LUA_UNSIGNED lua_Unsigned;
  74. /*
  75. ** generic extra include file
  76. */
  77. #if defined(LUA_USER_H)
  78. #include LUA_USER_H
  79. #endif
  80. /*
  81. ** state manipulation
  82. */
  83. LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
  84. LUA_API void (lua_close) (lua_State *L);
  85. LUA_API lua_State *(lua_newthread) (lua_State *L);
  86. LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
  87. LUA_API const lua_Number *(lua_version) (lua_State *L);
  88. /*
  89. ** basic stack manipulation
  90. */
  91. LUA_API int (lua_absindex) (lua_State *L, int idx);
  92. LUA_API int (lua_gettop) (lua_State *L);
  93. LUA_API void (lua_settop) (lua_State *L, int idx);
  94. LUA_API void (lua_pushvalue) (lua_State *L, int idx);
  95. LUA_API void (lua_remove) (lua_State *L, int idx);
  96. LUA_API void (lua_insert) (lua_State *L, int idx);
  97. LUA_API void (lua_replace) (lua_State *L, int idx);
  98. LUA_API void (lua_copy) (lua_State *L, int fromidx, int toidx);
  99. LUA_API int (lua_checkstack) (lua_State *L, int sz);
  100. LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n);
  101. /*
  102. ** access functions (stack -> C)
  103. */
  104. LUA_API int (lua_isnumber) (lua_State *L, int idx);
  105. LUA_API int (lua_isstring) (lua_State *L, int idx);
  106. LUA_API int (lua_iscfunction) (lua_State *L, int idx);
  107. LUA_API int (lua_isuserdata) (lua_State *L, int idx);
  108. LUA_API int (lua_type) (lua_State *L, int idx);
  109. LUA_API const char *(lua_typename) (lua_State *L, int tp);
  110. LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
  111. LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
  112. LUA_API lua_Unsigned (lua_tounsignedx) (lua_State *L, int idx, int *isnum);
  113. LUA_API int (lua_toboolean) (lua_State *L, int idx);
  114. LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
  115. LUA_API size_t (lua_rawlen) (lua_State *L, int idx);
  116. LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
  117. LUA_API void *(lua_touserdata) (lua_State *L, int idx);
  118. LUA_API lua_State *(lua_tothread) (lua_State *L, int idx);
  119. LUA_API const void *(lua_topointer) (lua_State *L, int idx);
  120. /*
  121. ** Comparison and arithmetic functions
  122. */
  123. #define LUA_OPADD 0 /* ORDER TM */
  124. #define LUA_OPSUB 1
  125. #define LUA_OPMUL 2
  126. #define LUA_OPDIV 3
  127. #define LUA_OPMOD 4
  128. #define LUA_OPPOW 5
  129. #define LUA_OPUNM 6
  130. LUA_API void (lua_arith) (lua_State *L, int op);
  131. #define LUA_OPEQ 0
  132. #define LUA_OPLT 1
  133. #define LUA_OPLE 2
  134. LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2);
  135. LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op);
  136. /*
  137. ** push functions (C -> stack)
  138. */
  139. LUA_API void (lua_pushnil) (lua_State *L);
  140. LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
  141. LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
  142. LUA_API void (lua_pushunsigned) (lua_State *L, lua_Unsigned n);
  143. LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l);
  144. LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
  145. LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
  146. va_list argp);
  147. LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);
  148. LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);
  149. LUA_API void (lua_pushboolean) (lua_State *L, int b);
  150. LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p);
  151. LUA_API int (lua_pushthread) (lua_State *L);
  152. /*
  153. ** get functions (Lua -> stack)
  154. */
  155. LUA_API void (lua_getglobal) (lua_State *L, const char *var);
  156. LUA_API void (lua_gettable) (lua_State *L, int idx);
  157. LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k);
  158. LUA_API void (lua_rawget) (lua_State *L, int idx);
  159. LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n);
  160. LUA_API void (lua_rawgetp) (lua_State *L, int idx, const void *p);
  161. LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec);
  162. LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);
  163. LUA_API int (lua_getmetatable) (lua_State *L, int objindex);
  164. LUA_API void (lua_getuservalue) (lua_State *L, int idx);
  165. /*
  166. ** set functions (stack -> Lua)
  167. */
  168. LUA_API void (lua_setglobal) (lua_State *L, const char *var);
  169. LUA_API void (lua_settable) (lua_State *L, int idx);
  170. LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k);
  171. LUA_API void (lua_rawset) (lua_State *L, int idx);
  172. LUA_API void (lua_rawseti) (lua_State *L, int idx, int n);
  173. LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
  174. LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
  175. LUA_API void (lua_setuservalue) (lua_State *L, int idx);
  176. /*
  177. ** 'load' and 'call' functions (load and run Lua code)
  178. */
  179. LUA_API void (lua_callk) (lua_State *L, int nargs, int nresults, int ctx,
  180. lua_CFunction k);
  181. #define lua_call(L,n,r) lua_callk(L, (n), (r), 0, NULL)
  182. LUA_API int (lua_getctx) (lua_State *L, int *ctx);
  183. LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
  184. int ctx, lua_CFunction k);
  185. #define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL)
  186. LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
  187. const char *chunkname,
  188. const char *mode);
  189. LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);
  190. /*
  191. ** coroutine functions
  192. */
  193. LUA_API int (lua_yieldk) (lua_State *L, int nresults, int ctx,
  194. lua_CFunction k);
  195. #define lua_yield(L,n) lua_yieldk(L, (n), 0, NULL)
  196. LUA_API int (lua_resume) (lua_State *L, lua_State *from, int narg);
  197. LUA_API int (lua_status) (lua_State *L);
  198. /*
  199. ** garbage-collection function and options
  200. */
  201. #define LUA_GCSTOP 0
  202. #define LUA_GCRESTART 1
  203. #define LUA_GCCOLLECT 2
  204. #define LUA_GCCOUNT 3
  205. #define LUA_GCCOUNTB 4
  206. #define LUA_GCSTEP 5
  207. #define LUA_GCSETPAUSE 6
  208. #define LUA_GCSETSTEPMUL 7
  209. #define LUA_GCSETMAJORINC 8
  210. #define LUA_GCISRUNNING 9
  211. #define LUA_GCGEN 10
  212. #define LUA_GCINC 11
  213. LUA_API int (lua_gc) (lua_State *L, int what, int data);
  214. /*
  215. ** miscellaneous functions
  216. */
  217. LUA_API int (lua_error) (lua_State *L);
  218. LUA_API int (lua_next) (lua_State *L, int idx);
  219. LUA_API void (lua_concat) (lua_State *L, int n);
  220. LUA_API void (lua_len) (lua_State *L, int idx);
  221. LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
  222. LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
  223. /*
  224. ** ===============================================================
  225. ** some useful macros
  226. ** ===============================================================
  227. */
  228. #define lua_tonumber(L,i) lua_tonumberx(L,i,NULL)
  229. #define lua_tointeger(L,i) lua_tointegerx(L,i,NULL)
  230. #define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL)
  231. #define lua_pop(L,n) lua_settop(L, -(n)-1)
  232. #define lua_newtable(L) lua_createtable(L, 0, 0)
  233. #define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
  234. #define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)
  235. #define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION)
  236. #define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE)
  237. #define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
  238. #define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL)
  239. #define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
  240. #define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
  241. #define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
  242. #define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0)
  243. #define lua_pushliteral(L, s) \
  244. lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)
  245. #define lua_pushglobaltable(L) \
  246. lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
  247. #define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
  248. /*
  249. ** {======================================================================
  250. ** Debug API
  251. ** =======================================================================
  252. */
  253. /*
  254. ** Event codes
  255. */
  256. #define LUA_HOOKCALL 0
  257. #define LUA_HOOKRET 1
  258. #define LUA_HOOKLINE 2
  259. #define LUA_HOOKCOUNT 3
  260. #define LUA_HOOKTAILCALL 4
  261. /*
  262. ** Event masks
  263. */
  264. #define LUA_MASKCALL (1 << LUA_HOOKCALL)
  265. #define LUA_MASKRET (1 << LUA_HOOKRET)
  266. #define LUA_MASKLINE (1 << LUA_HOOKLINE)
  267. #define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT)
  268. typedef struct lua_Debug lua_Debug; /* activation record */
  269. /* Functions to be called by the debugger in specific events */
  270. typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
  271. LUA_API int (lua_getstack) (lua_State *L, int level, lua_Debug *ar);
  272. LUA_API int (lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar);
  273. LUA_API const char *(lua_getlocal) (lua_State *L, const lua_Debug *ar, int n);
  274. LUA_API const char *(lua_setlocal) (lua_State *L, const lua_Debug *ar, int n);
  275. LUA_API const char *(lua_getupvalue) (lua_State *L, int funcindex, int n);
  276. LUA_API const char *(lua_setupvalue) (lua_State *L, int funcindex, int n);
  277. LUA_API void *(lua_upvalueid) (lua_State *L, int fidx, int n);
  278. LUA_API void (lua_upvaluejoin) (lua_State *L, int fidx1, int n1,
  279. int fidx2, int n2);
  280. LUA_API int (lua_sethook) (lua_State *L, lua_Hook func, int mask, int count);
  281. LUA_API lua_Hook (lua_gethook) (lua_State *L);
  282. LUA_API int (lua_gethookmask) (lua_State *L);
  283. LUA_API int (lua_gethookcount) (lua_State *L);
  284. struct lua_Debug {
  285. int event;
  286. const char *name; /* (n) */
  287. const char *namewhat; /* (n) 'global', 'local', 'field', 'method' */
  288. const char *what; /* (S) 'Lua', 'C', 'main', 'tail' */
  289. const char *source; /* (S) */
  290. int currentline; /* (l) */
  291. int linedefined; /* (S) */
  292. int lastlinedefined; /* (S) */
  293. unsigned char nups; /* (u) number of upvalues */
  294. unsigned char nparams;/* (u) number of parameters */
  295. char isvararg; /* (u) */
  296. char istailcall; /* (t) */
  297. char short_src[LUA_IDSIZE]; /* (S) */
  298. /* private part */
  299. struct CallInfo *i_ci; /* active function */
  300. };
  301. /* }====================================================================== */
  302. /******************************************************************************
  303. * Copyright (C) 1994-2012 Lua.org, PUC-Rio.
  304. *
  305. * Permission is hereby granted, free of charge, to any person obtaining
  306. * a copy of this software and associated documentation files (the
  307. * "Software"), to deal in the Software without restriction, including
  308. * without limitation the rights to use, copy, modify, merge, publish,
  309. * distribute, sublicense, and/or sell copies of the Software, and to
  310. * permit persons to whom the Software is furnished to do so, subject to
  311. * the following conditions:
  312. *
  313. * The above copyright notice and this permission notice shall be
  314. * included in all copies or substantial portions of the Software.
  315. *
  316. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  317. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  318. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  319. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  320. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  321. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  322. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  323. ******************************************************************************/
  324. #endif