lapi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. ** $Id: lapi.c,v 1.146 2001/06/15 20:36:57 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_type (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_tag2name (lua_State *L, int tag) {
  105. const l_char *s;
  106. lua_lock(L);
  107. s = (tag == LUA_TNONE) ? l_s("no value") : basictypename(G(L), tag);
  108. lua_unlock(L);
  109. return s;
  110. }
  111. LUA_API const l_char *lua_xtypename (lua_State *L, int index) {
  112. StkId o;
  113. const l_char *type;
  114. lua_lock(L);
  115. o = luaA_indexAcceptable(L, index);
  116. type = (o == NULL) ? l_s("no value") : luaT_typename(G(L), o);
  117. lua_unlock(L);
  118. return type;
  119. }
  120. LUA_API int lua_iscfunction (lua_State *L, int index) {
  121. StkId o = luaA_indexAcceptable(L, index);
  122. return (o == NULL) ? 0 : iscfunction(o);
  123. }
  124. LUA_API int lua_isnumber (lua_State *L, int index) {
  125. TObject n;
  126. TObject *o = luaA_indexAcceptable(L, index);
  127. return (o != NULL && (ttype(o) == LUA_TNUMBER || luaV_tonumber(o, &n)));
  128. }
  129. LUA_API int lua_isstring (lua_State *L, int index) {
  130. int t = lua_type(L, index);
  131. return (t == LUA_TSTRING || t == LUA_TNUMBER);
  132. }
  133. LUA_API int lua_tag (lua_State *L, int index) {
  134. StkId o;
  135. int i;
  136. lua_lock(L); /* other thread could be changing the tag */
  137. o = luaA_indexAcceptable(L, index);
  138. i = (o == NULL) ? LUA_NOTAG : luaT_tag(o);
  139. lua_unlock(L);
  140. return i;
  141. }
  142. LUA_API int lua_equal (lua_State *L, int index1, int index2) {
  143. StkId o1 = luaA_indexAcceptable(L, index1);
  144. StkId o2 = luaA_indexAcceptable(L, index2);
  145. return (o1 == NULL || o2 == NULL) ? 0 /* index out of range */
  146. : luaO_equalObj(o1, o2);
  147. }
  148. LUA_API int lua_lessthan (lua_State *L, int index1, int index2) {
  149. StkId o1, o2;
  150. int i;
  151. lua_lock(L); /* may call tag method */
  152. o1 = luaA_indexAcceptable(L, index1);
  153. o2 = luaA_indexAcceptable(L, index2);
  154. i = (o1 == NULL || o2 == NULL) ? 0 /* index out-of-range */
  155. : luaV_lessthan(L, o1, o2);
  156. lua_unlock(L);
  157. return i;
  158. }
  159. LUA_API lua_Number lua_tonumber (lua_State *L, int index) {
  160. TObject n;
  161. const TObject *o = luaA_indexAcceptable(L, index);
  162. if (o != NULL &&
  163. (ttype(o) == LUA_TNUMBER || (o = luaV_tonumber(o, &n)) != NULL))
  164. return nvalue(o);
  165. else
  166. return 0;
  167. }
  168. LUA_API const l_char *lua_tostring (lua_State *L, int index) {
  169. StkId o = luaA_indexAcceptable(L, index);
  170. if (o == NULL)
  171. return NULL;
  172. else if (ttype(o) == LUA_TSTRING)
  173. return svalue(o);
  174. else {
  175. const l_char *s;
  176. lua_lock(L); /* `luaV_tostring' may create a new string */
  177. s = (luaV_tostring(L, o) == 0) ? svalue(o) : NULL;
  178. lua_unlock(L);
  179. return s;
  180. }
  181. }
  182. LUA_API size_t lua_strlen (lua_State *L, int index) {
  183. StkId o = luaA_indexAcceptable(L, index);
  184. if (o == NULL)
  185. return 0;
  186. else if (ttype(o) == LUA_TSTRING)
  187. return tsvalue(o)->tsv.len;
  188. else {
  189. size_t l;
  190. lua_lock(L); /* `luaV_tostring' may create a new string */
  191. l = (luaV_tostring(L, o) == 0) ? tsvalue(o)->tsv.len : 0;
  192. lua_unlock(L);
  193. return l;
  194. }
  195. }
  196. LUA_API lua_CFunction lua_tocfunction (lua_State *L, int index) {
  197. StkId o = luaA_indexAcceptable(L, index);
  198. return (o == NULL || !iscfunction(o)) ? NULL : clvalue(o)->f.c;
  199. }
  200. LUA_API void *lua_touserdata (lua_State *L, int index) {
  201. StkId o = luaA_indexAcceptable(L, index);
  202. return (o == NULL || ttype(o) != LUA_TUSERDATA) ? NULL : uvalue(o)->uv.value;
  203. }
  204. LUA_API const void *lua_topointer (lua_State *L, int index) {
  205. StkId o = luaA_indexAcceptable(L, index);
  206. if (o == NULL) return NULL;
  207. else {
  208. switch (ttype(o)) {
  209. case LUA_TTABLE: return hvalue(o);
  210. case LUA_TFUNCTION: return clvalue(o);
  211. default: return NULL;
  212. }
  213. }
  214. }
  215. /*
  216. ** push functions (C -> stack)
  217. */
  218. LUA_API void lua_pushnil (lua_State *L) {
  219. lua_lock(L);
  220. setnilvalue(L->top);
  221. api_incr_top(L);
  222. lua_unlock(L);
  223. }
  224. LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
  225. lua_lock(L);
  226. setnvalue(L->top, n);
  227. api_incr_top(L);
  228. lua_unlock(L);
  229. }
  230. LUA_API void lua_pushlstring (lua_State *L, const l_char *s, size_t len) {
  231. lua_lock(L);
  232. setsvalue(L->top, luaS_newlstr(L, s, len));
  233. api_incr_top(L);
  234. lua_unlock(L);
  235. }
  236. LUA_API void lua_pushstring (lua_State *L, const l_char *s) {
  237. if (s == NULL)
  238. lua_pushnil(L);
  239. else
  240. lua_pushlstring(L, s, strlen(s));
  241. }
  242. LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
  243. lua_lock(L);
  244. api_checknelems(L, n);
  245. luaV_Cclosure(L, fn, n);
  246. lua_unlock(L);
  247. }
  248. /*
  249. ** get functions (Lua -> stack)
  250. */
  251. LUA_API void lua_getglobal (lua_State *L, const l_char *name) {
  252. lua_lock(L);
  253. luaV_getglobal(L, luaS_new(L, name), L->top);
  254. api_incr_top(L);
  255. lua_unlock(L);
  256. }
  257. LUA_API void lua_gettable (lua_State *L, int index) {
  258. StkId t;
  259. lua_lock(L);
  260. t = luaA_index(L, index);
  261. luaV_gettable(L, t, L->top-1, L->top-1);
  262. lua_unlock(L);
  263. }
  264. LUA_API void lua_rawget (lua_State *L, int index) {
  265. StkId t;
  266. lua_lock(L);
  267. t = luaA_index(L, index);
  268. api_check(L, ttype(t) == LUA_TTABLE);
  269. setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1));
  270. lua_unlock(L);
  271. }
  272. LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
  273. StkId o;
  274. lua_lock(L);
  275. o = luaA_index(L, index);
  276. api_check(L, ttype(o) == LUA_TTABLE);
  277. setobj(L->top, luaH_getnum(hvalue(o), n));
  278. api_incr_top(L);
  279. lua_unlock(L);
  280. }
  281. LUA_API void lua_getglobals (lua_State *L) {
  282. lua_lock(L);
  283. sethvalue(L->top, L->gt);
  284. api_incr_top(L);
  285. lua_unlock(L);
  286. }
  287. LUA_API int lua_getref (lua_State *L, int ref) {
  288. int status;
  289. lua_lock(L);
  290. if (ref == LUA_REFNIL) {
  291. setnilvalue(L->top);
  292. status = 1;
  293. }
  294. else {
  295. setobj(L->top, luaH_getnum(G(L)->weakregistry, ref));
  296. status = (ttype(L->top) != LUA_TNIL);
  297. }
  298. if (status)
  299. api_incr_top(L);
  300. lua_unlock(L);
  301. return status;
  302. }
  303. LUA_API void lua_newtable (lua_State *L) {
  304. lua_lock(L);
  305. sethvalue(L->top, luaH_new(L, 0));
  306. api_incr_top(L);
  307. lua_unlock(L);
  308. }
  309. LUA_API void lua_getregistry (lua_State *L) {
  310. lua_lock(L);
  311. sethvalue(L->top, G(L)->registry);
  312. api_incr_top(L);
  313. lua_unlock(L);
  314. }
  315. LUA_API void lua_getweakregistry (lua_State *L) {
  316. lua_lock(L);
  317. sethvalue(L->top, G(L)->weakregistry);
  318. api_incr_top(L);
  319. lua_unlock(L);
  320. }
  321. /*
  322. ** set functions (stack -> Lua)
  323. */
  324. LUA_API void lua_setglobal (lua_State *L, const l_char *name) {
  325. lua_lock(L);
  326. api_checknelems(L, 1);
  327. luaV_setglobal(L, luaS_new(L, name), L->top - 1);
  328. L->top--; /* remove element from the top */
  329. lua_unlock(L);
  330. }
  331. LUA_API void lua_settable (lua_State *L, int index) {
  332. StkId t;
  333. lua_lock(L);
  334. api_checknelems(L, 2);
  335. t = luaA_index(L, index);
  336. luaV_settable(L, t, L->top - 2, L->top - 1);
  337. L->top -= 2; /* pop index and value */
  338. lua_unlock(L);
  339. }
  340. LUA_API void lua_rawset (lua_State *L, int index) {
  341. StkId t;
  342. lua_lock(L);
  343. api_checknelems(L, 2);
  344. t = luaA_index(L, index);
  345. api_check(L, ttype(t) == LUA_TTABLE);
  346. setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1));
  347. L->top -= 2;
  348. lua_unlock(L);
  349. }
  350. LUA_API void lua_rawseti (lua_State *L, int index, int n) {
  351. StkId o;
  352. lua_lock(L);
  353. api_checknelems(L, 1);
  354. o = luaA_index(L, index);
  355. api_check(L, ttype(o) == LUA_TTABLE);
  356. setobj(luaH_setnum(L, hvalue(o), n), (L->top-1));
  357. L->top--;
  358. lua_unlock(L);
  359. }
  360. LUA_API void lua_setglobals (lua_State *L) {
  361. StkId newtable;
  362. lua_lock(L);
  363. api_checknelems(L, 1);
  364. newtable = --L->top;
  365. api_check(L, ttype(newtable) == LUA_TTABLE);
  366. L->gt = hvalue(newtable);
  367. lua_unlock(L);
  368. }
  369. LUA_API int lua_ref (lua_State *L, int lock) {
  370. int ref;
  371. if (lua_isnil(L, -1)) {
  372. lua_pop(L, 1);
  373. ref = LUA_REFNIL;
  374. }
  375. else {
  376. lua_getweakregistry(L);
  377. ref = lua_getn(L, -1) + 1;
  378. lua_pushvalue(L, -2);
  379. lua_rawseti(L, -2, ref);
  380. if (lock) {
  381. lua_getregistry(L);
  382. lua_pushvalue(L, -3);
  383. lua_rawseti(L, -2, ref);
  384. lua_pop(L, 1); /* remove registry */
  385. }
  386. lua_pushliteral(L, "n");
  387. lua_pushnumber(L, ref);
  388. lua_settable(L, -3);
  389. lua_pop(L, 2);
  390. }
  391. return ref;
  392. }
  393. /*
  394. ** `do' functions (run Lua code)
  395. */
  396. LUA_API void lua_rawcall (lua_State *L, int nargs, int nresults) {
  397. StkId func;
  398. lua_lock(L);
  399. api_checknelems(L, nargs+1);
  400. func = L->top - (nargs+1);
  401. luaD_call(L, func);
  402. if (nresults != LUA_MULTRET)
  403. luaD_adjusttop(L, func + nresults);
  404. lua_unlock(L);
  405. }
  406. LUA_API int lua_dofile (lua_State *L, const l_char *filename) {
  407. int status;
  408. status = lua_loadfile(L, filename);
  409. if (status == 0) /* parse OK? */
  410. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  411. return status;
  412. }
  413. LUA_API int lua_dobuffer (lua_State *L, const l_char *buff, size_t size,
  414. const l_char *name) {
  415. int status;
  416. status = lua_loadbuffer(L, buff, size, name);
  417. if (status == 0) /* parse OK? */
  418. status = lua_call(L, 0, LUA_MULTRET); /* call main */
  419. return status;
  420. }
  421. LUA_API int lua_dostring (lua_State *L, const l_char *str) {
  422. return lua_dobuffer(L, str, strlen(str), str);
  423. }
  424. /*
  425. ** Garbage-collection functions
  426. */
  427. /* GC values are expressed in Kbytes: #bytes/2^10 */
  428. #define GCscale(x) ((int)((x)>>10))
  429. #define GCunscale(x) ((lu_mem)(x)<<10)
  430. LUA_API int lua_getgcthreshold (lua_State *L) {
  431. int threshold;
  432. lua_lock(L);
  433. threshold = GCscale(G(L)->GCthreshold);
  434. lua_unlock(L);
  435. return threshold;
  436. }
  437. LUA_API int lua_getgccount (lua_State *L) {
  438. int count;
  439. lua_lock(L);
  440. count = GCscale(G(L)->nblocks);
  441. lua_unlock(L);
  442. return count;
  443. }
  444. LUA_API void lua_setgcthreshold (lua_State *L, int newthreshold) {
  445. lua_lock(L);
  446. if (newthreshold > GCscale(ULONG_MAX))
  447. G(L)->GCthreshold = ULONG_MAX;
  448. else
  449. G(L)->GCthreshold = GCunscale(newthreshold);
  450. luaC_checkGC(L);
  451. lua_unlock(L);
  452. }
  453. /*
  454. ** miscellaneous functions
  455. */
  456. LUA_API int lua_newxtype (lua_State *L, const l_char *name, int basictype) {
  457. int tag;
  458. lua_lock(L);
  459. if (basictype != LUA_TNONE &&
  460. basictype != LUA_TTABLE &&
  461. basictype != LUA_TUSERDATA)
  462. luaO_verror(L, l_s("invalid basic type (%d) for new type"), basictype);
  463. tag = luaT_newtag(L, name, basictype);
  464. if (tag == LUA_TNONE)
  465. luaO_verror(L, l_s("type name '%.30s' already exists"), name);
  466. lua_unlock(L);
  467. return tag;
  468. }
  469. LUA_API int lua_name2tag (lua_State *L, const l_char *name) {
  470. int tag;
  471. const TObject *v;
  472. lua_lock(L);
  473. v = luaH_getstr(G(L)->type2tag, luaS_new(L, name));
  474. if (ttype(v) == LUA_TNIL)
  475. tag = LUA_TNONE;
  476. else {
  477. lua_assert(ttype(v) == LUA_TNUMBER);
  478. tag = (int)nvalue(v);
  479. }
  480. lua_unlock(L);
  481. return tag;
  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. basictypename(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. }