lapi.c 15 KB

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