lbaselib.c 20 KB

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