lapi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. ** $Id: lapi.c,v 1.191 2002/05/15 18:57:44 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.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 lua_filerror
  27. #include <errno.h>
  28. #define lua_fileerror (strerror(errno))
  29. #endif
  30. #ifndef api_check
  31. #define api_check(L, o) ((void)1)
  32. #endif
  33. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->ci->base))
  34. #define api_incr_top(L) (api_check(L, L->top<L->ci->top), L->top++)
  35. static TObject *negindex (lua_State *L, int index) {
  36. if (index > LUA_REGISTRYINDEX) {
  37. api_check(L, index != 0 && -index <= L->top - L->ci->base);
  38. return L->top+index;
  39. }
  40. else switch (index) { /* pseudo-indices */
  41. case LUA_REGISTRYINDEX: return registry(L);
  42. case LUA_GLOBALSINDEX: return gt(L);
  43. default: {
  44. TObject *func = (L->ci->base - 1);
  45. index = LUA_GLOBALSINDEX - index;
  46. api_check(L, iscfunction(func) && index <= clvalue(func)->c.nupvalues);
  47. return &clvalue(func)->c.upvalue[index-1];
  48. }
  49. }
  50. }
  51. #define luaA_index(L, index) \
  52. ( (index > 0) ? \
  53. (api_check(L, index <= L->top - L->ci->base), L->ci->base+index-1) : \
  54. negindex(L, index))
  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_setpanicf (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_equal (lua_State *L, int index1, int index2) {
  166. StkId o1 = luaA_indexAcceptable(L, index1);
  167. StkId o2 = luaA_indexAcceptable(L, index2);
  168. return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
  169. : luaO_equalObj(o1, o2);
  170. }
  171. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  172. StkId o1, o2;
  173. int i;
  174. lua_lock(L); /* may call tag method */
  175. o1 = luaA_indexAcceptable(L, index1);
  176. o2 = luaA_indexAcceptable(L, index2);
  177. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  178. : luaV_cmp(L, o1, o2, CMP_LT);
  179. lua_unlock(L);
  180. return i;
  181. }
  182. LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
  183. TObject n;
  184. const TObject *o = luaA_indexAcceptable(L, index);
  185. if (o != NULL && tonumber(o, &n))
  186. return nvalue(o);
  187. else
  188. return 0;
  189. }
  190. LUA_API int lua_toboolean (lua_State *L, int index) {
  191. const TObject *o = luaA_indexAcceptable(L, index);
  192. return (o != NULL) && !l_isfalse(o);
  193. }
  194. LUA_API const char *lua_tostring (lua_State *L, int index) {
  195. StkId o = luaA_indexAcceptable(L, index);
  196. if (o == NULL)
  197. return NULL;
  198. else if (ttype(o) == LUA_TSTRING)
  199. return svalue(o);
  200. else {
  201. const char *s;
  202. lua_lock(L); /* `luaV_tostring' may create a new string */
  203. s = (luaV_tostring(L, o) ? svalue(o) : NULL);
  204. lua_unlock(L);
  205. return s;
  206. }
  207. }
  208. LUA_API size_t lua_strlen (lua_State *L, int index) {
  209. StkId o = luaA_indexAcceptable(L, index);
  210. if (o == NULL)
  211. return 0;
  212. else if (ttype(o) == LUA_TSTRING)
  213. return tsvalue(o)->tsv.len;
  214. else {
  215. size_t l;
  216. lua_lock(L); /* `luaV_tostring' may create a new string */
  217. l = (luaV_tostring(L, o) ? tsvalue(o)->tsv.len : 0);
  218. lua_unlock(L);
  219. return l;
  220. }
  221. }
  222. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  223. StkId o = luaA_indexAcceptable(L, index);
  224. return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->c.f;
  225. }
  226. LUA_API void *lua_touserdata (lua_State *L, int index) {
  227. StkId o = luaA_indexAcceptable(L, index);
  228. if (o == NULL) return NULL;
  229. switch (ttype(o)) {
  230. case LUA_TUSERDATA: return (uvalue(o) + 1);
  231. case LUA_TUDATAVAL: return pvalue(o);
  232. default: return NULL;
  233. }
  234. }
  235. LUA_API const void *lua_topointer (lua_State *L, int index) {
  236. StkId o = luaA_indexAcceptable(L, index);
  237. if (o == NULL) return NULL;
  238. else {
  239. switch (ttype(o)) {
  240. case LUA_TTABLE: return hvalue(o);
  241. case LUA_TFUNCTION: return clvalue(o);
  242. default: return NULL;
  243. }
  244. }
  245. }
  246. /*
  247. ** push functions (C -> stack)
  248. */
  249. LUA_API void lua_pushnil (lua_State *L) {
  250. lua_lock(L);
  251. setnilvalue(L->top);
  252. api_incr_top(L);
  253. lua_unlock(L);
  254. }
  255. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  256. lua_lock(L);
  257. setnvalue(L->top, n);
  258. api_incr_top(L);
  259. lua_unlock(L);
  260. }
  261. LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
  262. lua_lock(L);
  263. setsvalue(L->top, luaS_newlstr(L, s, len));
  264. api_incr_top(L);
  265. lua_unlock(L);
  266. }
  267. LUA_API void lua_pushstring (lua_State *L, const char *s) {
  268. if (s == NULL)
  269. lua_pushnil(L);
  270. else
  271. lua_pushlstring(L, s, strlen(s));
  272. }
  273. LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,
  274. va_list argp) {
  275. const char *ret;
  276. lua_lock(L);
  277. ret = luaO_pushvfstring(L, fmt, argp);
  278. lua_unlock(L);
  279. return ret;
  280. }
  281. LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {
  282. const char *ret;
  283. va_list argp;
  284. va_start(argp, fmt);
  285. ret = lua_pushvfstring(L, fmt, argp);
  286. va_end(argp);
  287. return ret;
  288. }
  289. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  290. Closure *cl;
  291. lua_lock(L);
  292. api_checknelems(L, n);
  293. cl = luaF_newCclosure(L, n);
  294. cl->c.f = fn;
  295. L->top -= n;
  296. while (n--)
  297. setobj(&cl->c.upvalue[n], L->top+n);
  298. setclvalue(L->top, cl);
  299. api_incr_top(L);
  300. lua_unlock(L);
  301. }
  302. LUA_API void lua_pushboolean (lua_State *L, int b) {
  303. lua_lock(L);
  304. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  305. api_incr_top(L);
  306. lua_unlock(L);
  307. }
  308. LUA_API void lua_pushudataval (lua_State *L, void *p) {
  309. lua_lock(L);
  310. setpvalue(L->top, p);
  311. api_incr_top(L);
  312. lua_unlock(L);
  313. }
  314. /*
  315. ** get functions (Lua -> stack)
  316. */
  317. LUA_API void lua_gettable (lua_State *L, int index) {
  318. StkId t;
  319. lua_lock(L);
  320. t = luaA_index(L, index);
  321. luaV_gettable(L, t, L->top-1, L->top-1);
  322. lua_unlock(L);
  323. }
  324. LUA_API void lua_rawget (lua_State *L, int index) {
  325. StkId t;
  326. lua_lock(L);
  327. t = luaA_index(L, index);
  328. api_check(L, ttype(t) == LUA_TTABLE);
  329. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  330. lua_unlock(L);
  331. }
  332. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  333. StkId o;
  334. lua_lock(L);
  335. o = luaA_index(L, index);
  336. api_check(L, ttype(o) == LUA_TTABLE);
  337. setobj(L->top, luaH_getnum(hvalue(o), n));
  338. api_incr_top(L);
  339. lua_unlock(L);
  340. }
  341. LUA_API void lua_newtable (lua_State *L) {
  342. lua_lock(L);
  343. sethvalue(L->top, luaH_new(L, 0, 0));
  344. api_incr_top(L);
  345. lua_unlock(L);
  346. }
  347. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  348. StkId obj;
  349. Table *mt;
  350. int res;
  351. lua_lock(L);
  352. obj = luaA_indexAcceptable(L, objindex);
  353. switch (ttype(obj)) {
  354. case LUA_TTABLE:
  355. mt = hvalue(obj)->metatable;
  356. break;
  357. case LUA_TUSERDATA:
  358. mt = uvalue(obj)->uv.metatable;
  359. break;
  360. default:
  361. mt = hvalue(defaultmeta(L));
  362. }
  363. if (mt == hvalue(defaultmeta(L)))
  364. res = 0;
  365. else {
  366. sethvalue(L->top, mt);
  367. api_incr_top(L);
  368. res = 1;
  369. }
  370. lua_unlock(L);
  371. return res;
  372. }
  373. /*
  374. ** set functions (stack -> Lua)
  375. */
  376. LUA_API void lua_settable (lua_State *L, int index) {
  377. StkId t;
  378. lua_lock(L);
  379. api_checknelems(L, 2);
  380. t = luaA_index(L, index);
  381. luaV_settable(L, t, L->top - 2, L->top - 1);
  382. L->top -= 2; /* pop index and value */
  383. lua_unlock(L);
  384. }
  385. LUA_API void lua_rawset (lua_State *L, int index) {
  386. StkId t;
  387. lua_lock(L);
  388. api_checknelems(L, 2);
  389. t = luaA_index(L, index);
  390. api_check(L, ttype(t) == LUA_TTABLE);
  391. luaH_set(L, hvalue(t), L->top-2, L->top-1);
  392. L->top -= 2;
  393. lua_unlock(L);
  394. }
  395. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  396. StkId o;
  397. lua_lock(L);
  398. api_checknelems(L, 1);
  399. o = luaA_index(L, index);
  400. api_check(L, ttype(o) == LUA_TTABLE);
  401. luaH_setnum(L, hvalue(o), n, L->top-1);
  402. L->top--;
  403. lua_unlock(L);
  404. }
  405. LUA_API void lua_setmetatable (lua_State *L, int objindex) {
  406. StkId obj, mt;
  407. lua_lock(L);
  408. api_checknelems(L, 1);
  409. obj = luaA_index(L, objindex);
  410. mt = --L->top;
  411. if (ttype(mt) == LUA_TNIL)
  412. mt = defaultmeta(L);
  413. api_check(L, ttype(mt) == LUA_TTABLE);
  414. switch (ttype(obj)) {
  415. case LUA_TTABLE:
  416. hvalue(obj)->metatable = hvalue(mt);
  417. break;
  418. case LUA_TUSERDATA:
  419. uvalue(obj)->uv.metatable = hvalue(mt);
  420. break;
  421. default:
  422. luaG_runerror(L, "cannot change the meta table of a %s",
  423. luaT_typenames[ttype(obj)]);
  424. }
  425. lua_unlock(L);
  426. }
  427. /*
  428. ** `load' and `call' functions (run Lua code)
  429. */
  430. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  431. StkId func;
  432. lua_lock(L);
  433. api_checknelems(L, nargs+1);
  434. func = L->top - (nargs+1);
  435. luaD_call(L, func, nresults);
  436. lua_unlock(L);
  437. }
  438. LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) {
  439. int status;
  440. const TObject *err;
  441. lua_lock(L);
  442. err = (errf == 0) ? &luaO_nilobject : luaA_index(L, errf);
  443. status = luaD_pcall(L, nargs, nresults, err);
  444. lua_unlock(L);
  445. return status;
  446. }
  447. static int errfile (lua_State *L, const char *filename) {
  448. if (filename == NULL) filename = "stdin";
  449. lua_pushfstring(L, "cannot read %s: %s", filename, lua_fileerror);
  450. return LUA_ERRFILE;
  451. }
  452. LUA_API int lua_loadfile (lua_State *L, const char *filename) {
  453. ZIO z;
  454. int fnindex;
  455. int status;
  456. int bin; /* flag for file mode */
  457. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  458. if (f == NULL) return errfile(L, filename); /* unable to open file */
  459. bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
  460. if (bin && f != stdin) {
  461. fclose(f);
  462. f = fopen(filename, "rb"); /* reopen in binary mode */
  463. if (f == NULL) return errfile(L, filename); /* unable to reopen file */
  464. }
  465. if (filename == NULL)
  466. lua_pushliteral(L, "=stdin");
  467. else
  468. lua_pushfstring(L, "@%s", filename);
  469. fnindex = lua_gettop(L); /* stack index of file name */
  470. luaZ_Fopen(&z, f, lua_tostring(L, fnindex));
  471. status = luaD_protectedparser(L, &z, bin);
  472. lua_remove(L, fnindex);
  473. if (ferror(f)) {
  474. if (status == 0) lua_pop(L, 1); /* remove chunk */
  475. return errfile(L, filename);
  476. }
  477. if (f != stdin)
  478. fclose(f);
  479. return status;
  480. }
  481. LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
  482. const char *name) {
  483. ZIO z;
  484. if (!name) name = "?";
  485. luaZ_mopen(&z, buff, size, name);
  486. return luaD_protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
  487. }
  488. /*
  489. ** Garbage-collection functions
  490. */
  491. /* GC values are expressed in Kbytes: #bytes/2^10 */
  492. #define GCscale(x) (cast(int, (x)>>10))
  493. #define GCunscale(x) (cast(lu_mem, (x)<<10))
  494. LUA_API int lua_getgcthreshold (lua_State *L) {
  495. int threshold;
  496. lua_lock(L);
  497. threshold = GCscale(G(L)->GCthreshold);
  498. lua_unlock(L);
  499. return threshold;
  500. }
  501. LUA_API int lua_getgccount (lua_State *L) {
  502. int count;
  503. lua_lock(L);
  504. count = GCscale(G(L)->nblocks);
  505. lua_unlock(L);
  506. return count;
  507. }
  508. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  509. lua_lock(L);
  510. if (newthreshold > GCscale(ULONG_MAX))
  511. G(L)->GCthreshold = ULONG_MAX;
  512. else
  513. G(L)->GCthreshold = GCunscale(newthreshold);
  514. luaC_checkGC(L);
  515. lua_unlock(L);
  516. }
  517. /*
  518. ** miscellaneous functions
  519. */
  520. LUA_API int lua_errorobj (lua_State *L) {
  521. lua_lock(L);
  522. api_checknelems(L, 1);
  523. luaD_errorobj(L, L->top - 1, LUA_ERRRUN);
  524. lua_unlock(L);
  525. return 0; /* to avoid warnings */
  526. }
  527. LUA_API int lua_next (lua_State *L, int index) {
  528. StkId t;
  529. int more;
  530. lua_lock(L);
  531. t = luaA_index(L, index);
  532. api_check(L, ttype(t) == LUA_TTABLE);
  533. more = luaH_next(L, hvalue(t), L->top - 1);
  534. if (more) {
  535. api_incr_top(L);
  536. }
  537. else /* no more elements */
  538. L->top -= 1; /* remove key */
  539. lua_unlock(L);
  540. return more;
  541. }
  542. LUA_API int lua_getn (lua_State *L, int index) {
  543. StkId t;
  544. const TObject *value;
  545. int n;
  546. lua_lock(L);
  547. t = luaA_index(L, index);
  548. api_check(L, ttype(t) == LUA_TTABLE);
  549. value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */
  550. if (ttype(value) == LUA_TNUMBER)
  551. lua_number2int(n, nvalue(value));
  552. else {
  553. Node *nd;
  554. Table *a = hvalue(t);
  555. lua_Number max = 0;
  556. int i;
  557. i = sizearray(a);
  558. while (i--) {
  559. if (ttype(&a->array[i]) != LUA_TNIL)
  560. break;
  561. }
  562. max = i+1;
  563. i = sizenode(a);
  564. nd = a->node;
  565. while (i--) {
  566. if (ttype(key(nd)) == LUA_TNUMBER &&
  567. ttype(val(nd)) != LUA_TNIL &&
  568. nvalue(key(nd)) > max)
  569. max = nvalue(key(nd));
  570. nd++;
  571. }
  572. lua_number2int(n, max);
  573. }
  574. lua_unlock(L);
  575. return n;
  576. }
  577. LUA_API void lua_concat (lua_State *L, int n) {
  578. lua_lock(L);
  579. api_checknelems(L, n);
  580. if (n >= 2) {
  581. luaV_strconc(L, n, L->top - L->ci->base - 1);
  582. L->top -= (n-1);
  583. luaC_checkGC(L);
  584. }
  585. else if (n == 0) { /* push empty string */
  586. setsvalue(L->top, luaS_newlstr(L, NULL, 0));
  587. api_incr_top(L);
  588. }
  589. /* else n == 1; nothing to do */
  590. lua_unlock(L);
  591. }
  592. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  593. Udata *u;
  594. lua_lock(L);
  595. u = luaS_newudata(L, size);
  596. setuvalue(L->top, u);
  597. api_incr_top(L);
  598. lua_unlock(L);
  599. return u + 1;
  600. }
  601. LUA_API int lua_pushupvalues (lua_State *L) {
  602. TObject *func;
  603. int n, i;
  604. lua_lock(L);
  605. func = (L->ci->base - 1);
  606. api_check(L, iscfunction(func));
  607. n = clvalue(func)->c.nupvalues;
  608. luaD_checkstack(L, n + LUA_MINSTACK);
  609. for (i=0; i<n; i++) {
  610. setobj(L->top, &clvalue(func)->c.upvalue[i]);
  611. L->top++;
  612. }
  613. lua_unlock(L);
  614. return n;
  615. }
  616. /*
  617. ** {======================================================
  618. ** compatibility code
  619. ** =======================================================
  620. */
  621. static void callalert (lua_State *L, int status) {
  622. if (status != 0) {
  623. int top = lua_gettop(L);
  624. lua_getglobal(L, "_ALERT");
  625. lua_insert(L, -2);
  626. lua_pcall(L, 1, 0, 0);
  627. lua_settop(L, top-1);
  628. }
  629. }
  630. LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
  631. int status;
  632. int errpos = lua_gettop(L) - nargs;
  633. lua_getglobal(L, "_ERRORMESSAGE");
  634. lua_insert(L, errpos); /* put below function and args */
  635. status = lua_pcall(L, nargs, nresults, errpos);
  636. lua_remove(L, errpos);
  637. callalert(L, status);
  638. return status;
  639. }
  640. static int aux_do (lua_State *L, int status) {
  641. if (status == 0) { /* parse OK? */
  642. int err = lua_gettop(L);
  643. lua_getglobal(L, "_ERRORMESSAGE");
  644. lua_insert(L, err);
  645. status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
  646. lua_remove(L, err); /* remove error function */
  647. }
  648. callalert(L, status);
  649. return status;
  650. }
  651. LUA_API int lua_dofile (lua_State *L, const char *filename) {
  652. return aux_do(L, lua_loadfile(L, filename));
  653. }
  654. LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  655. const char *name) {
  656. return aux_do(L, lua_loadbuffer(L, buff, size, name));
  657. }
  658. LUA_API int lua_dostring (lua_State *L, const char *str) {
  659. return lua_dobuffer(L, str, strlen(str), str);
  660. }
  661. /* }====================================================== */