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_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. if (status == 0) {
  181. int nresults = lua_gettop(L) - oldtop;
  182. if (nresults > 0)
  183. return nresults; /* results are already on the stack */
  184. else {
  185. lua_newuserdatabox(L, NULL); /* at least one result to signal no errors */
  186. return 1;
  187. }
  188. }
  189. else { /* error */
  190. lua_pushnil(L);
  191. lua_pushstring(L, luaL_errstr(status)); /* error code */
  192. return 2;
  193. }
  194. }
  195. static int luaB_dostring (lua_State *L) {
  196. int oldtop = lua_gettop(L);
  197. size_t l;
  198. const char *s = luaL_check_lstr(L, 1, &l);
  199. const char *chunkname = luaL_opt_string(L, 2, s);
  200. return passresults(L, lua_dobuffer(L, s, l, chunkname), oldtop);
  201. }
  202. static int luaB_loadstring (lua_State *L) {
  203. int oldtop = lua_gettop(L);
  204. size_t l;
  205. const char *s = luaL_check_lstr(L, 1, &l);
  206. const char *chunkname = luaL_opt_string(L, 2, s);
  207. return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop);
  208. }
  209. static int luaB_dofile (lua_State *L) {
  210. int oldtop = lua_gettop(L);
  211. const char *fname = luaL_opt_string(L, 1, NULL);
  212. return passresults(L, lua_dofile(L, fname), oldtop);
  213. }
  214. static int luaB_loadfile (lua_State *L) {
  215. int oldtop = lua_gettop(L);
  216. const char *fname = luaL_opt_string(L, 1, NULL);
  217. return passresults(L, lua_loadfile(L, fname), oldtop);
  218. }
  219. static int luaB_assert (lua_State *L) {
  220. luaL_check_any(L, 1);
  221. if (!lua_istrue(L, 1))
  222. luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
  223. lua_settop(L, 1);
  224. return 1;
  225. }
  226. #define LUA_PATH "LUA_PATH"
  227. #define LUA_PATH_SEP ";"
  228. #ifndef LUA_PATH_DEFAULT
  229. #define LUA_PATH_DEFAULT "./"
  230. #endif
  231. static int luaB_require (lua_State *L) {
  232. const char *path;
  233. luaL_check_string(L, 1);
  234. lua_settop(L, 1);
  235. lua_getglobal(L, LUA_PATH); /* get path */
  236. if (lua_isstring(L, 2)) /* is LUA_PATH defined? */
  237. path = lua_tostring(L, 2);
  238. else { /* LUA_PATH not defined */
  239. lua_pop(L, 1); /* pop old global value */
  240. path = getenv(LUA_PATH); /* try environment variable */
  241. if (path == NULL) path = LUA_PATH_DEFAULT; /* else use default */
  242. lua_pushstring(L, path);
  243. lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */
  244. lua_setglobal(L, LUA_PATH);
  245. }
  246. lua_pushvalue(L, 1); /* check package's name in book-keeping table */
  247. lua_gettable(L, lua_upvalueindex(1));
  248. if (!lua_isnil(L, -1)) /* is it there? */
  249. return 0; /* package is already loaded */
  250. else { /* must load it */
  251. for (;;) { /* traverse path */
  252. int res;
  253. int l = strcspn(path, LUA_PATH_SEP); /* find separator */
  254. lua_pushlstring(L, path, l); /* directory name */
  255. lua_pushvalue(L, 1); /* package name */
  256. lua_concat(L, 2); /* concat directory with package name */
  257. res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */
  258. lua_settop(L, 2); /* pop string and eventual results from dofile */
  259. if (res == 0) break; /* ok; file done */
  260. else if (res != LUA_ERRFILE)
  261. lua_error(L, NULL); /* error running package; propagate it */
  262. if (*(path+l) == '\0') /* no more directories? */
  263. luaL_verror(L, "could not load package `%.20s' from path `%.200s'",
  264. lua_tostring(L, 1), lua_tostring(L, 2));
  265. path += l+1; /* try next directory */
  266. }
  267. }
  268. lua_pushvalue(L, 1);
  269. lua_pushnumber(L, 1);
  270. lua_settable(L, lua_upvalueindex(1)); /* mark it as loaded */
  271. return 0;
  272. }
  273. static int aux_unpack (lua_State *L, int arg) {
  274. int n, i;
  275. luaL_check_type(L, arg, LUA_TTABLE);
  276. n = lua_getn(L, arg);
  277. luaL_check_stack(L, n, "table too big to unpack");
  278. for (i=1; i<=n; i++) /* push arg[1...n] */
  279. lua_rawgeti(L, arg, i);
  280. return n;
  281. }
  282. static int luaB_unpack (lua_State *L) {
  283. return aux_unpack(L, 1);
  284. }
  285. static int luaB_call (lua_State *L) {
  286. int oldtop;
  287. const char *options = luaL_opt_string(L, 3, "");
  288. int err = 0; /* index of old error method */
  289. int status;
  290. int n;
  291. if (!lua_isnone(L, 4)) { /* set new error method */
  292. lua_getglobal(L, LUA_ERRORMESSAGE);
  293. err = lua_gettop(L); /* get index */
  294. lua_pushvalue(L, 4);
  295. lua_setglobal(L, LUA_ERRORMESSAGE);
  296. }
  297. oldtop = lua_gettop(L); /* top before function-call preparation */
  298. /* push function */
  299. lua_pushvalue(L, 1);
  300. n = aux_unpack(L, 2); /* push arg[1...n] */
  301. status = lua_call(L, n, LUA_MULTRET);
  302. if (err != 0) { /* restore old error method */
  303. lua_pushvalue(L, err);
  304. lua_setglobal(L, LUA_ERRORMESSAGE);
  305. }
  306. if (status != 0) { /* error in call? */
  307. if (strchr(options, 'x'))
  308. lua_pushnil(L); /* return nil to signal the error */
  309. else
  310. lua_error(L, NULL); /* propagate error without additional messages */
  311. return 1;
  312. }
  313. if (strchr(options, 'p')) /* pack results? */
  314. lua_error(L, "obsolete option `p' in `call'");
  315. return lua_gettop(L) - oldtop; /* results are already on the stack */
  316. }
  317. static int luaB_tostring (lua_State *L) {
  318. char buff[64];
  319. switch (lua_type(L, 1)) {
  320. case LUA_TNUMBER:
  321. lua_pushstring(L, lua_tostring(L, 1));
  322. return 1;
  323. case LUA_TSTRING:
  324. lua_pushvalue(L, 1);
  325. return 1;
  326. case LUA_TBOOLEAN:
  327. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  328. return 1;
  329. case LUA_TTABLE:
  330. sprintf(buff, "%.40s: %p", lua_typename(L, lua_type(L, 1)),
  331. lua_topointer(L, 1));
  332. break;
  333. case LUA_TFUNCTION:
  334. sprintf(buff, "function: %p", lua_topointer(L, 1));
  335. break;
  336. case LUA_TUSERDATA: {
  337. const char *t = lua_typename(L, lua_type(L, 1));
  338. if (strcmp(t, "userdata") == 0)
  339. sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
  340. else
  341. sprintf(buff, "%.40s: %p", t, lua_touserdata(L, 1));
  342. break;
  343. }
  344. case LUA_TNIL:
  345. lua_pushliteral(L, "nil");
  346. return 1;
  347. default:
  348. luaL_argerror(L, 1, "value expected");
  349. }
  350. lua_pushstring(L, buff);
  351. return 1;
  352. }
  353. static int luaB_resume (lua_State *L) {
  354. lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1));
  355. if (lua_resume(L, co) != 0)
  356. lua_error(L, "error running co-routine");
  357. return lua_gettop(L);
  358. }
  359. static int gc_coroutine (lua_State *L) {
  360. lua_State *co = (lua_State *)lua_touserdata(L, 1);
  361. lua_closethread(L, co);
  362. return 0;
  363. }
  364. static int luaB_coroutine (lua_State *L) {
  365. lua_State *NL;
  366. int ref;
  367. int i;
  368. int n = lua_gettop(L);
  369. luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
  370. "Lua function expected");
  371. NL = lua_newthread(L, 0);
  372. if (NL == NULL) lua_error(L, "unable to create new thread");
  373. /* move function and arguments from L to NL */
  374. for (i=0; i<n; i++) {
  375. ref = lua_ref(L, 1);
  376. lua_getref(NL, ref);
  377. lua_insert(NL, 1);
  378. lua_unref(L, ref);
  379. }
  380. lua_cobegin(NL, n-1);
  381. lua_newuserdatabox(L, NL);
  382. lua_getstr(L, LUA_REGISTRYINDEX, "Coroutine");
  383. lua_setmetatable(L, -2);
  384. lua_pushcclosure(L, luaB_resume, 1);
  385. return 1;
  386. }
  387. static int luaB_yield (lua_State *L) {
  388. return lua_yield(L, lua_gettop(L));
  389. }
  390. /*
  391. ** {======================================================
  392. ** Auxiliar table-related functions
  393. */
  394. static int luaB_foreachi (lua_State *L) {
  395. int n, i;
  396. luaL_check_type(L, 1, LUA_TTABLE);
  397. luaL_check_type(L, 2, LUA_TFUNCTION);
  398. n = lua_getn(L, 1);
  399. for (i=1; i<=n; i++) {
  400. lua_pushvalue(L, 2); /* function */
  401. lua_pushnumber(L, i); /* 1st argument */
  402. lua_rawgeti(L, 1, i); /* 2nd argument */
  403. lua_rawcall(L, 2, 1);
  404. if (!lua_isnil(L, -1))
  405. return 1;
  406. lua_pop(L, 1); /* remove nil result */
  407. }
  408. return 0;
  409. }
  410. static int luaB_foreach (lua_State *L) {
  411. luaL_check_type(L, 1, LUA_TTABLE);
  412. luaL_check_type(L, 2, LUA_TFUNCTION);
  413. lua_pushnil(L); /* first key */
  414. for (;;) {
  415. if (lua_next(L, 1) == 0)
  416. return 0;
  417. lua_pushvalue(L, 2); /* function */
  418. lua_pushvalue(L, -3); /* key */
  419. lua_pushvalue(L, -3); /* value */
  420. lua_rawcall(L, 2, 1);
  421. if (!lua_isnil(L, -1))
  422. return 1;
  423. lua_pop(L, 2); /* remove value and result */
  424. }
  425. }
  426. static int luaB_getn (lua_State *L) {
  427. luaL_check_type(L, 1, LUA_TTABLE);
  428. lua_pushnumber(L, lua_getn(L, 1));
  429. return 1;
  430. }
  431. static int luaB_tinsert (lua_State *L) {
  432. int v = lua_gettop(L); /* number of arguments */
  433. int n, pos;
  434. luaL_check_type(L, 1, LUA_TTABLE);
  435. n = lua_getn(L, 1);
  436. if (v == 2) /* called with only 2 arguments */
  437. pos = n+1;
  438. else {
  439. v = 3; /* function may be called with more than 3 args */
  440. pos = luaL_check_int(L, 2); /* 2nd argument is the position */
  441. }
  442. if (pos > n+1) n = pos-1;
  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. {"metatable", luaB_metatable},
  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. }