lapi.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. ** $Id: lapi.c,v 1.84 2000/06/08 17:48:31 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #define LUA_REENTRANT
  8. #include "lua.h"
  9. #include "lapi.h"
  10. #include "lauxlib.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lgc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lref.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "ltable.h"
  20. #include "ltm.h"
  21. #include "lvm.h"
  22. const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  23. "$Authors: " LUA_AUTHORS " $";
  24. void luaA_checkCargs (lua_State *L, int nargs) {
  25. if (nargs > L->top-L->Cstack.base)
  26. luaL_verror(L, "Lua API error - "
  27. "expected at least %d arguments in C2lua stack", nargs);
  28. }
  29. lua_Object luaA_putluaObject (lua_State *L, const TObject *o) {
  30. luaD_openstack(L, L->Cstack.base);
  31. *L->Cstack.base++ = *o;
  32. return L->Cstack.base-1;
  33. }
  34. static void top2LC (lua_State *L, int n) {
  35. /* Put the `n' elements on the top as the Lua2C contents */
  36. L->Cstack.base = L->top; /* new base */
  37. L->Cstack.lua2C = L->Cstack.base-n; /* position of the new results */
  38. L->Cstack.num = n; /* number of results */
  39. }
  40. lua_Object lua_pop (lua_State *L) {
  41. luaA_checkCargs(L, 1);
  42. if (L->Cstack.base != L->top-1) {
  43. luaD_openstack(L, L->Cstack.base);
  44. *L->Cstack.base = *(--L->top);
  45. }
  46. return L->Cstack.base++;
  47. }
  48. void lua_pushglobaltable (lua_State *L) {
  49. hvalue(L->top) = L->gt;
  50. ttype(L->top) = TAG_TABLE;
  51. incr_top;
  52. }
  53. void lua_setglobaltable (lua_State *L, lua_Object newtable) {
  54. if (lua_type(L, newtable)[0] != 't') /* type == "table"? */
  55. lua_error(L, "Lua API error - invalid value for global table");
  56. L->gt = hvalue(newtable);
  57. }
  58. /*
  59. ** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
  60. ** `number' must be 1 to get the first parameter.
  61. */
  62. lua_Object lua_lua2C (lua_State *L, int number) {
  63. if (number <= 0 || number > L->Cstack.num) return LUA_NOOBJECT;
  64. return L->Cstack.lua2C+number-1;
  65. }
  66. int lua_callfunction (lua_State *L, lua_Object function) {
  67. if (function == LUA_NOOBJECT)
  68. return 1;
  69. else {
  70. luaD_openstack(L, L->Cstack.base);
  71. *L->Cstack.base = *function;
  72. return luaD_protectedrun(L);
  73. }
  74. }
  75. lua_Object lua_gettagmethod (lua_State *L, int tag, const char *event) {
  76. return luaA_putluaObject(L, luaT_gettagmethod(L, tag, event));
  77. }
  78. lua_Object lua_settagmethod (lua_State *L, int tag, const char *event) {
  79. TObject *method;
  80. luaA_checkCargs(L, 1);
  81. method = L->top-1;
  82. if ((ttype(method) != TAG_NIL) && (*lua_type(L, method) != 'f'))
  83. lua_error(L, "Lua API error - tag method must be a function or nil");
  84. luaT_settagmethod(L, tag, event, method);
  85. return lua_pop(L);
  86. }
  87. lua_Object lua_gettable (lua_State *L) {
  88. luaA_checkCargs(L, 2);
  89. luaV_gettable(L, L->top--);
  90. return lua_pop(L);
  91. }
  92. lua_Object lua_rawget (lua_State *L) {
  93. lua_Object res;
  94. luaA_checkCargs(L, 2);
  95. if (ttype(L->top-2) != TAG_TABLE)
  96. lua_error(L, "indexed expression not a table");
  97. res = luaA_putluaObject(L, luaH_get(L, hvalue(L->top-2), L->top-1));
  98. L->top -= 2;
  99. return res;
  100. }
  101. void lua_settable (lua_State *L) {
  102. StkId top;
  103. luaA_checkCargs(L, 3);
  104. top = L->top;
  105. luaV_settable(L, top-3, top);
  106. L->top = top-3; /* pop table, index, and value */
  107. }
  108. void lua_rawset (lua_State *L) {
  109. luaA_checkCargs(L, 3);
  110. if (ttype(L->top-3) != TAG_TABLE)
  111. lua_error(L, "indexed expression not a table");
  112. *luaH_set(L, hvalue(L->top-3), L->top-2) = *(L->top-1);
  113. L->top -= 3;
  114. }
  115. lua_Object lua_createtable (lua_State *L) {
  116. TObject o;
  117. luaC_checkGC(L);
  118. hvalue(&o) = luaH_new(L, 0);
  119. ttype(&o) = TAG_TABLE;
  120. return luaA_putluaObject(L, &o);
  121. }
  122. lua_Object lua_getglobal (lua_State *L, const char *name) {
  123. luaV_getglobal(L, luaS_new(L, name), L->top++);
  124. return lua_pop(L);
  125. }
  126. void lua_setglobal (lua_State *L, const char *name) {
  127. luaA_checkCargs(L, 1);
  128. luaV_setglobal(L, luaS_new(L, name), L->top--);
  129. }
  130. const char *lua_type (lua_State *L, lua_Object o) {
  131. UNUSED(L);
  132. return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(o);
  133. }
  134. int lua_isnil (lua_State *L, lua_Object o) {
  135. UNUSED(L);
  136. return (o != LUA_NOOBJECT) && (ttype(o) == TAG_NIL);
  137. }
  138. int lua_istable (lua_State *L, lua_Object o) {
  139. UNUSED(L);
  140. return (o != LUA_NOOBJECT) && (ttype(o) == TAG_TABLE);
  141. }
  142. int lua_isuserdata (lua_State *L, lua_Object o) {
  143. UNUSED(L);
  144. return (o != LUA_NOOBJECT) && (ttype(o) == TAG_USERDATA);
  145. }
  146. int lua_iscfunction (lua_State *L, lua_Object o) {
  147. UNUSED(L);
  148. return (o != LUA_NOOBJECT) && (ttype(o) == TAG_CCLOSURE);
  149. }
  150. int lua_isnumber (lua_State *L, lua_Object o) {
  151. UNUSED(L);
  152. return (o != LUA_NOOBJECT) && (tonumber(o) == 0);
  153. }
  154. int lua_isstring (lua_State *L, lua_Object o) {
  155. UNUSED(L);
  156. return (o != LUA_NOOBJECT && (ttype(o) == TAG_STRING ||
  157. ttype(o) == TAG_NUMBER));
  158. }
  159. int lua_isfunction (lua_State *L, lua_Object o) {
  160. return *lua_type(L, o) == 'f';
  161. }
  162. int lua_equal(lua_State *L, lua_Object o1, lua_Object o2) {
  163. UNUSED(L);
  164. if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT)
  165. return (o1 == o2);
  166. else return luaO_equalObj(o1, o2);
  167. }
  168. double lua_getnumber (lua_State *L, lua_Object obj) {
  169. UNUSED(L);
  170. if (obj == LUA_NOOBJECT || tonumber(obj))
  171. return 0.0;
  172. else return (nvalue(obj));
  173. }
  174. const char *lua_getstring (lua_State *L, lua_Object obj) {
  175. luaC_checkGC(L); /* `tostring' may create a new string */
  176. if (obj == LUA_NOOBJECT || tostring(L, obj))
  177. return NULL;
  178. else return (svalue(obj));
  179. }
  180. size_t lua_strlen (lua_State *L, lua_Object obj) {
  181. if (obj == LUA_NOOBJECT || tostring(L, obj))
  182. return 0L;
  183. else return (tsvalue(obj)->u.s.len);
  184. }
  185. void *lua_getuserdata (lua_State *L, lua_Object obj) {
  186. UNUSED(L);
  187. if (obj == LUA_NOOBJECT || ttype(obj) != TAG_USERDATA)
  188. return NULL;
  189. else return tsvalue(obj)->u.d.value;
  190. }
  191. lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj) {
  192. if (!lua_iscfunction(L, obj))
  193. return NULL;
  194. else return clvalue(obj)->f.c;
  195. }
  196. void lua_pushnil (lua_State *L) {
  197. ttype(L->top) = TAG_NIL;
  198. incr_top;
  199. }
  200. void lua_pushnumber (lua_State *L, double n) {
  201. ttype(L->top) = TAG_NUMBER;
  202. nvalue(L->top) = n;
  203. incr_top;
  204. }
  205. void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  206. tsvalue(L->top) = luaS_newlstr(L, s, len);
  207. ttype(L->top) = TAG_STRING;
  208. incr_top;
  209. luaC_checkGC(L);
  210. }
  211. void lua_pushstring (lua_State *L, const char *s) {
  212. if (s == NULL)
  213. lua_pushnil(L);
  214. else
  215. lua_pushlstring(L, s, strlen(s));
  216. }
  217. void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  218. if (fn == NULL)
  219. lua_error(L, "Lua API error - attempt to push a NULL Cfunction");
  220. luaA_checkCargs(L, n);
  221. luaV_Cclosure(L, fn, n);
  222. luaC_checkGC(L);
  223. }
  224. void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
  225. if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
  226. luaL_verror(L, "invalid tag for a userdata (%d)", tag);
  227. tsvalue(L->top) = luaS_createudata(L, u, tag);
  228. ttype(L->top) = TAG_USERDATA;
  229. incr_top;
  230. luaC_checkGC(L);
  231. }
  232. void luaA_pushobject (lua_State *L, const TObject *o) {
  233. *L->top = *o;
  234. incr_top;
  235. }
  236. void lua_pushobject (lua_State *L, lua_Object o) {
  237. if (o == LUA_NOOBJECT)
  238. lua_error(L, "Lua API error - attempt to push a NOOBJECT");
  239. *L->top = *o;
  240. incr_top;
  241. }
  242. int lua_tag (lua_State *L, lua_Object o) {
  243. if (o == LUA_NOOBJECT)
  244. return TAG_NIL;
  245. else if (ttype(o) == TAG_USERDATA) /* to allow `old' tags (deprecated) */
  246. return tsvalue(o)->u.d.tag;
  247. else
  248. return luaT_effectivetag(L, o);
  249. }
  250. void lua_settag (lua_State *L, int tag) {
  251. luaA_checkCargs(L, 1);
  252. luaT_realtag(L, tag);
  253. switch (ttype(L->top-1)) {
  254. case TAG_TABLE:
  255. hvalue(L->top-1)->htag = tag;
  256. break;
  257. case TAG_USERDATA:
  258. tsvalue(L->top-1)->u.d.tag = tag;
  259. break;
  260. default:
  261. luaL_verror(L, "cannot change the tag of a %.20s",
  262. luaO_typename(L->top-1));
  263. }
  264. L->top--;
  265. }
  266. int luaA_next (lua_State *L, const Hash *t, int i) {
  267. int tsize = t->size;
  268. for (; i<tsize; i++) {
  269. Node *n = node(t, i);
  270. if (ttype(val(n)) != TAG_NIL) {
  271. luaA_pushobject(L, key(n));
  272. luaA_pushobject(L, val(n));
  273. return i+1; /* index to be used next time */
  274. }
  275. }
  276. return 0; /* no more elements */
  277. }
  278. int lua_next (lua_State *L, lua_Object t, int i) {
  279. if (ttype(t) != TAG_TABLE)
  280. lua_error(L, "Lua API error - object is not a table in `lua_next'");
  281. i = luaA_next(L, hvalue(t), i);
  282. top2LC(L, (i==0) ? 0 : 2);
  283. return i;
  284. }
  285. #if LUA_DEPRECATETFUNCS
  286. /*
  287. ** obsolete functions
  288. */
  289. lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
  290. lua_pushglobaltable(L);
  291. lua_pushstring(L, name);
  292. return lua_rawget(L);
  293. }
  294. void lua_rawsetglobal (lua_State *L, const char *name) {
  295. lua_Object value;
  296. lua_beginblock(L);
  297. value = lua_pop(L);
  298. lua_pushglobaltable(L);
  299. lua_pushstring(L, name);
  300. lua_pushobject(L, value);
  301. lua_rawset(L);
  302. lua_endblock(L);
  303. }
  304. #endif