lapi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. ** $Id: lapi.c,v 1.119 2001/01/24 15:45:33 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 "ldo.h"
  10. #include "lfunc.h"
  11. #include "lgc.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. #include "lstring.h"
  16. #include "ltable.h"
  17. #include "ltm.h"
  18. #include "lvm.h"
  19. const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  20. "$Authors: " LUA_AUTHORS " $";
  21. #define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i)))
  22. #define api_incr_top(L) incr_top
  23. TObject *luaA_index (lua_State *L, int index) {
  24. return Index(L, index);
  25. }
  26. static TObject *luaA_indexAcceptable (lua_State *L, int index) {
  27. if (index >= 0) {
  28. TObject *o = L->Cbase+(index-1);
  29. if (o >= L->top) return NULL;
  30. else return o;
  31. }
  32. else return L->top+index;
  33. }
  34. void luaA_pushobject (lua_State *L, const TObject *o) {
  35. setobj(L->top, o);
  36. incr_top;
  37. }
  38. LUA_API int lua_stackspace (lua_State *L) {
  39. int i;
  40. LUA_ENTRY;
  41. i = (L->stack_last - L->top);
  42. LUA_EXIT;
  43. return i;
  44. }
  45. /*
  46. ** basic stack manipulation
  47. */
  48. LUA_API int lua_gettop (lua_State *L) {
  49. int i;
  50. LUA_ENTRY;
  51. i = (L->top - L->Cbase);
  52. LUA_EXIT;
  53. return i;
  54. }
  55. LUA_API void lua_settop (lua_State *L, int index) {
  56. LUA_ENTRY;
  57. if (index >= 0)
  58. luaD_adjusttop(L, L->Cbase, index);
  59. else
  60. L->top = L->top+index+1; /* index is negative */
  61. LUA_EXIT;
  62. }
  63. LUA_API void lua_remove (lua_State *L, int index) {
  64. StkId p;
  65. LUA_ENTRY;
  66. p = luaA_index(L, index);
  67. while (++p < L->top) setobj(p-1, p);
  68. L->top--;
  69. LUA_EXIT;
  70. }
  71. LUA_API void lua_insert (lua_State *L, int index) {
  72. StkId p;
  73. StkId q;
  74. LUA_ENTRY;
  75. p = luaA_index(L, index);
  76. for (q = L->top; q>p; q--) setobj(q, q-1);
  77. setobj(p, L->top);
  78. LUA_EXIT;
  79. }
  80. LUA_API void lua_pushvalue (lua_State *L, int index) {
  81. LUA_ENTRY;
  82. setobj(L->top, luaA_index(L, index));
  83. api_incr_top(L);
  84. LUA_EXIT;
  85. }
  86. /*
  87. ** access functions (stack -> C)
  88. */
  89. LUA_API int lua_type (lua_State *L, int index) {
  90. StkId o;
  91. int i;
  92. LUA_ENTRY;
  93. o = luaA_indexAcceptable(L, index);
  94. i = (o == NULL) ? LUA_TNONE : ttype(o);
  95. LUA_EXIT;
  96. return i;
  97. }
  98. LUA_API const char *lua_typename (lua_State *L, int t) {
  99. const char *s;
  100. LUA_ENTRY;
  101. s = (t == LUA_TNONE) ? "no value" : basictypename(G(L), t);
  102. LUA_EXIT;
  103. return s;
  104. }
  105. LUA_API const char *lua_xtype (lua_State *L, int index) {
  106. StkId o;
  107. const char *type;
  108. LUA_ENTRY;
  109. o = luaA_indexAcceptable(L, index);
  110. type = (o == NULL) ? "no value" : luaT_typename(G(L), o);
  111. LUA_EXIT;
  112. return type;
  113. }
  114. LUA_API int lua_iscfunction (lua_State *L, int index) {
  115. StkId o;
  116. int i;
  117. LUA_ENTRY;
  118. o = luaA_indexAcceptable(L, index);
  119. i = (o == NULL) ? 0 : iscfunction(o);
  120. LUA_EXIT;
  121. return i;
  122. }
  123. LUA_API int lua_isnumber (lua_State *L, int index) {
  124. TObject *o;
  125. int i;
  126. LUA_ENTRY;
  127. o = luaA_indexAcceptable(L, index);
  128. i = (o == NULL) ? 0 : (tonumber(o) == 0);
  129. LUA_EXIT;
  130. return i;
  131. }
  132. LUA_API int lua_isstring (lua_State *L, int index) {
  133. int t = lua_type(L, index);
  134. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  135. }
  136. LUA_API int lua_tag (lua_State *L, int index) {
  137. StkId o;
  138. int i;
  139. LUA_ENTRY;
  140. o = luaA_indexAcceptable(L, index);
  141. i = (o == NULL) ? LUA_NOTAG : luaT_tag(o);
  142. LUA_EXIT;
  143. return i;
  144. }
  145. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  146. StkId o1, o2;
  147. int i;
  148. LUA_ENTRY;
  149. o1 = luaA_indexAcceptable(L, index1);
  150. o2 = luaA_indexAcceptable(L, index2);
  151. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  152. : luaO_equalObj(o1, o2);
  153. LUA_EXIT;
  154. return i;
  155. }
  156. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  157. StkId o1, o2;
  158. int i;
  159. LUA_ENTRY;
  160. o1 = luaA_indexAcceptable(L, index1);
  161. o2 = luaA_indexAcceptable(L, index2);
  162. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  163. : luaV_lessthan(L, o1, o2, L->top);
  164. LUA_EXIT;
  165. return i;
  166. }
  167. LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
  168. StkId o;
  169. lua_Number n;
  170. LUA_ENTRY;
  171. o = luaA_indexAcceptable(L, index);
  172. n = (o == NULL || tonumber(o)) ? 0 : nvalue(o);
  173. LUA_EXIT;
  174. return n;
  175. }
  176. LUA_API const char *lua_tostring (lua_State *L, int index) {
  177. StkId o;
  178. const char *s;
  179. LUA_ENTRY;
  180. o = luaA_indexAcceptable(L, index);
  181. s = (o == NULL || tostring(L, o)) ? NULL : svalue(o);
  182. LUA_EXIT;
  183. return s;
  184. }
  185. LUA_API size_t lua_strlen (lua_State *L, int index) {
  186. StkId o;
  187. size_t l;
  188. LUA_ENTRY;
  189. o = luaA_indexAcceptable(L, index);
  190. l = (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len;
  191. LUA_EXIT;
  192. return l;
  193. }
  194. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  195. StkId o;
  196. lua_CFunction f;
  197. LUA_ENTRY;
  198. o = luaA_indexAcceptable(L, index);
  199. f = (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
  200. LUA_EXIT;
  201. return f;
  202. }
  203. LUA_API void *lua_touserdata (lua_State *L, int index) {
  204. StkId o;
  205. void *p;
  206. LUA_ENTRY;
  207. o = luaA_indexAcceptable(L, index);
  208. p = (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL :
  209. tsvalue(o)->u.d.value;
  210. LUA_EXIT;
  211. return p;
  212. }
  213. LUA_API const void *lua_topointer (lua_State *L, int index) {
  214. StkId o;
  215. const void *p;
  216. LUA_ENTRY;
  217. o = luaA_indexAcceptable(L, index);
  218. if (o == NULL) p = NULL;
  219. else {
  220. switch (ttype(o)) {
  221. case LUA_TTABLE:
  222. p = hvalue(o);
  223. break;
  224. case LUA_TFUNCTION:
  225. p = clvalue(o);
  226. break;
  227. default:
  228. p = NULL;
  229. break;
  230. }
  231. }
  232. LUA_EXIT;
  233. return p;
  234. }
  235. /*
  236. ** push functions (C -> stack)
  237. */
  238. LUA_API void lua_pushnil (lua_State *L) {
  239. LUA_ENTRY;
  240. setnilvalue(L->top);
  241. api_incr_top(L);
  242. LUA_EXIT;
  243. }
  244. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  245. LUA_ENTRY;
  246. setnvalue(L->top, n);
  247. api_incr_top(L);
  248. LUA_EXIT;
  249. }
  250. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  251. LUA_ENTRY;
  252. setsvalue(L->top, luaS_newlstr(L, s, len));
  253. api_incr_top(L);
  254. LUA_EXIT;
  255. }
  256. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  257. if (s == NULL)
  258. lua_pushnil(L);
  259. else
  260. lua_pushlstring(L, s, strlen(s));
  261. }
  262. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  263. LUA_ENTRY;
  264. luaV_Cclosure(L, fn, n);
  265. LUA_EXIT;
  266. }
  267. LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
  268. LUA_ENTRY;
  269. /* ORDER LUA_T */
  270. if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(G(L), tag)))
  271. luaO_verror(L, "invalid tag for a userdata (%d)", tag);
  272. setuvalue(L->top, luaS_createudata(L, u, tag));
  273. api_incr_top(L);
  274. LUA_EXIT;
  275. }
  276. /*
  277. ** get functions (Lua -> stack)
  278. */
  279. LUA_API void lua_getglobal (lua_State *L, const char *name) {
  280. StkId top;
  281. LUA_ENTRY;
  282. top = L->top;
  283. setobj(top, luaV_getglobal(L, luaS_new(L, name)));
  284. L->top = top;
  285. api_incr_top(L);
  286. LUA_EXIT;
  287. }
  288. LUA_API void lua_gettable (lua_State *L, int index) {
  289. StkId t, top;
  290. LUA_ENTRY;
  291. t = Index(L, index);
  292. top = L->top;
  293. setobj(top-1, luaV_gettable(L, t));
  294. L->top = top; /* tag method may change top */
  295. LUA_EXIT;
  296. }
  297. LUA_API void lua_rawget (lua_State *L, int index) {
  298. StkId t;
  299. LUA_ENTRY;
  300. t = Index(L, index);
  301. lua_assert(ttype(t) == LUA_TTABLE);
  302. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  303. LUA_EXIT;
  304. }
  305. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  306. StkId o;
  307. LUA_ENTRY;
  308. o = Index(L, index);
  309. lua_assert(ttype(o) == LUA_TTABLE);
  310. setobj(L->top, luaH_getnum(hvalue(o), n));
  311. api_incr_top(L);
  312. LUA_EXIT;
  313. }
  314. LUA_API void lua_getglobals (lua_State *L) {
  315. LUA_ENTRY;
  316. sethvalue(L->top, L->gt);
  317. api_incr_top(L);
  318. LUA_EXIT;
  319. }
  320. LUA_API int lua_getref (lua_State *L, int ref) {
  321. int status = 1;
  322. LUA_ENTRY;
  323. if (ref == LUA_REFNIL) {
  324. setnilvalue(L->top);
  325. api_incr_top(L);
  326. }
  327. else if (0 <= ref && ref < G(L)->nref &&
  328. (G(L)->refArray[ref].st == LOCK || G(L)->refArray[ref].st == HOLD)) {
  329. setobj(L->top, &G(L)->refArray[ref].o);
  330. api_incr_top(L);
  331. }
  332. else
  333. status = 0;
  334. LUA_EXIT;
  335. return status;
  336. }
  337. LUA_API void lua_newtable (lua_State *L) {
  338. LUA_ENTRY;
  339. sethvalue(L->top, luaH_new(L, 0));
  340. api_incr_top(L);
  341. LUA_EXIT;
  342. }
  343. /*
  344. ** set functions (stack -> Lua)
  345. */
  346. LUA_API void lua_setglobal (lua_State *L, const char *name) {
  347. StkId top;
  348. LUA_ENTRY;
  349. top = L->top;
  350. luaV_setglobal(L, luaS_new(L, name));
  351. L->top = top-1; /* remove element from the top */
  352. LUA_EXIT;
  353. }
  354. LUA_API void lua_settable (lua_State *L, int index) {
  355. StkId t, top;
  356. LUA_ENTRY;
  357. t = Index(L, index);
  358. top = L->top;
  359. luaV_settable(L, t, top-2);
  360. L->top = top-2; /* pop index and value */
  361. LUA_EXIT;
  362. }
  363. LUA_API void lua_rawset (lua_State *L, int index) {
  364. StkId t;
  365. LUA_ENTRY;
  366. t = Index(L, index);
  367. lua_assert(ttype(t) == LUA_TTABLE);
  368. setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
  369. L->top -= 2;
  370. LUA_EXIT;
  371. }
  372. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  373. StkId o;
  374. LUA_ENTRY;
  375. o = Index(L, index);
  376. lua_assert(ttype(o) == LUA_TTABLE);
  377. setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
  378. L->top--;
  379. LUA_EXIT;
  380. }
  381. LUA_API void lua_setglobals (lua_State *L) {
  382. StkId newtable;
  383. LUA_ENTRY;
  384. newtable = --L->top;
  385. lua_assert(ttype(newtable) == LUA_TTABLE);
  386. L->gt = hvalue(newtable);
  387. LUA_EXIT;
  388. }
  389. LUA_API int lua_ref (lua_State *L, int lock) {
  390. int ref;
  391. LUA_ENTRY;
  392. if (ttype(L->top-1) == LUA_TNIL)
  393. ref = LUA_REFNIL;
  394. else {
  395. if (G(L)->refFree != NONEXT) { /* is there a free place? */
  396. ref = G(L)->refFree;
  397. G(L)->refFree = G(L)->refArray[ref].st;
  398. }
  399. else { /* no more free places */
  400. luaM_growvector(L, G(L)->refArray, G(L)->nref, G(L)->sizeref, struct Ref,
  401. MAX_INT, "reference table overflow");
  402. ref = G(L)->nref++;
  403. }
  404. setobj(&G(L)->refArray[ref].o, L->top-1);
  405. G(L)->refArray[ref].st = lock ? LOCK : HOLD;
  406. }
  407. L->top--;
  408. LUA_EXIT;
  409. return ref;
  410. }
  411. /*
  412. ** "do" functions (run Lua code)
  413. ** (most of them are in ldo.c)
  414. */
  415. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  416. LUA_ENTRY;
  417. luaD_call(L, L->top-(nargs+1), nresults);
  418. LUA_EXIT;
  419. }
  420. /*
  421. ** Garbage-collection functions
  422. */
  423. /* GC values are expressed in Kbytes: #bytes/2^10 */
  424. #define GCscale(x) ((int)((x)>>10))
  425. #define GCunscale(x) ((mem_int)(x)<<10)
  426. LUA_API int lua_getgcthreshold (lua_State *L) {
  427. int threshold;
  428. LUA_ENTRY;
  429. threshold = GCscale(G(L)->GCthreshold);
  430. LUA_EXIT;
  431. return threshold;
  432. }
  433. LUA_API int lua_getgccount (lua_State *L) {
  434. int count;
  435. LUA_ENTRY;
  436. count = GCscale(G(L)->nblocks);
  437. LUA_EXIT;
  438. return count;
  439. }
  440. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  441. LUA_ENTRY;
  442. if (newthreshold > GCscale(ULONG_MAX))
  443. G(L)->GCthreshold = ULONG_MAX;
  444. else
  445. G(L)->GCthreshold = GCunscale(newthreshold);
  446. luaC_checkGC(L);
  447. LUA_EXIT;
  448. }
  449. /*
  450. ** miscellaneous functions
  451. */
  452. LUA_API int lua_newtype (lua_State *L, const char *name, int basictype) {
  453. int tag;
  454. LUA_ENTRY;
  455. if (basictype != LUA_TNONE &&
  456. basictype != LUA_TTABLE &&
  457. basictype != LUA_TUSERDATA)
  458. luaO_verror(L, "invalid basic type (%d) for new type", basictype);
  459. tag = luaT_newtag(L, name, basictype);
  460. if (tag == LUA_TNONE)
  461. luaO_verror(L, "type name '%.30s' already exists", name);
  462. LUA_EXIT;
  463. return tag;
  464. }
  465. LUA_API int lua_type2tag (lua_State *L, const char *name) {
  466. int tag;
  467. const TObject *v;
  468. LUA_ENTRY;
  469. v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
  470. if (ttype(v) == LUA_TNIL)
  471. tag = LUA_TNONE;
  472. else {
  473. lua_assert(ttype(v) == LUA_TNUMBER);
  474. tag = nvalue(v);
  475. }
  476. LUA_EXIT;
  477. return tag;
  478. }
  479. LUA_API void lua_settag (lua_State *L, int tag) {
  480. int basictype;
  481. LUA_ENTRY;
  482. if (tag < 0 || tag >= G(L)->ntag)
  483. luaO_verror(L, "%d is not a valid tag", tag);
  484. basictype = G(L)->TMtable[tag].basictype;
  485. if (basictype != LUA_TNONE && basictype != ttype(L->top-1))
  486. luaO_verror(L, "tag %d can only be used for type '%.20s'", tag,
  487. basictypename(G(L), basictype));
  488. switch (ttype(L->top-1)) {
  489. case LUA_TTABLE:
  490. hvalue(L->top-1)->htag = tag;
  491. break;
  492. case LUA_TUSERDATA:
  493. tsvalue(L->top-1)->u.d.tag = tag;
  494. break;
  495. default:
  496. luaO_verror(L, "cannot change the tag of a %.20s",
  497. luaT_typename(G(L), L->top-1));
  498. }
  499. LUA_EXIT;
  500. }
  501. LUA_API void lua_error (lua_State *L, const char *s) {
  502. LUA_ENTRY;
  503. luaD_error(L, s);
  504. LUA_EXIT;
  505. }
  506. LUA_API void lua_unref (lua_State *L, int ref) {
  507. LUA_ENTRY;
  508. if (ref >= 0) {
  509. lua_assert(ref < G(L)->nref && G(L)->refArray[ref].st < 0);
  510. G(L)->refArray[ref].st = G(L)->refFree;
  511. G(L)->refFree = ref;
  512. }
  513. LUA_EXIT;
  514. }
  515. LUA_API int lua_next (lua_State *L, int index) {
  516. StkId t;
  517. Node *n;
  518. int more;
  519. LUA_ENTRY;
  520. t = luaA_index(L, index);
  521. lua_assert(ttype(t) == LUA_TTABLE);
  522. n = luaH_next(L, hvalue(t), luaA_index(L, -1));
  523. if (n) {
  524. setobj(L->top-1, key(n));
  525. setobj(L->top, val(n));
  526. api_incr_top(L);
  527. more = 1;
  528. }
  529. else { /* no more elements */
  530. L->top -= 1; /* remove key */
  531. more = 0;
  532. }
  533. LUA_EXIT;
  534. return more;
  535. }
  536. LUA_API int lua_getn (lua_State *L, int index) {
  537. Hash *h;
  538. const TObject *value;
  539. int n;
  540. LUA_ENTRY;
  541. h = hvalue(luaA_index(L, index));
  542. value = luaH_getstr(h, luaS_newliteral(L, "n")); /* = h.n */
  543. if (ttype(value) == LUA_TNUMBER)
  544. n = (int)nvalue(value);
  545. else {
  546. lua_Number max = 0;
  547. int i = h->size;
  548. Node *nd = h->node;
  549. while (i--) {
  550. if (ttype(key(nd)) == LUA_TNUMBER &&
  551. ttype(val(nd)) != LUA_TNIL &&
  552. nvalue(key(nd)) > max)
  553. max = nvalue(key(nd));
  554. nd++;
  555. }
  556. n = (int)max;
  557. }
  558. LUA_EXIT;
  559. return n;
  560. }
  561. LUA_API void lua_concat (lua_State *L, int n) {
  562. StkId top;
  563. LUA_ENTRY;
  564. top = L->top;
  565. luaV_strconc(L, n, top);
  566. L->top = top-(n-1);
  567. luaC_checkGC(L);
  568. LUA_EXIT;
  569. }
  570. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  571. TString *ts;
  572. void *p;
  573. LUA_ENTRY;
  574. ts = luaS_newudata(L, size, NULL);
  575. setuvalue(L->top, ts);
  576. api_incr_top(L);
  577. p = ts->u.d.value;
  578. LUA_EXIT;
  579. return p;
  580. }