lbaselib.c 21 KB

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