lapi.c 8.8 KB

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