A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 734 lines 19 kB view raw
1/* 2** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $ 3** Basic library 4** See Copyright Notice in lua.h 5*/ 6 7 8 9#include <ctype.h> 10#include <stdio.h> 11#include <stdlib.h> 12#include <string.h> 13 14#define lbaselib_c 15#define LUA_LIB 16 17#include "lua.h" 18 19#include "lauxlib.h" 20#include "lualib.h" 21 22#ifdef LUA_OPTIMIZE_DEBUG_USER 23#include "lobject.h" 24#include "lstate.h" 25#include "ldebug.h" 26#include "lfunc.h" 27#endif 28 29 30 31 32/* 33** If your system does not support `stdout', you can just remove this function. 34** If you need, you can define your own `print' function, following this 35** model but changing `fputs' to put the strings at a proper place 36** (a console window or a log file, for instance). 37*/ 38#if 0 39static int luaB_print (lua_State *L) { 40 int n = lua_gettop(L); /* number of arguments */ 41 int i; 42 lua_getglobal(L, "tostring"); 43 for (i=1; i<=n; i++) { 44 const char *s; 45 lua_pushvalue(L, -1); /* function to be called */ 46 lua_pushvalue(L, i); /* value to print */ 47 lua_call(L, 1, 1); 48 s = lua_tostring(L, -1); /* get result */ 49 if (s == NULL) 50 return luaL_error(L, LUA_QL("tostring") " must return a string to " 51 LUA_QL("print")); 52 if (i>1) fputs("\t", stdout); 53 fputs(s, stdout); 54 lua_pop(L, 1); /* pop result */ 55 } 56 fputs("\n", stdout); 57 return 0; 58} 59#endif 60 61 62static int luaB_tonumber (lua_State *L) { 63 int base = luaL_optint(L, 2, 10); 64 if (base == 10) { /* standard conversion */ 65 luaL_checkany(L, 1); 66 if (lua_isnumber(L, 1)) { 67 lua_pushnumber(L, lua_tonumber(L, 1)); 68 return 1; 69 } 70 } 71 else { 72 const char *s1 = luaL_checkstring(L, 1); 73 char *s2; 74 unsigned long n; 75 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); 76 n = strtoul(s1, &s2, base); 77 if (s1 != s2) { /* at least one valid digit? */ 78 while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ 79 if (*s2 == '\0') { /* no invalid trailing characters? */ 80 lua_pushnumber(L, (lua_Number)n); 81 return 1; 82 } 83 } 84 } 85 lua_pushnil(L); /* else not a number */ 86 return 1; 87} 88 89 90static int luaB_error (lua_State *L) { 91 int level = luaL_optint(L, 2, 1); 92 lua_settop(L, 1); 93 if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ 94 luaL_where(L, level); 95 lua_pushvalue(L, 1); 96 lua_concat(L, 2); 97 } 98 return lua_error(L); 99} 100 101 102static int luaB_getmetatable (lua_State *L) { 103 luaL_checkany(L, 1); 104 if (!lua_getmetatable(L, 1)) { 105 lua_pushnil(L); 106 return 1; /* no metatable */ 107 } 108 luaL_getmetafield(L, 1, "__metatable"); 109 return 1; /* returns either __metatable field (if present) or metatable */ 110} 111 112 113static int luaB_setmetatable (lua_State *L) { 114 int t = lua_type(L, 2); 115 luaL_checktype(L, 1, LUA_TTABLE); 116 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 117 "nil or table expected"); 118 if (luaL_getmetafield(L, 1, "__metatable")) 119 luaL_error(L, "cannot change a protected metatable"); 120 lua_settop(L, 2); 121 lua_setmetatable(L, 1); 122 return 1; 123} 124 125 126static void getfunc (lua_State *L, int opt) { 127 if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); 128 else { 129 lua_Debug ar; 130 int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1); 131 luaL_argcheck(L, level >= 0, 1, "level must be non-negative"); 132 if (lua_getstack(L, level, &ar) == 0) 133 luaL_argerror(L, 1, "invalid level"); 134 lua_getinfo(L, "f", &ar); 135 if (lua_isnil(L, -1)) 136 luaL_error(L, "no function environment for tail call at level %d", 137 level); 138 } 139} 140 141 142static int luaB_getfenv (lua_State *L) { 143 getfunc(L, 1); 144 if (lua_iscfunction(L, -1)) /* is a C function? */ 145 lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */ 146 else 147 lua_getfenv(L, -1); 148 return 1; 149} 150 151 152static int luaB_setfenv (lua_State *L) { 153 luaL_checktype(L, 2, LUA_TTABLE); 154 getfunc(L, 0); 155 lua_pushvalue(L, 2); 156 if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) { 157 /* change environment of current thread */ 158 lua_pushthread(L); 159 lua_insert(L, -2); 160 lua_setfenv(L, -2); 161 return 0; 162 } 163 else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0) 164 luaL_error(L, 165 LUA_QL("setfenv") " cannot change environment of given object"); 166 return 1; 167} 168 169 170static int luaB_rawequal (lua_State *L) { 171 luaL_checkany(L, 1); 172 luaL_checkany(L, 2); 173 lua_pushboolean(L, lua_rawequal(L, 1, 2)); 174 return 1; 175} 176 177 178static int luaB_rawget (lua_State *L) { 179 luaL_checktype(L, 1, LUA_TTABLE); 180 luaL_checkany(L, 2); 181 lua_settop(L, 2); 182 lua_rawget(L, 1); 183 return 1; 184} 185 186static int luaB_rawset (lua_State *L) { 187 luaL_checktype(L, 1, LUA_TTABLE); 188 luaL_checkany(L, 2); 189 luaL_checkany(L, 3); 190 lua_settop(L, 3); 191 lua_rawset(L, 1); 192 return 1; 193} 194 195 196static int luaB_gcinfo (lua_State *L) { 197 lua_pushinteger(L, lua_getgccount(L)); 198 return 1; 199} 200 201 202static int luaB_collectgarbage (lua_State *L) { 203 static const char *const opts[] = {"stop", "restart", "collect", 204 "count", "step", "setpause", "setstepmul", NULL}; 205 static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, 206 LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; 207 int o = luaL_checkoption(L, 1, "collect", opts); 208 int ex = luaL_optint(L, 2, 0); 209 int res = lua_gc(L, optsnum[o], ex); 210 switch (optsnum[o]) { 211 case LUA_GCCOUNT: { 212 int b = lua_gc(L, LUA_GCCOUNTB, 0); 213 lua_pushnumber(L, res + ((lua_Number)b/1024)); 214 return 1; 215 } 216 case LUA_GCSTEP: { 217 lua_pushboolean(L, res); 218 return 1; 219 } 220 default: { 221 lua_pushnumber(L, res); 222 return 1; 223 } 224 } 225} 226 227 228static int luaB_type (lua_State *L) { 229 luaL_checkany(L, 1); 230 lua_pushstring(L, luaL_typename(L, 1)); 231 return 1; 232} 233 234 235/** $Id: lbaselib.c,v 1.276.1.1 2013/04/12 18:48:47 roberto Exp $ */ 236static int pairsmeta (lua_State *L, const char *method, int iszero, 237 lua_CFunction iter) { 238 if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */ 239 luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */ 240 lua_pushcfunction(L, iter); /* will return generator, */ 241 lua_pushvalue(L, 1); /* state, */ 242 if (iszero) lua_pushinteger(L, 0); /* and initial value */ 243 else lua_pushnil(L); 244 } 245 else { 246 lua_pushvalue(L, 1); /* argument 'self' to metamethod */ 247 lua_call(L, 1, 3); /* get 3 values from metamethod */ 248 } 249 return 3; 250} 251 252 253static int luaB_next (lua_State *L) { 254 luaL_checktype(L, 1, LUA_TTABLE); 255 lua_settop(L, 2); /* create a 2nd argument if there isn't one */ 256 if (lua_next(L, 1)) 257 return 2; 258 else { 259 lua_pushnil(L); 260 return 1; 261 } 262} 263 264 265static int luaB_pairs (lua_State *L) { 266 /* pairs function from lua 5.2 */ 267 return pairsmeta(L, "__pairs", 0, luaB_next); 268} 269 270 271static int ipairsaux (lua_State *L) { 272 int i = luaL_checkint(L, 2); 273 luaL_checktype(L, 1, LUA_TTABLE); 274 i++; /* next value */ 275 lua_pushinteger(L, i); 276 lua_rawgeti(L, 1, i); 277 return (lua_isnil(L, -1)) ? 1 : 2; 278} 279 280 281static int luaB_ipairs (lua_State *L) { 282 return pairsmeta(L, "__ipairs", 1, ipairsaux); 283 /* ipairs function from lua 5.2 */ 284} 285 286 287static int load_aux (lua_State *L, int status) { 288 if (status == 0) /* OK? */ 289 return 1; 290 else { 291 lua_pushnil(L); 292 lua_insert(L, -2); /* put before error message */ 293 return 2; /* return nil plus error message */ 294 } 295} 296 297 298static int luaB_loadstring (lua_State *L) { 299 size_t l; 300 const char *s = luaL_checklstring(L, 1, &l); 301 const char *chunkname = luaL_optstring(L, 2, s); 302 return load_aux(L, luaL_loadbuffer(L, s, l, chunkname)); 303} 304 305 306static int luaB_loadfile (lua_State *L) { 307 const char *fname = luaL_optstring(L, 1, NULL); 308 return load_aux(L, luaL_loadfile(L, fname)); 309} 310 311 312#ifdef LUA_OPTIMIZE_DEBUG_USER 313/* stripdebug([level[, function]]). 314 * level: 1 don't discard debug 315 * 2 discard Local and Upvalue debug info 316 * 3 discard Local, Upvalue and lineno debug info. 317 * function: Function to be stripped as per setfenv except 0 not permitted. 318 * If no arguments then the current default setting is returned. 319 * If function is omitted, this is the default setting for future compiles 320 * The function returns an estimated integer count of the bytes stripped. 321 */ 322static int luaB_stripdebug (lua_State *L) { 323 int level; 324 325 if (L->top == L->base) { 326 lua_pushlightuserdata(L, &luaG_stripdebug); 327 lua_gettable(L, LUA_REGISTRYINDEX); 328 if (lua_isnil(L, -1)) { 329 lua_pop(L, 1); 330 lua_pushinteger(L, LUA_OPTIMIZE_DEBUG); 331 } 332 return 1; 333 } 334 335 level = luaL_checkint(L, 1); 336 if ((level <= 0) || (level > 3)) luaL_argerror(L, 1, "must in range 1-3"); 337 338 if (L->top == L->base + 1) { 339 /* Store the default level in the registry if no function parameter */ 340 lua_pushlightuserdata(L, &luaG_stripdebug); 341 lua_pushinteger(L, level); 342 lua_settable(L, LUA_REGISTRYINDEX); 343 lua_settop(L,0); 344 return 0; 345 } 346 347 if (level == 1) { 348 lua_settop(L,0); 349 lua_pushinteger(L, 0); 350 return 1; 351 } 352 353 if (!lua_isfunction(L, 2)) { 354 int scope = luaL_checkint(L, 2); 355 if (scope > 0) { 356 /* if the function parameter is a +ve integer then climb to find function */ 357 lua_Debug ar; 358 lua_pop(L, 1); /* pop level as getinfo will replace it by the function */ 359 if (lua_getstack(L, scope, &ar)) { 360 lua_getinfo(L, "f", &ar); 361 } 362 } 363 } 364 365 if(!lua_isfunction(L, 2) || lua_iscfunction(L, -1)) luaL_argerror(L, 2, "must be a Lua Function"); 366 // lua_lock(L); 367 Proto *f = clvalue(L->base + 1)->l.p; 368 // lua_unlock(L); 369 lua_settop(L,0); 370 lua_pushinteger(L, luaG_stripdebug(L, f, level, 16)); 371 return 1; 372} 373#endif 374 375 376/* 377** Reader for generic `load' function: `lua_load' uses the 378** stack for internal stuff, so the reader cannot change the 379** stack top. Instead, it keeps its resulting string in a 380** reserved slot inside the stack. 381*/ 382static const char *generic_reader (lua_State *L, void *ud, size_t *size) { 383 (void)ud; /* to avoid warnings */ 384 luaL_checkstack(L, 2, "too many nested functions"); 385 lua_pushvalue(L, 1); /* get function */ 386 lua_call(L, 0, 1); /* call it */ 387 if (lua_isnil(L, -1)) { 388 *size = 0; 389 return NULL; 390 } 391 else if (lua_isstring(L, -1)) { 392 lua_replace(L, 3); /* save string in a reserved stack slot */ 393 return lua_tolstring(L, 3, size); 394 } 395 else luaL_error(L, "reader function must return a string"); 396 return NULL; /* to avoid warnings */ 397} 398 399 400static int luaB_load (lua_State *L) { 401 int status; 402 const char *cname = luaL_optstring(L, 2, "=(load)"); 403 luaL_checktype(L, 1, LUA_TFUNCTION); 404 lua_settop(L, 3); /* function, eventual name, plus one reserved slot */ 405 status = lua_load(L, generic_reader, NULL, cname); 406 return load_aux(L, status); 407} 408 409 410static int luaB_dofile (lua_State *L) { 411 const char *fname = luaL_optstring(L, 1, NULL); 412 int n = lua_gettop(L); 413 if (luaL_loadfile(L, fname) != 0) lua_error(L); 414 lua_call(L, 0, LUA_MULTRET); 415 return lua_gettop(L) - n; 416} 417 418 419static int luaB_assert (lua_State *L) { 420 luaL_checkany(L, 1); 421 if (!lua_toboolean(L, 1)) 422 return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); 423 return lua_gettop(L); 424} 425 426 427static int luaB_unpack (lua_State *L) { 428 int i, e, n; 429 luaL_checktype(L, 1, LUA_TTABLE); 430 i = luaL_optint(L, 2, 1); 431 e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); 432 if (i > e) return 0; /* empty range */ 433 n = e - i + 1; /* number of elements */ 434 if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ 435 return luaL_error(L, "too many results to unpack"); 436 lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ 437 while (i++ < e) /* push arg[i + 1...e] */ 438 lua_rawgeti(L, 1, i); 439 return n; 440} 441 442 443static int luaB_select (lua_State *L) { 444 int n = lua_gettop(L); 445 if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { 446 lua_pushinteger(L, n-1); 447 return 1; 448 } 449 else { 450 int i = luaL_checkint(L, 1); 451 if (i < 0) i = n + i; 452 else if (i > n) i = n; 453 luaL_argcheck(L, 1 <= i, 1, "index out of range"); 454 return n - i; 455 } 456} 457 458 459static int luaB_pcall (lua_State *L) { 460 int status; 461 luaL_checkany(L, 1); 462 status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); 463 lua_pushboolean(L, (status == 0)); 464 lua_insert(L, 1); 465 return lua_gettop(L); /* return status + all results */ 466} 467 468 469static int luaB_xpcall (lua_State *L) { 470 int status; 471 luaL_checkany(L, 2); 472 lua_settop(L, 2); 473 lua_insert(L, 1); /* put error function under function to be called */ 474 status = lua_pcall(L, 0, LUA_MULTRET, 1); 475 lua_pushboolean(L, (status == 0)); 476 lua_replace(L, 1); 477 return lua_gettop(L); /* return status + all results */ 478} 479 480 481static int luaB_tostring (lua_State *L) { 482 luaL_checkany(L, 1); 483 if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ 484 return 1; /* use its value */ 485 switch (lua_type(L, 1)) { 486 case LUA_TNUMBER: 487 lua_pushstring(L, lua_tostring(L, 1)); 488 break; 489 case LUA_TSTRING: 490 lua_pushvalue(L, 1); 491 break; 492 case LUA_TBOOLEAN: 493 lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); 494 break; 495 case LUA_TNIL: 496 lua_pushliteral(L, "nil"); 497 break; 498 default: 499 lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1)); 500 break; 501 } 502 return 1; 503} 504 505 506static int luaB_newproxy (lua_State *L) { 507 lua_settop(L, 1); 508 lua_newuserdata(L, 0); /* create proxy */ 509 if (lua_toboolean(L, 1) == 0) 510 return 1; /* no metatable */ 511 else if (lua_isboolean(L, 1)) { 512 lua_newtable(L); /* create a new metatable `m' ... */ 513 lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */ 514 lua_pushboolean(L, 1); 515 lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */ 516 } 517 else { 518 int validproxy = 0; /* to check if weaktable[metatable(u)] == true */ 519 if (lua_getmetatable(L, 1)) { 520 lua_rawget(L, lua_upvalueindex(1)); 521 validproxy = lua_toboolean(L, -1); 522 lua_pop(L, 1); /* remove value */ 523 } 524 luaL_argcheck(L, validproxy, 1, "boolean or proxy expected"); 525 lua_getmetatable(L, 1); /* metatable is valid; get it */ 526 } 527 lua_setmetatable(L, 2); 528 return 1; 529} 530 531 532static const luaL_Reg base_funcs[] = { 533 {"assert", luaB_assert}, 534 {"collectgarbage", luaB_collectgarbage}, 535 {"dofile", luaB_dofile}, 536 {"error", luaB_error}, 537 {"gcinfo", luaB_gcinfo}, 538 {"getfenv", luaB_getfenv}, 539 {"getmetatable", luaB_getmetatable}, 540 {"ipairs", luaB_ipairs}, 541 {"loadfile", luaB_loadfile}, 542 {"load", luaB_load}, 543 {"loadstring", luaB_loadstring}, 544 {"next", luaB_next}, 545 {"pairs", luaB_pairs}, 546 {"pcall", luaB_pcall}, 547#if 0 548 {"print", luaB_print}, 549#endif 550 {"rawequal", luaB_rawequal}, 551 {"rawget", luaB_rawget}, 552 {"rawset", luaB_rawset}, 553 {"select", luaB_select}, 554 {"setfenv", luaB_setfenv}, 555 {"setmetatable", luaB_setmetatable}, 556#ifdef LUA_OPTIMIZE_DEBUG_USER 557 {"stripdebug", luaB_stripdebug}, 558#endif 559 {"tonumber", luaB_tonumber}, 560 {"tostring", luaB_tostring}, 561 {"type", luaB_type}, 562 {"unpack", luaB_unpack}, 563 {"xpcall", luaB_xpcall}, 564 {NULL, NULL} 565}; 566 567 568/* 569** {====================================================== 570** Coroutine library 571** ======================================================= 572*/ 573 574#define CO_RUN 0 /* running */ 575#define CO_SUS 1 /* suspended */ 576#define CO_NOR 2 /* 'normal' (it resumed another coroutine) */ 577#define CO_DEAD 3 578 579static const char *const statnames[] = 580 {"running", "suspended", "normal", "dead"}; 581 582static int costatus (lua_State *L, lua_State *co) { 583 if (L == co) return CO_RUN; 584 switch (lua_status(co)) { 585 case LUA_YIELD: 586 return CO_SUS; 587 case 0: { 588 lua_Debug ar; 589 if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 590 return CO_NOR; /* it is running */ 591 else if (lua_gettop(co) == 0) 592 return CO_DEAD; 593 else 594 return CO_SUS; /* initial state */ 595 } 596 default: /* some error occured */ 597 return CO_DEAD; 598 } 599} 600 601 602static int luaB_costatus (lua_State *L) { 603 lua_State *co = lua_tothread(L, 1); 604 luaL_argcheck(L, co, 1, "coroutine expected"); 605 lua_pushstring(L, statnames[costatus(L, co)]); 606 return 1; 607} 608 609 610static int auxresume (lua_State *L, lua_State *co, int narg) { 611 int status = costatus(L, co); 612 if (!lua_checkstack(co, narg)) 613 luaL_error(L, "too many arguments to resume"); 614 if (status != CO_SUS) { 615 lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]); 616 return -1; /* error flag */ 617 } 618 lua_xmove(L, co, narg); 619 lua_setlevel(L, co); 620 status = lua_resume(co, narg); 621 if (status == 0 || status == LUA_YIELD) { 622 int nres = lua_gettop(co); 623 if (!lua_checkstack(L, nres + 1)) 624 luaL_error(L, "too many results to resume"); 625 lua_xmove(co, L, nres); /* move yielded values */ 626 return nres; 627 } 628 else { 629 lua_xmove(co, L, 1); /* move error message */ 630 return -1; /* error flag */ 631 } 632} 633 634 635static int luaB_coresume (lua_State *L) { 636 lua_State *co = lua_tothread(L, 1); 637 int r; 638 luaL_argcheck(L, co, 1, "coroutine expected"); 639 r = auxresume(L, co, lua_gettop(L) - 1); 640 if (r < 0) { 641 lua_pushboolean(L, 0); 642 lua_insert(L, -2); 643 return 2; /* return false + error message */ 644 } 645 else { 646 lua_pushboolean(L, 1); 647 lua_insert(L, -(r + 1)); 648 return r + 1; /* return true + `resume' returns */ 649 } 650} 651 652 653static int luaB_auxwrap (lua_State *L) { 654 lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 655 int r = auxresume(L, co, lua_gettop(L)); 656 if (r < 0) { 657 if (lua_isstring(L, -1)) { /* error object is a string? */ 658 luaL_where(L, 1); /* add extra info */ 659 lua_insert(L, -2); 660 lua_concat(L, 2); 661 } 662 lua_error(L); /* propagate error */ 663 } 664 return r; 665} 666 667 668static int luaB_cocreate (lua_State *L) { 669 lua_State *NL = lua_newthread(L); 670 luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, 671 "Lua function expected"); 672 lua_pushvalue(L, 1); /* move function to top */ 673 lua_xmove(L, NL, 1); /* move function from L to NL */ 674 return 1; 675} 676 677 678static int luaB_cowrap (lua_State *L) { 679 luaB_cocreate(L); 680 lua_pushcclosure(L, luaB_auxwrap, 1); 681 return 1; 682} 683 684 685static int luaB_yield (lua_State *L) { 686 return lua_yield(L, lua_gettop(L)); 687} 688 689 690static int luaB_corunning (lua_State *L) { 691 if (lua_pushthread(L)) 692 lua_pushnil(L); /* main thread is not a coroutine */ 693 return 1; 694} 695 696 697static const luaL_Reg co_funcs[] = { 698 {"create", luaB_cocreate}, 699 {"resume", luaB_coresume}, 700 {"running", luaB_corunning}, 701 {"status", luaB_costatus}, 702 {"wrap", luaB_cowrap}, 703 {"yield", luaB_yield}, 704 {NULL, NULL} 705}; 706 707/* }====================================================== */ 708 709 710static void base_open (lua_State *L) { 711 /* set global _G */ 712 lua_pushvalue(L, LUA_GLOBALSINDEX); 713 lua_setglobal(L, "_G"); 714 /* open lib into global table */ 715 luaL_register(L, "_G", base_funcs); 716 lua_pushliteral(L, LUA_VERSION); 717 lua_setglobal(L, "_VERSION"); /* set global _VERSION */ 718 /* `newproxy' needs a weaktable as upvalue */ 719 lua_createtable(L, 0, 1); /* new table `w' */ 720 lua_pushvalue(L, -1); /* `w' will be its own metatable */ 721 lua_setmetatable(L, -2); 722 lua_pushliteral(L, "kv"); 723 lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ 724 lua_pushcclosure(L, luaB_newproxy, 1); 725 lua_setglobal(L, "newproxy"); /* set global `newproxy' */ 726} 727 728 729LUALIB_API int luaopen_base (lua_State *L) { 730 base_open(L); 731 luaL_register(L, LUA_COLIBNAME, co_funcs); 732 return 2; 733} 734