lbaselib.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. ** $Id: lbaselib.c,v 1.30 2001/03/07 12:43:52 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 luaB_pack (lua_State *L) {
  315. int n = lua_gettop(L);
  316. lua_newtable(L);
  317. aux_setn(L, -3, n);
  318. lua_insert(L, 1);
  319. while (n)
  320. lua_rawseti(L, 1, n--);
  321. return 1;
  322. }
  323. static int aux_unpack (lua_State *L, int arg) {
  324. int n, i;
  325. luaL_checktype(L, arg, LUA_TTABLE);
  326. n = lua_getn(L, arg);
  327. luaL_checkstack(L, n, l_s("too many arguments"));
  328. for (i=1; i<=n; i++) /* push arg[1...n] */
  329. lua_rawgeti(L, arg, i);
  330. return n;
  331. }
  332. static int luaB_unpack (lua_State *L) {
  333. return aux_unpack(L, 1);
  334. }
  335. static int luaB_call (lua_State *L) {
  336. int oldtop;
  337. const l_char *options = luaL_opt_string(L, 3, l_s(""));
  338. int err = 0; /* index of old error method */
  339. int status;
  340. int n;
  341. if (!lua_isnull(L, 4)) { /* set new error method */
  342. lua_getglobal(L, l_s(LUA_ERRORMESSAGE));
  343. err = lua_gettop(L); /* get index */
  344. lua_pushvalue(L, 4);
  345. lua_setglobal(L, l_s(LUA_ERRORMESSAGE));
  346. }
  347. oldtop = lua_gettop(L); /* top before function-call preparation */
  348. /* push function */
  349. lua_pushvalue(L, 1);
  350. n = aux_unpack(L, 2); /* push arg[1...n] */
  351. status = lua_call(L, n, LUA_MULTRET);
  352. if (err != 0) { /* restore old error method */
  353. lua_pushvalue(L, err);
  354. lua_setglobal(L, l_s(LUA_ERRORMESSAGE));
  355. }
  356. if (status != 0) { /* error in call? */
  357. if (strchr(options, l_c('x')))
  358. lua_pushnil(L); /* return nil to signal the error */
  359. else
  360. lua_error(L, NULL); /* propagate error without additional messages */
  361. return 1;
  362. }
  363. if (strchr(options, l_c('p'))) /* pack results? */
  364. lua_error(L, l_s("deprecated option `p' in `call'"));
  365. return lua_gettop(L) - oldtop; /* results are already on the stack */
  366. }
  367. static int luaB_tostring (lua_State *L) {
  368. l_char buff[64];
  369. switch (lua_type(L, 1)) {
  370. case LUA_TNUMBER:
  371. lua_pushstring(L, lua_tostring(L, 1));
  372. return 1;
  373. case LUA_TSTRING:
  374. lua_pushvalue(L, 1);
  375. return 1;
  376. case LUA_TTABLE:
  377. sprintf(buff, l_s("%.40s: %p"), lua_xtype(L, 1), lua_topointer(L, 1));
  378. break;
  379. case LUA_TFUNCTION:
  380. sprintf(buff, l_s("function: %p"), lua_topointer(L, 1));
  381. break;
  382. case LUA_TUSERDATA: {
  383. const l_char *t = lua_xtype(L, 1);
  384. if (strcmp(t, l_s("userdata")) == 0)
  385. sprintf(buff, l_s("userdata(%d): %p"), lua_tag(L, 1),
  386. lua_touserdata(L, 1));
  387. else
  388. sprintf(buff, l_s("%.40s: %p"), t, lua_touserdata(L, 1));
  389. break;
  390. }
  391. case LUA_TNIL:
  392. lua_pushliteral(L, l_s("nil"));
  393. return 1;
  394. default:
  395. luaL_argerror(L, 1, l_s("value expected"));
  396. }
  397. lua_pushstring(L, buff);
  398. return 1;
  399. }
  400. static int luaB_foreachi (lua_State *L) {
  401. int n, i;
  402. luaL_checktype(L, 1, LUA_TTABLE);
  403. luaL_checktype(L, 2, LUA_TFUNCTION);
  404. n = lua_getn(L, 1);
  405. for (i=1; i<=n; i++) {
  406. lua_pushvalue(L, 2); /* function */
  407. lua_pushnumber(L, i); /* 1st argument */
  408. lua_rawgeti(L, 1, i); /* 2nd argument */
  409. lua_rawcall(L, 2, 1);
  410. if (!lua_isnil(L, -1))
  411. return 1;
  412. lua_pop(L, 1); /* remove nil result */
  413. }
  414. return 0;
  415. }
  416. static int luaB_foreach (lua_State *L) {
  417. luaL_checktype(L, 1, LUA_TTABLE);
  418. luaL_checktype(L, 2, LUA_TFUNCTION);
  419. lua_pushnil(L); /* first index */
  420. for (;;) {
  421. if (lua_next(L, 1) == 0)
  422. return 0;
  423. lua_pushvalue(L, 2); /* function */
  424. lua_pushvalue(L, -3); /* key */
  425. lua_pushvalue(L, -3); /* value */
  426. lua_rawcall(L, 2, 1);
  427. if (!lua_isnil(L, -1))
  428. return 1;
  429. lua_pop(L, 2); /* remove value and result */
  430. }
  431. }
  432. static int luaB_assert (lua_State *L) {
  433. luaL_checkany(L, 1);
  434. if (lua_isnil(L, 1))
  435. luaL_verror(L, l_s("assertion failed! %.90s"), luaL_opt_string(L, 2, l_s("")));
  436. lua_settop(L, 1);
  437. return 1;
  438. }
  439. static int luaB_getn (lua_State *L) {
  440. luaL_checktype(L, 1, LUA_TTABLE);
  441. lua_pushnumber(L, lua_getn(L, 1));
  442. return 1;
  443. }
  444. static int luaB_tinsert (lua_State *L) {
  445. int v = lua_gettop(L); /* last argument: to be inserted */
  446. int n, pos;
  447. luaL_checktype(L, 1, LUA_TTABLE);
  448. n = lua_getn(L, 1);
  449. if (v == 2) /* called with only 2 arguments */
  450. pos = n+1;
  451. else
  452. pos = luaL_check_int(L, 2); /* 2nd argument is the position */
  453. aux_setn(L, 1, n+1); /* t.n = n+1 */
  454. for (; n>=pos; n--) {
  455. lua_rawgeti(L, 1, n);
  456. lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
  457. }
  458. lua_pushvalue(L, v);
  459. lua_rawseti(L, 1, pos); /* t[pos] = v */
  460. return 0;
  461. }
  462. static int luaB_tremove (lua_State *L) {
  463. int pos, n;
  464. luaL_checktype(L, 1, LUA_TTABLE);
  465. n = lua_getn(L, 1);
  466. pos = luaL_opt_int(L, 2, n);
  467. if (n <= 0) return 0; /* table is `empty' */
  468. aux_setn(L, 1, n-1); /* t.n = n-1 */
  469. lua_rawgeti(L, 1, pos); /* result = t[pos] */
  470. for ( ;pos<n; pos++) {
  471. lua_rawgeti(L, 1, pos+1);
  472. lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */
  473. }
  474. lua_pushnil(L);
  475. lua_rawseti(L, 1, n); /* t[n] = nil */
  476. return 1;
  477. }
  478. /*
  479. ** {======================================================
  480. ** Quicksort
  481. ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
  482. ** Addison-Wesley, 1993.)
  483. */
  484. static void set2 (lua_State *L, int i, int j) {
  485. lua_rawseti(L, 1, i);
  486. lua_rawseti(L, 1, j);
  487. }
  488. static int sort_comp (lua_State *L, int a, int b) {
  489. /* WARNING: the caller (auxsort) must ensure stack space */
  490. if (!lua_isnil(L, 2)) { /* function? */
  491. int res;
  492. lua_pushvalue(L, 2);
  493. lua_pushvalue(L, a-1); /* -1 to compensate function */
  494. lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
  495. lua_rawcall(L, 2, 1);
  496. res = !lua_isnil(L, -1);
  497. lua_pop(L, 1);
  498. return res;
  499. }
  500. else /* a < b? */
  501. return lua_lessthan(L, a, b);
  502. }
  503. static void auxsort (lua_State *L, int l, int u) {
  504. while (l < u) { /* for tail recursion */
  505. int i, j;
  506. /* sort elements a[l], a[(l+u)/2] and a[u] */
  507. lua_rawgeti(L, 1, l);
  508. lua_rawgeti(L, 1, u);
  509. if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */
  510. set2(L, l, u); /* swap a[l] - a[u] */
  511. else
  512. lua_pop(L, 2);
  513. if (u-l == 1) break; /* only 2 elements */
  514. i = (l+u)/2;
  515. lua_rawgeti(L, 1, i);
  516. lua_rawgeti(L, 1, l);
  517. if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
  518. set2(L, i, l);
  519. else {
  520. lua_pop(L, 1); /* remove a[l] */
  521. lua_rawgeti(L, 1, u);
  522. if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */
  523. set2(L, i, u);
  524. else
  525. lua_pop(L, 2);
  526. }
  527. if (u-l == 2) break; /* only 3 elements */
  528. lua_rawgeti(L, 1, i); /* Pivot */
  529. lua_pushvalue(L, -1);
  530. lua_rawgeti(L, 1, u-1);
  531. set2(L, i, u-1);
  532. /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
  533. i = l; j = u-1;
  534. for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
  535. /* repeat ++i until a[i] >= P */
  536. while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
  537. if (i>u) lua_error(L, l_s("invalid order function for sorting"));
  538. lua_pop(L, 1); /* remove a[i] */
  539. }
  540. /* repeat --j until a[j] <= P */
  541. while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
  542. if (j<l) lua_error(L, l_s("invalid order function for sorting"));
  543. lua_pop(L, 1); /* remove a[j] */
  544. }
  545. if (j<i) {
  546. lua_pop(L, 3); /* pop pivot, a[i], a[j] */
  547. break;
  548. }
  549. set2(L, i, j);
  550. }
  551. lua_rawgeti(L, 1, u-1);
  552. lua_rawgeti(L, 1, i);
  553. set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
  554. /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
  555. /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
  556. if (i-l < u-i) {
  557. j=l; i=i-1; l=i+2;
  558. }
  559. else {
  560. j=i+1; i=u; u=j-2;
  561. }
  562. auxsort(L, j, i); /* call recursively the smaller one */
  563. } /* repeat the routine for the larger one */
  564. }
  565. static int luaB_sort (lua_State *L) {
  566. int n;
  567. luaL_checktype(L, 1, LUA_TTABLE);
  568. n = lua_getn(L, 1);
  569. if (!lua_isnull(L, 2)) /* is there a 2nd argument? */
  570. luaL_checktype(L, 2, LUA_TFUNCTION);
  571. lua_settop(L, 2); /* make sure there is two arguments */
  572. auxsort(L, 1, n);
  573. return 0;
  574. }
  575. /* }====================================================== */
  576. /*
  577. ** {======================================================
  578. ** Deprecated functions to manipulate global environment.
  579. ** =======================================================
  580. */
  581. #define num_deprecated 4
  582. static const luaL_reg deprecated_names [num_deprecated] = {
  583. {l_s("foreachvar"), luaB_foreach},
  584. {l_s("nextvar"), luaB_next},
  585. {l_s("rawgetglobal"), luaB_rawget},
  586. {l_s("rawsetglobal"), luaB_rawset}
  587. };
  588. #ifdef LUA_DEPRECATEDFUNCS
  589. /*
  590. ** call corresponding function inserting `globals' as first argument
  591. */
  592. static int deprecated_func (lua_State *L) {
  593. lua_insert(L, 1); /* upvalue is the function to be called */
  594. lua_getglobals(L);
  595. lua_insert(L, 2); /* table of globals is 1o argument */
  596. lua_rawcall(L, lua_gettop(L)-1, LUA_MULTRET);
  597. return lua_gettop(L); /* return all results */
  598. }
  599. static void deprecated_funcs (lua_State *L) {
  600. int i;
  601. for (i=0; i<num_deprecated; i++) {
  602. lua_pushcfunction(L, deprecated_names[i].func);
  603. lua_pushcclosure(L, deprecated_func, 1);
  604. lua_setglobal(L, deprecated_names[i].name);
  605. }
  606. }
  607. #else
  608. /*
  609. ** gives an explicit error in any attempt to call a deprecated function
  610. */
  611. static int deprecated_func (lua_State *L) {
  612. luaL_verror(L, l_s("function `%.20s' is deprecated"), lua_tostring(L, -1));
  613. return 0; /* to avoid warnings */
  614. }
  615. static void deprecated_funcs (lua_State *L) {
  616. int i;
  617. for (i=0; i<num_deprecated; i++) {
  618. lua_pushstring(L, deprecated_names[i].name);
  619. lua_pushcclosure(L, deprecated_func, 1);
  620. lua_setglobal(L, deprecated_names[i].name);
  621. }
  622. }
  623. #endif
  624. /* }====================================================== */
  625. static const luaL_reg base_funcs[] = {
  626. {l_s(LUA_ALERT), luaB__ALERT},
  627. {l_s(LUA_ERRORMESSAGE), luaB__ERRORMESSAGE},
  628. {l_s("call"), luaB_call},
  629. {l_s("collectgarbage"), luaB_collectgarbage},
  630. {l_s("copytagmethods"), luaB_copytagmethods},
  631. {l_s("dofile"), luaB_dofile},
  632. {l_s("dostring"), luaB_dostring},
  633. {l_s("error"), luaB_error},
  634. {l_s("foreach"), luaB_foreach},
  635. {l_s("foreachi"), luaB_foreachi},
  636. {l_s("gcinfo"), luaB_gcinfo},
  637. {l_s("getglobal"), luaB_getglobal},
  638. {l_s("gettagmethod"), luaB_gettagmethod},
  639. {l_s("globals"), luaB_globals},
  640. {l_s("newtype"), luaB_newtype},
  641. {l_s("newtag"), luaB_newtype}, /* for compatibility 4.0 */
  642. {l_s("next"), luaB_next},
  643. {l_s("print"), luaB_print},
  644. {l_s("rawget"), luaB_rawget},
  645. {l_s("rawset"), luaB_rawset},
  646. {l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */
  647. {l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */
  648. {l_s("require"), luaB_require},
  649. {l_s("setglobal"), luaB_setglobal},
  650. {l_s("settag"), luaB_settag},
  651. {l_s("settagmethod"), luaB_settagmethod},
  652. {l_s("tag"), luaB_tag},
  653. {l_s("tonumber"), luaB_tonumber},
  654. {l_s("tostring"), luaB_tostring},
  655. {l_s("type"), luaB_type},
  656. {l_s("assert"), luaB_assert},
  657. {l_s("getn"), luaB_getn},
  658. {l_s("sort"), luaB_sort},
  659. {l_s("tinsert"), luaB_tinsert},
  660. {l_s("tremove"), luaB_tremove},
  661. {l_s("pack"), luaB_pack},
  662. {l_s("unpack"), luaB_unpack},
  663. {l_s("xtype"), luaB_xtype},
  664. };
  665. LUALIB_API int lua_baselibopen (lua_State *L) {
  666. luaL_openl(L, base_funcs);
  667. lua_pushliteral(L, l_s(LUA_VERSION));
  668. lua_setglobal(L, l_s("_VERSION"));
  669. deprecated_funcs(L);
  670. return 0;
  671. }