lapi.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. ** $Id: lapi.c,v 1.79 2000/05/08 19:32:53 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #define LUA_REENTRANT
  8. #include "lapi.h"
  9. #include "lauxlib.h"
  10. #include "ldo.h"
  11. #include "lfunc.h"
  12. #include "lgc.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lref.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "ltm.h"
  20. #include "lua.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. avalue(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 = avalue(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_rawgettable (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 in rawgettable");
  97. res = luaA_putluaObject(L, luaH_get(L, avalue(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_rawsettable (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, avalue(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. avalue(&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. /* deprecated */
  131. lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
  132. lua_pushglobaltable(L);
  133. lua_pushstring(L, name);
  134. return lua_rawgettable(L);
  135. }
  136. /* deprecated */
  137. void lua_rawsetglobal (lua_State *L, const char *name) {
  138. TObject key;
  139. luaA_checkCargs(L, 1);
  140. ttype(&key) = TAG_STRING;
  141. tsvalue(&key) = luaS_new(L, name);
  142. luaH_set(L, L->gt, &key, --L->top);
  143. }
  144. const char *lua_type (lua_State *L, lua_Object o) {
  145. UNUSED(L);
  146. return (o == LUA_NOOBJECT) ? "NOOBJECT" : luaO_typename(o);
  147. }
  148. int lua_isnil (lua_State *L, lua_Object o) {
  149. UNUSED(L);
  150. return (o != LUA_NOOBJECT) && (ttype(o) == TAG_NIL);
  151. }
  152. int lua_istable (lua_State *L, lua_Object o) {
  153. UNUSED(L);
  154. return (o != LUA_NOOBJECT) && (ttype(o) == TAG_TABLE);
  155. }
  156. int lua_isuserdata (lua_State *L, lua_Object o) {
  157. UNUSED(L);
  158. return (o != LUA_NOOBJECT) && (ttype(o) == TAG_USERDATA);
  159. }
  160. int lua_iscfunction (lua_State *L, lua_Object o) {
  161. UNUSED(L);
  162. return (o != LUA_NOOBJECT) && (ttype(o) == TAG_CCLOSURE);
  163. }
  164. int lua_isnumber (lua_State *L, lua_Object o) {
  165. UNUSED(L);
  166. return (o != LUA_NOOBJECT) && (tonumber(o) == 0);
  167. }
  168. int lua_isstring (lua_State *L, lua_Object o) {
  169. UNUSED(L);
  170. return (o != LUA_NOOBJECT && (ttype(o) == TAG_STRING ||
  171. ttype(o) == TAG_NUMBER));
  172. }
  173. int lua_isfunction (lua_State *L, lua_Object o) {
  174. return *lua_type(L, o) == 'f';
  175. }
  176. int lua_equal(lua_State *L, lua_Object o1, lua_Object o2) {
  177. UNUSED(L);
  178. if (o1 == LUA_NOOBJECT || o2 == LUA_NOOBJECT)
  179. return (o1 == o2);
  180. else return luaO_equalObj(o1, o2);
  181. }
  182. double lua_getnumber (lua_State *L, lua_Object obj) {
  183. UNUSED(L);
  184. if (obj == LUA_NOOBJECT || tonumber(obj))
  185. return 0.0;
  186. else return (nvalue(obj));
  187. }
  188. const char *lua_getstring (lua_State *L, lua_Object obj) {
  189. luaC_checkGC(L); /* `tostring' may create a new string */
  190. if (obj == LUA_NOOBJECT || tostring(L, obj))
  191. return NULL;
  192. else return (svalue(obj));
  193. }
  194. long lua_strlen (lua_State *L, lua_Object obj) {
  195. if (obj == LUA_NOOBJECT || tostring(L, obj))
  196. return 0L;
  197. else return (tsvalue(obj)->u.s.len);
  198. }
  199. void *lua_getuserdata (lua_State *L, lua_Object obj) {
  200. UNUSED(L);
  201. if (obj == LUA_NOOBJECT || ttype(obj) != TAG_USERDATA)
  202. return NULL;
  203. else return tsvalue(obj)->u.d.value;
  204. }
  205. lua_CFunction lua_getcfunction (lua_State *L, lua_Object obj) {
  206. if (!lua_iscfunction(L, obj))
  207. return NULL;
  208. else return clvalue(obj)->f.c;
  209. }
  210. void lua_pushnil (lua_State *L) {
  211. ttype(L->top) = TAG_NIL;
  212. incr_top;
  213. }
  214. void lua_pushnumber (lua_State *L, double n) {
  215. ttype(L->top) = TAG_NUMBER;
  216. nvalue(L->top) = n;
  217. incr_top;
  218. }
  219. void lua_pushlstring (lua_State *L, const char *s, long len) {
  220. tsvalue(L->top) = luaS_newlstr(L, s, len);
  221. ttype(L->top) = TAG_STRING;
  222. incr_top;
  223. luaC_checkGC(L);
  224. }
  225. void lua_pushstring (lua_State *L, const char *s) {
  226. if (s == NULL)
  227. lua_pushnil(L);
  228. else
  229. lua_pushlstring(L, s, strlen(s));
  230. }
  231. void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  232. if (fn == NULL)
  233. lua_error(L, "Lua API error - attempt to push a NULL Cfunction");
  234. luaA_checkCargs(L, n);
  235. luaV_Cclosure(L, fn, n);
  236. luaC_checkGC(L);
  237. }
  238. void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
  239. if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
  240. luaL_verror(L, "invalid tag for a userdata (%d)", tag);
  241. tsvalue(L->top) = luaS_createudata(L, u, tag);
  242. ttype(L->top) = TAG_USERDATA;
  243. incr_top;
  244. luaC_checkGC(L);
  245. }
  246. void luaA_pushobject (lua_State *L, const TObject *o) {
  247. *L->top = *o;
  248. incr_top;
  249. }
  250. void lua_pushobject (lua_State *L, lua_Object o) {
  251. if (o == LUA_NOOBJECT)
  252. lua_error(L, "Lua API error - attempt to push a NOOBJECT");
  253. *L->top = *o;
  254. incr_top;
  255. }
  256. int lua_tag (lua_State *L, lua_Object o) {
  257. if (o == LUA_NOOBJECT)
  258. return TAG_NIL;
  259. else if (ttype(o) == TAG_USERDATA) /* to allow `old' tags (deprecated) */
  260. return o->value.ts->u.d.tag;
  261. else
  262. return luaT_effectivetag(L, o);
  263. }
  264. void lua_settag (lua_State *L, int tag) {
  265. luaA_checkCargs(L, 1);
  266. luaT_realtag(L, tag);
  267. switch (ttype(L->top-1)) {
  268. case TAG_TABLE:
  269. (L->top-1)->value.a->htag = tag;
  270. break;
  271. case TAG_USERDATA:
  272. (L->top-1)->value.ts->u.d.tag = tag;
  273. break;
  274. default:
  275. luaL_verror(L, "cannot change the tag of a %.20s",
  276. luaO_typename(L->top-1));
  277. }
  278. L->top--;
  279. }
  280. int luaA_next (lua_State *L, const Hash *t, int i) {
  281. int tsize = t->size;
  282. for (; i<tsize; i++) {
  283. Node *n = node(t, i);
  284. if (ttype(val(n)) != TAG_NIL) {
  285. luaA_pushobject(L, key(n));
  286. luaA_pushobject(L, val(n));
  287. return i+1; /* index to be used next time */
  288. }
  289. }
  290. return 0; /* no more elements */
  291. }
  292. int lua_next (lua_State *L, lua_Object t, int i) {
  293. if (ttype(t) != TAG_TABLE)
  294. lua_error(L, "Lua API error - object is not a table in `lua_next'");
  295. i = luaA_next(L, avalue(t), i);
  296. top2LC(L, (i==0) ? 0 : 2);
  297. return i;
  298. }