00001 /* 00002 ** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $ 00003 ** Basic library 00004 ** See Copyright Notice in lua.h 00005 */ 00006 00007 00008 00009 #include <ctype.h> 00010 #include <stdio.h> 00011 #include <stdlib.h> 00012 #include <string.h> 00013 00014 #define lbaselib_c 00015 #define LUA_LIB 00016 00017 #include "lua.h" 00018 00019 #include "lauxlib.h" 00020 #include "lualib.h" 00021 00022 #ifdef LUA_USE_ALUA_LOGGER 00023 // Use generic output routine instead 00024 // of fputs 00025 #include "out.h" 00026 #endif 00027 00028 00029 /* 00030 ** If your system does not support `stdout', you can just remove this function. 00031 ** If you need, you can define your own `print' function, following this 00032 ** model but changing `fputs' to put the strings at a proper place 00033 ** (a console window or a log file, for instance). 00034 */ 00035 #ifdef LUA_USE_ALUA_LOGGER 00036 static int luaB_print (lua_State *L) { 00037 int n = lua_gettop(L); /* number of arguments */ 00038 int i; 00039 lua_getglobal(L, "tostring"); 00040 for (i=1; i<=n; i++) { 00041 const char *s; 00042 lua_pushvalue(L, -1); /* function to be called */ 00043 lua_pushvalue(L, i); /* value to print */ 00044 lua_call(L, 1, 1); 00045 s = lua_tostring(L, -1); /* get result */ 00046 if (s == NULL) 00047 return luaL_error(L, LUA_QL("tostring") " must return a string to " 00048 LUA_QL("print")); 00049 if (i>1) ALua::ROut::Puts("\t"); 00050 ALua::ROut::Puts(s); 00051 lua_pop(L, 1); /* pop result */ 00052 } 00053 ALua::ROut::Puts("\n"); 00054 return 0; 00055 } 00056 #else 00057 static int luaB_print (lua_State *L) { 00058 int n = lua_gettop(L); /* number of arguments */ 00059 int i; 00060 lua_getglobal(L, "tostring"); 00061 for (i=1; i<=n; i++) { 00062 const char *s; 00063 lua_pushvalue(L, -1); /* function to be called */ 00064 lua_pushvalue(L, i); /* value to print */ 00065 lua_call(L, 1, 1); 00066 s = lua_tostring(L, -1); /* get result */ 00067 if (s == NULL) 00068 return luaL_error(L, LUA_QL("tostring") " must return a string to " 00069 LUA_QL("print")); 00070 if (i>1) fputs("\t", stdout); 00071 fputs(s, stdout); 00072 lua_pop(L, 1); /* pop result */ 00073 } 00074 fputs("\n", stdout); 00075 return 0; 00076 } 00077 #endif 00078 00079 00080 static int luaB_tonumber (lua_State *L) { 00081 int base = luaL_optint(L, 2, 10); 00082 if (base == 10) { /* standard conversion */ 00083 luaL_checkany(L, 1); 00084 if (lua_isnumber(L, 1)) { 00085 lua_pushnumber(L, lua_tonumber(L, 1)); 00086 return 1; 00087 } 00088 } 00089 else { 00090 const char *s1 = luaL_checkstring(L, 1); 00091 char *s2; 00092 unsigned long n; 00093 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); 00094 n = strtoul(s1, &s2, base); 00095 if (s1 != s2) { /* at least one valid digit? */ 00096 while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ 00097 if (*s2 == '\0') { /* no invalid trailing characters? */ 00098 lua_pushnumber(L, (lua_Number)n); 00099 return 1; 00100 } 00101 } 00102 } 00103 lua_pushnil(L); /* else not a number */ 00104 return 1; 00105 } 00106 00107 00108 static int luaB_error (lua_State *L) { 00109 int level = luaL_optint(L, 2, 1); 00110 lua_settop(L, 1); 00111 if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ 00112 luaL_where(L, level); 00113 lua_pushvalue(L, 1); 00114 lua_concat(L, 2); 00115 } 00116 return lua_error(L); 00117 } 00118 00119 00120 static int luaB_getmetatable (lua_State *L) { 00121 luaL_checkany(L, 1); 00122 if (!lua_getmetatable(L, 1)) { 00123 lua_pushnil(L); 00124 return 1; /* no metatable */ 00125 } 00126 luaL_getmetafield(L, 1, "__metatable"); 00127 return 1; /* returns either __metatable field (if present) or metatable */ 00128 } 00129 00130 00131 static int luaB_setmetatable (lua_State *L) { 00132 int t = lua_type(L, 2); 00133 luaL_checktype(L, 1, LUA_TTABLE); 00134 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, 00135 "nil or table expected"); 00136 if (luaL_getmetafield(L, 1, "__metatable")) 00137 luaL_error(L, "cannot change a protected metatable"); 00138 lua_settop(L, 2); 00139 lua_setmetatable(L, 1); 00140 return 1; 00141 } 00142 00143 00144 static void getfunc (lua_State *L, int opt) { 00145 if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); 00146 else { 00147 lua_Debug ar; 00148 int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1); 00149 luaL_argcheck(L, level >= 0, 1, "level must be non-negative"); 00150 if (lua_getstack(L, level, &ar) == 0) 00151 luaL_argerror(L, 1, "invalid level"); 00152 lua_getinfo(L, "f", &ar); 00153 if (lua_isnil(L, -1)) 00154 luaL_error(L, "no function environment for tail call at level %d", 00155 level); 00156 } 00157 } 00158 00159 00160 static int luaB_getfenv (lua_State *L) { 00161 getfunc(L, 1); 00162 if (lua_iscfunction(L, -1)) /* is a C function? */ 00163 lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */ 00164 else 00165 lua_getfenv(L, -1); 00166 return 1; 00167 } 00168 00169 00170 static int luaB_setfenv (lua_State *L) { 00171 luaL_checktype(L, 2, LUA_TTABLE); 00172 getfunc(L, 0); 00173 lua_pushvalue(L, 2); 00174 if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) { 00175 /* change environment of current thread */ 00176 lua_pushthread(L); 00177 lua_insert(L, -2); 00178 lua_setfenv(L, -2); 00179 return 0; 00180 } 00181 else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0) 00182 luaL_error(L, 00183 LUA_QL("setfenv") " cannot change environment of given object"); 00184 return 1; 00185 } 00186 00187 00188 static int luaB_rawequal (lua_State *L) { 00189 luaL_checkany(L, 1); 00190 luaL_checkany(L, 2); 00191 lua_pushboolean(L, lua_rawequal(L, 1, 2)); 00192 return 1; 00193 } 00194 00195 00196 static int luaB_rawget (lua_State *L) { 00197 luaL_checktype(L, 1, LUA_TTABLE); 00198 luaL_checkany(L, 2); 00199 lua_settop(L, 2); 00200 lua_rawget(L, 1); 00201 return 1; 00202 } 00203 00204 static int luaB_rawset (lua_State *L) { 00205 luaL_checktype(L, 1, LUA_TTABLE); 00206 luaL_checkany(L, 2); 00207 luaL_checkany(L, 3); 00208 lua_settop(L, 3); 00209 lua_rawset(L, 1); 00210 return 1; 00211 } 00212 00213 00214 static int luaB_gcinfo (lua_State *L) { 00215 lua_pushinteger(L, lua_getgccount(L)); 00216 return 1; 00217 } 00218 00219 00220 static int luaB_collectgarbage (lua_State *L) { 00221 static const char *const opts[] = {"stop", "restart", "collect", 00222 "count", "step", "setpause", "setstepmul", NULL}; 00223 static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, 00224 LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; 00225 int o = luaL_checkoption(L, 1, "collect", opts); 00226 int ex = luaL_optint(L, 2, 0); 00227 int res = lua_gc(L, optsnum[o], ex); 00228 switch (optsnum[o]) { 00229 case LUA_GCCOUNT: { 00230 int b = lua_gc(L, LUA_GCCOUNTB, 0); 00231 lua_pushnumber(L, res + ((lua_Number)b/1024)); 00232 return 1; 00233 } 00234 case LUA_GCSTEP: { 00235 lua_pushboolean(L, res); 00236 return 1; 00237 } 00238 default: { 00239 lua_pushnumber(L, res); 00240 return 1; 00241 } 00242 } 00243 } 00244 00245 00246 static int luaB_type (lua_State *L) { 00247 luaL_checkany(L, 1); 00248 lua_pushstring(L, luaL_typename(L, 1)); 00249 return 1; 00250 } 00251 00252 00253 static int luaB_next (lua_State *L) { 00254 luaL_checktype(L, 1, LUA_TTABLE); 00255 lua_settop(L, 2); /* create a 2nd argument if there isn't one */ 00256 if (lua_next(L, 1)) 00257 return 2; 00258 else { 00259 lua_pushnil(L); 00260 return 1; 00261 } 00262 } 00263 00264 00265 static int luaB_pairs (lua_State *L) { 00266 luaL_checktype(L, 1, LUA_TTABLE); 00267 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ 00268 lua_pushvalue(L, 1); /* state, */ 00269 lua_pushnil(L); /* and initial value */ 00270 return 3; 00271 } 00272 00273 00274 static int ipairsaux (lua_State *L) { 00275 int i = luaL_checkint(L, 2); 00276 luaL_checktype(L, 1, LUA_TTABLE); 00277 i++; /* next value */ 00278 lua_pushinteger(L, i); 00279 lua_rawgeti(L, 1, i); 00280 return (lua_isnil(L, -1)) ? 0 : 2; 00281 } 00282 00283 00284 static int luaB_ipairs (lua_State *L) { 00285 luaL_checktype(L, 1, LUA_TTABLE); 00286 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ 00287 lua_pushvalue(L, 1); /* state, */ 00288 lua_pushinteger(L, 0); /* and initial value */ 00289 return 3; 00290 } 00291 00292 00293 static int load_aux (lua_State *L, int status) { 00294 if (status == 0) /* OK? */ 00295 return 1; 00296 else { 00297 lua_pushnil(L); 00298 lua_insert(L, -2); /* put before error message */ 00299 return 2; /* return nil plus error message */ 00300 } 00301 } 00302 00303 00304 static int luaB_loadstring (lua_State *L) { 00305 size_t l; 00306 const char *s = luaL_checklstring(L, 1, &l); 00307 const char *chunkname = luaL_optstring(L, 2, s); 00308 return load_aux(L, luaL_loadbuffer(L, s, l, chunkname)); 00309 } 00310 00311 00312 static int luaB_loadfile (lua_State *L) { 00313 const char *fname = luaL_optstring(L, 1, NULL); 00314 return load_aux(L, luaL_loadfile(L, fname)); 00315 } 00316 00317 00318 /* 00319 ** Reader for generic `load' function: `lua_load' uses the 00320 ** stack for internal stuff, so the reader cannot change the 00321 ** stack top. Instead, it keeps its resulting string in a 00322 ** reserved slot inside the stack. 00323 */ 00324 static const char *generic_reader (lua_State *L, void *ud, size_t *size) { 00325 (void)ud; /* to avoid warnings */ 00326 luaL_checkstack(L, 2, "too many nested functions"); 00327 lua_pushvalue(L, 1); /* get function */ 00328 lua_call(L, 0, 1); /* call it */ 00329 if (lua_isnil(L, -1)) { 00330 *size = 0; 00331 return NULL; 00332 } 00333 else if (lua_isstring(L, -1)) { 00334 lua_replace(L, 3); /* save string in a reserved stack slot */ 00335 return lua_tolstring(L, 3, size); 00336 } 00337 else luaL_error(L, "reader function must return a string"); 00338 return NULL; /* to avoid warnings */ 00339 } 00340 00341 00342 static int luaB_load (lua_State *L) { 00343 int status; 00344 const char *cname = luaL_optstring(L, 2, "=(load)"); 00345 luaL_checktype(L, 1, LUA_TFUNCTION); 00346 lua_settop(L, 3); /* function, eventual name, plus one reserved slot */ 00347 status = lua_load(L, generic_reader, NULL, cname); 00348 return load_aux(L, status); 00349 } 00350 00351 00352 static int luaB_dofile (lua_State *L) { 00353 const char *fname = luaL_optstring(L, 1, NULL); 00354 int n = lua_gettop(L); 00355 if (luaL_loadfile(L, fname) != 0) lua_error(L); 00356 lua_call(L, 0, LUA_MULTRET); 00357 return lua_gettop(L) - n; 00358 } 00359 00360 00361 static int luaB_assert (lua_State *L) { 00362 luaL_checkany(L, 1); 00363 if (!lua_toboolean(L, 1)) 00364 return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); 00365 return lua_gettop(L); 00366 } 00367 00368 00369 static int luaB_unpack (lua_State *L) { 00370 int i, e, n; 00371 luaL_checktype(L, 1, LUA_TTABLE); 00372 i = luaL_optint(L, 2, 1); 00373 e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); 00374 if (i > e) return 0; /* empty range */ 00375 n = e - i + 1; /* number of elements */ 00376 if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ 00377 return luaL_error(L, "too many results to unpack"); 00378 lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ 00379 while (i++ < e) /* push arg[i + 1...e] */ 00380 lua_rawgeti(L, 1, i); 00381 return n; 00382 } 00383 00384 00385 static int luaB_select (lua_State *L) { 00386 int n = lua_gettop(L); 00387 if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { 00388 lua_pushinteger(L, n-1); 00389 return 1; 00390 } 00391 else { 00392 int i = luaL_checkint(L, 1); 00393 if (i < 0) i = n + i; 00394 else if (i > n) i = n; 00395 luaL_argcheck(L, 1 <= i, 1, "index out of range"); 00396 return n - i; 00397 } 00398 } 00399 00400 00401 static int luaB_pcall (lua_State *L) { 00402 int status; 00403 luaL_checkany(L, 1); 00404 status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); 00405 lua_pushboolean(L, (status == 0)); 00406 lua_insert(L, 1); 00407 return lua_gettop(L); /* return status + all results */ 00408 } 00409 00410 00411 static int luaB_xpcall (lua_State *L) { 00412 int status; 00413 luaL_checkany(L, 2); 00414 lua_settop(L, 2); 00415 lua_insert(L, 1); /* put error function under function to be called */ 00416 status = lua_pcall(L, 0, LUA_MULTRET, 1); 00417 lua_pushboolean(L, (status == 0)); 00418 lua_replace(L, 1); 00419 return lua_gettop(L); /* return status + all results */ 00420 } 00421 00422 00423 static int luaB_tostring (lua_State *L) { 00424 luaL_checkany(L, 1); 00425 if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ 00426 return 1; /* use its value */ 00427 switch (lua_type(L, 1)) { 00428 case LUA_TNUMBER: 00429 lua_pushstring(L, lua_tostring(L, 1)); 00430 break; 00431 case LUA_TSTRING: 00432 lua_pushvalue(L, 1); 00433 break; 00434 case LUA_TBOOLEAN: 00435 lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); 00436 break; 00437 case LUA_TNIL: 00438 lua_pushliteral(L, "nil"); 00439 break; 00440 default: 00441 lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1)); 00442 break; 00443 } 00444 return 1; 00445 } 00446 00447 00448 static int luaB_newproxy (lua_State *L) { 00449 lua_settop(L, 1); 00450 lua_newuserdata(L, 0); /* create proxy */ 00451 if (lua_toboolean(L, 1) == 0) 00452 return 1; /* no metatable */ 00453 else if (lua_isboolean(L, 1)) { 00454 lua_newtable(L); /* create a new metatable `m' ... */ 00455 lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */ 00456 lua_pushboolean(L, 1); 00457 lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */ 00458 } 00459 else { 00460 int validproxy = 0; /* to check if weaktable[metatable(u)] == true */ 00461 if (lua_getmetatable(L, 1)) { 00462 lua_rawget(L, lua_upvalueindex(1)); 00463 validproxy = lua_toboolean(L, -1); 00464 lua_pop(L, 1); /* remove value */ 00465 } 00466 luaL_argcheck(L, validproxy, 1, "boolean or proxy expected"); 00467 lua_getmetatable(L, 1); /* metatable is valid; get it */ 00468 } 00469 lua_setmetatable(L, 2); 00470 return 1; 00471 } 00472 00473 00474 static const luaL_Reg base_funcs[] = { 00475 {"assert", luaB_assert}, 00476 {"collectgarbage", luaB_collectgarbage}, 00477 {"dofile", luaB_dofile}, 00478 {"error", luaB_error}, 00479 {"gcinfo", luaB_gcinfo}, 00480 {"getfenv", luaB_getfenv}, 00481 {"getmetatable", luaB_getmetatable}, 00482 {"loadfile", luaB_loadfile}, 00483 {"load", luaB_load}, 00484 {"loadstring", luaB_loadstring}, 00485 {"next", luaB_next}, 00486 {"pcall", luaB_pcall}, 00487 {"print", luaB_print}, 00488 {"rawequal", luaB_rawequal}, 00489 {"rawget", luaB_rawget}, 00490 {"rawset", luaB_rawset}, 00491 {"select", luaB_select}, 00492 {"setfenv", luaB_setfenv}, 00493 {"setmetatable", luaB_setmetatable}, 00494 {"tonumber", luaB_tonumber}, 00495 {"tostring", luaB_tostring}, 00496 {"type", luaB_type}, 00497 {"unpack", luaB_unpack}, 00498 {"xpcall", luaB_xpcall}, 00499 {NULL, NULL} 00500 }; 00501 00502 00503 /* 00504 ** {====================================================== 00505 ** Coroutine library 00506 ** ======================================================= 00507 */ 00508 00509 #define CO_RUN 0 /* running */ 00510 #define CO_SUS 1 /* suspended */ 00511 #define CO_NOR 2 /* 'normal' (it resumed another coroutine) */ 00512 #define CO_DEAD 3 00513 00514 static const char *const statnames[] = 00515 {"running", "suspended", "normal", "dead"}; 00516 00517 static int costatus (lua_State *L, lua_State *co) { 00518 if (L == co) return CO_RUN; 00519 switch (lua_status(co)) { 00520 case LUA_YIELD: 00521 return CO_SUS; 00522 case 0: { 00523 lua_Debug ar; 00524 if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 00525 return CO_NOR; /* it is running */ 00526 else if (lua_gettop(co) == 0) 00527 return CO_DEAD; 00528 else 00529 return CO_SUS; /* initial state */ 00530 } 00531 default: /* some error occured */ 00532 return CO_DEAD; 00533 } 00534 } 00535 00536 00537 static int luaB_costatus (lua_State *L) { 00538 lua_State *co = lua_tothread(L, 1); 00539 luaL_argcheck(L, co, 1, "coroutine expected"); 00540 lua_pushstring(L, statnames[costatus(L, co)]); 00541 return 1; 00542 } 00543 00544 00545 static int auxresume (lua_State *L, lua_State *co, int narg) { 00546 int status = costatus(L, co); 00547 if (!lua_checkstack(co, narg)) 00548 luaL_error(L, "too many arguments to resume"); 00549 if (status != CO_SUS) { 00550 lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]); 00551 return -1; /* error flag */ 00552 } 00553 lua_xmove(L, co, narg); 00554 lua_setlevel(L, co); 00555 status = lua_resume(co, narg); 00556 if (status == 0 || status == LUA_YIELD) { 00557 int nres = lua_gettop(co); 00558 if (!lua_checkstack(L, nres + 1)) 00559 luaL_error(L, "too many results to resume"); 00560 lua_xmove(co, L, nres); /* move yielded values */ 00561 return nres; 00562 } 00563 else { 00564 lua_xmove(co, L, 1); /* move error message */ 00565 return -1; /* error flag */ 00566 } 00567 } 00568 00569 00570 static int luaB_coresume (lua_State *L) { 00571 lua_State *co = lua_tothread(L, 1); 00572 int r; 00573 luaL_argcheck(L, co, 1, "coroutine expected"); 00574 r = auxresume(L, co, lua_gettop(L) - 1); 00575 if (r < 0) { 00576 lua_pushboolean(L, 0); 00577 lua_insert(L, -2); 00578 return 2; /* return false + error message */ 00579 } 00580 else { 00581 lua_pushboolean(L, 1); 00582 lua_insert(L, -(r + 1)); 00583 return r + 1; /* return true + `resume' returns */ 00584 } 00585 } 00586 00587 00588 static int luaB_auxwrap (lua_State *L) { 00589 lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 00590 int r = auxresume(L, co, lua_gettop(L)); 00591 if (r < 0) { 00592 if (lua_isstring(L, -1)) { /* error object is a string? */ 00593 luaL_where(L, 1); /* add extra info */ 00594 lua_insert(L, -2); 00595 lua_concat(L, 2); 00596 } 00597 lua_error(L); /* propagate error */ 00598 } 00599 return r; 00600 } 00601 00602 00603 static int luaB_cocreate (lua_State *L) { 00604 lua_State *NL = lua_newthread(L); 00605 luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, 00606 "Lua function expected"); 00607 lua_pushvalue(L, 1); /* move function to top */ 00608 lua_xmove(L, NL, 1); /* move function from L to NL */ 00609 return 1; 00610 } 00611 00612 00613 static int luaB_cowrap (lua_State *L) { 00614 luaB_cocreate(L); 00615 lua_pushcclosure(L, luaB_auxwrap, 1); 00616 return 1; 00617 } 00618 00619 00620 static int luaB_yield (lua_State *L) { 00621 return lua_yield(L, lua_gettop(L)); 00622 } 00623 00624 00625 static int luaB_corunning (lua_State *L) { 00626 if (lua_pushthread(L)) 00627 lua_pushnil(L); /* main thread is not a coroutine */ 00628 return 1; 00629 } 00630 00631 00632 static const luaL_Reg co_funcs[] = { 00633 {"create", luaB_cocreate}, 00634 {"resume", luaB_coresume}, 00635 {"running", luaB_corunning}, 00636 {"status", luaB_costatus}, 00637 {"wrap", luaB_cowrap}, 00638 {"yield", luaB_yield}, 00639 {NULL, NULL} 00640 }; 00641 00642 /* }====================================================== */ 00643 00644 00645 static void auxopen (lua_State *L, const char *name, 00646 lua_CFunction f, lua_CFunction u) { 00647 lua_pushcfunction(L, u); 00648 lua_pushcclosure(L, f, 1); 00649 lua_setfield(L, -2, name); 00650 } 00651 00652 00653 static void base_open (lua_State *L) { 00654 /* set global _G */ 00655 lua_pushvalue(L, LUA_GLOBALSINDEX); 00656 lua_setglobal(L, "_G"); 00657 /* open lib into global table */ 00658 luaL_register(L, "_G", base_funcs); 00659 lua_pushliteral(L, LUA_VERSION); 00660 lua_setglobal(L, "_VERSION"); /* set global _VERSION */ 00661 /* `ipairs' and `pairs' need auxliliary functions as upvalues */ 00662 auxopen(L, "ipairs", luaB_ipairs, ipairsaux); 00663 auxopen(L, "pairs", luaB_pairs, luaB_next); 00664 /* `newproxy' needs a weaktable as upvalue */ 00665 lua_createtable(L, 0, 1); /* new table `w' */ 00666 lua_pushvalue(L, -1); /* `w' will be its own metatable */ 00667 lua_setmetatable(L, -2); 00668 lua_pushliteral(L, "kv"); 00669 lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ 00670 lua_pushcclosure(L, luaB_newproxy, 1); 00671 lua_setglobal(L, "newproxy"); /* set global `newproxy' */ 00672 } 00673 00674 00675 LUALIB_API int luaopen_base (lua_State *L) { 00676 base_open(L); 00677 luaL_register(L, LUA_COLIBNAME, co_funcs); 00678 return 2; 00679 } 00680
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1