lapi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. ** $Id: lapi.c,v 1.148 2001/07/12 18:11:58 roberto Exp roberto $
  3. ** Lua API
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #define LUA_PRIVATE
  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 "lvm.h"
  20. const l_char lua_ident[] = l_s("$Lua: ") l_s(LUA_VERSION) l_s(" ")
  21. l_s(LUA_COPYRIGHT) l_s(" $\n") l_s("$Authors: ") l_s(LUA_AUTHORS) l_s(" $");
  22. #ifndef api_check
  23. #define api_check(L, o) /* nothing */
  24. #endif
  25. #define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->ci->base))
  26. #define api_incr_top(L) incr_top
  27. TObject *luaA_index (lua_State *L, int index) {
  28. if (index > 0) {
  29. api_check(L, index <= L->top - L->ci->base);
  30. return L->ci->base+index-1;
  31. }
  32. else {
  33. api_check(L, index != 0 && -index <= L->top - L->ci->base);
  34. return L->top+index;
  35. }
  36. }
  37. static TObject *luaA_indexAcceptable (lua_State *L, int index) {
  38. if (index > 0) {
  39. TObject *o = L->ci->base+(index-1);
  40. api_check(L, index <= L->stack_last - L->ci->base);
  41. if (o >= L->top) return NULL;
  42. else return o;
  43. }
  44. else {
  45. api_check(L, index != 0 && -index <= L->top - L->ci->base);
  46. return L->top+index;
  47. }
  48. }
  49. void luaA_pushobject (lua_State *L, const TObject *o) {
  50. setobj(L->top, o);
  51. incr_top;
  52. }
  53. LUA_API int lua_stackspace (lua_State *L) {
  54. return (L->stack_last - L->top);
  55. }
  56. /*
  57. ** basic stack manipulation
  58. */
  59. LUA_API int lua_gettop (lua_State *L) {
  60. return (L->top - L->ci->base);
  61. }
  62. LUA_API void lua_settop (lua_State *L, int index) {
  63. lua_lock(L);
  64. if (index >= 0) {
  65. api_check(L, index <= L->stack_last - L->ci->base);
  66. luaD_adjusttop(L, L->ci->base+index);
  67. }
  68. else {
  69. api_check(L, -(index+1) <= (L->top - L->ci->base));
  70. L->top += index+1; /* `subtract' index (index is negative) */
  71. }
  72. lua_unlock(L);
  73. }
  74. LUA_API void lua_remove (lua_State *L, int index) {
  75. StkId p;
  76. lua_lock(L);
  77. p = luaA_index(L, index);
  78. while (++p < L->top) setobj(p-1, p);
  79. L->top--;
  80. lua_unlock(L);
  81. }
  82. LUA_API void lua_insert (lua_State *L, int index) {
  83. StkId p;
  84. StkId q;
  85. lua_lock(L);
  86. p = luaA_index(L, index);
  87. for (q = L->top; q>p; q--) setobj(q, q-1);
  88. setobj(p, L->top);
  89. lua_unlock(L);
  90. }
  91. LUA_API void lua_pushvalue (lua_State *L, int index) {
  92. lua_lock(L);
  93. setobj(L->top, luaA_index(L, index));
  94. api_incr_top(L);
  95. lua_unlock(L);
  96. }
  97. /*
  98. ** access functions (stack -> C)
  99. */
  100. LUA_API int lua_rawtag (lua_State *L, int index) {
  101. StkId o = luaA_indexAcceptable(L, index);
  102. return (o == NULL) ? LUA_TNONE : ttype(o);
  103. }
  104. LUA_API const l_char *lua_type (lua_State *L, int index) {
  105. StkId o;
  106. const l_char *type;
  107. lua_lock(L);
  108. o = luaA_indexAcceptable(L, index);
  109. type = (o == NULL) ? l_s("no value") : luaT_typename(G(L), o);
  110. lua_unlock(L);
  111. return type;
  112. }
  113. LUA_API int lua_iscfunction (lua_State *L, int index) {
  114. StkId o = luaA_indexAcceptable(L, index);
  115. return (o == NULL) ? 0 : iscfunction(o);
  116. }
  117. LUA_API int lua_isnumber (lua_State *L, int index) {
  118. TObject n;
  119. TObject *o = luaA_indexAcceptable(L, index);
  120. return (o != NULL && (ttype(o) == LUA_TNUMBER || luaV_tonumber(o, &n)));
  121. }
  122. LUA_API int lua_isstring (lua_State *L, int index) {
  123. int t = lua_rawtag(L, index);
  124. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  125. }
  126. LUA_API int lua_tag (lua_State *L, int index) {
  127. StkId o;
  128. int i;
  129. lua_lock(L); /* other thread could be changing the tag */
  130. o = luaA_indexAcceptable(L, index);
  131. i = (o == NULL) ? LUA_NOTAG : luaT_tag(o);
  132. lua_unlock(L);
  133. return i;
  134. }
  135. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  136. StkId o1 = luaA_indexAcceptable(L, index1);
  137. StkId o2 = luaA_indexAcceptable(L, index2);
  138. return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
  139. : luaO_equalObj(o1, o2);
  140. }
  141. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  142. StkId o1, o2;
  143. int i;
  144. lua_lock(L); /* may call tag method */
  145. o1 = luaA_indexAcceptable(L, index1);
  146. o2 = luaA_indexAcceptable(L, index2);
  147. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  148. : luaV_lessthan(L, o1, o2);
  149. lua_unlock(L);
  150. return i;
  151. }
  152. LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
  153. TObject n;
  154. const TObject *o = luaA_indexAcceptable(L, index);
  155. if (o != NULL &&
  156. (ttype(o) == LUA_TNUMBER || (o = luaV_tonumber(o, &n)) != NULL))
  157. return nvalue(o);
  158. else
  159. return 0;
  160. }
  161. LUA_API const l_char *lua_tostring (lua_State *L, int index) {
  162. StkId o = luaA_indexAcceptable(L, index);
  163. if (o == NULL)
  164. return NULL;
  165. else if (ttype(o) == LUA_TSTRING)
  166. return svalue(o);
  167. else {
  168. const l_char *s;
  169. lua_lock(L); /* `luaV_tostring' may create a new string */
  170. s = (luaV_tostring(L, o) == 0) ? svalue(o) : NULL;
  171. lua_unlock(L);
  172. return s;
  173. }
  174. }
  175. LUA_API size_t lua_strlen (lua_State *L, int index) {
  176. StkId o = luaA_indexAcceptable(L, index);
  177. if (o == NULL)
  178. return 0;
  179. else if (ttype(o) == LUA_TSTRING)
  180. return tsvalue(o)->tsv.len;
  181. else {
  182. size_t l;
  183. lua_lock(L); /* `luaV_tostring' may create a new string */
  184. l = (luaV_tostring(L, o) == 0) ? tsvalue(o)->tsv.len : 0;
  185. lua_unlock(L);
  186. return l;
  187. }
  188. }
  189. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  190. StkId o = luaA_indexAcceptable(L, index);
  191. return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
  192. }
  193. LUA_API void *lua_touserdata (lua_State *L, int index) {
  194. StkId o = luaA_indexAcceptable(L, index);
  195. return (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL : uvalue(o)->uv.value;
  196. }
  197. LUA_API const void *lua_topointer (lua_State *L, int index) {
  198. StkId o = luaA_indexAcceptable(L, index);
  199. if (o == NULL) return NULL;
  200. else {
  201. switch (ttype(o)) {
  202. case LUA_TTABLE: return hvalue(o);
  203. case LUA_TFUNCTION: return clvalue(o);
  204. default: return NULL;
  205. }
  206. }
  207. }
  208. /*
  209. ** push functions (C -> stack)
  210. */
  211. LUA_API void lua_pushnil (lua_State *L) {
  212. lua_lock(L);
  213. setnilvalue(L->top);
  214. api_incr_top(L);
  215. lua_unlock(L);
  216. }
  217. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  218. lua_lock(L);
  219. setnvalue(L->top, n);
  220. api_incr_top(L);
  221. lua_unlock(L);
  222. }
  223. LUA_API void lua_pushlstring (lua_State *L, const l_char *s, size_t len) {
  224. lua_lock(L);
  225. setsvalue(L->top, luaS_newlstr(L, s, len));
  226. api_incr_top(L);
  227. lua_unlock(L);
  228. }
  229. LUA_API void lua_pushstring (lua_State *L, const l_char *s) {
  230. if (s == NULL)
  231. lua_pushnil(L);
  232. else
  233. lua_pushlstring(L, s, strlen(s));
  234. }
  235. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  236. lua_lock(L);
  237. api_checknelems(L, n);
  238. luaV_Cclosure(L, fn, n);
  239. lua_unlock(L);
  240. }
  241. /*
  242. ** get functions (Lua -> stack)
  243. */
  244. LUA_API void lua_getglobal (lua_State *L, const l_char *name) {
  245. lua_lock(L);
  246. luaV_getglobal(L, luaS_new(L, name), L->top);
  247. api_incr_top(L);
  248. lua_unlock(L);
  249. }
  250. LUA_API void lua_gettable (lua_State *L, int index) {
  251. StkId t;
  252. lua_lock(L);
  253. t = luaA_index(L, index);
  254. luaV_gettable(L, t, L->top-1, L->top-1);
  255. lua_unlock(L);
  256. }
  257. LUA_API void lua_rawget (lua_State *L, int index) {
  258. StkId t;
  259. lua_lock(L);
  260. t = luaA_index(L, index);
  261. api_check(L, ttype(t) == LUA_TTABLE);
  262. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  263. lua_unlock(L);
  264. }
  265. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  266. StkId o;
  267. lua_lock(L);
  268. o = luaA_index(L, index);
  269. api_check(L, ttype(o) == LUA_TTABLE);
  270. setobj(L->top, luaH_getnum(hvalue(o), n));
  271. api_incr_top(L);
  272. lua_unlock(L);
  273. }
  274. LUA_API void lua_getglobals (lua_State *L) {
  275. lua_lock(L);
  276. sethvalue(L->top, L->gt);
  277. api_incr_top(L);
  278. lua_unlock(L);
  279. }
  280. LUA_API int lua_getref (lua_State *L, int ref) {
  281. int status;
  282. lua_lock(L);
  283. if (ref == LUA_REFNIL) {
  284. setnilvalue(L->top);
  285. status = 1;
  286. }
  287. else {
  288. setobj(L->top, luaH_getnum(G(L)->weakregistry, ref));
  289. status = (ttype(L->top) != LUA_TNIL);
  290. }
  291. if (status)
  292. api_incr_top(L);
  293. lua_unlock(L);
  294. return status;
  295. }
  296. LUA_API void lua_newtable (lua_State *L) {
  297. lua_lock(L);
  298. sethvalue(L->top, luaH_new(L, 0));
  299. api_incr_top(L);
  300. lua_unlock(L);
  301. }
  302. LUA_API void lua_getregistry (lua_State *L) {
  303. lua_lock(L);
  304. sethvalue(L->top, G(L)->registry);
  305. api_incr_top(L);
  306. lua_unlock(L);
  307. }
  308. LUA_API void lua_getweakregistry (lua_State *L) {
  309. lua_lock(L);
  310. sethvalue(L->top, G(L)->weakregistry);
  311. api_incr_top(L);
  312. lua_unlock(L);
  313. }
  314. /*
  315. ** set functions (stack -> Lua)
  316. */
  317. LUA_API void lua_setglobal (lua_State *L, const l_char *name) {
  318. lua_lock(L);
  319. api_checknelems(L, 1);
  320. luaV_setglobal(L, luaS_new(L, name), L->top - 1);
  321. L->top--; /* remove element from the top */
  322. lua_unlock(L);
  323. }
  324. LUA_API void lua_settable (lua_State *L, int index) {
  325. StkId t;
  326. lua_lock(L);
  327. api_checknelems(L, 2);
  328. t = luaA_index(L, index);
  329. luaV_settable(L, t, L->top - 2, L->top - 1);
  330. L->top -= 2; /* pop index and value */
  331. lua_unlock(L);
  332. }
  333. LUA_API void lua_rawset (lua_State *L, int index) {
  334. StkId t;
  335. lua_lock(L);
  336. api_checknelems(L, 2);
  337. t = luaA_index(L, index);
  338. api_check(L, ttype(t) == LUA_TTABLE);
  339. setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
  340. L->top -= 2;
  341. lua_unlock(L);
  342. }
  343. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  344. StkId o;
  345. lua_lock(L);
  346. api_checknelems(L, 1);
  347. o = luaA_index(L, index);
  348. api_check(L, ttype(o) == LUA_TTABLE);
  349. setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
  350. L->top--;
  351. lua_unlock(L);
  352. }
  353. LUA_API void lua_setglobals (lua_State *L) {
  354. StkId newtable;
  355. lua_lock(L);
  356. api_checknelems(L, 1);
  357. newtable = --L->top;
  358. api_check(L, ttype(newtable) == LUA_TTABLE);
  359. L->gt = hvalue(newtable);
  360. lua_unlock(L);
  361. }
  362. LUA_API int lua_ref (lua_State *L, int lock) {
  363. int ref;
  364. if (lua_isnil(L, -1)) {
  365. lua_pop(L, 1);
  366. ref = LUA_REFNIL;
  367. }
  368. else {
  369. lua_getweakregistry(L);
  370. ref = lua_getn(L, -1) + 1;
  371. lua_pushvalue(L, -2);
  372. lua_rawseti(L, -2, ref);
  373. if (lock) {
  374. lua_getregistry(L);
  375. lua_pushvalue(L, -3);
  376. lua_rawseti(L, -2, ref);
  377. lua_pop(L, 1); /* remove registry */
  378. }
  379. lua_pushliteral(L, l_s("n"));
  380. lua_pushnumber(L, ref);
  381. lua_settable(L, -3);
  382. lua_pop(L, 2);
  383. }
  384. return ref;
  385. }
  386. /*
  387. ** `do' functions (run Lua code)
  388. */
  389. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  390. StkId func;
  391. lua_lock(L);
  392. api_checknelems(L, nargs+1);
  393. func = L->top - (nargs+1);
  394. luaD_call(L, func);
  395. if (nresults != LUA_MULTRET)
  396. luaD_adjusttop(L, func + nresults);
  397. lua_unlock(L);
  398. }
  399. LUA_API int lua_dofile (lua_State *L, const l_char *filename) {
  400. int status;
  401. status = lua_loadfile(L, filename);
  402. if (status == 0) /* parse OK? */
  403. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  404. return status;
  405. }
  406. LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size,
  407. const l_char *name) {
  408. int status;
  409. status = lua_loadbuffer(L, buff, size, name);
  410. if (status == 0) /* parse OK? */
  411. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  412. return status;
  413. }
  414. LUA_API int lua_dostring (lua_State *L, const l_char *str) {
  415. return lua_dobuffer(L, str, strlen(str), str);
  416. }
  417. /*
  418. ** Garbage-collection functions
  419. */
  420. /* GC values are expressed in Kbytes: #bytes/2^10 */
  421. #define GCscale(x) ((int)((x)>>10))
  422. #define GCunscale(x) ((lu_mem)(x)<<10)
  423. LUA_API int lua_getgcthreshold (lua_State *L) {
  424. int threshold;
  425. lua_lock(L);
  426. threshold = GCscale(G(L)->GCthreshold);
  427. lua_unlock(L);
  428. return threshold;
  429. }
  430. LUA_API int lua_getgccount (lua_State *L) {
  431. int count;
  432. lua_lock(L);
  433. count = GCscale(G(L)->nblocks);
  434. lua_unlock(L);
  435. return count;
  436. }
  437. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  438. lua_lock(L);
  439. if (newthreshold > GCscale(ULONG_MAX))
  440. G(L)->GCthreshold = ULONG_MAX;
  441. else
  442. G(L)->GCthreshold = GCunscale(newthreshold);
  443. luaC_checkGC(L);
  444. lua_unlock(L);
  445. }
  446. /*
  447. ** miscellaneous functions
  448. */
  449. LUA_API int lua_newtype (lua_State *L, const l_char *name, int basictype) {
  450. int tag;
  451. lua_lock(L);
  452. if (basictype != LUA_TNONE &&
  453. basictype != LUA_TTABLE &&
  454. basictype != LUA_TUSERDATA)
  455. luaO_verror(L, l_s("invalid basic type (%d) for new type"), basictype);
  456. tag = luaT_newtag(L, name, basictype);
  457. if (tag == LUA_TNONE)
  458. luaO_verror(L, l_s("type name '%.30s' already exists"), name);
  459. lua_unlock(L);
  460. return tag;
  461. }
  462. LUA_API int lua_name2tag (lua_State *L, const l_char *name) {
  463. int tag;
  464. const TObject *v;
  465. lua_lock(L);
  466. v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
  467. if (ttype(v) == LUA_TNIL)
  468. tag = LUA_TNONE;
  469. else {
  470. lua_assert(ttype(v) == LUA_TNUMBER);
  471. tag = (int)nvalue(v);
  472. }
  473. lua_unlock(L);
  474. return tag;
  475. }
  476. LUA_API const l_char *lua_tag2name (lua_State *L, int tag) {
  477. const l_char *s;
  478. lua_lock(L);
  479. s = (tag == LUA_TNONE) ? l_s("no value") : typenamebytag(G(L), tag);
  480. lua_unlock(L);
  481. return s;
  482. }
  483. LUA_API void lua_settag (lua_State *L, int tag) {
  484. int basictype;
  485. lua_lock(L);
  486. api_checknelems(L, 1);
  487. if (tag < 0 || tag >= G(L)->ntag)
  488. luaO_verror(L, l_s("%d is not a valid tag"), tag);
  489. basictype = G(L)->TMtable[tag].basictype;
  490. if (basictype != LUA_TNONE && basictype != ttype(L->top-1))
  491. luaO_verror(L, l_s("tag %d can only be used for type '%.20s'"), tag,
  492. typenamebytag(G(L), basictype));
  493. switch (ttype(L->top-1)) {
  494. case LUA_TTABLE:
  495. hvalue(L->top-1)->htag = tag;
  496. break;
  497. case LUA_TUSERDATA:
  498. uvalue(L->top-1)->uv.tag = tag;
  499. break;
  500. default:
  501. luaO_verror(L, l_s("cannot change the tag of a %.20s"),
  502. luaT_typename(G(L), L->top-1));
  503. }
  504. lua_unlock(L);
  505. }
  506. LUA_API void lua_error (lua_State *L, const l_char *s) {
  507. lua_lock(L);
  508. luaD_error(L, s);
  509. lua_unlock(L);
  510. }
  511. LUA_API void lua_unref (lua_State *L, int ref) {
  512. if (ref >= 0) {
  513. lua_getregistry(L);
  514. lua_pushnil(L);
  515. lua_rawseti(L, -2, ref);
  516. lua_getweakregistry(L);
  517. lua_pushnil(L);
  518. lua_rawseti(L, -2, ref);
  519. lua_pop(L, 2); /* remove both registries */
  520. }
  521. }
  522. LUA_API int lua_next (lua_State *L, int index) {
  523. StkId t;
  524. Node *n;
  525. int more;
  526. lua_lock(L);
  527. t = luaA_index(L, index);
  528. api_check(L, ttype(t) == LUA_TTABLE);
  529. n = luaH_next(L, hvalue(t), luaA_index(L, -1));
  530. if (n) {
  531. setobj(L->top-1, key(n));
  532. setobj(L->top, val(n));
  533. api_incr_top(L);
  534. more = 1;
  535. }
  536. else { /* no more elements */
  537. L->top -= 1; /* remove key */
  538. more = 0;
  539. }
  540. lua_unlock(L);
  541. return more;
  542. }
  543. LUA_API int lua_getn (lua_State *L, int index) {
  544. StkId t;
  545. const TObject *value;
  546. int n;
  547. lua_lock(L);
  548. t = luaA_index(L, index);
  549. api_check(L, ttype(t) == LUA_TTABLE);
  550. value = luaH_getstr(hvalue(t), luaS_newliteral(L, l_s("n"))); /* = t.n */
  551. if (ttype(value) == LUA_TNUMBER)
  552. n = (int)nvalue(value);
  553. else {
  554. lua_Number max = 0;
  555. int i = hvalue(t)->size;
  556. Node *nd = hvalue(t)->node;
  557. while (i--) {
  558. if (ttype(key(nd)) == LUA_TNUMBER &&
  559. ttype(val(nd)) != LUA_TNIL &&
  560. nvalue(key(nd)) > max)
  561. max = nvalue(key(nd));
  562. nd++;
  563. }
  564. n = (int)max;
  565. }
  566. lua_unlock(L);
  567. return n;
  568. }
  569. LUA_API void lua_concat (lua_State *L, int n) {
  570. lua_lock(L);
  571. api_checknelems(L, n);
  572. if (n >= 2) {
  573. luaV_strconc(L, n, L->top);
  574. L->top -= (n-1);
  575. luaC_checkGC(L);
  576. }
  577. else if (n == 0) { /* push null string */
  578. setsvalue(L->top, luaS_newlstr(L, NULL, 0));
  579. api_incr_top(L);
  580. }
  581. /* else n == 1; nothing to do */
  582. lua_unlock(L);
  583. }
  584. static Udata *pushnewudata (lua_State *L, size_t size) {
  585. Udata *u = luaS_newudata(L, size);
  586. setuvalue(L->top, u);
  587. api_incr_top(L);
  588. return uvalue(L->top-1);
  589. }
  590. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  591. Udata *u;
  592. void *p;
  593. lua_lock(L);
  594. u = pushnewudata(L, size);
  595. p = u->uv.value;
  596. lua_unlock(L);
  597. return p;
  598. }
  599. LUA_API void lua_newuserdatabox (lua_State *L, void *p) {
  600. Udata *u;
  601. lua_lock(L);
  602. u = pushnewudata(L, 0);
  603. u->uv.value = p;
  604. lua_unlock(L);
  605. }
  606. LUA_API int lua_getweakmode (lua_State *L, int index) {
  607. StkId t;
  608. int mode;
  609. lua_lock(L);
  610. t = luaA_index(L, index);
  611. api_check(L, ttype(t) == LUA_TTABLE);
  612. mode = hvalue(t)->weakmode;
  613. lua_unlock(L);
  614. return mode;
  615. }
  616. LUA_API void lua_setweakmode (lua_State *L, int mode) {
  617. lua_lock(L);
  618. api_check(L, ttype(L->top-1) == LUA_TTABLE);
  619. hvalue(L->top-1)->weakmode = mode;
  620. lua_unlock(L);
  621. }