static int db_traceback_count (lv_State *L) { lv_Debug ar; int index = 1; while (lv_getstack(L, index, &ar)) index++; lv_pushnumber( L, index - 1 ); return 1; }
static int maxn (lv_State *L) { lv_clearFirstTableValue(L); lv_Number max = 0; lvL_checktype(L, 1, LV_TTABLE); lv_pushnil(L); /* first key */ while (lv_next(L, 1)) { lv_pop(L, 1); /* remove value */ if (lv_type(L, -1) == LV_TNUMBER) { lv_Number v = lv_tonumber(L, -1); if (v > max) max = v; } } lv_pushnumber(L, max); return 1; }
static int os_time (lv_State *L) { time_t t; if (lv_isnoneornil(L, 1)) /* called without args? */ t = time(NULL); /* get current time */ else { struct tm ts; lvL_checktype(L, 1, LV_TTABLE); lv_settop(L, 1); /* make sure table is at the top */ ts.tm_sec = getfield(L, "sec", 0); ts.tm_min = getfield(L, "min", 0); ts.tm_hour = getfield(L, "hour", 12); ts.tm_mday = getfield(L, "day", -1); ts.tm_mon = getfield(L, "month", -1) - 1; ts.tm_year = getfield(L, "year", -1) - 1900; ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); } if (t == (time_t)(-1)) lv_pushnil(L); else lv_pushnumber(L, (lv_Number)t); return 1; }
static int os_clock (lv_State *L) { lv_pushnumber(L, ((lv_Number)clock())/(lv_Number)CLOCKS_PER_SEC); return 1; }
static int os_difftime (lv_State *L) { lv_pushnumber(L, difftime((time_t)(lvL_checknumber(L, 1)), (time_t)(lvL_optnumber(L, 2, 0)))); return 1; }