lapi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. ** $Id: lapi.c,v 1.184 2002/04/16 17:08:28 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #include "lua.h"
  8. #include "lapi.h"
  9. #include "ldo.h"
  10. #include "lfunc.h"
  11. #include "lgc.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. #include "lstring.h"
  16. #include "ltable.h"
  17. #include "ltm.h"
  18. #include "lundump.h"
  19. #include "lvm.h"
  20. const char lua_ident[] =
  21. "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
  22. "$Authors: " LUA_AUTHORS " $\n"
  23. "$URL: www.lua.org $\n";
  24. #ifndef api_check
  25. #define api_check(L, o) ((void)1)
  26. #endif
  27. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->ci->base))
  28. #define api_incr_top(L) (api_check(L, L->top<L->ci->top), L->top++)
  29. static TObject *negindex (lua_State *L, int index) {
  30. if (index > LUA_REGISTRYINDEX) {
  31. api_check(L, index != 0 && -index <= L->top - L->ci->base);
  32. return L->top+index;
  33. }
  34. else switch (index) { /* pseudo-indices */
  35. case LUA_REGISTRYINDEX: return registry(L);
  36. case LUA_GLOBALSINDEX: return gt(L);
  37. default: {
  38. TObject *func = (L->ci->base - 1);
  39. index = LUA_GLOBALSINDEX - index;
  40. api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues);
  41. return &clvalue(func)->c.upvalue[index-1];
  42. }
  43. }
  44. }
  45. #define luaA_index(L, index) \
  46. ( (index > 0) ? \
  47. (api_check(L, index <= L->top - L->ci->base), L->ci->base+index-1) : \
  48. negindex(L, index))
  49. static TObject *luaA_indexAcceptable (lua_State *L, int index) {
  50. if (index > 0) {
  51. TObject *o = L->ci->base+(index-1);
  52. api_check(L, index <= L->stack_last - L->ci->base);
  53. if (o >= L->top) return NULL;
  54. else return o;
  55. }
  56. else
  57. return negindex(L, index);
  58. }
  59. void luaA_pushobject (lua_State *L, const TObject *o) {
  60. setobj(L->top, o);
  61. incr_top(L);
  62. }
  63. LUA_API int lua_checkstack (lua_State *L, int size) {
  64. int res;
  65. lua_lock(L);
  66. if ((L->top - L->ci->base + size) > LUA_MAXCSTACK)
  67. res = 0; /* stack overflow */
  68. else {
  69. luaD_checkstack(L, size);
  70. if (L->ci->top < L->top + size)
  71. L->ci->top = L->top + size;
  72. res = 1;
  73. }
  74. lua_unlock(L);
  75. return res;
  76. }
  77. LUA_API lua_CFunction lua_setpanicf (lua_State *L, lua_CFunction panicf) {
  78. lua_CFunction old;
  79. lua_lock(L);
  80. old = G(L)->panic;
  81. G(L)->panic = panicf;
  82. lua_unlock(L);
  83. return old;
  84. }
  85. /*
  86. ** basic stack manipulation
  87. */
  88. LUA_API int lua_gettop (lua_State *L) {
  89. return (L->top - L->ci->base);
  90. }
  91. LUA_API void lua_settop (lua_State *L, int index) {
  92. lua_lock(L);
  93. if (index >= 0) {
  94. api_check(L, index <= L->stack_last - L->ci->base);
  95. while (L->top < L->ci->base + index)
  96. setnilvalue(L->top++);
  97. L->top = L->ci->base + index;
  98. }
  99. else {
  100. api_check(L, -(index+1) <= (L->top - L->ci->base));
  101. L->top += index+1; /* `subtract' index (index is negative) */
  102. }
  103. lua_unlock(L);
  104. }
  105. LUA_API void lua_remove (lua_State *L, int index) {
  106. StkId p;
  107. lua_lock(L);
  108. p = luaA_index(L, index);
  109. while (++p < L->top) setobj(p-1, p);
  110. L->top--;
  111. lua_unlock(L);
  112. }
  113. LUA_API void lua_insert (lua_State *L, int index) {
  114. StkId p;
  115. StkId q;
  116. lua_lock(L);
  117. p = luaA_index(L, index);
  118. for (q = L->top; q>p; q--) setobj(q, q-1);
  119. setobj(p, L->top);
  120. lua_unlock(L);
  121. }
  122. LUA_API void lua_replace (lua_State *L, int index) {
  123. lua_lock(L);
  124. api_checknelems(L, 1);
  125. setobj(luaA_index(L, index), L->top - 1);
  126. L->top--;
  127. lua_unlock(L);
  128. }
  129. LUA_API void lua_pushvalue (lua_State *L, int index) {
  130. lua_lock(L);
  131. setobj(L->top, luaA_index(L, index));
  132. api_incr_top(L);
  133. lua_unlock(L);
  134. }
  135. /*
  136. ** access functions (stack -> C)
  137. */
  138. LUA_API int lua_type (lua_State *L, int index) {
  139. StkId o = luaA_indexAcceptable(L, index);
  140. return (o == NULL) ? LUA_TNONE : ttype(o);
  141. }
  142. LUA_API const char *lua_typename (lua_State *L, int t) {
  143. UNUSED(L);
  144. return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
  145. }
  146. LUA_API int lua_iscfunction (lua_State *L, int index) {
  147. StkId o = luaA_indexAcceptable(L, index);
  148. return (o == NULL) ? 0 : iscfunction(o);
  149. }
  150. LUA_API int lua_isnumber (lua_State *L, int index) {
  151. TObject n;
  152. const TObject *o = luaA_indexAcceptable(L, index);
  153. return (o != NULL && tonumber(o, &n));
  154. }
  155. LUA_API int lua_isstring (lua_State *L, int index) {
  156. int t = lua_type(L, index);
  157. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  158. }
  159. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  160. StkId o1 = luaA_indexAcceptable(L, index1);
  161. StkId o2 = luaA_indexAcceptable(L, index2);
  162. return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
  163. : luaO_equalObj(o1, o2);
  164. }
  165. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  166. StkId o1, o2;
  167. int i;
  168. lua_lock(L); /* may call tag method */
  169. o1 = luaA_indexAcceptable(L, index1);
  170. o2 = luaA_indexAcceptable(L, index2);
  171. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  172. : luaV_lessthan(L, o1, o2);
  173. lua_unlock(L);
  174. return i;
  175. }
  176. LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
  177. TObject n;
  178. const TObject *o = luaA_indexAcceptable(L, index);
  179. if (o != NULL && tonumber(o, &n))
  180. return nvalue(o);
  181. else
  182. return 0;
  183. }
  184. LUA_API int lua_toboolean (lua_State *L, int index) {
  185. const TObject *o = luaA_indexAcceptable(L, index);
  186. return (o != NULL) && !l_isfalse(o);
  187. }
  188. LUA_API const char *lua_tostring (lua_State *L, int index) {
  189. StkId o = luaA_indexAcceptable(L, index);
  190. if (o == NULL)
  191. return NULL;
  192. else if (ttype(o) == LUA_TSTRING)
  193. return svalue(o);
  194. else {
  195. const char *s;
  196. lua_lock(L); /* `luaV_tostring' may create a new string */
  197. s = (luaV_tostring(L, o) ? svalue(o) : NULL);
  198. lua_unlock(L);
  199. return s;
  200. }
  201. }
  202. LUA_API size_t lua_strlen (lua_State *L, int index) {
  203. StkId o = luaA_indexAcceptable(L, index);
  204. if (o == NULL)
  205. return 0;
  206. else if (ttype(o) == LUA_TSTRING)
  207. return tsvalue(o)->tsv.len;
  208. else {
  209. size_t l;
  210. lua_lock(L); /* `luaV_tostring' may create a new string */
  211. l = (luaV_tostring(L, o) ? tsvalue(o)->tsv.len : 0);
  212. lua_unlock(L);
  213. return l;
  214. }
  215. }
  216. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  217. StkId o = luaA_indexAcceptable(L, index);
  218. return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->c.f;
  219. }
  220. LUA_API void *lua_touserdata (lua_State *L, int index) {
  221. StkId o = luaA_indexAcceptable(L, index);
  222. if (o == NULL) return NULL;
  223. switch (ttype(o)) {
  224. case LUA_TUSERDATA: return (uvalue(o) + 1);
  225. case LUA_TUDATAVAL: return pvalue(o);
  226. default: return NULL;
  227. }
  228. }
  229. LUA_API const void *lua_topointer (lua_State *L, int index) {
  230. StkId o = luaA_indexAcceptable(L, index);
  231. if (o == NULL) return NULL;
  232. else {
  233. switch (ttype(o)) {
  234. case LUA_TTABLE: return hvalue(o);
  235. case LUA_TFUNCTION: return clvalue(o);
  236. default: return NULL;
  237. }
  238. }
  239. }
  240. /*
  241. ** push functions (C -> stack)
  242. */
  243. LUA_API void lua_pushnil (lua_State *L) {
  244. lua_lock(L);
  245. setnilvalue(L->top);
  246. api_incr_top(L);
  247. lua_unlock(L);
  248. }
  249. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  250. lua_lock(L);
  251. setnvalue(L->top, n);
  252. api_incr_top(L);
  253. lua_unlock(L);
  254. }
  255. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  256. lua_lock(L);
  257. setsvalue(L->top, luaS_newlstr(L, s, len));
  258. api_incr_top(L);
  259. lua_unlock(L);
  260. }
  261. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  262. if (s == NULL)
  263. lua_pushnil(L);
  264. else
  265. lua_pushlstring(L, s, strlen(s));
  266. }
  267. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  268. Closure *cl;
  269. lua_lock(L);
  270. api_checknelems(L, n);
  271. cl = luaF_newCclosure(L, n);
  272. cl->c.f = fn;
  273. L->top -= n;
  274. while (n--)
  275. setobj(&cl->c.upvalue[n], L->top+n);
  276. setclvalue(L->top, cl);
  277. api_incr_top(L);
  278. lua_unlock(L);
  279. }
  280. LUA_API void lua_pushboolean (lua_State *L, int b) {
  281. lua_lock(L);
  282. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  283. api_incr_top(L);
  284. lua_unlock(L);
  285. }
  286. LUA_API void lua_pushudataval (lua_State *L, void *p) {
  287. lua_lock(L);
  288. setpvalue(L->top, p);
  289. api_incr_top(L);
  290. lua_unlock(L);
  291. }
  292. /*
  293. ** get functions (Lua -> stack)
  294. */
  295. LUA_API void lua_gettable (lua_State *L, int index) {
  296. StkId t;
  297. lua_lock(L);
  298. t = luaA_index(L, index);
  299. luaV_gettable(L, t, L->top-1, L->top-1);
  300. lua_unlock(L);
  301. }
  302. LUA_API void lua_rawget (lua_State *L, int index) {
  303. StkId t;
  304. lua_lock(L);
  305. t = luaA_index(L, index);
  306. api_check(L, ttype(t) == LUA_TTABLE);
  307. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  308. lua_unlock(L);
  309. }
  310. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  311. StkId o;
  312. lua_lock(L);
  313. o = luaA_index(L, index);
  314. api_check(L, ttype(o) == LUA_TTABLE);
  315. setobj(L->top, luaH_getnum(hvalue(o), n));
  316. api_incr_top(L);
  317. lua_unlock(L);
  318. }
  319. LUA_API void lua_newtable (lua_State *L) {
  320. lua_lock(L);
  321. sethvalue(L->top, luaH_new(L, 0, 0));
  322. api_incr_top(L);
  323. lua_unlock(L);
  324. }
  325. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  326. StkId obj;
  327. Table *mt;
  328. int res;
  329. lua_lock(L);
  330. obj = luaA_indexAcceptable(L, objindex);
  331. switch (ttype(obj)) {
  332. case LUA_TTABLE:
  333. mt = hvalue(obj)->metatable;
  334. break;
  335. case LUA_TUSERDATA:
  336. mt = uvalue(obj)->uv.metatable;
  337. break;
  338. default:
  339. mt = hvalue(defaultmeta(L));
  340. }
  341. if (mt == hvalue(defaultmeta(L)))
  342. res = 0;
  343. else {
  344. sethvalue(L->top, mt);
  345. api_incr_top(L);
  346. res = 1;
  347. }
  348. lua_unlock(L);
  349. return res;
  350. }
  351. /*
  352. ** set functions (stack -> Lua)
  353. */
  354. LUA_API void lua_settable (lua_State *L, int index) {
  355. StkId t;
  356. lua_lock(L);
  357. api_checknelems(L, 2);
  358. t = luaA_index(L, index);
  359. luaV_settable(L, t, L->top - 2, L->top - 1);
  360. L->top -= 2; /* pop index and value */
  361. lua_unlock(L);
  362. }
  363. LUA_API void lua_rawset (lua_State *L, int index) {
  364. StkId t;
  365. lua_lock(L);
  366. api_checknelems(L, 2);
  367. t = luaA_index(L, index);
  368. api_check(L, ttype(t) == LUA_TTABLE);
  369. luaH_set(L, hvalue(t), L->top-2, L->top-1);
  370. L->top -= 2;
  371. lua_unlock(L);
  372. }
  373. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  374. StkId o;
  375. lua_lock(L);
  376. api_checknelems(L, 1);
  377. o = luaA_index(L, index);
  378. api_check(L, ttype(o) == LUA_TTABLE);
  379. luaH_setnum(L, hvalue(o), n, L->top-1);
  380. L->top--;
  381. lua_unlock(L);
  382. }
  383. LUA_API void lua_setmetatable (lua_State *L, int objindex) {
  384. StkId obj, mt;
  385. lua_lock(L);
  386. api_checknelems(L, 1);
  387. obj = luaA_index(L, objindex);
  388. mt = --L->top;
  389. if (ttype(mt) == LUA_TNIL)
  390. mt = defaultmeta(L);
  391. api_check(L, ttype(mt) == LUA_TTABLE);
  392. switch (ttype(obj)) {
  393. case LUA_TTABLE:
  394. hvalue(obj)->metatable = hvalue(mt);
  395. break;
  396. case LUA_TUSERDATA:
  397. uvalue(obj)->uv.metatable = hvalue(mt);
  398. break;
  399. default:
  400. luaO_verror(L, "cannot change the meta table of a %.20s",
  401. luaT_typenames[ttype(obj)]);
  402. }
  403. lua_unlock(L);
  404. }
  405. /*
  406. ** `do' functions (run Lua code)
  407. */
  408. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  409. StkId func;
  410. lua_lock(L);
  411. api_checknelems(L, nargs+1);
  412. func = L->top - (nargs+1);
  413. luaD_call(L, func, nresults);
  414. lua_unlock(L);
  415. }
  416. LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
  417. int status;
  418. int errpos = lua_gettop(L) - nargs;
  419. lua_getglobal(L, "_ERRORMESSAGE");
  420. lua_insert(L, errpos); /* put below function and args */
  421. status = lua_pcall(L, nargs, nresults, errpos);
  422. lua_remove(L, errpos);
  423. return status;
  424. }
  425. LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) {
  426. int status;
  427. const TObject *err;
  428. lua_lock(L);
  429. err = (errf == 0) ? &luaO_nilobject : luaA_index(L, errf);
  430. status = luaD_pcall(L, nargs, nresults, err);
  431. lua_unlock(L);
  432. return status;
  433. }
  434. static int aux_do (lua_State *L, int status) {
  435. if (status == 0) { /* parse OK? */
  436. int err = lua_gettop(L);
  437. lua_getglobal(L, "_ERRORMESSAGE");
  438. lua_insert(L, err);
  439. status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
  440. lua_remove(L, err); /* remove error function */
  441. }
  442. return status;
  443. }
  444. LUA_API int lua_dofile (lua_State *L, const char *filename) {
  445. return aux_do(L, lua_loadfile(L, filename));
  446. }
  447. LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  448. const char *name) {
  449. return aux_do(L, lua_loadbuffer(L, buff, size, name));
  450. }
  451. LUA_API int lua_dostring (lua_State *L, const char *str) {
  452. return lua_dobuffer(L, str, strlen(str), str);
  453. }
  454. LUA_API int lua_loadfile (lua_State *L, const char *filename) {
  455. ZIO z;
  456. int status;
  457. int bin; /* flag for file mode */
  458. int nlevel; /* level on the stack of filename */
  459. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  460. if (f == NULL) return LUA_ERRFILE; /* unable to open file */
  461. bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
  462. if (bin && f != stdin) {
  463. fclose(f);
  464. f = fopen(filename, "rb"); /* reopen in binary mode */
  465. if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
  466. }
  467. if (filename == NULL)
  468. lua_pushstring(L, "=stdin");
  469. else {
  470. lua_pushliteral(L, "@");
  471. lua_pushstring(L, filename);
  472. lua_concat(L, 2);
  473. }
  474. nlevel = lua_gettop(L);
  475. filename = lua_tostring(L, -1); /* filename = `@'..filename */
  476. luaZ_Fopen(&z, f, filename);
  477. status = luaD_protectedparser(L, &z, bin);
  478. if (ferror(f)) status = LUA_ERRFILE;
  479. lua_remove(L, nlevel); /* remove filename */
  480. if (f != stdin)
  481. fclose(f);
  482. return status;
  483. }
  484. LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
  485. const char *name) {
  486. ZIO z;
  487. if (!name) name = "?";
  488. luaZ_mopen(&z, buff, size, name);
  489. return luaD_protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
  490. }
  491. /*
  492. ** Garbage-collection functions
  493. */
  494. /* GC values are expressed in Kbytes: #bytes/2^10 */
  495. #define GCscale(x) (cast(int, (x)>>10))
  496. #define GCunscale(x) (cast(lu_mem, (x)<<10))
  497. LUA_API int lua_getgcthreshold (lua_State *L) {
  498. int threshold;
  499. lua_lock(L);
  500. threshold = GCscale(G(L)->GCthreshold);
  501. lua_unlock(L);
  502. return threshold;
  503. }
  504. LUA_API int lua_getgccount (lua_State *L) {
  505. int count;
  506. lua_lock(L);
  507. count = GCscale(G(L)->nblocks);
  508. lua_unlock(L);
  509. return count;
  510. }
  511. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  512. lua_lock(L);
  513. if (newthreshold > GCscale(ULONG_MAX))
  514. G(L)->GCthreshold = ULONG_MAX;
  515. else
  516. G(L)->GCthreshold = GCunscale(newthreshold);
  517. luaC_checkGC(L);
  518. lua_unlock(L);
  519. }
  520. /*
  521. ** miscellaneous functions
  522. */
  523. LUA_API void lua_error (lua_State *L, const char *s) {
  524. lua_lock(L);
  525. luaD_runerror(L, s);
  526. lua_unlock(L);
  527. }
  528. LUA_API int lua_next (lua_State *L, int index) {
  529. StkId t;
  530. int more;
  531. lua_lock(L);
  532. t = luaA_index(L, index);
  533. api_check(L, ttype(t) == LUA_TTABLE);
  534. more = luaH_next(L, hvalue(t), L->top - 1);
  535. if (more) {
  536. api_incr_top(L);
  537. }
  538. else /* no more elements */
  539. L->top -= 1; /* remove key */
  540. lua_unlock(L);
  541. return more;
  542. }
  543. LUA_API int lua_getn (lua_State *L, int index) {
  544. StkId t;
  545. const TObject *value;
  546. int n;
  547. lua_lock(L);
  548. t = luaA_index(L, index);
  549. api_check(L, ttype(t) == LUA_TTABLE);
  550. value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */
  551. if (ttype(value) == LUA_TNUMBER)
  552. lua_number2int(n, nvalue(value));
  553. else {
  554. Node *nd;
  555. Table *a = hvalue(t);
  556. lua_Number max = 0;
  557. int i;
  558. i = sizearray(a);
  559. while (i--) {
  560. if (ttype(&a->array[i]) != LUA_TNIL)
  561. break;
  562. }
  563. max = i+1;
  564. i = sizenode(a);
  565. nd = a->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. lua_number2int(n, 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 - L->ci->base - 1);
  583. L->top -= (n-1);
  584. luaC_checkGC(L);
  585. }
  586. else if (n == 0) { /* push empty 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. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  594. Udata *u;
  595. lua_lock(L);
  596. u = luaS_newudata(L, size);
  597. setuvalue(L->top, u);
  598. api_incr_top(L);
  599. lua_unlock(L);
  600. return u + 1;
  601. }
  602. LUA_API int lua_pushupvalues (lua_State *L) {
  603. TObject *func;
  604. int n, i;
  605. lua_lock(L);
  606. func = (L->ci->base - 1);
  607. api_check(L, iscfunction(func));
  608. n = clvalue(func)->c.nupvalues;
  609. luaD_checkstack(L, n + LUA_MINSTACK);
  610. for (i=0; i<n; i++) {
  611. setobj(L->top, &clvalue(func)->c.upvalue[i]);
  612. L->top++;
  613. }
  614. lua_unlock(L);
  615. return n;
  616. }