lapi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. ** $Id: lapi.c,v 1.139 2001/04/11 18:39:37 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;
  334. lua_lock(L);
  335. if (ref == LUA_REFNIL) {
  336. setnilvalue(L->top);
  337. status = 1;
  338. }
  339. else {
  340. setobj(L->top, luaH_getnum(G(L)->weakregistry, ref));
  341. status = (ttype(L->top) != LUA_TNIL);
  342. }
  343. if (status)
  344. api_incr_top(L);
  345. lua_unlock(L);
  346. return status;
  347. }
  348. LUA_API void lua_newtable (lua_State *L) {
  349. lua_lock(L);
  350. sethvalue(L->top, luaH_new(L, 0));
  351. api_incr_top(L);
  352. lua_unlock(L);
  353. }
  354. LUA_API void lua_getregistry (lua_State *L) {
  355. lua_lock(L);
  356. sethvalue(L->top, G(L)->registry);
  357. api_incr_top(L);
  358. lua_unlock(L);
  359. }
  360. LUA_API void lua_getweakregistry (lua_State *L) {
  361. lua_lock(L);
  362. sethvalue(L->top, G(L)->weakregistry);
  363. api_incr_top(L);
  364. lua_unlock(L);
  365. }
  366. /*
  367. ** set functions (stack -> Lua)
  368. */
  369. LUA_API void lua_setglobal (lua_State *L, const l_char *name) {
  370. lua_lock(L);
  371. api_checknelems(L, 1);
  372. luaV_setglobal(L, luaS_new(L, name), L->top - 1);
  373. L->top--; /* remove element from the top */
  374. lua_unlock(L);
  375. }
  376. LUA_API void lua_settable (lua_State *L, int index) {
  377. StkId t;
  378. lua_lock(L);
  379. api_checknelems(L, 2);
  380. t = luaA_index(L, index);
  381. luaV_settable(L, t, L->top - 2, L->top - 1);
  382. L->top -= 2; /* pop index and value */
  383. lua_unlock(L);
  384. }
  385. LUA_API void lua_rawset (lua_State *L, int index) {
  386. StkId t;
  387. lua_lock(L);
  388. api_checknelems(L, 2);
  389. t = luaA_index(L, index);
  390. api_check(L, ttype(t) == LUA_TTABLE);
  391. setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
  392. L->top -= 2;
  393. lua_unlock(L);
  394. }
  395. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  396. StkId o;
  397. lua_lock(L);
  398. api_checknelems(L, 1);
  399. o = luaA_index(L, index);
  400. api_check(L, ttype(o) == LUA_TTABLE);
  401. setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
  402. L->top--;
  403. lua_unlock(L);
  404. }
  405. LUA_API void lua_setglobals (lua_State *L) {
  406. StkId newtable;
  407. lua_lock(L);
  408. api_checknelems(L, 1);
  409. newtable = --L->top;
  410. api_check(L, ttype(newtable) == LUA_TTABLE);
  411. L->gt = hvalue(newtable);
  412. lua_unlock(L);
  413. }
  414. LUA_API int lua_ref (lua_State *L, int lock) {
  415. int ref;
  416. if (lua_isnil(L, -1)) {
  417. lua_pop(L, 1);
  418. ref = LUA_REFNIL;
  419. }
  420. else {
  421. lua_getweakregistry(L);
  422. ref = lua_getn(L, -1) + 1;
  423. lua_pushvalue(L, -2);
  424. lua_rawseti(L, -2, ref);
  425. if (lock) {
  426. lua_getregistry(L);
  427. lua_pushvalue(L, -3);
  428. lua_rawseti(L, -2, ref);
  429. lua_pop(L, 1); /* remove registry */
  430. }
  431. lua_pushliteral(L, "n");
  432. lua_pushnumber(L, ref);
  433. lua_settable(L, -3);
  434. lua_pop(L, 2);
  435. }
  436. return ref;
  437. }
  438. /*
  439. ** `do' functions (run Lua code)
  440. */
  441. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  442. lua_lock(L);
  443. api_checknelems(L, nargs+1);
  444. luaD_call(L, L->top-(nargs+1), nresults);
  445. lua_unlock(L);
  446. }
  447. LUA_API int lua_dofile (lua_State *L, const l_char *filename) {
  448. int status;
  449. status = lua_loadfile(L, filename);
  450. if (status == 0) /* parse OK? */
  451. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  452. return status;
  453. }
  454. LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size,
  455. const l_char *name) {
  456. int status;
  457. status = lua_loadbuffer(L, buff, size, name);
  458. if (status == 0) /* parse OK? */
  459. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  460. return status;
  461. }
  462. LUA_API int lua_dostring (lua_State *L, const l_char *str) {
  463. return lua_dobuffer(L, str, strlen(str), str);
  464. }
  465. /*
  466. ** Garbage-collection functions
  467. */
  468. /* GC values are expressed in Kbytes: #bytes/2^10 */
  469. #define GCscale(x) ((int)((x)>>10))
  470. #define GCunscale(x) ((lu_mem)(x)<<10)
  471. LUA_API int lua_getgcthreshold (lua_State *L) {
  472. int threshold;
  473. lua_lock(L);
  474. threshold = GCscale(G(L)->GCthreshold);
  475. lua_unlock(L);
  476. return threshold;
  477. }
  478. LUA_API int lua_getgccount (lua_State *L) {
  479. int count;
  480. lua_lock(L);
  481. count = GCscale(G(L)->nblocks);
  482. lua_unlock(L);
  483. return count;
  484. }
  485. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  486. lua_lock(L);
  487. if (newthreshold > GCscale(ULONG_MAX))
  488. G(L)->GCthreshold = ULONG_MAX;
  489. else
  490. G(L)->GCthreshold = GCunscale(newthreshold);
  491. luaC_checkGC(L);
  492. lua_unlock(L);
  493. }
  494. /*
  495. ** miscellaneous functions
  496. */
  497. LUA_API int lua_newtype (lua_State *L, const l_char *name, int basictype) {
  498. int tag;
  499. lua_lock(L);
  500. if (basictype != LUA_TNONE &&
  501. basictype != LUA_TTABLE &&
  502. basictype != LUA_TUSERDATA)
  503. luaO_verror(L, l_s("invalid basic type (%d) for new type"), basictype);
  504. tag = luaT_newtag(L, name, basictype);
  505. if (tag == LUA_TNONE)
  506. luaO_verror(L, l_s("type name '%.30s' already exists"), name);
  507. lua_unlock(L);
  508. return tag;
  509. }
  510. LUA_API int lua_type2tag (lua_State *L, const l_char *name) {
  511. int tag;
  512. const TObject *v;
  513. lua_lock(L);
  514. v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
  515. if (ttype(v) == LUA_TNIL)
  516. tag = LUA_TNONE;
  517. else {
  518. lua_assert(ttype(v) == LUA_TNUMBER);
  519. tag = (int)nvalue(v);
  520. }
  521. lua_unlock(L);
  522. return tag;
  523. }
  524. LUA_API void lua_settag (lua_State *L, int tag) {
  525. int basictype;
  526. lua_lock(L);
  527. api_checknelems(L, 1);
  528. if (tag < 0 || tag >= G(L)->ntag)
  529. luaO_verror(L, l_s("%d is not a valid tag"), tag);
  530. basictype = G(L)->TMtable[tag].basictype;
  531. if (basictype != LUA_TNONE && basictype != ttype(L->top-1))
  532. luaO_verror(L, l_s("tag %d can only be used for type '%.20s'"), tag,
  533. basictypename(G(L), basictype));
  534. switch (ttype(L->top-1)) {
  535. case LUA_TTABLE:
  536. hvalue(L->top-1)->htag = tag;
  537. break;
  538. case LUA_TUSERDATA:
  539. tsvalue(L->top-1)->u.d.tag = tag;
  540. break;
  541. default:
  542. luaO_verror(L, l_s("cannot change the tag of a %.20s"),
  543. luaT_typename(G(L), L->top-1));
  544. }
  545. lua_unlock(L);
  546. }
  547. LUA_API void lua_error (lua_State *L, const l_char *s) {
  548. lua_lock(L);
  549. luaD_error(L, s);
  550. lua_unlock(L);
  551. }
  552. LUA_API void lua_unref (lua_State *L, int ref) {
  553. if (ref >= 0) {
  554. lua_getregistry(L);
  555. lua_pushnil(L);
  556. lua_rawseti(L, -2, ref);
  557. lua_getweakregistry(L);
  558. lua_pushnil(L);
  559. lua_rawseti(L, -2, ref);
  560. lua_pop(L, 2); /* remove both registries */
  561. }
  562. }
  563. LUA_API int lua_next (lua_State *L, int index) {
  564. StkId t;
  565. Node *n;
  566. int more;
  567. lua_lock(L);
  568. t = luaA_index(L, index);
  569. api_check(L, ttype(t) == LUA_TTABLE);
  570. n = luaH_next(L, hvalue(t), luaA_index(L, -1));
  571. if (n) {
  572. setkey2obj(L->top-1, n);
  573. setobj(L->top, val(n));
  574. api_incr_top(L);
  575. more = 1;
  576. }
  577. else { /* no more elements */
  578. L->top -= 1; /* remove key */
  579. more = 0;
  580. }
  581. lua_unlock(L);
  582. return more;
  583. }
  584. LUA_API int lua_getn (lua_State *L, int index) {
  585. StkId t;
  586. const TObject *value;
  587. int n;
  588. lua_lock(L);
  589. t = luaA_index(L, index);
  590. api_check(L, ttype(t) == LUA_TTABLE);
  591. value = luaH_getstr(hvalue(t), luaS_newliteral(L, l_s("n"))); /* = t.n */
  592. if (ttype(value) == LUA_TNUMBER)
  593. n = (int)nvalue(value);
  594. else {
  595. lua_Number max = 0;
  596. int i = hvalue(t)->size;
  597. Node *nd = hvalue(t)->node;
  598. while (i--) {
  599. if (ttype_key(nd) == LUA_TNUMBER &&
  600. ttype(val(nd)) != LUA_TNIL &&
  601. nvalue_key(nd) > max)
  602. max = nvalue_key(nd);
  603. nd++;
  604. }
  605. n = (int)max;
  606. }
  607. lua_unlock(L);
  608. return n;
  609. }
  610. LUA_API void lua_concat (lua_State *L, int n) {
  611. lua_lock(L);
  612. api_checknelems(L, n);
  613. if (n >= 2) {
  614. luaV_strconc(L, n, L->top);
  615. L->top -= (n-1);
  616. luaC_checkGC(L);
  617. }
  618. else if (n == 0) { /* push null string */
  619. setsvalue(L->top, luaS_newlstr(L, NULL, 0));
  620. api_incr_top(L);
  621. }
  622. /* else n == 1; nothing to do */
  623. lua_unlock(L);
  624. }
  625. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  626. TString *ts;
  627. void *p;
  628. lua_lock(L);
  629. if (size == 0) size = 1;
  630. ts = luaS_newudata(L, size, NULL);
  631. setuvalue(L->top, ts);
  632. api_incr_top(L);
  633. p = ts->u.d.value;
  634. lua_unlock(L);
  635. return p;
  636. }
  637. LUA_API int lua_getweakmode (lua_State *L, int index) {
  638. StkId t;
  639. int mode;
  640. lua_lock(L);
  641. t = luaA_index(L, index);
  642. api_check(L, ttype(t) == LUA_TTABLE);
  643. mode = hvalue(t)->weakmode;
  644. lua_unlock(L);
  645. return mode;
  646. }
  647. LUA_API void lua_setweakmode (lua_State *L, int mode) {
  648. lua_lock(L);
  649. api_check(L, ttype(L->top-1) == LUA_TTABLE);
  650. hvalue(L->top-1)->weakmode = mode;
  651. lua_unlock(L);
  652. }