diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-09-05 16:28:46 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-09-05 16:28:46 -0300 |
| commit | f67f324377aff66d78479eaaffbb94a6b092ae45 (patch) | |
| tree | 9b0966baf279d7467a8f52102df176653ef284c3 | |
| parent | eebc9729e4614d1c3da7757022cb6d73babd0e0a (diff) | |
| download | lua-f67f324377aff66d78479eaaffbb94a6b092ae45.tar.gz lua-f67f324377aff66d78479eaaffbb94a6b092ae45.tar.bz2 lua-f67f324377aff66d78479eaaffbb94a6b092ae45.zip | |
deprecated files
| -rw-r--r-- | lbuiltin.c | 701 | ||||
| -rw-r--r-- | lbuiltin.h | 44 |
2 files changed, 0 insertions, 745 deletions
diff --git a/lbuiltin.c b/lbuiltin.c deleted file mode 100644 index 4f75a5bc..00000000 --- a/lbuiltin.c +++ /dev/null | |||
| @@ -1,701 +0,0 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: lbuiltin.c,v 1.127 2000/08/31 20:23:40 roberto Exp roberto $ | ||
| 3 | ** Built-in functions | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | |||
| 8 | /* | ||
| 9 | ** ========================================================================= | ||
| 10 | ** All built-in functions are public (i.e. not static) and are named luaB_f, | ||
| 11 | ** where f is the function name in Lua. So, if you do not need all these | ||
| 12 | ** functions, you may register manually only the ones that you need. | ||
| 13 | ** ========================================================================= | ||
| 14 | */ | ||
| 15 | |||
| 16 | |||
| 17 | #include <ctype.h> | ||
| 18 | #include <stdio.h> | ||
| 19 | #include <stdlib.h> | ||
| 20 | #include <string.h> | ||
| 21 | |||
| 22 | #include "lua.h" | ||
| 23 | |||
| 24 | #include "lauxlib.h" | ||
| 25 | #include "lbuiltin.h" | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | /* | ||
| 30 | ** function defined in ltests.c, to open some internal-test functions | ||
| 31 | */ | ||
| 32 | void luaB_opentests (lua_State *L); | ||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | /* | ||
| 38 | ** {====================================================== | ||
| 39 | ** Functions that use only the official API | ||
| 40 | ** ======================================================= | ||
| 41 | */ | ||
| 42 | |||
| 43 | |||
| 44 | /* | ||
| 45 | ** If your system does not support `stderr', redefine this function, or | ||
| 46 | ** redefine _ERRORMESSAGE so that it won't need _ALERT. | ||
| 47 | */ | ||
| 48 | int luaB__ALERT (lua_State *L) { | ||
| 49 | fputs(luaL_check_string(L, 1), stderr); | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | /* | ||
| 55 | ** Standard implementation of _ERRORMESSAGE. | ||
| 56 | ** The library `liolib' redefines _ERRORMESSAGE for better error information. | ||
| 57 | */ | ||
| 58 | int luaB__ERRORMESSAGE (lua_State *L) { | ||
| 59 | lua_getglobals(L); | ||
| 60 | lua_pushstring(L, LUA_ALERT); | ||
| 61 | lua_rawget(L); | ||
| 62 | if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */ | ||
| 63 | const char *s = luaL_check_string(L, 1); | ||
| 64 | char *buff = luaL_openspace(L, strlen(s)+sizeof("error: \n")); | ||
| 65 | strcpy(buff, "error: "); strcat(buff, s); strcat(buff, "\n"); | ||
| 66 | lua_pushobject(L, -1); /* function to be called */ | ||
| 67 | lua_pushstring(L, buff); | ||
| 68 | lua_call(L, 1, 0); | ||
| 69 | } | ||
| 70 | return 0; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | /* | ||
| 75 | ** If your system does not support `stdout', you can just remove this function. | ||
| 76 | ** If you need, you can define your own `print' function, following this | ||
| 77 | ** model but changing `fputs' to put the strings at a proper place | ||
| 78 | ** (a console window or a log file, for instance). | ||
| 79 | */ | ||
| 80 | int luaB_print (lua_State *L) { | ||
| 81 | int n = lua_gettop(L); /* number of arguments */ | ||
| 82 | int i; | ||
| 83 | lua_getglobal(L, "tostring"); | ||
| 84 | for (i=1; i<=n; i++) { | ||
| 85 | const char *s; | ||
| 86 | lua_pushobject(L, -1); /* function to be called */ | ||
| 87 | lua_pushobject(L, i); | ||
| 88 | if (lua_call(L, 1, 1) != 0) | ||
| 89 | lua_error(L, "error in `tostring' called by `print'"); | ||
| 90 | s = lua_tostring(L, -1); /* get result */ | ||
| 91 | if (s == NULL) | ||
| 92 | lua_error(L, "`tostring' must return a string to `print'"); | ||
| 93 | if (i>1) fputs("\t", stdout); | ||
| 94 | fputs(s, stdout); | ||
| 95 | lua_pop(L, 1); /* pop result */ | ||
| 96 | } | ||
| 97 | fputs("\n", stdout); | ||
| 98 | return 0; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | int luaB_tonumber (lua_State *L) { | ||
| 103 | int base = luaL_opt_int(L, 2, 10); | ||
| 104 | if (base == 10) { /* standard conversion */ | ||
| 105 | luaL_checktype(L, 1, "any"); | ||
| 106 | if (lua_isnumber(L, 1)) { | ||
| 107 | lua_pushnumber(L, lua_tonumber(L, 1)); | ||
| 108 | return 1; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | const char *s1 = luaL_check_string(L, 1); | ||
| 113 | char *s2; | ||
| 114 | unsigned long n; | ||
| 115 | luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range"); | ||
| 116 | n = strtoul(s1, &s2, base); | ||
| 117 | if (s1 != s2) { /* at least one valid digit? */ | ||
| 118 | while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */ | ||
| 119 | if (*s2 == '\0') { /* no invalid trailing characters? */ | ||
| 120 | lua_pushnumber(L, n); | ||
| 121 | return 1; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | lua_pushnil(L); /* else not a number */ | ||
| 126 | return 1; | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | int luaB_error (lua_State *L) { | ||
| 131 | lua_error(L, luaL_opt_string(L, 1, NULL)); | ||
| 132 | return 0; /* to avoid errors */ | ||
| 133 | } | ||
| 134 | |||
| 135 | int luaB_setglobal (lua_State *L) { | ||
| 136 | luaL_checktype(L, 2, "any"); | ||
| 137 | lua_setglobal(L, luaL_check_string(L, 1)); | ||
| 138 | return 0; | ||
| 139 | } | ||
| 140 | |||
| 141 | int luaB_getglobal (lua_State *L) { | ||
| 142 | lua_getglobal(L, luaL_check_string(L, 1)); | ||
| 143 | return 1; | ||
| 144 | } | ||
| 145 | |||
| 146 | int luaB_tag (lua_State *L) { | ||
| 147 | luaL_checktype(L, 1, "any"); | ||
| 148 | lua_pushnumber(L, lua_tag(L, 1)); | ||
| 149 | return 1; | ||
| 150 | } | ||
| 151 | |||
| 152 | int luaB_settag (lua_State *L) { | ||
| 153 | luaL_checktype(L, 1, "table"); | ||
| 154 | lua_pushobject(L, 1); /* push table */ | ||
| 155 | lua_settag(L, luaL_check_int(L, 2)); | ||
| 156 | lua_pushobject(L, 1); /* return first argument */ | ||
| 157 | return 1; | ||
| 158 | } | ||
| 159 | |||
| 160 | int luaB_newtag (lua_State *L) { | ||
| 161 | lua_pushnumber(L, lua_newtag(L)); | ||
| 162 | return 1; | ||
| 163 | } | ||
| 164 | |||
| 165 | int luaB_copytagmethods (lua_State *L) { | ||
| 166 | lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1), | ||
| 167 | luaL_check_int(L, 2))); | ||
| 168 | return 1; | ||
| 169 | } | ||
| 170 | |||
| 171 | int luaB_globals (lua_State *L) { | ||
| 172 | lua_getglobals(L); /* value to be returned */ | ||
| 173 | if (!lua_isnull(L, 1)) { | ||
| 174 | luaL_checktype(L, 1, "table"); | ||
| 175 | lua_pushobject(L, 1); /* new table of globals */ | ||
| 176 | lua_setglobals(L); | ||
| 177 | } | ||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | int luaB_rawget (lua_State *L) { | ||
| 182 | luaL_checktype(L, 1, "table"); | ||
| 183 | luaL_checktype(L, 2, "any"); | ||
| 184 | lua_rawget(L); | ||
| 185 | return 1; | ||
| 186 | } | ||
| 187 | |||
| 188 | int luaB_rawset (lua_State *L) { | ||
| 189 | luaL_checktype(L, 1, "table"); | ||
| 190 | luaL_checktype(L, 2, "any"); | ||
| 191 | luaL_checktype(L, 3, "any"); | ||
| 192 | lua_rawset(L); | ||
| 193 | return 1; | ||
| 194 | } | ||
| 195 | |||
| 196 | int luaB_settagmethod (lua_State *L) { | ||
| 197 | int tag = (int)luaL_check_int(L, 1); | ||
| 198 | const char *event = luaL_check_string(L, 2); | ||
| 199 | luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3, | ||
| 200 | "function or nil expected"); | ||
| 201 | lua_pushnil(L); /* to get its tag */ | ||
| 202 | if (strcmp(event, "gc") == 0 && tag != lua_tag(L, -1)) | ||
| 203 | lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua"); | ||
| 204 | lua_pop(L, 1); /* remove the nil */ | ||
| 205 | lua_settagmethod(L, tag, event); | ||
| 206 | return 1; | ||
| 207 | } | ||
| 208 | |||
| 209 | int luaB_gettagmethod (lua_State *L) { | ||
| 210 | lua_gettagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2)); | ||
| 211 | return 1; | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | int luaB_collectgarbage (lua_State *L) { | ||
| 216 | lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0))); | ||
| 217 | return 1; | ||
| 218 | } | ||
| 219 | |||
| 220 | |||
| 221 | int luaB_type (lua_State *L) { | ||
| 222 | luaL_checktype(L, 1, "any"); | ||
| 223 | lua_pushstring(L, lua_type(L, 1)); | ||
| 224 | return 1; | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | int luaB_next (lua_State *L) { | ||
| 229 | luaL_checktype(L, 1, "table"); | ||
| 230 | lua_settop(L, 2); /* create a 2nd argument if there isn't one */ | ||
| 231 | if (lua_next(L)) | ||
| 232 | return 2; | ||
| 233 | else { | ||
| 234 | lua_pushnil(L); | ||
| 235 | return 1; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | static int passresults (lua_State *L, int status, int oldtop) { | ||
| 241 | if (status == 0) { | ||
| 242 | int nresults = lua_gettop(L) - oldtop; | ||
| 243 | if (nresults > 0) | ||
| 244 | return nresults; /* results are already on the stack */ | ||
| 245 | else { | ||
| 246 | lua_pushuserdata(L, NULL); /* at least one result to signal no errors */ | ||
| 247 | return 1; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | else { /* error */ | ||
| 251 | lua_pushnil(L); | ||
| 252 | lua_pushnumber(L, status); /* error code */ | ||
| 253 | return 2; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | int luaB_dostring (lua_State *L) { | ||
| 258 | int oldtop = lua_gettop(L); | ||
| 259 | size_t l; | ||
| 260 | const char *s = luaL_check_lstr(L, 1, &l); | ||
| 261 | if (*s == '\27') /* binary files start with ESC... */ | ||
| 262 | lua_error(L, "`dostring' cannot run pre-compiled code"); | ||
| 263 | return passresults(L, lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)), oldtop); | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | int luaB_dofile (lua_State *L) { | ||
| 268 | int oldtop = lua_gettop(L); | ||
| 269 | const char *fname = luaL_opt_string(L, 1, NULL); | ||
| 270 | return passresults(L, lua_dofile(L, fname), oldtop); | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | int luaB_call (lua_State *L) { | ||
| 275 | int oldtop; | ||
| 276 | const char *options = luaL_opt_string(L, 3, ""); | ||
| 277 | int err = 0; /* index of old error method */ | ||
| 278 | int i, status; | ||
| 279 | int n; | ||
| 280 | luaL_checktype(L, 2, "table"); | ||
| 281 | n = lua_getn(L, 2); | ||
| 282 | if (!lua_isnull(L, 4)) { /* set new error method */ | ||
| 283 | lua_getglobal(L, LUA_ERRORMESSAGE); | ||
| 284 | err = lua_gettop(L); /* get index */ | ||
| 285 | lua_pushobject(L, 4); | ||
| 286 | lua_setglobal(L, LUA_ERRORMESSAGE); | ||
| 287 | } | ||
| 288 | oldtop = lua_gettop(L); /* top before function-call preparation */ | ||
| 289 | /* push function */ | ||
| 290 | lua_pushobject(L, 1); | ||
| 291 | /* push arg[1...n] */ | ||
| 292 | luaL_checkstack(L, n, "too many arguments"); | ||
| 293 | for (i=0; i<n; i++) { | ||
| 294 | lua_pushobject(L, 2); | ||
| 295 | lua_pushnumber(L, i+1); | ||
| 296 | lua_rawget(L); | ||
| 297 | } | ||
| 298 | status = lua_call(L, n, LUA_MULTRET); | ||
| 299 | if (err != 0) { /* restore old error method */ | ||
| 300 | lua_pushobject(L, err); | ||
| 301 | lua_setglobal(L, LUA_ERRORMESSAGE); | ||
| 302 | } | ||
| 303 | if (status != 0) { /* error in call? */ | ||
| 304 | if (strchr(options, 'x')) | ||
| 305 | lua_pushnil(L); /* return nil to signal the error */ | ||
| 306 | else | ||
| 307 | lua_error(L, NULL); /* propagate error without additional messages */ | ||
| 308 | return 1; | ||
| 309 | } | ||
| 310 | if (strchr(options, 'p')) /* pack results? */ | ||
| 311 | lua_error(L, "deprecated option `p' in `call'"); | ||
| 312 | return lua_gettop(L) - oldtop; /* results are already on the stack */ | ||
| 313 | } | ||
| 314 | |||
| 315 | |||
| 316 | int luaB_tostring (lua_State *L) { | ||
| 317 | char buff[64]; | ||
| 318 | switch (lua_type(L, 1)[2]) { | ||
| 319 | case 'm': /* nuMber */ | ||
| 320 | lua_pushstring(L, lua_tostring(L, 1)); | ||
| 321 | return 1; | ||
| 322 | case 'r': /* stRing */ | ||
| 323 | lua_pushobject(L, 1); | ||
| 324 | return 1; | ||
| 325 | case 'b': /* taBle */ | ||
| 326 | sprintf(buff, "table: %p", lua_topointer(L, 1)); | ||
| 327 | break; | ||
| 328 | case 'n': /* fuNction */ | ||
| 329 | sprintf(buff, "function: %p", lua_topointer(L, 1)); | ||
| 330 | break; | ||
| 331 | case 'e': /* usErdata */ | ||
| 332 | sprintf(buff, "userdata(%d): %p", lua_tag(L, 1), lua_touserdata(L, 1)); | ||
| 333 | break; | ||
| 334 | case 'l': /* niL */ | ||
| 335 | lua_pushstring(L, "nil"); | ||
| 336 | return 1; | ||
| 337 | default: | ||
| 338 | luaL_argerror(L, 1, "value expected"); | ||
| 339 | } | ||
| 340 | lua_pushstring(L, buff); | ||
| 341 | return 1; | ||
| 342 | } | ||
| 343 | |||
| 344 | /* }====================================================== */ | ||
| 345 | |||
| 346 | |||
| 347 | /* | ||
| 348 | ** {====================================================== | ||
| 349 | ** Functions that could use only the official API but | ||
| 350 | ** do not, for efficiency. | ||
| 351 | ** ======================================================= | ||
| 352 | */ | ||
| 353 | |||
| 354 | #include "lapi.h" | ||
| 355 | #include "ldo.h" | ||
| 356 | #include "lmem.h" | ||
| 357 | #include "lobject.h" | ||
| 358 | #include "lstate.h" | ||
| 359 | #include "lstring.h" | ||
| 360 | #include "ltable.h" | ||
| 361 | #include "ltm.h" | ||
| 362 | #include "lvm.h" | ||
| 363 | |||
| 364 | |||
| 365 | /* | ||
| 366 | ** {====================================================== | ||
| 367 | ** Auxiliary functions | ||
| 368 | ** ======================================================= | ||
| 369 | */ | ||
| 370 | |||
| 371 | |||
| 372 | static Hash *gettable (lua_State *L, int arg) { | ||
| 373 | luaL_checktype(L, arg, "table"); | ||
| 374 | return hvalue(luaA_index(L, arg)); | ||
| 375 | } | ||
| 376 | |||
| 377 | /* }====================================================== */ | ||
| 378 | |||
| 379 | |||
| 380 | |||
| 381 | |||
| 382 | |||
| 383 | |||
| 384 | /* }====================================================== */ | ||
| 385 | |||
| 386 | |||
| 387 | |||
| 388 | /* | ||
| 389 | ** {====================================================== | ||
| 390 | ** "Extra" functions | ||
| 391 | ** These functions can be written in Lua, so you can | ||
| 392 | ** delete them if you need a tiny Lua implementation. | ||
| 393 | ** If you delete them, remove their entries in array | ||
| 394 | ** "builtin_funcs". | ||
| 395 | ** ======================================================= | ||
| 396 | */ | ||
| 397 | |||
| 398 | int luaB_assert (lua_State *L) { | ||
| 399 | luaL_checktype(L, 1, "any"); | ||
| 400 | if (lua_isnil(L, 1)) | ||
| 401 | luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, "")); | ||
| 402 | return 0; | ||
| 403 | } | ||
| 404 | |||
| 405 | |||
| 406 | int luaB_getn (lua_State *L) { | ||
| 407 | luaL_checktype(L, 1, "table"); | ||
| 408 | lua_pushnumber(L, lua_getn(L, 1)); | ||
| 409 | return 1; | ||
| 410 | } | ||
| 411 | |||
| 412 | |||
| 413 | /* auxiliary function */ | ||
| 414 | static void t_move (lua_State *L, Hash *t, int from, int to) { | ||
| 415 | TObject *p = luaH_setint(L, t, to); /* may change following `get' */ | ||
| 416 | *p = *luaH_getnum(t, from); | ||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | int luaB_tinsert (lua_State *L) { | ||
| 421 | Hash *a = gettable(L, 1); | ||
| 422 | int n = lua_getn(L, 1); | ||
| 423 | int v = lua_gettop(L); /* last argument: to be inserted */ | ||
| 424 | int pos; | ||
| 425 | if (v == 2) /* called with only 2 arguments */ | ||
| 426 | pos = n+1; | ||
| 427 | else | ||
| 428 | pos = luaL_check_int(L, 2); /* 2nd argument is the position */ | ||
| 429 | luaH_setstrnum(L, a, luaS_new(L, "n"), n+1); /* a.n = n+1 */ | ||
| 430 | for (; n>=pos; n--) | ||
| 431 | t_move(L, a, n, n+1); /* a[n+1] = a[n] */ | ||
| 432 | *luaH_setint(L, a, pos) = *luaA_index(L, v); /* a[pos] = v */ | ||
| 433 | return 0; | ||
| 434 | } | ||
| 435 | |||
| 436 | |||
| 437 | int luaB_tremove (lua_State *L) { | ||
| 438 | Hash *a = gettable(L, 1); | ||
| 439 | int n = lua_getn(L, 1); | ||
| 440 | int pos = luaL_opt_int(L, 2, n); | ||
| 441 | if (n <= 0) return 0; /* table is "empty" */ | ||
| 442 | luaA_pushobject(L, luaH_getnum(a, pos)); /* result = a[pos] */ | ||
| 443 | for ( ;pos<n; pos++) | ||
| 444 | t_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */ | ||
| 445 | luaH_setstrnum(L, a, luaS_new(L, "n"), n-1); /* a.n = n-1 */ | ||
| 446 | ttype(luaH_setint(L, a, n)) = TAG_NIL; /* a[n] = nil */ | ||
| 447 | return 1; | ||
| 448 | } | ||
| 449 | |||
| 450 | |||
| 451 | static int luaB_foreachi (lua_State *L) { | ||
| 452 | const Hash *t = gettable(L, 1); | ||
| 453 | int n = lua_getn(L, 1); | ||
| 454 | int i; | ||
| 455 | luaL_checktype(L, 2, "function"); | ||
| 456 | for (i=1; i<=n; i++) { | ||
| 457 | lua_pushobject(L, 2); | ||
| 458 | ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i; | ||
| 459 | *(L->top++) = *luaH_getnum(t, i); | ||
| 460 | luaD_call(L, L->top-3, 1); | ||
| 461 | if (ttype(L->top-1) != TAG_NIL) | ||
| 462 | return 1; | ||
| 463 | L->top--; /* remove nil result */ | ||
| 464 | } | ||
| 465 | return 0; | ||
| 466 | } | ||
| 467 | |||
| 468 | |||
| 469 | static int luaB_foreach (lua_State *L) { | ||
| 470 | luaL_checktype(L, 1, "table"); | ||
| 471 | luaL_checktype(L, 2, "function"); | ||
| 472 | lua_pushobject(L, 1); /* put table at top */ | ||
| 473 | lua_pushnil(L); /* first index */ | ||
| 474 | for (;;) { | ||
| 475 | if (lua_next(L) == 0) | ||
| 476 | return 0; | ||
| 477 | lua_pushobject(L, 2); /* function */ | ||
| 478 | lua_pushobject(L, -3); /* key */ | ||
| 479 | lua_pushobject(L, -3); /* value */ | ||
| 480 | if (lua_call(L, 2, 1) != 0) lua_error(L, NULL); | ||
| 481 | if (!lua_isnil(L, -1)) | ||
| 482 | return 1; | ||
| 483 | lua_pop(L, 2); /* remove value and result */ | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | |||
| 488 | |||
| 489 | /* | ||
| 490 | ** {====================================================== | ||
| 491 | ** Quicksort | ||
| 492 | ** (based on `Algorithms in MODULA-3', Robert Sedgewick; | ||
| 493 | ** Addison-Wesley, 1993.) | ||
| 494 | */ | ||
| 495 | |||
| 496 | |||
| 497 | static void swap (lua_State *L, Hash *a, int i, int j) { | ||
| 498 | TObject temp; | ||
| 499 | temp = *luaH_getnum(a, i); | ||
| 500 | t_move(L, a, j, i); | ||
| 501 | *luaH_setint(L, a, j) = temp; | ||
| 502 | } | ||
| 503 | |||
| 504 | static int sort_comp (lua_State *L, const TObject *f, const TObject *a, | ||
| 505 | const TObject *b) { | ||
| 506 | /* WARNING: the caller (auxsort) must ensure stack space */ | ||
| 507 | if (f != NULL) { | ||
| 508 | *(L->top) = *f; | ||
| 509 | *(L->top+1) = *a; | ||
| 510 | *(L->top+2) = *b; | ||
| 511 | L->top += 3; | ||
| 512 | luaD_call(L, L->top-3, 1); | ||
| 513 | L->top--; | ||
| 514 | return (ttype(L->top) != TAG_NIL); | ||
| 515 | } | ||
| 516 | else /* a < b? */ | ||
| 517 | return luaV_lessthan(L, a, b, L->top); | ||
| 518 | } | ||
| 519 | |||
| 520 | static void auxsort (lua_State *L, Hash *a, int l, int u, const TObject *f) { | ||
| 521 | StkId P = L->top++; /* temporary place for pivot */ | ||
| 522 | ttype(P) = TAG_NIL; | ||
| 523 | while (l < u) { /* for tail recursion */ | ||
| 524 | int i, j; | ||
| 525 | /* sort elements a[l], a[(l+u)/2] and a[u] */ | ||
| 526 | if (sort_comp(L, f, luaH_getnum(a, u), luaH_getnum(a, l))) | ||
| 527 | swap(L, a, l, u); /* a[u]<a[l] */ | ||
| 528 | if (u-l == 1) break; /* only 2 elements */ | ||
| 529 | i = (l+u)/2; | ||
| 530 | *P = *luaH_getnum(a, i); /* P = a[i] */ | ||
| 531 | if (sort_comp(L, f, P, luaH_getnum(a, l))) /* a[i]<a[l]? */ | ||
| 532 | swap(L, a, l, i); | ||
| 533 | else if (sort_comp(L, f, luaH_getnum(a, u), P)) /* a[u]<a[i]? */ | ||
| 534 | swap(L, a, i, u); | ||
| 535 | if (u-l == 2) break; /* only 3 elements */ | ||
| 536 | *P = *luaH_getnum(a, i); /* save pivot on stack (GC) */ | ||
| 537 | swap(L, a, i, u-1); /* put median element as pivot (a[u-1]) */ | ||
| 538 | /* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */ | ||
| 539 | i = l; j = u-1; | ||
| 540 | for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ | ||
| 541 | /* repeat i++ until a[i] >= P */ | ||
| 542 | while (sort_comp(L, f, luaH_getnum(a, ++i), P)) | ||
| 543 | if (i>u) lua_error(L, "invalid order function for sorting"); | ||
| 544 | /* repeat j-- until a[j] <= P */ | ||
| 545 | while (sort_comp(L, f, P, luaH_getnum(a, --j))) | ||
| 546 | if (j<l) lua_error(L, "invalid order function for sorting"); | ||
| 547 | if (j<i) break; | ||
| 548 | swap(L, a, i, j); | ||
| 549 | } | ||
| 550 | swap(L, a, u-1, i); /* swap pivot (a[u-1]) with a[i] */ | ||
| 551 | /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ | ||
| 552 | /* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */ | ||
| 553 | if (i-l < u-i) { | ||
| 554 | j=l; i=i-1; l=i+2; | ||
| 555 | } | ||
| 556 | else { | ||
| 557 | j=i+1; i=u; u=j-2; | ||
| 558 | } | ||
| 559 | auxsort(L, a, j, i, f); /* call recursively the smaller one */ | ||
| 560 | } /* repeat the routine for the larger one */ | ||
| 561 | L->top--; /* remove pivot from stack */ | ||
| 562 | } | ||
| 563 | |||
| 564 | int luaB_sort (lua_State *L) { | ||
| 565 | Hash *a = gettable(L, 1); | ||
| 566 | int n = lua_getn(L, 1); | ||
| 567 | const TObject *func = NULL; | ||
| 568 | if (!lua_isnull(L, 2)) { /* is there a 2nd argument? */ | ||
| 569 | luaL_checktype(L, 2, "function"); | ||
| 570 | func = luaA_index(L, 2); | ||
| 571 | } | ||
| 572 | auxsort(L, a, 1, n, func); | ||
| 573 | return 0; | ||
| 574 | } | ||
| 575 | |||
| 576 | /* }====================================================== */ | ||
| 577 | |||
| 578 | |||
| 579 | /* }====================================================== */ | ||
| 580 | |||
| 581 | |||
| 582 | |||
| 583 | /* | ||
| 584 | ** {====================================================== | ||
| 585 | ** Deprecated functions to manipulate global environment: | ||
| 586 | ** some of them can be simulated through table operations | ||
| 587 | ** over the global table. | ||
| 588 | ** ======================================================= | ||
| 589 | */ | ||
| 590 | |||
| 591 | |||
| 592 | #ifdef LUA_DEPRECATETFUNCS | ||
| 593 | |||
| 594 | #define num_deprecated 4 | ||
| 595 | |||
| 596 | static const struct luaL_reg deprecated_global_funcs[num_deprecated] = { | ||
| 597 | {"foreachvar", luaB_foreach}, | ||
| 598 | {"nextvar", luaB_next}, | ||
| 599 | {"rawgetglobal", luaB_rawget}, | ||
| 600 | {"rawsetglobal", luaB_rawset} | ||
| 601 | }; | ||
| 602 | |||
| 603 | |||
| 604 | static const struct luaL_reg other_deprecated_global_funcs[] = { | ||
| 605 | {"rawgettable", luaB_rawget}, | ||
| 606 | {"rawsettable", luaB_rawset} | ||
| 607 | }; | ||
| 608 | |||
| 609 | |||
| 610 | |||
| 611 | static void deprecated_funcs (lua_State *L) { | ||
| 612 | TObject gt; | ||
| 613 | int i; | ||
| 614 | ttype(>) = TAG_TABLE; | ||
| 615 | hvalue(>) = L->gt; | ||
| 616 | for (i=0; i<num_deprecated; i++) { | ||
| 617 | lua_pushobject(L, >); | ||
| 618 | lua_pushcclosure(L, deprecated_global_funcs[i].func, 1); ?? | ||
| 619 | lua_setglobal(L, deprecated_global_funcs[i].name); | ||
| 620 | } | ||
| 621 | luaL_openl(L, other_deprecated_global_funcs); | ||
| 622 | } | ||
| 623 | |||
| 624 | #else | ||
| 625 | |||
| 626 | /* | ||
| 627 | ** gives an explicit error in any attempt to call a deprecated function | ||
| 628 | */ | ||
| 629 | static int deprecated_func (lua_State *L) { | ||
| 630 | luaL_verror(L, "function `%.20s' is deprecated", luaL_check_string(L, -1)); | ||
| 631 | return 0; /* to avoid warnings */ | ||
| 632 | } | ||
| 633 | |||
| 634 | |||
| 635 | #define num_deprecated 6 | ||
| 636 | |||
| 637 | static const char *const deprecated_names [num_deprecated] = { | ||
| 638 | "foreachvar", "nextvar", "rawgetglobal", | ||
| 639 | "rawgettable", "rawsetglobal", "rawsettable" | ||
| 640 | }; | ||
| 641 | |||
| 642 | |||
| 643 | static void deprecated_funcs (lua_State *L) { | ||
| 644 | int i; | ||
| 645 | for (i=0; i<num_deprecated; i++) { | ||
| 646 | lua_pushstring(L, deprecated_names[i]); | ||
| 647 | lua_pushcclosure(L, deprecated_func, 1); | ||
| 648 | lua_setglobal(L, deprecated_names[i]); | ||
| 649 | } | ||
| 650 | } | ||
| 651 | |||
| 652 | #endif | ||
| 653 | |||
| 654 | /* }====================================================== */ | ||
| 655 | |||
| 656 | static const struct luaL_reg builtin_funcs[] = { | ||
| 657 | {LUA_ALERT, luaB__ALERT}, | ||
| 658 | {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE}, | ||
| 659 | {"call", luaB_call}, | ||
| 660 | {"collectgarbage", luaB_collectgarbage}, | ||
| 661 | {"copytagmethods", luaB_copytagmethods}, | ||
| 662 | {"dofile", luaB_dofile}, | ||
| 663 | {"dostring", luaB_dostring}, | ||
| 664 | {"error", luaB_error}, | ||
| 665 | {"foreach", luaB_foreach}, | ||
| 666 | {"foreachi", luaB_foreachi}, | ||
| 667 | {"getglobal", luaB_getglobal}, | ||
| 668 | {"gettagmethod", luaB_gettagmethod}, | ||
| 669 | {"globals", luaB_globals}, | ||
| 670 | {"newtag", luaB_newtag}, | ||
| 671 | {"next", luaB_next}, | ||
| 672 | {"print", luaB_print}, | ||
| 673 | {"rawget", luaB_rawget}, | ||
| 674 | {"rawset", luaB_rawset}, | ||
| 675 | {"setglobal", luaB_setglobal}, | ||
| 676 | {"settag", luaB_settag}, | ||
| 677 | {"settagmethod", luaB_settagmethod}, | ||
| 678 | {"tag", luaB_tag}, | ||
| 679 | {"tonumber", luaB_tonumber}, | ||
| 680 | {"tostring", luaB_tostring}, | ||
| 681 | {"type", luaB_type}, | ||
| 682 | /* "Extra" functions */ | ||
| 683 | {"assert", luaB_assert}, | ||
| 684 | {"getn", luaB_getn}, | ||
| 685 | {"sort", luaB_sort}, | ||
| 686 | {"tinsert", luaB_tinsert}, | ||
| 687 | {"tremove", luaB_tremove} | ||
| 688 | }; | ||
| 689 | |||
| 690 | |||
| 691 | |||
| 692 | void luaB_predefine (lua_State *L) { | ||
| 693 | luaL_openl(L, builtin_funcs); | ||
| 694 | #ifdef DEBUG | ||
| 695 | luaB_opentests(L); /* internal test functions */ | ||
| 696 | #endif | ||
| 697 | lua_pushstring(L, LUA_VERSION); | ||
| 698 | lua_setglobal(L, "_VERSION"); | ||
| 699 | deprecated_funcs(L); | ||
| 700 | } | ||
| 701 | |||
diff --git a/lbuiltin.h b/lbuiltin.h deleted file mode 100644 index 7d8a2236..00000000 --- a/lbuiltin.h +++ /dev/null | |||
| @@ -1,44 +0,0 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: lbuiltin.h,v 1.9 2000/05/26 19:17:57 roberto Exp roberto $ | ||
| 3 | ** Built-in functions | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #ifndef lbuiltin_h | ||
| 8 | #define lbuiltin_h | ||
| 9 | |||
| 10 | #include "lua.h" | ||
| 11 | |||
| 12 | int luaB__ALERT (lua_State *L); | ||
| 13 | int luaB__ERRORMESSAGE (lua_State *L); | ||
| 14 | int luaB_assert (lua_State *L); | ||
| 15 | int luaB_call (lua_State *L); | ||
| 16 | int luaB_collectgarbage (lua_State *L); | ||
| 17 | int luaB_copytagmethods (lua_State *L); | ||
| 18 | int luaB_dofile (lua_State *L); | ||
| 19 | int luaB_dostring (lua_State *L); | ||
| 20 | int luaB_error (lua_State *L); | ||
| 21 | int luaB_getglobal (lua_State *L); | ||
| 22 | int luaB_getn (lua_State *L); | ||
| 23 | int luaB_gettagmethod (lua_State *L); | ||
| 24 | int luaB_globals (lua_State *L); | ||
| 25 | int luaB_newtag (lua_State *L); | ||
| 26 | int luaB_next (lua_State *L); | ||
| 27 | int luaB_print (lua_State *L); | ||
| 28 | int luaB_rawget (lua_State *L); | ||
| 29 | int luaB_rawset (lua_State *L); | ||
| 30 | int luaB_setglobal (lua_State *L); | ||
| 31 | int luaB_settag (lua_State *L); | ||
| 32 | int luaB_settagmethod (lua_State *L); | ||
| 33 | int luaB_sort (lua_State *L); | ||
| 34 | int luaB_tag (lua_State *L); | ||
| 35 | int luaB_tinsert (lua_State *L); | ||
| 36 | int luaB_tonumber (lua_State *L); | ||
| 37 | int luaB_tostring (lua_State *L); | ||
| 38 | int luaB_tremove (lua_State *L); | ||
| 39 | int luaB_type (lua_State *L); | ||
| 40 | |||
| 41 | void luaB_predefine (lua_State *L); | ||
| 42 | |||
| 43 | |||
| 44 | #endif | ||
