lbaselib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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_pushliteral(L, "n");
  16. lua_pushnumber(L, n);
  17. lua_rawset(L, t);
  18. }
  19. /*
  20. ** If your system does not support `stderr', redefine this function, or
  21. ** redefine _ERRORMESSAGE so that it won't need _ALERT.
  22. */
  23. static int luaB__ALERT (lua_State *L) {
  24. fputs(luaL_check_string(L, 1), stderr);
  25. return 0;
  26. }
  27. /*
  28. ** Basic implementation of _ERRORMESSAGE.
  29. ** The library `liolib' redefines _ERRORMESSAGE for better error information.
  30. */
  31. static int luaB__ERRORMESSAGE (lua_State *L) {
  32. luaL_check_type(L, 1, LUA_TSTRING);
  33. lua_getglobal(L, LUA_ALERT);
  34. if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
  35. lua_Debug ar;
  36. lua_pushliteral(L, "error: ");
  37. lua_pushvalue(L, 1);
  38. if (lua_getstack(L, 1, &ar)) {
  39. lua_getinfo(L, "Sl", &ar);
  40. if (ar.source && ar.currentline > 0) {
  41. char buff[100];
  42. sprintf(buff, "\n <%.70s: line %d>", ar.short_src, ar.currentline);
  43. lua_pushstring(L, buff);
  44. lua_concat(L, 2);
  45. }
  46. }
  47. lua_pushliteral(L, "\n");
  48. lua_concat(L, 3);
  49. lua_rawcall(L, 1, 0);
  50. }
  51. return 0;
  52. }
  53. /*
  54. ** If your system does not support `stdout', you can just remove this function.
  55. ** If you need, you can define your own `print' function, following this
  56. ** model but changing `fputs' to put the strings at a proper place
  57. ** (a console window or a log file, for instance).
  58. */
  59. static int luaB_print (lua_State *L) {
  60. int n = lua_gettop(L); /* number of arguments */
  61. int i;
  62. lua_getglobal(L, "tostring");
  63. for (i=1; i<=n; i++) {
  64. const char *s;
  65. lua_pushvalue(L, -1); /* function to be called */
  66. lua_pushvalue(L, i); /* value to print */
  67. lua_rawcall(L, 1, 1);
  68. s = lua_tostring(L, -1); /* get result */
  69. if (s == NULL)
  70. lua_error(L, "`tostring' must return a string to `print'");
  71. if (i>1) fputs("\t", stdout);
  72. fputs(s, stdout);
  73. lua_pop(L, 1); /* pop result */
  74. }
  75. fputs("\n", stdout);
  76. return 0;
  77. }
  78. static int luaB_tonumber (lua_State *L) {
  79. int base = luaL_opt_int(L, 2, 10);
  80. if (base == 10) { /* standard conversion */
  81. luaL_check_any(L, 1);
  82. if (lua_isnumber(L, 1)) {
  83. lua_pushnumber(L, lua_tonumber(L, 1));
  84. return 1;
  85. }
  86. }
  87. else {
  88. const char *s1 = luaL_check_string(L, 1);
  89. char *s2;
  90. unsigned long n;
  91. luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
  92. n = strtoul(s1, &s2, base);
  93. if (s1 != s2) { /* at least one valid digit? */
  94. while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
  95. if (*s2 == '\0') { /* no invalid trailing characters? */
  96. lua_pushnumber(L, n);
  97. return 1;
  98. }
  99. }
  100. }
  101. lua_pushnil(L); /* else not a number */
  102. return 1;
  103. }
  104. static int luaB_error (lua_State *L) {
  105. lua_error(L, luaL_opt_string(L, 1, NULL));
  106. return 0; /* to avoid warnings */
  107. }
  108. static int luaB_setglobal (lua_State *L) {
  109. luaL_check_any(L, 2);
  110. lua_setglobal(L, luaL_check_string(L, 1));
  111. return 0;
  112. }
  113. static int luaB_getglobal (lua_State *L) {
  114. lua_getglobal(L, luaL_check_string(L, 1));
  115. return 1;
  116. }
  117. static int luaB_metatable (lua_State *L) {
  118. luaL_check_type(L, 1, LUA_TTABLE);
  119. if (lua_isnone(L, 2))
  120. lua_getmetatable(L, 1);
  121. else {
  122. int t = lua_type(L, 2);
  123. luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, "nil/table expected");
  124. lua_settop(L, 2);
  125. lua_setmetatable(L, 1);
  126. }
  127. return 1;
  128. }
  129. static int luaB_globals (lua_State *L) {
  130. lua_getglobals(L); /* value to be returned */
  131. if (!lua_isnone(L, 1)) {
  132. luaL_check_type(L, 1, LUA_TTABLE);
  133. lua_pushvalue(L, 1); /* new table of globals */
  134. lua_setglobals(L);
  135. }
  136. return 1;
  137. }
  138. static int luaB_rawget (lua_State *L) {
  139. luaL_check_type(L, 1, LUA_TTABLE);
  140. luaL_check_any(L, 2);
  141. lua_rawget(L, 1);
  142. return 1;
  143. }
  144. static int luaB_rawset (lua_State *L) {
  145. luaL_check_type(L, 1, LUA_TTABLE);
  146. luaL_check_any(L, 2);
  147. luaL_check_any(L, 3);
  148. lua_rawset(L, 1);
  149. return 1;
  150. }
  151. static int luaB_gcinfo (lua_State *L) {
  152. lua_pushnumber(L, lua_getgccount(L));
  153. lua_pushnumber(L, lua_getgcthreshold(L));
  154. return 2;
  155. }
  156. static int luaB_collectgarbage (lua_State *L) {
  157. lua_setgcthreshold(L, luaL_opt_int(L, 1, 0));
  158. return 0;
  159. }
  160. static int luaB_type (lua_State *L) {
  161. luaL_check_any(L, 1);
  162. if (lua_isnone(L, 2))
  163. lua_pushstring(L, lua_typename(L, lua_type(L, 1)));
  164. else {
  165. lua_pushboolean(L,
  166. (strcmp(lua_typename(L, lua_type(L, 1)), luaL_check_string(L, 2)) == 0));
  167. }
  168. return 1;
  169. }
  170. static int luaB_next (lua_State *L) {
  171. luaL_check_type(L, 1, LUA_TTABLE);
  172. lua_settop(L, 2); /* create a 2nd argument if there isn't one */
  173. if (lua_next(L, 1))
  174. return 2;
  175. else {
  176. lua_pushnil(L);
  177. return 1;
  178. }
  179. }
  180. static int passresults (lua_State *L, int status, int oldtop) {
  181. if (status == 0) {
  182. int nresults = lua_gettop(L) - oldtop;
  183. if (nresults > 0)
  184. return nresults; /* results are already on the stack */
  185. else {
  186. lua_newuserdatabox(L, NULL); /* at least one result to signal no errors */
  187. return 1;
  188. }
  189. }
  190. else { /* error */
  191. lua_pushnil(L);
  192. lua_pushstring(L, luaL_errstr(status)); /* error code */
  193. return 2;
  194. }
  195. }
  196. static int luaB_dostring (lua_State *L) {
  197. int oldtop = lua_gettop(L);
  198. size_t l;
  199. const char *s = luaL_check_lstr(L, 1, &l);
  200. const char *chunkname = luaL_opt_string(L, 2, s);
  201. return passresults(L, lua_dobuffer(L, s, l, chunkname), oldtop);
  202. }
  203. static int luaB_loadstring (lua_State *L) {
  204. int oldtop = lua_gettop(L);
  205. size_t l;
  206. const char *s = luaL_check_lstr(L, 1, &l);
  207. const char *chunkname = luaL_opt_string(L, 2, s);
  208. return passresults(L, lua_loadbuffer(L, s, l, chunkname), oldtop);
  209. }
  210. static int luaB_dofile (lua_State *L) {
  211. int oldtop = lua_gettop(L);
  212. const char *fname = luaL_opt_string(L, 1, NULL);
  213. return passresults(L, lua_dofile(L, fname), oldtop);
  214. }
  215. static int luaB_loadfile (lua_State *L) {
  216. int oldtop = lua_gettop(L);
  217. const char *fname = luaL_opt_string(L, 1, NULL);
  218. return passresults(L, lua_loadfile(L, fname), oldtop);
  219. }
  220. static int luaB_assert (lua_State *L) {
  221. luaL_check_any(L, 1);
  222. if (!lua_istrue(L, 1))
  223. luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
  224. lua_settop(L, 1);
  225. return 1;
  226. }
  227. #define LUA_PATH "LUA_PATH"
  228. #define LUA_PATH_SEP ";"
  229. #ifndef LUA_PATH_DEFAULT
  230. #define LUA_PATH_DEFAULT "./"
  231. #endif
  232. static int luaB_require (lua_State *L) {
  233. const char *path;
  234. luaL_check_string(L, 1);
  235. lua_settop(L, 1);
  236. lua_getglobal(L, LUA_PATH); /* get path */
  237. if (lua_isstring(L, 2)) /* is LUA_PATH defined? */
  238. path = lua_tostring(L, 2);
  239. else { /* LUA_PATH not defined */
  240. lua_pop(L, 1); /* pop old global value */
  241. path = getenv(LUA_PATH); /* try environment variable */
  242. if (path == NULL) path = LUA_PATH_DEFAULT; /* else use default */
  243. lua_pushstring(L, path);
  244. lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */
  245. lua_setglobal(L, LUA_PATH);
  246. }
  247. lua_pushvalue(L, 1); /* check package's name in book-keeping table */
  248. lua_gettable(L, lua_upvalueindex(1));
  249. if (!lua_isnil(L, -1)) /* is it there? */
  250. return 0; /* package is already loaded */
  251. else { /* must load it */
  252. for (;;) { /* traverse path */
  253. int res;
  254. int l = strcspn(path, LUA_PATH_SEP); /* find separator */
  255. lua_pushlstring(L, path, l); /* directory name */
  256. lua_pushvalue(L, 1); /* package name */
  257. lua_concat(L, 2); /* concat directory with package name */
  258. res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */
  259. lua_settop(L, 2); /* pop string and eventual results from dofile */
  260. if (res == 0) break; /* ok; file done */
  261. else if (res != LUA_ERRFILE)
  262. lua_error(L, NULL); /* error running package; propagate it */
  263. if (*(path+l) == '\0') /* no more directories? */
  264. luaL_verror(L, "could not load package `%.20s' from path `%.200s'",
  265. lua_tostring(L, 1), lua_tostring(L, 2));
  266. path += l+1; /* try next directory */
  267. }
  268. }
  269. lua_pushvalue(L, 1);
  270. lua_pushnumber(L, 1);
  271. lua_settable(L, lua_upvalueindex(1)); /* mark it as loaded */
  272. return 0;
  273. }
  274. static int aux_unpack (lua_State *L, int arg) {
  275. int n, i;
  276. luaL_check_type(L, arg, LUA_TTABLE);
  277. n = lua_getn(L, arg);
  278. luaL_check_stack(L, n, "table too big to unpack");
  279. for (i=1; i<=n; i++) /* push arg[1...n] */
  280. lua_rawgeti(L, arg, i);
  281. return n;
  282. }
  283. static int luaB_unpack (lua_State *L) {
  284. return aux_unpack(L, 1);
  285. }
  286. static int luaB_call (lua_State *L) {
  287. int oldtop;
  288. const char *options = luaL_opt_string(L, 3, "");
  289. int err = 0; /* index of old error method */
  290. int status;
  291. int n;
  292. if (!lua_isnone(L, 4)) { /* set new error method */
  293. lua_getglobal(L, LUA_ERRORMESSAGE);
  294. err = lua_gettop(L); /* get index */
  295. lua_pushvalue(L, 4);
  296. lua_setglobal(L, LUA_ERRORMESSAGE);
  297. }
  298. oldtop = lua_gettop(L); /* top before function-call preparation */
  299. /* push function */
  300. lua_pushvalue(L, 1);
  301. n = aux_unpack(L, 2); /* push arg[1...n] */
  302. status = lua_call(L, n, LUA_MULTRET);
  303. if (err != 0) { /* restore old error method */
  304. lua_pushvalue(L, err);
  305. lua_setglobal(L, LUA_ERRORMESSAGE);
  306. }
  307. if (status != 0) { /* error in call? */
  308. if (strchr(options, 'x'))
  309. lua_pushnil(L); /* return nil to signal the error */
  310. else
  311. lua_error(L, NULL); /* propagate error without additional messages */
  312. return 1;
  313. }
  314. if (strchr(options, 'p')) /* pack results? */
  315. lua_error(L, "obsolete option `p' in `call'");
  316. return lua_gettop(L) - oldtop; /* results are already on the stack */
  317. }
  318. static int luaB_tostring (lua_State *L) {
  319. char buff[64];
  320. switch (lua_type(L, 1)) {
  321. case LUA_TNUMBER:
  322. lua_pushstring(L, lua_tostring(L, 1));
  323. return 1;
  324. case LUA_TSTRING:
  325. lua_pushvalue(L, 1);
  326. return 1;
  327. case LUA_TBOOLEAN:
  328. lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
  329. return 1;
  330. case LUA_TTABLE:
  331. sprintf(buff, "%.40s: %p", lua_typename(L, lua_type(L, 1)),
  332. lua_topointer(L, 1));
  333. break;
  334. case LUA_TFUNCTION:
  335. sprintf(buff, "function: %p", lua_topointer(L, 1));
  336. break;
  337. case LUA_TUSERDATA: {
  338. const char *t = lua_typename(L, lua_type(L, 1));
  339. if (strcmp(t, "userdata") == 0)
  340. sprintf(buff, "userdata: %p", lua_touserdata(L, 1));
  341. else
  342. sprintf(buff, "%.40s: %p", t, lua_touserdata(L, 1));
  343. break;
  344. }
  345. case LUA_TNIL:
  346. lua_pushliteral(L, "nil");
  347. return 1;
  348. default:
  349. luaL_argerror(L, 1, "value expected");
  350. }
  351. lua_pushstring(L, buff);
  352. return 1;
  353. }
  354. static int luaB_resume (lua_State *L) {
  355. lua_State *co = (lua_State *)lua_touserdata(L, lua_upvalueindex(1));
  356. if (lua_resume(L, co) != 0)
  357. lua_error(L, "error running co-routine");
  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_pushliteral(L, "Coroutine");
  384. lua_rawget(L, LUA_REGISTRYINDEX);
  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_pushliteral(L, "Coroutine");
  612. lua_newtable(L);
  613. lua_pushliteral(L, "gc");
  614. lua_pushcfunction(L, gc_coroutine);
  615. lua_rawset(L, -3);
  616. lua_rawset(L, LUA_REGISTRYINDEX);
  617. return 0;
  618. }