2
0

lapi.c 16 KB

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