lapi.c 16 KB

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