lbaselib.c 19 KB

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