lbaselib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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_globals (lua_State *L) {
  128. lua_getglobals(L); /* value to be returned */
  129. if (!lua_isnone(L, 1)) {
  130. luaL_check_type(L, 1, LUA_TTABLE);
  131. lua_pushvalue(L, 1); /* new table of globals */
  132. lua_setglobals(L);
  133. }
  134. return 1;
  135. }
  136. static int luaB_rawget (lua_State *L) {
  137. luaL_check_type(L, 1, LUA_TTABLE);
  138. luaL_check_any(L, 2);
  139. lua_rawget(L, 1);
  140. return 1;
  141. }
  142. static int luaB_rawset (lua_State *L) {
  143. luaL_check_type(L, 1, LUA_TTABLE);
  144. luaL_check_any(L, 2);
  145. luaL_check_any(L, 3);
  146. lua_rawset(L, 1);
  147. return 1;
  148. }
  149. static int luaB_gcinfo (lua_State *L) {
  150. lua_pushnumber(L, lua_getgccount(L));
  151. lua_pushnumber(L, lua_getgcthreshold(L));
  152. return 2;
  153. }
  154. static int luaB_collectgarbage (lua_State *L) {
  155. lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
  156. return 0;
  157. }
  158. static int luaB_type (lua_State *L) {
  159. luaL_check_any(L, 1);
  160. if (lua_isnone(L, 2))
  161. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  162. else {
  163. lua_pushboolean(L,
  164. (strcmp(lua_typename(L, lua_type(L, 1)), luaL_check_string(L, 2)) == 0));
  165. }
  166. return 1;
  167. }
  168. static int luaB_next (lua_State *L) {
  169. luaL_check_type(L, 1, LUA_TTABLE);
  170. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  171. if (lua_next(L, 1))
  172. return 2;
  173. else {
  174. lua_pushnil(L);
  175. return 1;
  176. }
  177. }
  178. static int passresults (lua_State *L, int status, int oldtop) {
  179. static const char *const errornames[] =
  180. {"ok", "run-time error", "file error", "syntax error",
  181. "memory error", "error in error handling"};
  182. if (status == 0) {
  183. int nresults = lua_gettop(L) - oldtop;
  184. if (nresults > 0)
  185. return nresults; /* results are already on the stack */
  186. else {
  187. lua_newuserdatabox(L, NULL); /* at least one result to signal no errors */
  188. return 1;
  189. }
  190. }
  191. else { /* error */
  192. lua_pushnil(L);
  193. lua_pushstring(L, errornames[status]); /* error code */
  194. return 2;
  195. }
  196. }
  197. static int luaB_dostring (lua_State *L) {
  198. int oldtop = lua_gettop(L);
  199. size_t l;
  200. const char *s = luaL_check_lstr(L, 1, &l);
  201. const char *chunkname = luaL_opt_string(L, 2, s);
  202. return passresults(L, lua_dobuffer(L, s, l, chunkname), oldtop);
  203. }
  204. static int luaB_loadstring (lua_State *L) {
  205. int oldtop = lua_gettop(L);
  206. size_t l;
  207. const char *s = luaL_check_lstr(L, 1, &l);
  208. const char *chunkname = luaL_opt_string(L, 2, s);
  209. return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop);
  210. }
  211. static int luaB_dofile (lua_State *L) {
  212. int oldtop = lua_gettop(L);
  213. const char *fname = luaL_opt_string(L, 1, NULL);
  214. return passresults(L, lua_dofile(L, fname), oldtop);
  215. }
  216. static int luaB_loadfile (lua_State *L) {
  217. int oldtop = lua_gettop(L);
  218. const char *fname = luaL_opt_string(L, 1, NULL);
  219. return passresults(L, lua_loadfile(L, fname), oldtop);
  220. }
  221. static int luaB_assert (lua_State *L) {
  222. luaL_check_any(L, 1);
  223. if (!lua_istrue(L, 1))
  224. luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
  225. lua_settop(L, 1);
  226. return 1;
  227. }
  228. #define LUA_PATH "LUA_PATH"
  229. #define LUA_PATH_SEP ";"
  230. #ifndef LUA_PATH_DEFAULT
  231. #define LUA_PATH_DEFAULT "./"
  232. #endif
  233. static int luaB_require (lua_State *L) {
  234. const char *path;
  235. luaL_check_string(L, 1);
  236. lua_settop(L, 1);
  237. lua_getglobal(L, LUA_PATH); /* get path */
  238. if (lua_isstring(L, 2)) /* is LUA_PATH defined? */
  239. path = lua_tostring(L, 2);
  240. else { /* LUA_PATH not defined */
  241. lua_pop(L, 1); /* pop old global value */
  242. path = getenv(LUA_PATH); /* try environment variable */
  243. if (path == NULL) path = LUA_PATH_DEFAULT; /* else use default */
  244. lua_pushstring(L, path);
  245. lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */
  246. lua_setglobal(L, LUA_PATH);
  247. }
  248. lua_pushvalue(L, 1); /* check package's name in book-keeping table */
  249. lua_gettable(L, lua_upvalueindex(1));
  250. if (!lua_isnil(L, -1)) /* is it there? */
  251. return 0; /* package is already loaded */
  252. else { /* must load it */
  253. for (;;) { /* traverse path */
  254. int res;
  255. int l = strcspn(path, LUA_PATH_SEP); /* find separator */
  256. lua_pushlstring(L, path, l); /* directory name */
  257. lua_pushvalue(L, 1); /* package name */
  258. lua_concat(L, 2); /* concat directory with package name */
  259. res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */
  260. lua_settop(L, 2); /* pop string and eventual results from dofile */
  261. if (res == 0) break; /* ok; file done */
  262. else if (res != LUA_ERRFILE)
  263. lua_error(L, NULL); /* error running package; propagate it */
  264. if (*(path+l) == '\0') /* no more directories? */
  265. luaL_verror(L, "could not load package `%.20s' from path `%.200s'",
  266. lua_tostring(L, 1), lua_tostring(L, 2));
  267. path += l+1; /* try next directory */
  268. }
  269. }
  270. lua_pushvalue(L, 1);
  271. lua_pushnumber(L, 1);
  272. lua_settable(L, lua_upvalueindex(1)); /* mark it as loaded */
  273. return 0;
  274. }
  275. static int aux_unpack (lua_State *L, int arg) {
  276. int n, i;
  277. luaL_check_type(L, arg, LUA_TTABLE);
  278. n = lua_getn(L, arg);
  279. luaL_check_stack(L, n, "table too big to unpack");
  280. for (i=1; i<=n; i++) /* push arg[1...n] */
  281. lua_rawgeti(L, arg, i);
  282. return n;
  283. }
  284. static int luaB_unpack (lua_State *L) {
  285. return aux_unpack(L, 1);
  286. }
  287. static int luaB_call (lua_State *L) {
  288. int oldtop;
  289. const char *options = luaL_opt_string(L, 3, "");
  290. int err = 0; /* index of old error method */
  291. int status;
  292. int n;
  293. if (!lua_isnone(L, 4)) { /* set new error method */
  294. lua_getglobal(L, LUA_ERRORMESSAGE);
  295. err = lua_gettop(L); /* get index */
  296. lua_pushvalue(L, 4);
  297. lua_setglobal(L, LUA_ERRORMESSAGE);
  298. }
  299. oldtop = lua_gettop(L); /* top before function-call preparation */
  300. /* push function */
  301. lua_pushvalue(L, 1);
  302. n = aux_unpack(L, 2); /* push arg[1...n] */
  303. status = lua_call(L, n, LUA_MULTRET);
  304. if (err != 0) { /* restore old error method */
  305. lua_pushvalue(L, err);
  306. lua_setglobal(L, LUA_ERRORMESSAGE);
  307. }
  308. if (status != 0) { /* error in call? */
  309. if (strchr(options, 'x'))
  310. lua_pushnil(L); /* return nil to signal the error */
  311. else
  312. lua_error(L, NULL); /* propagate error without additional messages */
  313. return 1;
  314. }
  315. if (strchr(options, 'p')) /* pack results? */
  316. lua_error(L, "obsolete option `p' in `call'");
  317. return lua_gettop(L) - oldtop; /* results are already on the stack */
  318. }
  319. static int luaB_tostring (lua_State *L) {
  320. char buff[64];
  321. switch (lua_type(L, 1)) {
  322. case LUA_TNUMBER:
  323. lua_pushstring(L, lua_tostring(L, 1));
  324. return 1;
  325. case LUA_TSTRING:
  326. lua_pushvalue(L, 1);
  327. return 1;
  328. case LUA_TBOOLEAN:
  329. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  330. return 1;
  331. case LUA_TTABLE:
  332. sprintf(buff, "%.40s: %p", lua_typename(L, lua_type(L, 1)),
  333. lua_topointer(L, 1));
  334. break;
  335. case LUA_TFUNCTION:
  336. sprintf(buff, "function: %p", lua_topointer(L, 1));
  337. break;
  338. case LUA_TUSERDATA: {
  339. const char *t = lua_typename(L, lua_type(L, 1));
  340. if (strcmp(t, "userdata") == 0)
  341. sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
  342. else
  343. sprintf(buff, "%.40s: %p", t, lua_touserdata(L, 1));
  344. break;
  345. }
  346. case LUA_TNIL:
  347. lua_pushliteral(L, "nil");
  348. return 1;
  349. default:
  350. luaL_argerror(L, 1, "value expected");
  351. }
  352. lua_pushstring(L, buff);
  353. return 1;
  354. }
  355. static int luaB_resume (lua_State *L) {
  356. lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1));
  357. lua_resume(L, co);
  358. return lua_gettop(L);
  359. }
  360. static int gc_coroutine (lua_State *L) {
  361. lua_State *co = (lua_State *)lua_touserdata(L, 1);
  362. lua_closethread(L, co);
  363. return 0;
  364. }
  365. static int luaB_coroutine (lua_State *L) {
  366. lua_State *NL;
  367. int ref;
  368. int i;
  369. int n = lua_gettop(L);
  370. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  371. "Lua function expected");
  372. NL = lua_newthread(L, 0);
  373. if (NL == NULL) lua_error(L, "unable to create new thread");
  374. /* move function and arguments from L to NL */
  375. for (i=0; i<n; i++) {
  376. ref = lua_ref(L, 1);
  377. lua_getref(NL, ref);
  378. lua_insert(NL, 1);
  379. lua_unref(L, ref);
  380. }
  381. lua_cobegin(NL, n-1);
  382. lua_newuserdatabox(L, NL);
  383. lua_getstr(L, LUA_REGISTRYINDEX, "Coroutine");
  384. lua_seteventtable(L, -2);
  385. lua_pushcclosure(L, luaB_resume, 1);
  386. return 1;
  387. }
  388. static int luaB_yield (lua_State *L) {
  389. return lua_yield(L, lua_gettop(L));
  390. }
  391. /*
  392. ** {======================================================
  393. ** Auxiliar table-related functions
  394. */
  395. static int luaB_foreachi (lua_State *L) {
  396. int n, i;
  397. luaL_check_type(L, 1, LUA_TTABLE);
  398. luaL_check_type(L, 2, LUA_TFUNCTION);
  399. n = lua_getn(L, 1);
  400. for (i=1; i<=n; i++) {
  401. lua_pushvalue(L, 2); /* function */
  402. lua_pushnumber(L, i); /* 1st argument */
  403. lua_rawgeti(L, 1, i); /* 2nd argument */
  404. lua_rawcall(L, 2, 1);
  405. if (!lua_isnil(L, -1))
  406. return 1;
  407. lua_pop(L, 1); /* remove nil result */
  408. }
  409. return 0;
  410. }
  411. static int luaB_foreach (lua_State *L) {
  412. luaL_check_type(L, 1, LUA_TTABLE);
  413. luaL_check_type(L, 2, LUA_TFUNCTION);
  414. lua_pushnil(L); /* first key */
  415. for (;;) {
  416. if (lua_next(L, 1) == 0)
  417. return 0;
  418. lua_pushvalue(L, 2); /* function */
  419. lua_pushvalue(L, -3); /* key */
  420. lua_pushvalue(L, -3); /* value */
  421. lua_rawcall(L, 2, 1);
  422. if (!lua_isnil(L, -1))
  423. return 1;
  424. lua_pop(L, 2); /* remove value and result */
  425. }
  426. }
  427. static int luaB_getn (lua_State *L) {
  428. luaL_check_type(L, 1, LUA_TTABLE);
  429. lua_pushnumber(L, lua_getn(L, 1));
  430. return 1;
  431. }
  432. static int luaB_tinsert (lua_State *L) {
  433. int v = lua_gettop(L); /* number of arguments */
  434. int n, pos;
  435. luaL_check_type(L, 1, LUA_TTABLE);
  436. n = lua_getn(L, 1);
  437. if (v == 2) /* called with only 2 arguments */
  438. pos = n+1;
  439. else {
  440. v = 3; /* function may be called with more than 3 args */
  441. pos = luaL_check_int(L, 2); /* 2nd argument is the position */
  442. }
  443. aux_setn(L, 1, n+1); /* t.n = n+1 */
  444. for (; n>=pos; n--) {
  445. lua_rawgeti(L, 1, n);
  446. lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
  447. }
  448. lua_pushvalue(L, v);
  449. lua_rawseti(L, 1, pos); /* t[pos] = v */
  450. return 0;
  451. }
  452. static int luaB_tremove (lua_State *L) {
  453. int pos, n;
  454. luaL_check_type(L, 1, LUA_TTABLE);
  455. n = lua_getn(L, 1);
  456. pos = luaL_opt_int(L, 2, n);
  457. if (n <= 0) return 0; /* table is `empty' */
  458. aux_setn(L, 1, n-1); /* t.n = n-1 */
  459. lua_rawgeti(L, 1, pos); /* result = t[pos] */
  460. for ( ;pos<n; pos++) {
  461. lua_rawgeti(L, 1, pos+1);
  462. lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */
  463. }
  464. lua_pushnil(L);
  465. lua_rawseti(L, 1, n); /* t[n] = nil */
  466. return 1;
  467. }
  468. /*
  469. ** {======================================================
  470. ** Quicksort
  471. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  472. ** Addison-Wesley, 1993.)
  473. */
  474. static void set2 (lua_State *L, int i, int j) {
  475. lua_rawseti(L, 1, i);
  476. lua_rawseti(L, 1, j);
  477. }
  478. static int sort_comp (lua_State *L, int a, int b) {
  479. /* WARNING: the caller (auxsort) must ensure stack space */
  480. if (!lua_isnil(L, 2)) { /* function? */
  481. int res;
  482. lua_pushvalue(L, 2);
  483. lua_pushvalue(L, a-1); /* -1 to compensate function */
  484. lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
  485. lua_rawcall(L, 2, 1);
  486. res = lua_istrue(L, -1);
  487. lua_pop(L, 1);
  488. return res;
  489. }
  490. else /* a < b? */
  491. return lua_lessthan(L, a, b);
  492. }
  493. static void auxsort (lua_State *L, int l, int u) {
  494. while (l < u) { /* for tail recursion */
  495. int i, j;
  496. /* sort elements a[l], a[(l+u)/2] and a[u] */
  497. lua_rawgeti(L, 1, l);
  498. lua_rawgeti(L, 1, u);
  499. if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
  500. set2(L, l, u); /* swap a[l] - a[u] */
  501. else
  502. lua_pop(L, 2);
  503. if (u-l == 1) break; /* only 2 elements */
  504. i = (l+u)/2;
  505. lua_rawgeti(L, 1, i);
  506. lua_rawgeti(L, 1, l);
  507. if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
  508. set2(L, i, l);
  509. else {
  510. lua_pop(L, 1); /* remove a[l] */
  511. lua_rawgeti(L, 1, u);
  512. if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
  513. set2(L, i, u);
  514. else
  515. lua_pop(L, 2);
  516. }
  517. if (u-l == 2) break; /* only 3 elements */
  518. lua_rawgeti(L, 1, i); /* Pivot */
  519. lua_pushvalue(L, -1);
  520. lua_rawgeti(L, 1, u-1);
  521. set2(L, i, u-1);
  522. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
  523. i = l; j = u-1;
  524. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  525. /* repeat ++i until a[i] >= P */
  526. while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
  527. if (i>u) lua_error(L, "invalid order function for sorting");
  528. lua_pop(L, 1); /* remove a[i] */
  529. }
  530. /* repeat --j until a[j] <= P */
  531. while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
  532. if (j<l) lua_error(L, "invalid order function for sorting");
  533. lua_pop(L, 1); /* remove a[j] */
  534. }
  535. if (j<i) {
  536. lua_pop(L, 3); /* pop pivot, a[i], a[j] */
  537. break;
  538. }
  539. set2(L, i, j);
  540. }
  541. lua_rawgeti(L, 1, u-1);
  542. lua_rawgeti(L, 1, i);
  543. set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  544. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  545. /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
  546. if (i-l < u-i) {
  547. j=l; i=i-1; l=i+2;
  548. }
  549. else {
  550. j=i+1; i=u; u=j-2;
  551. }
  552. auxsort(L, j, i); /* call recursively the smaller one */
  553. } /* repeat the routine for the larger one */
  554. }
  555. static int luaB_sort (lua_State *L) {
  556. int n;
  557. luaL_check_type(L, 1, LUA_TTABLE);
  558. n = lua_getn(L, 1);
  559. if (!lua_isnone(L, 2)) /* is there a 2nd argument? */
  560. luaL_check_type(L, 2, LUA_TFUNCTION);
  561. lua_settop(L, 2); /* make sure there is two arguments */
  562. auxsort(L, 1, n);
  563. return 0;
  564. }
  565. /* }====================================================== */
  566. /* }====================================================== */
  567. static const luaL_reg base_funcs[] = {
  568. {LUA_ALERT, luaB__ALERT},
  569. {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
  570. {"call", luaB_call},
  571. {"collectgarbage", luaB_collectgarbage},
  572. {"coroutine", luaB_coroutine},
  573. {"dofile", luaB_dofile},
  574. {"dostring", luaB_dostring},
  575. {"error", luaB_error},
  576. {"eventtable", luaB_eventtable},
  577. {"foreach", luaB_foreach},
  578. {"foreachi", luaB_foreachi},
  579. {"gcinfo", luaB_gcinfo},
  580. {"getglobal", luaB_getglobal}, /* compatibility with 4.0 */
  581. {"globals", luaB_globals},
  582. {"loadfile", luaB_loadfile},
  583. {"loadstring", luaB_loadstring},
  584. {"next", luaB_next},
  585. {"print", luaB_print},
  586. {"rawget", luaB_rawget},
  587. {"rawset", luaB_rawset},
  588. {"setglobal", luaB_setglobal}, /* compatibility with 4.0 */
  589. {"tonumber", luaB_tonumber},
  590. {"tostring", luaB_tostring},
  591. {"type", luaB_type},
  592. {"assert", luaB_assert},
  593. {"getn", luaB_getn},
  594. {"sort", luaB_sort},
  595. {"tinsert", luaB_tinsert},
  596. {"tremove", luaB_tremove},
  597. {"unpack", luaB_unpack},
  598. {"yield", luaB_yield}
  599. };
  600. LUALIB_API int lua_baselibopen (lua_State *L) {
  601. luaL_openl(L, base_funcs);
  602. lua_pushliteral(L, LUA_VERSION);
  603. lua_setglobal(L, "_VERSION");
  604. /* `require' needs an empty table as upvalue */
  605. lua_newtable(L);
  606. lua_pushcclosure(L, luaB_require, 1);
  607. lua_setglobal(L, "require");
  608. /* create metatable for coroutines */
  609. lua_newtable(L);
  610. lua_pushcfunction(L, gc_coroutine);
  611. lua_setstr(L, -2, "gc");
  612. lua_setstr(L, LUA_REGISTRYINDEX, "Coroutine");
  613. return 0;
  614. }