static int luaB_unpack (lua_State *L) { int i, e, n; luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optint(L, 2, 1); e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); if (i > e) return 0; /* empty range */ n = e - i + 1; /* number of elements */ if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ return luaL_error(L, "too many results to unpack"); lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ while (i++ < e) /* push arg[i + 1...e] */ lua_rawgeti(L, 1, i); return n; }
static int tunpack (lua_State *L) { lua_Unsigned n; lua_Integer i = luaL_optinteger(L, 2, 1); lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) return luaL_error(L, "too many results to unpack"); for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ lua_geti(L, 1, i); } lua_geti(L, 1, e); /* push last element */ return (int)n; }
static int unpack (lua_State *L) { int i, e; unsigned int n; luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optint(L, 2, 1); e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */ if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n)) return luaL_error(L, "too many results to unpack"); lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ while (i++ < e) /* push arg[i + 1...e] */ lua_rawgeti(L, 1, i); return n; }
static int tshuffle (lua_State *L) { lua_Integer begin, end; checktab(L, 1, TAB_RW); begin = luaL_optinteger(L, 2, 1); end = luaL_opt(L, luaL_checkinteger, 3, check_n(L, 1)); while (end >= begin) { double f = l_rand() * (1.0/(L_RANDMAX+1.0)); lua_Integer j = begin + (lua_Integer)(f * (end-begin+1)); lua_geti(L, 1, end); lua_geti(L, 1, j); lua_seti(L, 1, end); lua_seti(L, 1, j); --end; } return 0; }
static int unpack (lua_State *L) { lua_Integer i, e; lua_Unsigned n; luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optinteger(L, 2, 1); e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); if (i > e) return 0; /* empty range */ n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, ++n)) return luaL_error(L, "too many results to unpack"); do { /* must have at least one element */ lua_rawgeti(L, 1, i); /* push arg[i..e] */ } while (i++ < e); return n; }
static int os_date (LuaThread *L) { THREAD_CHECK(L); const char *s = luaL_optstring(L, 1, "%c"); time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); struct tm tmr, *stm; if (*s == '!') { /* UTC? */ stm = l_gmtime(&t, &tmr); s++; /* skip `!' */ } else stm = l_localtime(&t, &tmr); if (stm == NULL) { /* invalid date? */ L->stack_.push(LuaValue::Nil()); } else if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setfield(L, "sec", stm->tm_sec); setfield(L, "min", stm->tm_min); setfield(L, "hour", stm->tm_hour); setfield(L, "day", stm->tm_mday); setfield(L, "month", stm->tm_mon+1); setfield(L, "year", stm->tm_year+1900); setfield(L, "wday", stm->tm_wday+1); setfield(L, "yday", stm->tm_yday+1); setboolfield(L, "isdst", stm->tm_isdst); } else { char cc[4]; luaL_Buffer b; cc[0] = '%'; luaL_buffinit(L, &b); while (*s) { if (*s != '%') /* no conversion specifier? */ luaL_addchar(&b, *s++); else { size_t reslen; char buff[200]; /* should be big enough for any conversion result */ s = checkoption(L, s + 1, cc); reslen = strftime(buff, sizeof(buff), cc, stm); luaL_addlstring(&b, buff, reslen); } } luaL_pushresult(&b); } return 1; }
static int tconcat (lua_State *L) { luaL_Buffer b; lua_Integer last = aux_getn(L, 1, TAB_R); size_t lsep; const char *sep = luaL_optlstring(L, 2, "", &lsep); lua_Integer i = luaL_optinteger(L, 3, 1); last = luaL_opt(L, luaL_checkinteger, 4, last); luaL_buffinit(L, &b); for (; i < last; i++) { addfield(L, &b, i); luaL_addlstring(&b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ addfield(L, &b, i); luaL_pushresult(&b); return 1; }
static int os_date (lua_State *L) { const char *s = luaL_optstring(L, 1, "%c"); time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); struct tm *stm; if (*s == '!') { /* UTC? */ stm = gmtime(&t); s++; /* skip `!' */ } else stm = localtime(&t); if (stm == NULL) /* invalid date? */ lua_pushnil(L); else if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setfield(L, "sec", stm->tm_sec); setfield(L, "min", stm->tm_min); setfield(L, "hour", stm->tm_hour); setfield(L, "day", stm->tm_mday); setfield(L, "month", stm->tm_mon+1); setfield(L, "year", stm->tm_year+1900); setfield(L, "wday", stm->tm_wday+1); setfield(L, "yday", stm->tm_yday+1); setboolfield(L, "isdst", stm->tm_isdst); } else { char cc[3]; luaL_Buffer b; cc[0] = '%'; cc[2] = '\0'; luaL_buffinit(L, &b); for (; *s; s++) { if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ luaL_addchar(&b, *s); else { size_t reslen; char buff[200]; /* should be big enough for any conversion result */ cc[1] = *(++s); reslen = strftime(buff, sizeof(buff), cc, stm); luaL_addlstring(&b, buff, reslen); } } luaL_pushresult(&b); } return 1; }
static int tconcat (lua_State *L) { luaL_Buffer b; size_t lsep; int i, last; const char *sep = luaL_optlstring(L, 2, "", &lsep); luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optint(L, 3, 1); last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1)); luaL_buffinit(L, &b); for (; i < last; i++) { addfield(L, &b, i); luaL_addlstring(&b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ addfield(L, &b, i); luaL_pushresult(&b); return 1; }
static int os_date (lua_State *L) { const char *s = luaL_optstring(L, 1, "%c"); time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); struct tm tmr, *stm; if (*s == '!') { /* UTC? */ stm = l_gmtime(&t, &tmr); s++; /* skip '!' */ } else stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ luaL_error(L, "time result cannot be represented in this installation"); if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setfield(L, "sec", stm->tm_sec); setfield(L, "min", stm->tm_min); setfield(L, "hour", stm->tm_hour); setfield(L, "day", stm->tm_mday); setfield(L, "month", stm->tm_mon+1); setfield(L, "year", stm->tm_year+1900); setfield(L, "wday", stm->tm_wday+1); setfield(L, "yday", stm->tm_yday+1); setboolfield(L, "isdst", stm->tm_isdst); } else { char cc[4]; luaL_Buffer b; cc[0] = '%'; luaL_buffinit(L, &b); while (*s) { if (*s != '%') /* not a conversion specifier? */ luaL_addchar(&b, *s++); else { size_t reslen; char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); s = checkoption(L, s + 1, cc); reslen = strftime(buff, SIZETIMEFMT, cc, stm); luaL_addsize(&b, reslen); } } luaL_pushresult(&b); } return 1; }
static int tconcat (lua_State *L) { TabA ta; luaL_Buffer b; size_t lsep; lua_Integer i, last; const char *sep = luaL_optlstring(L, 2, "", &lsep); checktab(L, 1, &ta); i = luaL_optinteger(L, 3, 1); last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1)); luaL_buffinit(L, &b); for (; i < last; i++) { addfield(L, &b, &ta, i); luaL_addlstring(&b, sep, lsep); } if (i == last) /* add last value (if interval was not empty) */ addfield(L, &b, &ta, i); luaL_pushresult(&b); return 1; }
// This one I literally copied and pasted from Lua and added the count check. int luaL_unpack_exact (lua_State *L, int count) { int i, e, n; int tablepos = lua_gettop(L); luaL_checktype(L, -1, LUA_TTABLE); i = luaL_optint(L, tablepos + 1, 1); e = luaL_opt(L, luaL_checkint, tablepos + 2, luaL_len(L, tablepos)); if (i > e) return 0; /* empty range */ n = e - i + 1; /* number of elements */ if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ return luaL_error(L, "too many results to unpack"); if (n != count) return luaL_error(L, "wrong number of results to unpack (%d, expected %d)", n, count); lua_rawgeti(L, tablepos, i); /* push arg[i] (avoiding overflow problems) */ while (i++ < e) /* push arg[i + 1...e] */ lua_rawgeti(L, tablepos, i); return n; }
static int tconcat (lua_State *L) { luaL_Buffer b; size_t lsep; int i, last; const char *sep = luaL_optlstring(L, 2, "", &lsep); luaL_checktype(L, 1, LUA_TTABLE); i = luaL_optint(L, 3, 1); last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1)); luaL_buffinit(L, &b); for (; i <= last; i++) { lua_rawgeti(L, 1, i); luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings"); luaL_addvalue(&b); if (i != last) luaL_addlstring(&b, sep, lsep); } luaL_pushresult(&b); return 1; }
static int trotate (lua_State *L) { lua_Integer n, begin, end; checktab(L, 1, TAB_RW); n = -luaL_checkinteger(L, 2); begin = luaL_optinteger(L, 3, 1); end = luaL_opt(L, luaL_checkinteger, 4, check_n(L, 1)); if (end > begin) { n %= end - begin + 1; if (n < 0) n += end - begin + 1; if (n != 0) { lua_settop(L, 1); reverse(L, begin, begin+n-1); reverse(L, begin+n, end); reverse(L, begin, end); } } return 0; }
static int os_date (lua_State *L) { size_t slen; const char *s = luaL_optlstring(L, 1, "%c", &slen); time_t t = luaL_opt(L, l_checktime, 2, time(NULL)); const char *se = s + slen; /* 's' end */ struct tm tmr, *stm; if (*s == '!') { /* UTC? */ stm = l_gmtime(&t, &tmr); s++; /* skip '!' */ } else stm = l_localtime(&t, &tmr); if (stm == NULL) /* invalid date? */ return luaL_error(L, "time result cannot be represented in this installation"); if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setallfields(L, stm); } else { char cc[4]; /* buffer for individual conversion specifiers */ luaL_Buffer b; cc[0] = '%'; luaL_buffinit(L, &b); while (s < se) { if (*s != '%') /* not a conversion specifier? */ luaL_addchar(&b, *s++); else { size_t reslen; char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT); s++; /* skip '%' */ s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */ reslen = strftime(buff, SIZETIMEFMT, cc, stm); luaL_addsize(&b, reslen); } } luaL_pushresult(&b); } return 1; }
static int openssl_ec_key_parse(lua_State*L) { EC_KEY* ec = CHECK_OBJECT(1, EC_KEY, "openssl.ec_key"); int basic = luaL_opt(L,lua_toboolean, 2, 0); const EC_POINT* point = EC_KEY_get0_public_key(ec); const EC_GROUP* group = EC_KEY_get0_group(ec); const BIGNUM *priv = EC_KEY_get0_private_key(ec); lua_newtable(L); if (basic) { BIGNUM* x = BN_new(); BIGNUM* y = BN_new(); AUXILIAR_SET(L, -1, "enc_flag", EC_KEY_get_enc_flags(ec), integer); AUXILIAR_SET(L, -1, "conv_form", EC_KEY_get_conv_form(ec), integer); AUXILIAR_SET(L, -1, "curve_name", EC_GROUP_get_curve_name(group), integer); AUXILIAR_SETOBJECT(L, BN_dup(priv), "openssl.bn", -1, "d"); if (EC_POINT_get_affine_coordinates_GFp(group, point, x, y, NULL) == 1) { AUXILIAR_SETOBJECT(L, x, "openssl.bn", -1, "x"); AUXILIAR_SETOBJECT(L, y, "openssl.bn", -1, "y"); }; } else { AUXILIAR_SET(L, -1, "enc_flag", EC_KEY_get_enc_flags(ec), integer); AUXILIAR_SET(L, -1, "conv_form", EC_KEY_get_conv_form(ec), integer); point = EC_POINT_dup(point, group); AUXILIAR_SETOBJECT(L, point, "openssl.ec_point", -1, "pub_key"); group = EC_GROUP_dup(group); AUXILIAR_SETOBJECT(L, group, "openssl.ec_group", -1, "group"); OPENSSL_PKEY_GET_BN(priv, priv_key); } return 1; };
LUALIB_API lua_CBaseEntity *luaL_optentity (lua_State *L, int narg, CBaseEntity *def) { return luaL_opt(L, luaL_checkentity, narg, def); }
static int BeginRenderingTo( T* p, lua_State *L ) { bool bPreserveTexture = !!luaL_opt( L, lua_toboolean, 1, false ); p->BeginRenderingTo( bPreserveTexture ); COMMON_RETURN_SELF; }
LUALIB_API lua_KeyValues *luaL_optkeyvalues (lua_State *L, int narg, KeyValues *def) { return luaL_opt(L, luaL_checkkeyvalues, narg, def); }
lua_Unsigned luaL_optunsigned (lua_State *L, int i, lua_Unsigned def) { return luaL_opt(L, luaL_checkunsigned, i, def); }
static int BeginRenderingTo( T* p, lua_State *L ) { bool bPreserveTexture = !!luaL_opt( L, lua_toboolean, 1, false ); p->BeginRenderingTo( bPreserveTexture ); return 0; }
uint32_t luaL_optunsigned (LuaThread *L, int narg, uint32_t def) { THREAD_CHECK(L); return luaL_opt(L, luaL_checkunsigned, narg, def); }
ptrdiff_t luaL_optinteger (LuaThread *L, int narg, ptrdiff_t def) { THREAD_CHECK(L); return luaL_opt(L, luaL_checkinteger, narg, def); }
double luaL_optnumber (LuaThread *L, int narg, double def) { THREAD_CHECK(L); return luaL_opt(L, luaL_checknumber, narg, def); }
LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, lua_Integer def) { return luaL_opt(L, luaL_checkinteger, narg, def); }
LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { return luaL_opt(L, luaL_checknumber, narg, def); }
static int treplace (lua_State *L) { lua_Integer len, tpos, start, end, start2, end2, i; len = aux_getn(L, 1, TAB_RW); if (lua_type(L, 2) == LUA_TNUMBER) { start = luaL_checkinteger(L, 2); luaL_argcheck(L, start >= 1 && start <= len+1, 2, "index out of bounds"); if (lua_type(L, 3) == LUA_TNUMBER) { end = luaL_checkinteger(L, 3); luaL_argcheck(L, end >= start-1 && end <= len, 3, "invalid end index"); tpos = 4; } else { end = start; if (end > len) end = len; tpos = 3; } } else { start = len+1; end = len; tpos = 2; } checktab(L, tpos, TAB_R); start2 = luaL_optinteger(L, tpos+1, 1); end2 = luaL_opt(L, luaL_checkinteger, tpos+2, check_n(L, tpos)); luaL_argcheck(L, end2 >= start2-1, tpos+2, "invalid end index"); if (end2-start2 > end-start) { /* array needs to grow */ lua_pushinteger(L, len+end2-start2-end+start); lua_setfield(L, 1, "n"); /* t.n = number of elements */ } if (start <= len) { /* replace values */ lua_Integer shift = end2-start2-end+start; if (shift < 0) { /* shift to left */ for (i = end+1; i <= len; ++i) { lua_geti(L, 1, i); lua_seti(L, 1, i+shift); } for (i = len; i > len+shift; --i) { lua_pushnil(L); lua_seti(L, 1, i); } } else if (shift != 0) { /* shift to right */ for (i = len-shift+1; i <= len; ++i) { lua_geti(L, 1, i); lua_seti(L, 1, i+shift); } for (i = len-shift; i > end; --i) { lua_geti(L, 1, i); lua_seti(L, 1, i+shift); } } } /* copy from list2 to list1 */ for (i = start2; i <= end2; ++i) { lua_geti(L, tpos, i); lua_seti(L, 1, start+i-start2); } /* array must shrink */ if (end2-start2 < end-start) { lua_pushinteger(L, len+end2-start2-end+start); lua_setfield(L, 1, "n"); /* t.n = number of elements */ } return 0; }
LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg, lua_Unsigned def) { return luaL_opt(L, luaL_checkunsigned, narg, def); }
LUALIB_API lua_Integer luaL_optboolean (lua_State *L, int narg, int def) { return luaL_opt(L, luaL_checkboolean, narg, def); }