lapi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. ** $Id: lapi.c,v 1.200 2002/06/18 15:19:27 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_rawequal (lua_State *L, int index1, int index2) {
  161. StkId o1 = luaA_indexAcceptable(L, index1);
  162. StkId o2 = luaA_indexAcceptable(L, index2);
  163. return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
  164. : luaO_rawequalObj(o1, o2);
  165. }
  166. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  167. StkId o1, o2;
  168. int i;
  169. lua_lock(L); /* may call tag method */
  170. o1 = luaA_indexAcceptable(L, index1);
  171. o2 = luaA_indexAcceptable(L, index2);
  172. i = (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
  173. : equalobj(L, o1, o2);
  174. lua_unlock(L);
  175. return i;
  176. }
  177. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  178. StkId o1, o2;
  179. int i;
  180. lua_lock(L); /* may call tag method */
  181. o1 = luaA_indexAcceptable(L, index1);
  182. o2 = luaA_indexAcceptable(L, index2);
  183. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  184. : luaV_lessthan(L, o1, o2);
  185. lua_unlock(L);
  186. return i;
  187. }
  188. LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
  189. TObject n;
  190. const TObject *o = luaA_indexAcceptable(L, index);
  191. if (o != NULL && tonumber(o, &n))
  192. return nvalue(o);
  193. else
  194. return 0;
  195. }
  196. LUA_API int lua_toboolean (lua_State *L, int index) {
  197. const TObject *o = luaA_indexAcceptable(L, index);
  198. return (o != NULL) && !l_isfalse(o);
  199. }
  200. LUA_API const char *lua_tostring (lua_State *L, int index) {
  201. StkId o = luaA_indexAcceptable(L, index);
  202. if (o == NULL)
  203. return NULL;
  204. else if (ttype(o) == LUA_TSTRING)
  205. return svalue(o);
  206. else {
  207. const char *s;
  208. lua_lock(L); /* `luaV_tostring' may create a new string */
  209. s = (luaV_tostring(L, o) ? svalue(o) : NULL);
  210. lua_unlock(L);
  211. return s;
  212. }
  213. }
  214. LUA_API size_t lua_strlen (lua_State *L, int index) {
  215. StkId o = luaA_indexAcceptable(L, index);
  216. if (o == NULL)
  217. return 0;
  218. else if (ttype(o) == LUA_TSTRING)
  219. return tsvalue(o)->tsv.len;
  220. else {
  221. size_t l;
  222. lua_lock(L); /* `luaV_tostring' may create a new string */
  223. l = (luaV_tostring(L, o) ? tsvalue(o)->tsv.len : 0);
  224. lua_unlock(L);
  225. return l;
  226. }
  227. }
  228. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  229. StkId o = luaA_indexAcceptable(L, index);
  230. return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->c.f;
  231. }
  232. LUA_API void *lua_touserdata (lua_State *L, int index) {
  233. StkId o = luaA_indexAcceptable(L, index);
  234. if (o == NULL) return NULL;
  235. switch (ttype(o)) {
  236. case LUA_TUSERDATA: return (uvalue(o) + 1);
  237. case LUA_TUDATAVAL: return pvalue(o);
  238. default: return NULL;
  239. }
  240. }
  241. LUA_API const void *lua_topointer (lua_State *L, int index) {
  242. StkId o = luaA_indexAcceptable(L, index);
  243. if (o == NULL) return NULL;
  244. else {
  245. switch (ttype(o)) {
  246. case LUA_TTABLE: return hvalue(o);
  247. case LUA_TFUNCTION: return clvalue(o);
  248. default: return NULL;
  249. }
  250. }
  251. }
  252. /*
  253. ** push functions (C -> stack)
  254. */
  255. LUA_API void lua_pushnil (lua_State *L) {
  256. lua_lock(L);
  257. setnilvalue(L->top);
  258. api_incr_top(L);
  259. lua_unlock(L);
  260. }
  261. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  262. lua_lock(L);
  263. setnvalue(L->top, n);
  264. api_incr_top(L);
  265. lua_unlock(L);
  266. }
  267. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  268. lua_lock(L);
  269. setsvalue(L->top, luaS_newlstr(L, s, len));
  270. api_incr_top(L);
  271. lua_unlock(L);
  272. }
  273. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  274. if (s == NULL)
  275. lua_pushnil(L);
  276. else
  277. lua_pushlstring(L, s, strlen(s));
  278. }
  279. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  280. va_list argp) {
  281. const char *ret;
  282. lua_lock(L);
  283. ret = luaO_pushvfstring(L, fmt, argp);
  284. lua_unlock(L);
  285. return ret;
  286. }
  287. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  288. const char *ret;
  289. va_list argp;
  290. lua_lock(L);
  291. va_start(argp, fmt);
  292. ret = luaO_pushvfstring(L, fmt, argp);
  293. va_end(argp);
  294. lua_unlock(L);
  295. return ret;
  296. }
  297. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  298. Closure *cl;
  299. lua_lock(L);
  300. api_checknelems(L, n);
  301. cl = luaF_newCclosure(L, n);
  302. cl->c.f = fn;
  303. L->top -= n;
  304. while (n--)
  305. setobj(&cl->c.upvalue[n], L->top+n);
  306. setclvalue(L->top, cl);
  307. api_incr_top(L);
  308. lua_unlock(L);
  309. }
  310. LUA_API void lua_pushboolean (lua_State *L, int b) {
  311. lua_lock(L);
  312. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  313. api_incr_top(L);
  314. lua_unlock(L);
  315. }
  316. LUA_API void lua_pushudataval (lua_State *L, void *p) {
  317. lua_lock(L);
  318. setpvalue(L->top, p);
  319. api_incr_top(L);
  320. lua_unlock(L);
  321. }
  322. /*
  323. ** get functions (Lua -> stack)
  324. */
  325. LUA_API void lua_gettable (lua_State *L, int index) {
  326. StkId t;
  327. lua_lock(L);
  328. t = luaA_index(L, index);
  329. luaV_gettable(L, t, L->top-1, L->top-1);
  330. lua_unlock(L);
  331. }
  332. LUA_API void lua_rawget (lua_State *L, int index) {
  333. StkId t;
  334. lua_lock(L);
  335. t = luaA_index(L, index);
  336. api_check(L, ttype(t) == LUA_TTABLE);
  337. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  338. lua_unlock(L);
  339. }
  340. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  341. StkId o;
  342. lua_lock(L);
  343. o = luaA_index(L, index);
  344. api_check(L, ttype(o) == LUA_TTABLE);
  345. setobj(L->top, luaH_getnum(hvalue(o), n));
  346. api_incr_top(L);
  347. lua_unlock(L);
  348. }
  349. LUA_API void lua_newtable (lua_State *L) {
  350. lua_lock(L);
  351. sethvalue(L->top, luaH_new(L, 0, 0));
  352. api_incr_top(L);
  353. lua_unlock(L);
  354. }
  355. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  356. StkId obj;
  357. Table *mt;
  358. int res;
  359. lua_lock(L);
  360. obj = luaA_indexAcceptable(L, objindex);
  361. switch (ttype(obj)) {
  362. case LUA_TTABLE:
  363. mt = hvalue(obj)->metatable;
  364. break;
  365. case LUA_TUSERDATA:
  366. mt = uvalue(obj)->uv.metatable;
  367. break;
  368. default:
  369. mt = hvalue(defaultmeta(L));
  370. }
  371. if (mt == hvalue(defaultmeta(L)))
  372. res = 0;
  373. else {
  374. sethvalue(L->top, mt);
  375. api_incr_top(L);
  376. res = 1;
  377. }
  378. lua_unlock(L);
  379. return res;
  380. }
  381. static LClosure *getfunc (lua_State *L, int level) {
  382. CallInfo *ci;
  383. TObject *f;
  384. if (L->ci - L->base_ci < level) ci = L->base_ci;
  385. else ci = L->ci - level;
  386. f = ci->base - 1;
  387. if (isLfunction(f))
  388. return &clvalue(f)->l;
  389. else
  390. return NULL;
  391. }
  392. LUA_API void lua_getglobals (lua_State *L, int level) {
  393. LClosure *f;
  394. lua_lock(L);
  395. f = getfunc(L, level);
  396. setobj(L->top, (f ? &f->g : gt(L)));
  397. api_incr_top(L);
  398. lua_unlock(L);
  399. }
  400. /*
  401. ** set functions (stack -> Lua)
  402. */
  403. LUA_API void lua_settable (lua_State *L, int index) {
  404. StkId t;
  405. lua_lock(L);
  406. api_checknelems(L, 2);
  407. t = luaA_index(L, index);
  408. luaV_settable(L, t, L->top - 2, L->top - 1);
  409. L->top -= 2; /* pop index and value */
  410. lua_unlock(L);
  411. }
  412. LUA_API void lua_rawset (lua_State *L, int index) {
  413. StkId t;
  414. lua_lock(L);
  415. api_checknelems(L, 2);
  416. t = luaA_index(L, index);
  417. api_check(L, ttype(t) == LUA_TTABLE);
  418. setobj(luaH_set(L, hvalue(t), L->top-2), L->top-1);
  419. L->top -= 2;
  420. lua_unlock(L);
  421. }
  422. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  423. StkId o;
  424. lua_lock(L);
  425. api_checknelems(L, 1);
  426. o = luaA_index(L, index);
  427. api_check(L, ttype(o) == LUA_TTABLE);
  428. setobj(luaH_setnum(L, hvalue(o), n), L->top-1);
  429. L->top--;
  430. lua_unlock(L);
  431. }
  432. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  433. TObject *obj, *mt;
  434. int res = 1;
  435. lua_lock(L);
  436. api_checknelems(L, 1);
  437. obj = luaA_index(L, objindex);
  438. mt = (ttype(L->top - 1) != LUA_TNIL) ? L->top - 1 : defaultmeta(L);
  439. api_check(L, ttype(mt) == LUA_TTABLE);
  440. switch (ttype(obj)) {
  441. case LUA_TTABLE: {
  442. hvalue(obj)->metatable = hvalue(mt);
  443. break;
  444. }
  445. case LUA_TUSERDATA: {
  446. uvalue(obj)->uv.metatable = hvalue(mt);
  447. break;
  448. }
  449. default: {
  450. res = 0; /* cannot set */
  451. break;
  452. }
  453. }
  454. L->top--;
  455. lua_unlock(L);
  456. return res;
  457. }
  458. LUA_API int lua_setglobals (lua_State *L, int level) {
  459. LClosure *f;
  460. lua_lock(L);
  461. api_checknelems(L, 1);
  462. f = getfunc(L, level);
  463. L->top--;
  464. api_check(L, ttype(L->top) == LUA_TTABLE);
  465. if (f) f->g = *(L->top);
  466. lua_unlock(L);
  467. return (f != NULL);
  468. }
  469. /*
  470. ** `load' and `call' functions (run Lua code)
  471. */
  472. LUA_API void lua_upcall (lua_State *L, int nargs, int nresults) {
  473. StkId func;
  474. lua_lock(L);
  475. api_checknelems(L, nargs+1);
  476. func = L->top - (nargs+1);
  477. luaD_call(L, func, nresults);
  478. lua_unlock(L);
  479. }
  480. LUA_API int lua_pcall (lua_State *L, int nargs, int nresults) {
  481. int status;
  482. lua_lock(L);
  483. status = luaD_pcall(L, nargs, nresults);
  484. lua_unlock(L);
  485. return status;
  486. }
  487. LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data,
  488. const char *chunkname) {
  489. ZIO z;
  490. int status;
  491. int c;
  492. lua_lock(L);
  493. if (!chunkname) chunkname = "?";
  494. luaZ_init(&z, reader, data, chunkname);
  495. c = luaZ_lookahead(&z);
  496. status = luaD_protectedparser(L, &z, (c == LUA_SIGNATURE[0]));
  497. lua_unlock(L);
  498. return status;
  499. }
  500. /*
  501. ** Garbage-collection functions
  502. */
  503. /* GC values are expressed in Kbytes: #bytes/2^10 */
  504. #define GCscale(x) (cast(int, (x)>>10))
  505. #define GCunscale(x) (cast(lu_mem, (x)<<10))
  506. LUA_API int lua_getgcthreshold (lua_State *L) {
  507. int threshold;
  508. lua_lock(L);
  509. threshold = GCscale(G(L)->GCthreshold);
  510. lua_unlock(L);
  511. return threshold;
  512. }
  513. LUA_API int lua_getgccount (lua_State *L) {
  514. int count;
  515. lua_lock(L);
  516. count = GCscale(G(L)->nblocks);
  517. lua_unlock(L);
  518. return count;
  519. }
  520. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  521. lua_lock(L);
  522. if (newthreshold > GCscale(ULONG_MAX))
  523. G(L)->GCthreshold = ULONG_MAX;
  524. else
  525. G(L)->GCthreshold = GCunscale(newthreshold);
  526. luaC_checkGC(L);
  527. lua_unlock(L);
  528. }
  529. /*
  530. ** miscellaneous functions
  531. */
  532. LUA_API int lua_error (lua_State *L) {
  533. lua_lock(L);
  534. api_checknelems(L, 1);
  535. luaG_errormsg(L, 0);
  536. lua_unlock(L);
  537. return 0; /* to avoid warnings */
  538. }
  539. LUA_API int lua_next (lua_State *L, int index) {
  540. StkId t;
  541. int more;
  542. lua_lock(L);
  543. t = luaA_index(L, index);
  544. api_check(L, ttype(t) == LUA_TTABLE);
  545. more = luaH_next(L, hvalue(t), L->top - 1);
  546. if (more) {
  547. api_incr_top(L);
  548. }
  549. else /* no more elements */
  550. L->top -= 1; /* remove key */
  551. lua_unlock(L);
  552. return more;
  553. }
  554. LUA_API int lua_getn (lua_State *L, int index) {
  555. StkId t;
  556. const TObject *value;
  557. int n;
  558. lua_lock(L);
  559. t = luaA_index(L, index);
  560. api_check(L, ttype(t) == LUA_TTABLE);
  561. value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */
  562. if (ttype(value) == LUA_TNUMBER)
  563. lua_number2int(n, nvalue(value));
  564. else {
  565. Node *nd;
  566. Table *a = hvalue(t);
  567. lua_Number max = 0;
  568. int i;
  569. i = sizearray(a);
  570. while (i--) {
  571. if (ttype(&a->array[i]) != LUA_TNIL)
  572. break;
  573. }
  574. max = i+1;
  575. i = sizenode(a);
  576. nd = a->node;
  577. while (i--) {
  578. if (ttype(key(nd)) == LUA_TNUMBER &&
  579. ttype(val(nd)) != LUA_TNIL &&
  580. nvalue(key(nd)) > max)
  581. max = nvalue(key(nd));
  582. nd++;
  583. }
  584. lua_number2int(n, max);
  585. }
  586. lua_unlock(L);
  587. return n;
  588. }
  589. LUA_API void lua_concat (lua_State *L, int n) {
  590. lua_lock(L);
  591. api_checknelems(L, n);
  592. if (n >= 2) {
  593. luaV_concat(L, n, L->top - L->ci->base - 1);
  594. L->top -= (n-1);
  595. luaC_checkGC(L);
  596. }
  597. else if (n == 0) { /* push empty string */
  598. setsvalue(L->top, luaS_newlstr(L, NULL, 0));
  599. api_incr_top(L);
  600. }
  601. /* else n == 1; nothing to do */
  602. lua_unlock(L);
  603. }
  604. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  605. Udata *u;
  606. lua_lock(L);
  607. u = luaS_newudata(L, size);
  608. setuvalue(L->top, u);
  609. api_incr_top(L);
  610. lua_unlock(L);
  611. return u + 1;
  612. }
  613. LUA_API int lua_pushupvalues (lua_State *L) {
  614. TObject *func;
  615. int n, i;
  616. lua_lock(L);
  617. func = (L->ci->base - 1);
  618. api_check(L, iscfunction(func));
  619. n = clvalue(func)->c.nupvalues;
  620. luaD_checkstack(L, n + LUA_MINSTACK);
  621. for (i=0; i<n; i++) {
  622. setobj(L->top, &clvalue(func)->c.upvalue[i]);
  623. L->top++;
  624. }
  625. lua_unlock(L);
  626. return n;
  627. }