lapi.c 17 KB

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