lbaselib.c 19 KB

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