lapi.c 16 KB

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