lapi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. ** $Id: lapi.c,v 1.143 2001/06/06 18:00:19 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. api_check(L, index <= L->stack_last - L->ci->base);
  74. luaD_adjusttop(L, L->ci->base+index);
  75. }
  76. else {
  77. api_check(L, -(index+1) <= (L->top - L->ci->base));
  78. L->top += index+1; /* `subtract' index (index is negative) */
  79. }
  80. lua_unlock(L);
  81. }
  82. LUA_API void lua_remove (lua_State *L, int index) {
  83. StkId p;
  84. lua_lock(L);
  85. p = luaA_index(L, index);
  86. while (++p < L->top) setobj(p-1, p);
  87. L->top--;
  88. lua_unlock(L);
  89. }
  90. LUA_API void lua_insert (lua_State *L, int index) {
  91. StkId p;
  92. StkId q;
  93. lua_lock(L);
  94. p = luaA_index(L, index);
  95. for (q = L->top; q>p; q--) setobj(q, q-1);
  96. setobj(p, L->top);
  97. lua_unlock(L);
  98. }
  99. LUA_API void lua_pushvalue (lua_State *L, int index) {
  100. lua_lock(L);
  101. setobj(L->top, luaA_index(L, index));
  102. api_incr_top(L);
  103. lua_unlock(L);
  104. }
  105. /*
  106. ** access functions (stack -> C)
  107. */
  108. LUA_API int lua_type (lua_State *L, int index) {
  109. StkId o;
  110. int i;
  111. lua_lock(L);
  112. o = luaA_indexAcceptable(L, index);
  113. i = (o == NULL) ? LUA_TNONE : ttype(o);
  114. lua_unlock(L);
  115. return i;
  116. }
  117. LUA_API const l_char *lua_tag2name (lua_State *L, int tag) {
  118. const l_char *s;
  119. lua_lock(L);
  120. s = (tag == LUA_TNONE) ? l_s("no value") : basictypename(G(L), tag);
  121. lua_unlock(L);
  122. return s;
  123. }
  124. LUA_API const l_char *lua_xtypename (lua_State *L, int index) {
  125. StkId o;
  126. const l_char *type;
  127. lua_lock(L);
  128. o = luaA_indexAcceptable(L, index);
  129. type = (o == NULL) ? l_s("no value") : luaT_typename(G(L), o);
  130. lua_unlock(L);
  131. return type;
  132. }
  133. LUA_API int lua_iscfunction (lua_State *L, int index) {
  134. StkId o;
  135. int i;
  136. lua_lock(L);
  137. o = luaA_indexAcceptable(L, index);
  138. i = (o == NULL) ? 0 : iscfunction(o);
  139. lua_unlock(L);
  140. return i;
  141. }
  142. LUA_API int lua_isnumber (lua_State *L, int index) {
  143. TObject *o;
  144. int i;
  145. TObject n;
  146. lua_lock(L);
  147. o = luaA_indexAcceptable(L, index);
  148. i = (o != NULL && (ttype(o) == LUA_TNUMBER || luaV_tonumber(o, &n)));
  149. lua_unlock(L);
  150. return i;
  151. }
  152. LUA_API int lua_isstring (lua_State *L, int index) {
  153. int t = lua_type(L, index);
  154. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  155. }
  156. LUA_API int lua_tag (lua_State *L, int index) {
  157. StkId o;
  158. int i;
  159. lua_lock(L);
  160. o = luaA_indexAcceptable(L, index);
  161. i = (o == NULL) ? LUA_NOTAG : luaT_tag(o);
  162. lua_unlock(L);
  163. return i;
  164. }
  165. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  166. StkId o1, o2;
  167. int i;
  168. lua_lock(L);
  169. o1 = luaA_indexAcceptable(L, index1);
  170. o2 = luaA_indexAcceptable(L, index2);
  171. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  172. : luaO_equalObj(o1, o2);
  173. lua_unlock(L);
  174. return i;
  175. }
  176. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  177. StkId o1, o2;
  178. int i;
  179. lua_lock(L);
  180. o1 = luaA_indexAcceptable(L, index1);
  181. o2 = luaA_indexAcceptable(L, index2);
  182. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  183. : luaV_lessthan(L, o1, o2);
  184. lua_unlock(L);
  185. return i;
  186. }
  187. LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
  188. const TObject *o;
  189. TObject n;
  190. lua_Number res;
  191. lua_lock(L);
  192. o = luaA_indexAcceptable(L, index);
  193. if (o != NULL &&
  194. (ttype(o) == LUA_TNUMBER || (o = luaV_tonumber(o, &n)) != NULL))
  195. res = nvalue(o);
  196. else
  197. res = 0;
  198. lua_unlock(L);
  199. return res;
  200. }
  201. LUA_API const l_char *lua_tostring (lua_State *L, int index) {
  202. StkId o;
  203. const l_char *s;
  204. lua_lock(L);
  205. o = luaA_indexAcceptable(L, index);
  206. s = (o == NULL || tostring(L, o)) ? NULL : svalue(o);
  207. lua_unlock(L);
  208. return s;
  209. }
  210. LUA_API size_t lua_strlen (lua_State *L, int index) {
  211. StkId o;
  212. size_t l;
  213. lua_lock(L);
  214. o = luaA_indexAcceptable(L, index);
  215. l = (o == NULL || tostring(L, o)) ? 0 : tsvalue(o)->len;
  216. lua_unlock(L);
  217. return l;
  218. }
  219. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  220. StkId o;
  221. lua_CFunction f;
  222. lua_lock(L);
  223. o = luaA_indexAcceptable(L, index);
  224. f = (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
  225. lua_unlock(L);
  226. return f;
  227. }
  228. LUA_API void *lua_touserdata (lua_State *L, int index) {
  229. StkId o;
  230. void *p;
  231. lua_lock(L);
  232. o = luaA_indexAcceptable(L, index);
  233. p = (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL : uvalue(o)->value;
  234. lua_unlock(L);
  235. return p;
  236. }
  237. LUA_API const void *lua_topointer (lua_State *L, int index) {
  238. StkId o;
  239. const void *p;
  240. lua_lock(L);
  241. o = luaA_indexAcceptable(L, index);
  242. if (o == NULL) p = NULL;
  243. else {
  244. switch (ttype(o)) {
  245. case LUA_TTABLE:
  246. p = hvalue(o);
  247. break;
  248. case LUA_TFUNCTION:
  249. p = clvalue(o);
  250. break;
  251. default:
  252. p = NULL;
  253. break;
  254. }
  255. }
  256. lua_unlock(L);
  257. return p;
  258. }
  259. /*
  260. ** push functions (C -> stack)
  261. */
  262. LUA_API void lua_pushnil (lua_State *L) {
  263. lua_lock(L);
  264. setnilvalue(L->top);
  265. api_incr_top(L);
  266. lua_unlock(L);
  267. }
  268. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  269. lua_lock(L);
  270. setnvalue(L->top, n);
  271. api_incr_top(L);
  272. lua_unlock(L);
  273. }
  274. LUA_API void lua_pushlstring (lua_State *L, const l_char *s, size_t len) {
  275. lua_lock(L);
  276. setsvalue(L->top, luaS_newlstr(L, s, len));
  277. api_incr_top(L);
  278. lua_unlock(L);
  279. }
  280. LUA_API void lua_pushstring (lua_State *L, const l_char *s) {
  281. if (s == NULL)
  282. lua_pushnil(L);
  283. else
  284. lua_pushlstring(L, s, strlen(s));
  285. }
  286. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  287. lua_lock(L);
  288. api_checknelems(L, n);
  289. luaV_Cclosure(L, fn, n);
  290. lua_unlock(L);
  291. }
  292. /*
  293. ** get functions (Lua -> stack)
  294. */
  295. LUA_API void lua_getglobal (lua_State *L, const l_char *name) {
  296. lua_lock(L);
  297. luaV_getglobal(L, luaS_new(L, name), L->top);
  298. api_incr_top(L);
  299. lua_unlock(L);
  300. }
  301. LUA_API void lua_gettable (lua_State *L, int index) {
  302. StkId t;
  303. lua_lock(L);
  304. t = luaA_index(L, index);
  305. luaV_gettable(L, t, L->top-1, L->top-1);
  306. lua_unlock(L);
  307. }
  308. LUA_API void lua_rawget (lua_State *L, int index) {
  309. StkId t;
  310. lua_lock(L);
  311. t = luaA_index(L, index);
  312. api_check(L, ttype(t) == LUA_TTABLE);
  313. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  314. lua_unlock(L);
  315. }
  316. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  317. StkId o;
  318. lua_lock(L);
  319. o = luaA_index(L, index);
  320. api_check(L, ttype(o) == LUA_TTABLE);
  321. setobj(L->top, luaH_getnum(hvalue(o), n));
  322. api_incr_top(L);
  323. lua_unlock(L);
  324. }
  325. LUA_API void lua_getglobals (lua_State *L) {
  326. lua_lock(L);
  327. sethvalue(L->top, L->gt);
  328. api_incr_top(L);
  329. lua_unlock(L);
  330. }
  331. LUA_API int lua_getref (lua_State *L, int ref) {
  332. int status;
  333. lua_lock(L);
  334. if (ref == LUA_REFNIL) {
  335. setnilvalue(L->top);
  336. status = 1;
  337. }
  338. else {
  339. setobj(L->top, luaH_getnum(G(L)->weakregistry, ref));
  340. status = (ttype(L->top) != LUA_TNIL);
  341. }
  342. if (status)
  343. api_incr_top(L);
  344. lua_unlock(L);
  345. return status;
  346. }
  347. LUA_API void lua_newtable (lua_State *L) {
  348. lua_lock(L);
  349. sethvalue(L->top, luaH_new(L, 0));
  350. api_incr_top(L);
  351. lua_unlock(L);
  352. }
  353. LUA_API void lua_getregistry (lua_State *L) {
  354. lua_lock(L);
  355. sethvalue(L->top, G(L)->registry);
  356. api_incr_top(L);
  357. lua_unlock(L);
  358. }
  359. LUA_API void lua_getweakregistry (lua_State *L) {
  360. lua_lock(L);
  361. sethvalue(L->top, G(L)->weakregistry);
  362. api_incr_top(L);
  363. lua_unlock(L);
  364. }
  365. /*
  366. ** set functions (stack -> Lua)
  367. */
  368. LUA_API void lua_setglobal (lua_State *L, const l_char *name) {
  369. lua_lock(L);
  370. api_checknelems(L, 1);
  371. luaV_setglobal(L, luaS_new(L, name), L->top - 1);
  372. L->top--; /* remove element from the top */
  373. lua_unlock(L);
  374. }
  375. LUA_API void lua_settable (lua_State *L, int index) {
  376. StkId t;
  377. lua_lock(L);
  378. api_checknelems(L, 2);
  379. t = luaA_index(L, index);
  380. luaV_settable(L, t, L->top - 2, L->top - 1);
  381. L->top -= 2; /* pop index and value */
  382. lua_unlock(L);
  383. }
  384. LUA_API void lua_rawset (lua_State *L, int index) {
  385. StkId t;
  386. lua_lock(L);
  387. api_checknelems(L, 2);
  388. t = luaA_index(L, index);
  389. api_check(L, ttype(t) == LUA_TTABLE);
  390. setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
  391. L->top -= 2;
  392. lua_unlock(L);
  393. }
  394. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  395. StkId o;
  396. lua_lock(L);
  397. api_checknelems(L, 1);
  398. o = luaA_index(L, index);
  399. api_check(L, ttype(o) == LUA_TTABLE);
  400. setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
  401. L->top--;
  402. lua_unlock(L);
  403. }
  404. LUA_API void lua_setglobals (lua_State *L) {
  405. StkId newtable;
  406. lua_lock(L);
  407. api_checknelems(L, 1);
  408. newtable = --L->top;
  409. api_check(L, ttype(newtable) == LUA_TTABLE);
  410. L->gt = hvalue(newtable);
  411. lua_unlock(L);
  412. }
  413. LUA_API int lua_ref (lua_State *L, int lock) {
  414. int ref;
  415. if (lua_isnil(L, -1)) {
  416. lua_pop(L, 1);
  417. ref = LUA_REFNIL;
  418. }
  419. else {
  420. lua_getweakregistry(L);
  421. ref = lua_getn(L, -1) + 1;
  422. lua_pushvalue(L, -2);
  423. lua_rawseti(L, -2, ref);
  424. if (lock) {
  425. lua_getregistry(L);
  426. lua_pushvalue(L, -3);
  427. lua_rawseti(L, -2, ref);
  428. lua_pop(L, 1); /* remove registry */
  429. }
  430. lua_pushliteral(L, "n");
  431. lua_pushnumber(L, ref);
  432. lua_settable(L, -3);
  433. lua_pop(L, 2);
  434. }
  435. return ref;
  436. }
  437. /*
  438. ** `do' functions (run Lua code)
  439. */
  440. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  441. StkId func;
  442. lua_lock(L);
  443. api_checknelems(L, nargs+1);
  444. func = L->top - (nargs+1);
  445. luaD_call(L, func);
  446. if (nresults != LUA_MULTRET)
  447. luaD_adjusttop(L, func + nresults);
  448. lua_unlock(L);
  449. }
  450. LUA_API int lua_dofile (lua_State *L, const l_char *filename) {
  451. int status;
  452. status = lua_loadfile(L, filename);
  453. if (status == 0) /* parse OK? */
  454. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  455. return status;
  456. }
  457. LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size,
  458. const l_char *name) {
  459. int status;
  460. status = lua_loadbuffer(L, buff, size, name);
  461. if (status == 0) /* parse OK? */
  462. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  463. return status;
  464. }
  465. LUA_API int lua_dostring (lua_State *L, const l_char *str) {
  466. return lua_dobuffer(L, str, strlen(str), str);
  467. }
  468. /*
  469. ** Garbage-collection functions
  470. */
  471. /* GC values are expressed in Kbytes: #bytes/2^10 */
  472. #define GCscale(x) ((int)((x)>>10))
  473. #define GCunscale(x) ((lu_mem)(x)<<10)
  474. LUA_API int lua_getgcthreshold (lua_State *L) {
  475. int threshold;
  476. lua_lock(L);
  477. threshold = GCscale(G(L)->GCthreshold);
  478. lua_unlock(L);
  479. return threshold;
  480. }
  481. LUA_API int lua_getgccount (lua_State *L) {
  482. int count;
  483. lua_lock(L);
  484. count = GCscale(G(L)->nblocks);
  485. lua_unlock(L);
  486. return count;
  487. }
  488. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  489. lua_lock(L);
  490. if (newthreshold > GCscale(ULONG_MAX))
  491. G(L)->GCthreshold = ULONG_MAX;
  492. else
  493. G(L)->GCthreshold = GCunscale(newthreshold);
  494. luaC_checkGC(L);
  495. lua_unlock(L);
  496. }
  497. /*
  498. ** miscellaneous functions
  499. */
  500. LUA_API int lua_newxtype (lua_State *L, const l_char *name, int basictype) {
  501. int tag;
  502. lua_lock(L);
  503. if (basictype != LUA_TNONE &&
  504. basictype != LUA_TTABLE &&
  505. basictype != LUA_TUSERDATA)
  506. luaO_verror(L, l_s("invalid basic type (%d) for new type"), basictype);
  507. tag = luaT_newtag(L, name, basictype);
  508. if (tag == LUA_TNONE)
  509. luaO_verror(L, l_s("type name '%.30s' already exists"), name);
  510. lua_unlock(L);
  511. return tag;
  512. }
  513. LUA_API int lua_name2tag (lua_State *L, const l_char *name) {
  514. int tag;
  515. const TObject *v;
  516. lua_lock(L);
  517. v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
  518. if (ttype(v) == LUA_TNIL)
  519. tag = LUA_TNONE;
  520. else {
  521. lua_assert(ttype(v) == LUA_TNUMBER);
  522. tag = (int)nvalue(v);
  523. }
  524. lua_unlock(L);
  525. return tag;
  526. }
  527. LUA_API void lua_settag (lua_State *L, int tag) {
  528. int basictype;
  529. lua_lock(L);
  530. api_checknelems(L, 1);
  531. if (tag < 0 || tag >= G(L)->ntag)
  532. luaO_verror(L, l_s("%d is not a valid tag"), tag);
  533. basictype = G(L)->TMtable[tag].basictype;
  534. if (basictype != LUA_TNONE && basictype != ttype(L->top-1))
  535. luaO_verror(L, l_s("tag %d can only be used for type '%.20s'"), tag,
  536. basictypename(G(L), basictype));
  537. switch (ttype(L->top-1)) {
  538. case LUA_TTABLE:
  539. hvalue(L->top-1)->htag = tag;
  540. break;
  541. case LUA_TUSERDATA:
  542. uvalue(L->top-1)->tag = tag;
  543. break;
  544. default:
  545. luaO_verror(L, l_s("cannot change the tag of a %.20s"),
  546. luaT_typename(G(L), L->top-1));
  547. }
  548. lua_unlock(L);
  549. }
  550. LUA_API void lua_error (lua_State *L, const l_char *s) {
  551. lua_lock(L);
  552. luaD_error(L, s);
  553. lua_unlock(L);
  554. }
  555. LUA_API void lua_unref (lua_State *L, int ref) {
  556. if (ref >= 0) {
  557. lua_getregistry(L);
  558. lua_pushnil(L);
  559. lua_rawseti(L, -2, ref);
  560. lua_getweakregistry(L);
  561. lua_pushnil(L);
  562. lua_rawseti(L, -2, ref);
  563. lua_pop(L, 2); /* remove both registries */
  564. }
  565. }
  566. LUA_API int lua_next (lua_State *L, int index) {
  567. StkId t;
  568. Node *n;
  569. int more;
  570. lua_lock(L);
  571. t = luaA_index(L, index);
  572. api_check(L, ttype(t) == LUA_TTABLE);
  573. n = luaH_next(L, hvalue(t), luaA_index(L, -1));
  574. if (n) {
  575. setkey2obj(L->top-1, n);
  576. setobj(L->top, val(n));
  577. api_incr_top(L);
  578. more = 1;
  579. }
  580. else { /* no more elements */
  581. L->top -= 1; /* remove key */
  582. more = 0;
  583. }
  584. lua_unlock(L);
  585. return more;
  586. }
  587. LUA_API int lua_getn (lua_State *L, int index) {
  588. StkId t;
  589. const TObject *value;
  590. int n;
  591. lua_lock(L);
  592. t = luaA_index(L, index);
  593. api_check(L, ttype(t) == LUA_TTABLE);
  594. value = luaH_getstr(hvalue(t), luaS_newliteral(L, l_s("n"))); /* = t.n */
  595. if (ttype(value) == LUA_TNUMBER)
  596. n = (int)nvalue(value);
  597. else {
  598. lua_Number max = 0;
  599. int i = hvalue(t)->size;
  600. Node *nd = hvalue(t)->node;
  601. while (i--) {
  602. if (ttype_key(nd) == LUA_TNUMBER &&
  603. ttype(val(nd)) != LUA_TNIL &&
  604. nvalue_key(nd) > max)
  605. max = nvalue_key(nd);
  606. nd++;
  607. }
  608. n = (int)max;
  609. }
  610. lua_unlock(L);
  611. return n;
  612. }
  613. LUA_API void lua_concat (lua_State *L, int n) {
  614. lua_lock(L);
  615. api_checknelems(L, n);
  616. if (n >= 2) {
  617. luaV_strconc(L, n, L->top);
  618. L->top -= (n-1);
  619. luaC_checkGC(L);
  620. }
  621. else if (n == 0) { /* push null string */
  622. setsvalue(L->top, luaS_newlstr(L, NULL, 0));
  623. api_incr_top(L);
  624. }
  625. /* else n == 1; nothing to do */
  626. lua_unlock(L);
  627. }
  628. static Udata *pushnewudata (lua_State *L, size_t size) {
  629. Udata *u = luaS_newudata(L, size);
  630. setuvalue(L->top, u);
  631. api_incr_top(L);
  632. return uvalue(L->top-1);
  633. }
  634. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  635. Udata *u;
  636. void *p;
  637. lua_lock(L);
  638. u = pushnewudata(L, size);
  639. p = u->value;
  640. lua_unlock(L);
  641. return p;
  642. }
  643. LUA_API void lua_newuserdatabox (lua_State *L, void *p) {
  644. Udata *u;
  645. lua_lock(L);
  646. u = pushnewudata(L, 0);
  647. u->value = p;
  648. lua_unlock(L);
  649. }
  650. LUA_API int lua_getweakmode (lua_State *L, int index) {
  651. StkId t;
  652. int mode;
  653. lua_lock(L);
  654. t = luaA_index(L, index);
  655. api_check(L, ttype(t) == LUA_TTABLE);
  656. mode = hvalue(t)->weakmode;
  657. lua_unlock(L);
  658. return mode;
  659. }
  660. LUA_API void lua_setweakmode (lua_State *L, int mode) {
  661. lua_lock(L);
  662. api_check(L, ttype(L->top-1) == LUA_TTABLE);
  663. hvalue(L->top-1)->weakmode = mode;
  664. lua_unlock(L);
  665. }
  666. #if 0
  667. /*
  668. ** deprecated function
  669. */
  670. LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
  671. /* ???????? */
  672. if (lua_pushuserdata(L, u) || 1) /* new udata? */
  673. lua_settag(L, tag); /* OK, no conflit */
  674. else {
  675. if (lua_tag(L, -1) != tag) {
  676. lua_error(L, "conflicting tags for the same userdata");
  677. }
  678. }
  679. }
  680. #endif