lapi.c 16 KB

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