2
0

lapi.c 15 KB

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