lapi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. ** $Id: lapi.c,v 1.188 2002/05/06 15:51:41 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_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  273. Closure *cl;
  274. lua_lock(L);
  275. api_checknelems(L, n);
  276. cl = luaF_newCclosure(L, n);
  277. cl->c.f = fn;
  278. L->top -= n;
  279. while (n--)
  280. setobj(&cl->c.upvalue[n], L->top+n);
  281. setclvalue(L->top, cl);
  282. api_incr_top(L);
  283. lua_unlock(L);
  284. }
  285. LUA_API void lua_pushboolean (lua_State *L, int b) {
  286. lua_lock(L);
  287. setbvalue(L->top, (b != 0)); /* ensure that true is 1 */
  288. api_incr_top(L);
  289. lua_unlock(L);
  290. }
  291. LUA_API void lua_pushudataval (lua_State *L, void *p) {
  292. lua_lock(L);
  293. setpvalue(L->top, p);
  294. api_incr_top(L);
  295. lua_unlock(L);
  296. }
  297. /*
  298. ** get functions (Lua -> stack)
  299. */
  300. LUA_API void lua_gettable (lua_State *L, int index) {
  301. StkId t;
  302. lua_lock(L);
  303. t = luaA_index(L, index);
  304. luaV_gettable(L, t, L->top-1, L->top-1);
  305. lua_unlock(L);
  306. }
  307. LUA_API void lua_rawget (lua_State *L, int index) {
  308. StkId t;
  309. lua_lock(L);
  310. t = luaA_index(L, index);
  311. api_check(L, ttype(t) == LUA_TTABLE);
  312. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  313. lua_unlock(L);
  314. }
  315. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  316. StkId o;
  317. lua_lock(L);
  318. o = luaA_index(L, index);
  319. api_check(L, ttype(o) == LUA_TTABLE);
  320. setobj(L->top, luaH_getnum(hvalue(o), n));
  321. api_incr_top(L);
  322. lua_unlock(L);
  323. }
  324. LUA_API void lua_newtable (lua_State *L) {
  325. lua_lock(L);
  326. sethvalue(L->top, luaH_new(L, 0, 0));
  327. api_incr_top(L);
  328. lua_unlock(L);
  329. }
  330. LUA_API int lua_getmetatable (lua_State *L, int objindex) {
  331. StkId obj;
  332. Table *mt;
  333. int res;
  334. lua_lock(L);
  335. obj = luaA_indexAcceptable(L, objindex);
  336. switch (ttype(obj)) {
  337. case LUA_TTABLE:
  338. mt = hvalue(obj)->metatable;
  339. break;
  340. case LUA_TUSERDATA:
  341. mt = uvalue(obj)->uv.metatable;
  342. break;
  343. default:
  344. mt = hvalue(defaultmeta(L));
  345. }
  346. if (mt == hvalue(defaultmeta(L)))
  347. res = 0;
  348. else {
  349. sethvalue(L->top, mt);
  350. api_incr_top(L);
  351. res = 1;
  352. }
  353. lua_unlock(L);
  354. return res;
  355. }
  356. /*
  357. ** set functions (stack -> Lua)
  358. */
  359. LUA_API void lua_settable (lua_State *L, int index) {
  360. StkId t;
  361. lua_lock(L);
  362. api_checknelems(L, 2);
  363. t = luaA_index(L, index);
  364. luaV_settable(L, t, L->top - 2, L->top - 1);
  365. L->top -= 2; /* pop index and value */
  366. lua_unlock(L);
  367. }
  368. LUA_API void lua_rawset (lua_State *L, int index) {
  369. StkId t;
  370. lua_lock(L);
  371. api_checknelems(L, 2);
  372. t = luaA_index(L, index);
  373. api_check(L, ttype(t) == LUA_TTABLE);
  374. luaH_set(L, hvalue(t), L->top-2, L->top-1);
  375. L->top -= 2;
  376. lua_unlock(L);
  377. }
  378. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  379. StkId o;
  380. lua_lock(L);
  381. api_checknelems(L, 1);
  382. o = luaA_index(L, index);
  383. api_check(L, ttype(o) == LUA_TTABLE);
  384. luaH_setnum(L, hvalue(o), n, L->top-1);
  385. L->top--;
  386. lua_unlock(L);
  387. }
  388. LUA_API void lua_setmetatable (lua_State *L, int objindex) {
  389. StkId obj, mt;
  390. lua_lock(L);
  391. api_checknelems(L, 1);
  392. obj = luaA_index(L, objindex);
  393. mt = --L->top;
  394. if (ttype(mt) == LUA_TNIL)
  395. mt = defaultmeta(L);
  396. api_check(L, ttype(mt) == LUA_TTABLE);
  397. switch (ttype(obj)) {
  398. case LUA_TTABLE:
  399. hvalue(obj)->metatable = hvalue(mt);
  400. break;
  401. case LUA_TUSERDATA:
  402. uvalue(obj)->uv.metatable = hvalue(mt);
  403. break;
  404. default:
  405. luaO_verror(L, "cannot change the meta table of a %.20s",
  406. luaT_typenames[ttype(obj)]);
  407. }
  408. lua_unlock(L);
  409. }
  410. /*
  411. ** `load' and `call' functions (run Lua code)
  412. */
  413. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  414. StkId func;
  415. lua_lock(L);
  416. api_checknelems(L, nargs+1);
  417. func = L->top - (nargs+1);
  418. luaD_call(L, func, nresults);
  419. lua_unlock(L);
  420. }
  421. LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errf) {
  422. int status;
  423. const TObject *err;
  424. lua_lock(L);
  425. err = (errf == 0) ? &luaO_nilobject : luaA_index(L, errf);
  426. status = luaD_pcall(L, nargs, nresults, err);
  427. lua_unlock(L);
  428. return status;
  429. }
  430. static int errfile (lua_State *L, const char *filename) {
  431. if (filename == NULL) filename = "stdin";
  432. lua_pushliteral(L, "cannot read ");
  433. lua_pushstring(L, filename);
  434. lua_pushliteral(L, ": ");
  435. lua_pushstring(L, lua_fileerror);
  436. lua_concat(L, 4);
  437. return LUA_ERRFILE;
  438. }
  439. LUA_API int lua_loadfile (lua_State *L, const char *filename) {
  440. ZIO z;
  441. const char *luafname; /* name used by lua */
  442. int status;
  443. int bin; /* flag for file mode */
  444. int nlevel; /* level on the stack of filename */
  445. FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
  446. if (f == NULL) return errfile(L, filename); /* unable to open file */
  447. bin = (ungetc(getc(f), f) == LUA_SIGNATURE[0]);
  448. if (bin && f != stdin) {
  449. fclose(f);
  450. f = fopen(filename, "rb"); /* reopen in binary mode */
  451. if (f == NULL) return errfile(L, filename); /* unable to reopen file */
  452. }
  453. if (filename == NULL)
  454. lua_pushstring(L, "=stdin");
  455. else {
  456. lua_pushliteral(L, "@");
  457. lua_pushstring(L, filename);
  458. lua_concat(L, 2);
  459. }
  460. nlevel = lua_gettop(L);
  461. luafname = lua_tostring(L, -1); /* luafname = `@'..filename */
  462. luaZ_Fopen(&z, f, luafname);
  463. status = luaD_protectedparser(L, &z, bin);
  464. if (ferror(f))
  465. return errfile(L, filename);
  466. lua_remove(L, nlevel); /* remove filename */
  467. if (f != stdin)
  468. fclose(f);
  469. return status;
  470. }
  471. LUA_API int lua_loadbuffer (lua_State *L, const char *buff, size_t size,
  472. const char *name) {
  473. ZIO z;
  474. if (!name) name = "?";
  475. luaZ_mopen(&z, buff, size, name);
  476. return luaD_protectedparser(L, &z, buff[0]==LUA_SIGNATURE[0]);
  477. }
  478. /*
  479. ** Garbage-collection functions
  480. */
  481. /* GC values are expressed in Kbytes: #bytes/2^10 */
  482. #define GCscale(x) (cast(int, (x)>>10))
  483. #define GCunscale(x) (cast(lu_mem, (x)<<10))
  484. LUA_API int lua_getgcthreshold (lua_State *L) {
  485. int threshold;
  486. lua_lock(L);
  487. threshold = GCscale(G(L)->GCthreshold);
  488. lua_unlock(L);
  489. return threshold;
  490. }
  491. LUA_API int lua_getgccount (lua_State *L) {
  492. int count;
  493. lua_lock(L);
  494. count = GCscale(G(L)->nblocks);
  495. lua_unlock(L);
  496. return count;
  497. }
  498. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  499. lua_lock(L);
  500. if (newthreshold > GCscale(ULONG_MAX))
  501. G(L)->GCthreshold = ULONG_MAX;
  502. else
  503. G(L)->GCthreshold = GCunscale(newthreshold);
  504. luaC_checkGC(L);
  505. lua_unlock(L);
  506. }
  507. /*
  508. ** miscellaneous functions
  509. */
  510. LUA_API int lua_errorobj (lua_State *L) {
  511. lua_lock(L);
  512. api_checknelems(L, 1);
  513. luaD_errorobj(L, L->top - 1, LUA_ERRRUN);
  514. lua_unlock(L);
  515. return 0; /* to avoid warnings */
  516. }
  517. LUA_API int lua_next (lua_State *L, int index) {
  518. StkId t;
  519. int more;
  520. lua_lock(L);
  521. t = luaA_index(L, index);
  522. api_check(L, ttype(t) == LUA_TTABLE);
  523. more = luaH_next(L, hvalue(t), L->top - 1);
  524. if (more) {
  525. api_incr_top(L);
  526. }
  527. else /* no more elements */
  528. L->top -= 1; /* remove key */
  529. lua_unlock(L);
  530. return more;
  531. }
  532. LUA_API int lua_getn (lua_State *L, int index) {
  533. StkId t;
  534. const TObject *value;
  535. int n;
  536. lua_lock(L);
  537. t = luaA_index(L, index);
  538. api_check(L, ttype(t) == LUA_TTABLE);
  539. value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */
  540. if (ttype(value) == LUA_TNUMBER)
  541. lua_number2int(n, nvalue(value));
  542. else {
  543. Node *nd;
  544. Table *a = hvalue(t);
  545. lua_Number max = 0;
  546. int i;
  547. i = sizearray(a);
  548. while (i--) {
  549. if (ttype(&a->array[i]) != LUA_TNIL)
  550. break;
  551. }
  552. max = i+1;
  553. i = sizenode(a);
  554. nd = a->node;
  555. while (i--) {
  556. if (ttype(key(nd)) == LUA_TNUMBER &&
  557. ttype(val(nd)) != LUA_TNIL &&
  558. nvalue(key(nd)) > max)
  559. max = nvalue(key(nd));
  560. nd++;
  561. }
  562. lua_number2int(n, max);
  563. }
  564. lua_unlock(L);
  565. return n;
  566. }
  567. LUA_API void lua_concat (lua_State *L, int n) {
  568. lua_lock(L);
  569. api_checknelems(L, n);
  570. if (n >= 2) {
  571. luaV_strconc(L, n, L->top - L->ci->base - 1);
  572. L->top -= (n-1);
  573. luaC_checkGC(L);
  574. }
  575. else if (n == 0) { /* push empty string */
  576. setsvalue(L->top, luaS_newlstr(L, NULL, 0));
  577. api_incr_top(L);
  578. }
  579. /* else n == 1; nothing to do */
  580. lua_unlock(L);
  581. }
  582. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  583. Udata *u;
  584. lua_lock(L);
  585. u = luaS_newudata(L, size);
  586. setuvalue(L->top, u);
  587. api_incr_top(L);
  588. lua_unlock(L);
  589. return u + 1;
  590. }
  591. LUA_API int lua_pushupvalues (lua_State *L) {
  592. TObject *func;
  593. int n, i;
  594. lua_lock(L);
  595. func = (L->ci->base - 1);
  596. api_check(L, iscfunction(func));
  597. n = clvalue(func)->c.nupvalues;
  598. luaD_checkstack(L, n + LUA_MINSTACK);
  599. for (i=0; i<n; i++) {
  600. setobj(L->top, &clvalue(func)->c.upvalue[i]);
  601. L->top++;
  602. }
  603. lua_unlock(L);
  604. return n;
  605. }
  606. /*
  607. ** {======================================================
  608. ** compatibility code
  609. ** =======================================================
  610. */
  611. static void callalert (lua_State *L, int status) {
  612. if (status != 0) {
  613. int top = lua_gettop(L);
  614. lua_getglobal(L, "_ALERT");
  615. lua_insert(L, -2);
  616. lua_pcall(L, 1, 0, 0);
  617. lua_settop(L, top-1);
  618. }
  619. }
  620. LUA_API int lua_call (lua_State *L, int nargs, int nresults) {
  621. int status;
  622. int errpos = lua_gettop(L) - nargs;
  623. lua_getglobal(L, "_ERRORMESSAGE");
  624. lua_insert(L, errpos); /* put below function and args */
  625. status = lua_pcall(L, nargs, nresults, errpos);
  626. lua_remove(L, errpos);
  627. callalert(L, status);
  628. return status;
  629. }
  630. static int aux_do (lua_State *L, int status) {
  631. if (status == 0) { /* parse OK? */
  632. int err = lua_gettop(L);
  633. lua_getglobal(L, "_ERRORMESSAGE");
  634. lua_insert(L, err);
  635. status = lua_pcall(L, 0, LUA_MULTRET, err); /* call main */
  636. lua_remove(L, err); /* remove error function */
  637. }
  638. callalert(L, status);
  639. return status;
  640. }
  641. LUA_API int lua_dofile (lua_State *L, const char *filename) {
  642. return aux_do(L, lua_loadfile(L, filename));
  643. }
  644. LUA_API int lua_dobuffer (lua_State *L, const char *buff, size_t size,
  645. const char *name) {
  646. return aux_do(L, lua_loadbuffer(L, buff, size, name));
  647. }
  648. LUA_API int lua_dostring (lua_State *L, const char *str) {
  649. return lua_dobuffer(L, str, strlen(str), str);
  650. }
  651. /* }====================================================== */