lapi.c 8.7 KB

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