LCOV - code coverage report
Current view: top level - src - lbaselib.c Coverage Total Hit
Test: Lua 5.1.5 Lines: 96.4 % 366 353
Test Date: 2024-04-28 10:23:09
Legend: Lines: hit not hit

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

Generated by: LCOV version 2.0-1