lapi.c 16 KB

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