lbaselib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. ** $Id: lbaselib.c,v 1.18 2001/01/10 16:58:11 roberto Exp roberto $
  3. ** Basic library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "lua.h"
  11. #include "lauxlib.h"
  12. #include "luadebug.h"
  13. #include "lualib.h"
  14. /*
  15. ** If your system does not support `stderr', redefine this function, or
  16. ** redefine _ERRORMESSAGE so that it won't need _ALERT.
  17. */
  18. static int luaB__ALERT (lua_State *L) {
  19. fputs(luaL_check_string(L, 1), stderr);
  20. return 0;
  21. }
  22. /*
  23. ** Basic implementation of _ERRORMESSAGE.
  24. ** The library `liolib' redefines _ERRORMESSAGE for better error information.
  25. */
  26. static int luaB__ERRORMESSAGE (lua_State *L) {
  27. luaL_checktype(L, 1, LUA_TSTRING);
  28. lua_getglobal(L, LUA_ALERT);
  29. if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
  30. lua_Debug ar;
  31. lua_pushliteral(L, "error: ");
  32. lua_pushvalue(L, 1);
  33. if (lua_getstack(L, 1, &ar)) {
  34. lua_getinfo(L, "Sl", &ar);
  35. if (ar.source && ar.currentline > 0) {
  36. char buff[100];
  37. sprintf(buff, "\n <%.70s: line %d>", ar.short_src, ar.currentline);
  38. lua_pushstring(L, buff);
  39. lua_concat(L, 2);
  40. }
  41. }
  42. lua_pushliteral(L, "\n");
  43. lua_concat(L, 3);
  44. lua_rawcall(L, 1, 0);
  45. }
  46. return 0;
  47. }
  48. /*
  49. ** If your system does not support `stdout', you can just remove this function.
  50. ** If you need, you can define your own `print' function, following this
  51. ** model but changing `fputs' to put the strings at a proper place
  52. ** (a console window or a log file, for instance).
  53. */
  54. static int luaB_print (lua_State *L) {
  55. int n = lua_gettop(L); /* number of arguments */
  56. int i;
  57. lua_getglobal(L, "tostring");
  58. for (i=1; i<=n; i++) {
  59. const char *s;
  60. lua_pushvalue(L, -1); /* function to be called */
  61. lua_pushvalue(L, i); /* value to print */
  62. lua_rawcall(L, 1, 1);
  63. s = lua_tostring(L, -1); /* get result */
  64. if (s == NULL)
  65. lua_error(L, "`tostring' must return a string to `print'");
  66. if (i>1) fputs("\t", stdout);
  67. fputs(s, stdout);
  68. lua_pop(L, 1); /* pop result */
  69. }
  70. fputs("\n", stdout);
  71. return 0;
  72. }
  73. static int luaB_tonumber (lua_State *L) {
  74. int base = luaL_opt_int(L, 2, 10);
  75. if (base == 10) { /* standard conversion */
  76. luaL_checkany(L, 1);
  77. if (lua_isnumber(L, 1)) {
  78. lua_pushnumber(L, lua_tonumber(L, 1));
  79. return 1;
  80. }
  81. }
  82. else {
  83. const char *s1 = luaL_check_string(L, 1);
  84. char *s2;
  85. unsigned long n;
  86. luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
  87. n = strtoul(s1, &s2, base);
  88. if (s1 != s2) { /* at least one valid digit? */
  89. while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */
  90. if (*s2 == '\0') { /* no invalid trailing characters? */
  91. lua_pushnumber(L, n);
  92. return 1;
  93. }
  94. }
  95. }
  96. lua_pushnil(L); /* else not a number */
  97. return 1;
  98. }
  99. static int luaB_error (lua_State *L) {
  100. lua_error(L, luaL_opt_string(L, 1, NULL));
  101. return 0; /* to avoid warnings */
  102. }
  103. static int luaB_setglobal (lua_State *L) {
  104. luaL_checkany(L, 2);
  105. lua_setglobal(L, luaL_check_string(L, 1));
  106. return 0;
  107. }
  108. static int luaB_getglobal (lua_State *L) {
  109. lua_getglobal(L, luaL_check_string(L, 1));
  110. return 1;
  111. }
  112. /* auxiliary function to get `tags' */
  113. static int gettag (lua_State *L, int narg) {
  114. switch (lua_type(L, narg)) {
  115. case LUA_TNUMBER:
  116. return (int)lua_tonumber(L, narg);
  117. case LUA_TSTRING: {
  118. const char *name = lua_tostring(L, narg);
  119. int tag = lua_type2tag(L, name);
  120. if (tag == LUA_TNONE)
  121. luaL_verror(L, "'%.30s' is not a valid type name", name);
  122. return tag;
  123. }
  124. default:
  125. luaL_argerror(L, narg, "tag or type name expected");
  126. return 0; /* to avoid warnings */
  127. }
  128. }
  129. static int luaB_tag (lua_State *L) {
  130. luaL_checkany(L, 1);
  131. lua_pushnumber(L, lua_tag(L, 1));
  132. return 1;
  133. }
  134. static int luaB_settag (lua_State *L) {
  135. luaL_checktype(L, 1, LUA_TTABLE);
  136. lua_pushvalue(L, 1); /* push table */
  137. lua_settag(L, gettag(L, 2));
  138. return 1; /* return table */
  139. }
  140. static int luaB_newtype (lua_State *L) {
  141. const char *name = luaL_opt_string(L, 1, NULL);
  142. lua_pushnumber(L, lua_newtype(L, name, LUA_TTABLE));
  143. return 1;
  144. }
  145. static int luaB_copytagmethods (lua_State *L) {
  146. lua_pushnumber(L, lua_copytagmethods(L, gettag(L, 1), gettag(L, 2)));
  147. return 1;
  148. }
  149. static int luaB_globals (lua_State *L) {
  150. lua_getglobals(L); /* value to be returned */
  151. if (!lua_isnull(L, 1)) {
  152. luaL_checktype(L, 1, LUA_TTABLE);
  153. lua_pushvalue(L, 1); /* new table of globals */
  154. lua_setglobals(L);
  155. }
  156. return 1;
  157. }
  158. static int luaB_rawget (lua_State *L) {
  159. luaL_checktype(L, 1, LUA_TTABLE);
  160. luaL_checkany(L, 2);
  161. lua_rawget(L, -2);
  162. return 1;
  163. }
  164. static int luaB_rawset (lua_State *L) {
  165. luaL_checktype(L, 1, LUA_TTABLE);
  166. luaL_checkany(L, 2);
  167. luaL_checkany(L, 3);
  168. lua_rawset(L, -3);
  169. return 1;
  170. }
  171. static int luaB_settagmethod (lua_State *L) {
  172. int tag = gettag(L, 1);
  173. const char *event = luaL_check_string(L, 2);
  174. luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
  175. "function or nil expected");
  176. if (strcmp(event, "gc") == 0)
  177. lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
  178. lua_gettagmethod(L, tag, event);
  179. lua_pushvalue(L, 3);
  180. lua_settagmethod(L, tag, event);
  181. return 1;
  182. }
  183. static int luaB_gettagmethod (lua_State *L) {
  184. int tag = gettag(L, 1);
  185. const char *event = luaL_check_string(L, 2);
  186. if (strcmp(event, "gc") == 0)
  187. lua_error(L, "deprecated use: cannot get the `gc' tag method from Lua");
  188. lua_gettagmethod(L, tag, event);
  189. return 1;
  190. }
  191. static int luaB_gcinfo (lua_State *L) {
  192. lua_pushnumber(L, lua_getgccount(L));
  193. lua_pushnumber(L, lua_getgcthreshold(L));
  194. return 2;
  195. }
  196. static int luaB_collectgarbage (lua_State *L) {
  197. lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
  198. return 0;
  199. }
  200. static int luaB_type (lua_State *L) {
  201. luaL_checkany(L, 1);
  202. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  203. return 1;
  204. }
  205. static int luaB_xtype (lua_State *L) {
  206. luaL_checkany(L, 1);
  207. lua_pushstring(L, lua_xtype(L, 1));
  208. return 1;
  209. }
  210. static int luaB_next (lua_State *L) {
  211. luaL_checktype(L, 1, LUA_TTABLE);
  212. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  213. if (lua_next(L, 1))
  214. return 2;
  215. else {
  216. lua_pushnil(L);
  217. return 1;
  218. }
  219. }
  220. static int passresults (lua_State *L, int status, int oldtop) {
  221. static const char *const errornames[] =
  222. {"ok", "run-time error", "file error", "syntax error",
  223. "memory error", "error in error handling"};
  224. if (status == 0) {
  225. int nresults = lua_gettop(L) - oldtop;
  226. if (nresults > 0)
  227. return nresults; /* results are already on the stack */
  228. else {
  229. lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
  230. return 1;
  231. }
  232. }
  233. else { /* error */
  234. lua_pushnil(L);
  235. lua_pushstring(L, errornames[status]); /* error code */
  236. return 2;
  237. }
  238. }
  239. static int luaB_dostring (lua_State *L) {
  240. int oldtop = lua_gettop(L);
  241. size_t l;
  242. const char *s = luaL_check_lstr(L, 1, &l);
  243. if (*s == '\27') /* binary files start with ESC... */
  244. lua_error(L, "`dostring' cannot run pre-compiled code");
  245. return passresults(L, lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)), oldtop);
  246. }
  247. static int luaB_dofile (lua_State *L) {
  248. int oldtop = lua_gettop(L);
  249. const char *fname = luaL_opt_string(L, 1, NULL);
  250. return passresults(L, lua_dofile(L, fname), oldtop);
  251. }
  252. static int luaB_call (lua_State *L) {
  253. int oldtop;
  254. const char *options = luaL_opt_string(L, 3, "");
  255. int err = 0; /* index of old error method */
  256. int i, status;
  257. int n;
  258. luaL_checktype(L, 2, LUA_TTABLE);
  259. n = lua_getn(L, 2);
  260. if (!lua_isnull(L, 4)) { /* set new error method */
  261. lua_getglobal(L, LUA_ERRORMESSAGE);
  262. err = lua_gettop(L); /* get index */
  263. lua_pushvalue(L, 4);
  264. lua_setglobal(L, LUA_ERRORMESSAGE);
  265. }
  266. oldtop = lua_gettop(L); /* top before function-call preparation */
  267. /* push function */
  268. lua_pushvalue(L, 1);
  269. luaL_checkstack(L, n, "too many arguments");
  270. for (i=0; i<n; i++) /* push arg[1...n] */
  271. lua_rawgeti(L, 2, i+1);
  272. status = lua_call(L, n, LUA_MULTRET);
  273. if (err != 0) { /* restore old error method */
  274. lua_pushvalue(L, err);
  275. lua_setglobal(L, LUA_ERRORMESSAGE);
  276. }
  277. if (status != 0) { /* error in call? */
  278. if (strchr(options, 'x'))
  279. lua_pushnil(L); /* return nil to signal the error */
  280. else
  281. lua_error(L, NULL); /* propagate error without additional messages */
  282. return 1;
  283. }
  284. if (strchr(options, 'p')) /* pack results? */
  285. lua_error(L, "deprecated option `p' in `call'");
  286. return lua_gettop(L) - oldtop; /* results are already on the stack */
  287. }
  288. static int luaB_tostring (lua_State *L) {
  289. char buff[64];
  290. switch (lua_type(L, 1)) {
  291. case LUA_TNUMBER:
  292. lua_pushstring(L, lua_tostring(L, 1));
  293. return 1;
  294. case LUA_TSTRING:
  295. lua_pushvalue(L, 1);
  296. return 1;
  297. case LUA_TTABLE:
  298. sprintf(buff, "table: %p", lua_topointer(L, 1));
  299. break;
  300. case LUA_TFUNCTION:
  301. sprintf(buff, "function: %p", lua_topointer(L, 1));
  302. break;
  303. case LUA_TUSERDATA:
  304. sprintf(buff, "userdata(%d): %p", lua_tag(L, 1), lua_touserdata(L, 1));
  305. break;
  306. case LUA_TNIL:
  307. lua_pushliteral(L, "nil");
  308. return 1;
  309. default:
  310. luaL_argerror(L, 1, "value expected");
  311. }
  312. lua_pushstring(L, buff);
  313. return 1;
  314. }
  315. static int luaB_foreachi (lua_State *L) {
  316. int n, i;
  317. luaL_checktype(L, 1, LUA_TTABLE);
  318. luaL_checktype(L, 2, LUA_TFUNCTION);
  319. n = lua_getn(L, 1);
  320. for (i=1; i<=n; i++) {
  321. lua_pushvalue(L, 2); /* function */
  322. lua_pushnumber(L, i); /* 1st argument */
  323. lua_rawgeti(L, 1, i); /* 2nd argument */
  324. lua_rawcall(L, 2, 1);
  325. if (!lua_isnil(L, -1))
  326. return 1;
  327. lua_pop(L, 1); /* remove nil result */
  328. }
  329. return 0;
  330. }
  331. static int luaB_foreach (lua_State *L) {
  332. luaL_checktype(L, 1, LUA_TTABLE);
  333. luaL_checktype(L, 2, LUA_TFUNCTION);
  334. lua_pushnil(L); /* first index */
  335. for (;;) {
  336. if (lua_next(L, 1) == 0)
  337. return 0;
  338. lua_pushvalue(L, 2); /* function */
  339. lua_pushvalue(L, -3); /* key */
  340. lua_pushvalue(L, -3); /* value */
  341. lua_rawcall(L, 2, 1);
  342. if (!lua_isnil(L, -1))
  343. return 1;
  344. lua_pop(L, 2); /* remove value and result */
  345. }
  346. }
  347. static int luaB_assert (lua_State *L) {
  348. luaL_checkany(L, 1);
  349. if (lua_isnil(L, 1))
  350. luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
  351. return 0;
  352. }
  353. static int luaB_getn (lua_State *L) {
  354. luaL_checktype(L, 1, LUA_TTABLE);
  355. lua_pushnumber(L, lua_getn(L, 1));
  356. return 1;
  357. }
  358. static int luaB_tinsert (lua_State *L) {
  359. int v = lua_gettop(L); /* last argument: to be inserted */
  360. int n, pos;
  361. luaL_checktype(L, 1, LUA_TTABLE);
  362. n = lua_getn(L, 1);
  363. if (v == 2) /* called with only 2 arguments */
  364. pos = n+1;
  365. else
  366. pos = luaL_check_int(L, 2); /* 2nd argument is the position */
  367. lua_pushliteral(L, "n");
  368. lua_pushnumber(L, n+1);
  369. lua_rawset(L, 1); /* t.n = n+1 */
  370. for (; n>=pos; n--) {
  371. lua_rawgeti(L, 1, n);
  372. lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
  373. }
  374. lua_pushvalue(L, v);
  375. lua_rawseti(L, 1, pos); /* t[pos] = v */
  376. return 0;
  377. }
  378. static int luaB_tremove (lua_State *L) {
  379. int pos, n;
  380. luaL_checktype(L, 1, LUA_TTABLE);
  381. n = lua_getn(L, 1);
  382. pos = luaL_opt_int(L, 2, n);
  383. if (n <= 0) return 0; /* table is "empty" */
  384. lua_rawgeti(L, 1, pos); /* result = t[pos] */
  385. for ( ;pos<n; pos++) {
  386. lua_rawgeti(L, 1, pos+1);
  387. lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */
  388. }
  389. lua_pushliteral(L, "n");
  390. lua_pushnumber(L, n-1);
  391. lua_rawset(L, 1); /* t.n = n-1 */
  392. lua_pushnil(L);
  393. lua_rawseti(L, 1, n); /* t[n] = nil */
  394. return 1;
  395. }
  396. /*
  397. ** {======================================================
  398. ** Quicksort
  399. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  400. ** Addison-Wesley, 1993.)
  401. */
  402. static void set2 (lua_State *L, int i, int j) {
  403. lua_rawseti(L, 1, i);
  404. lua_rawseti(L, 1, j);
  405. }
  406. static int sort_comp (lua_State *L, int a, int b) {
  407. /* WARNING: the caller (auxsort) must ensure stack space */
  408. if (!lua_isnil(L, 2)) { /* function? */
  409. int res;
  410. lua_pushvalue(L, 2);
  411. lua_pushvalue(L, a-1); /* -1 to compensate function */
  412. lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
  413. lua_rawcall(L, 2, 1);
  414. res = !lua_isnil(L, -1);
  415. lua_pop(L, 1);
  416. return res;
  417. }
  418. else /* a < b? */
  419. return lua_lessthan(L, a, b);
  420. }
  421. static void auxsort (lua_State *L, int l, int u) {
  422. while (l < u) { /* for tail recursion */
  423. int i, j;
  424. /* sort elements a[l], a[(l+u)/2] and a[u] */
  425. lua_rawgeti(L, 1, l);
  426. lua_rawgeti(L, 1, u);
  427. if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
  428. set2(L, l, u); /* swap a[l] - a[u] */
  429. else
  430. lua_pop(L, 2);
  431. if (u-l == 1) break; /* only 2 elements */
  432. i = (l+u)/2;
  433. lua_rawgeti(L, 1, i);
  434. lua_rawgeti(L, 1, l);
  435. if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
  436. set2(L, i, l);
  437. else {
  438. lua_pop(L, 1); /* remove a[l] */
  439. lua_rawgeti(L, 1, u);
  440. if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
  441. set2(L, i, u);
  442. else
  443. lua_pop(L, 2);
  444. }
  445. if (u-l == 2) break; /* only 3 elements */
  446. lua_rawgeti(L, 1, i); /* Pivot */
  447. lua_pushvalue(L, -1);
  448. lua_rawgeti(L, 1, u-1);
  449. set2(L, i, u-1);
  450. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
  451. i = l; j = u-1;
  452. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  453. /* repeat ++i until a[i] >= P */
  454. while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
  455. if (i>u) lua_error(L, "invalid order function for sorting");
  456. lua_pop(L, 1); /* remove a[i] */
  457. }
  458. /* repeat --j until a[j] <= P */
  459. while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
  460. if (j<l) lua_error(L, "invalid order function for sorting");
  461. lua_pop(L, 1); /* remove a[j] */
  462. }
  463. if (j<i) {
  464. lua_pop(L, 3); /* pop pivot, a[i], a[j] */
  465. break;
  466. }
  467. set2(L, i, j);
  468. }
  469. lua_rawgeti(L, 1, u-1);
  470. lua_rawgeti(L, 1, i);
  471. set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  472. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  473. /* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
  474. if (i-l < u-i) {
  475. j=l; i=i-1; l=i+2;
  476. }
  477. else {
  478. j=i+1; i=u; u=j-2;
  479. }
  480. auxsort(L, j, i); /* call recursively the smaller one */
  481. } /* repeat the routine for the larger one */
  482. }
  483. static int luaB_sort (lua_State *L) {
  484. int n;
  485. luaL_checktype(L, 1, LUA_TTABLE);
  486. n = lua_getn(L, 1);
  487. if (!lua_isnull(L, 2)) /* is there a 2nd argument? */
  488. luaL_checktype(L, 2, LUA_TFUNCTION);
  489. lua_settop(L, 2); /* make sure there is two arguments */
  490. auxsort(L, 1, n);
  491. return 0;
  492. }
  493. /* }====================================================== */
  494. /*
  495. ** {======================================================
  496. ** Deprecated functions to manipulate global environment.
  497. ** =======================================================
  498. */
  499. #define num_deprecated 4
  500. static const struct luaL_reg deprecated_names [num_deprecated] = {
  501. {"foreachvar", luaB_foreach},
  502. {"nextvar", luaB_next},
  503. {"rawgetglobal", luaB_rawget},
  504. {"rawsetglobal", luaB_rawset}
  505. };
  506. #ifdef LUA_DEPRECATEDFUNCS
  507. /*
  508. ** call corresponding function inserting `globals' as first argument
  509. */
  510. static int deprecated_func (lua_State *L) {
  511. lua_insert(L, 1); /* upvalue is the function to be called */
  512. lua_getglobals(L);
  513. lua_insert(L, 2); /* table of globals is 1o argument */
  514. lua_rawcall(L, lua_gettop(L)-1, LUA_MULTRET);
  515. return lua_gettop(L); /* return all results */
  516. }
  517. static void deprecated_funcs (lua_State *L) {
  518. int i;
  519. for (i=0; i<num_deprecated; i++) {
  520. lua_pushcfunction(L, deprecated_names[i].func);
  521. lua_pushcclosure(L, deprecated_func, 1);
  522. lua_setglobal(L, deprecated_names[i].name);
  523. }
  524. }
  525. #else
  526. /*
  527. ** gives an explicit error in any attempt to call a deprecated function
  528. */
  529. static int deprecated_func (lua_State *L) {
  530. luaL_verror(L, "function `%.20s' is deprecated", lua_tostring(L, -1));
  531. return 0; /* to avoid warnings */
  532. }
  533. static void deprecated_funcs (lua_State *L) {
  534. int i;
  535. for (i=0; i<num_deprecated; i++) {
  536. lua_pushstring(L, deprecated_names[i].name);
  537. lua_pushcclosure(L, deprecated_func, 1);
  538. lua_setglobal(L, deprecated_names[i].name);
  539. }
  540. }
  541. #endif
  542. /* }====================================================== */
  543. static const struct luaL_reg base_funcs[] = {
  544. {LUA_ALERT, luaB__ALERT},
  545. {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
  546. {"call", luaB_call},
  547. {"collectgarbage", luaB_collectgarbage},
  548. {"copytagmethods", luaB_copytagmethods},
  549. {"dofile", luaB_dofile},
  550. {"dostring", luaB_dostring},
  551. {"error", luaB_error},
  552. {"foreach", luaB_foreach},
  553. {"foreachi", luaB_foreachi},
  554. {"gcinfo", luaB_gcinfo},
  555. {"getglobal", luaB_getglobal},
  556. {"gettagmethod", luaB_gettagmethod},
  557. {"globals", luaB_globals},
  558. {"newtype", luaB_newtype},
  559. {"newtag", luaB_newtype}, /* for compatibility 4.0 */
  560. {"next", luaB_next},
  561. {"print", luaB_print},
  562. {"rawget", luaB_rawget},
  563. {"rawset", luaB_rawset},
  564. {"rawgettable", luaB_rawget}, /* for compatibility 3.2 */
  565. {"rawsettable", luaB_rawset}, /* for compatibility 3.2 */
  566. {"setglobal", luaB_setglobal},
  567. {"settag", luaB_settag},
  568. {"settagmethod", luaB_settagmethod},
  569. {"tag", luaB_tag},
  570. {"tonumber", luaB_tonumber},
  571. {"tostring", luaB_tostring},
  572. {"type", luaB_type},
  573. {"assert", luaB_assert},
  574. {"getn", luaB_getn},
  575. {"sort", luaB_sort},
  576. {"tinsert", luaB_tinsert},
  577. {"tremove", luaB_tremove},
  578. {"xtype", luaB_xtype},
  579. };
  580. LUALIB_API void lua_baselibopen (lua_State *L) {
  581. luaL_openl(L, base_funcs);
  582. lua_pushliteral(L, LUA_VERSION);
  583. lua_setglobal(L, "_VERSION");
  584. deprecated_funcs(L);
  585. }