lbaselib.c 18 KB

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