static IOFileUD *io_file_open(lua_State *L, const char *mode) { const char *fname = strdata(lj_lib_checkstr(L, 1)); IOFileUD *iof = io_file_new(L); iof->fp = fopen(fname, mode); if (iof->fp == NULL) luaL_argerror(L, 1, lj_str_pushf(L, "%s: %s", fname, strerror(errno))); return iof; }
/* Helper for ISTYPE and ISNUM. Implicit coercion or error. */ void lj_meta_istype(lua_State *L, BCReg ra, BCReg tp) { L->top = curr_topL(L); ra++; tp--; lua_assert(LJ_DUALNUM || tp != ~LJ_TNUMX); /* ISTYPE -> ISNUM broken. */ if (LJ_DUALNUM && tp == ~LJ_TNUMX) lj_lib_checkint(L, ra); else if (tp == ~LJ_TNUMX+1) lj_lib_checknum(L, ra); else if (tp == ~LJ_TSTR) lj_lib_checkstr(L, ra); else lj_err_argtype(L, ra, lj_obj_itypename[tp]); }
int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst) { GCstr *s = def >= 0 ? lj_lib_optstr(L, narg) : lj_lib_checkstr(L, narg); if (s) { const char *opt = strdata(s); MSize len = s->len; int i; for (i = 0; *(const uint8_t *)lst; i++) { if (*(const uint8_t *)lst == len && memcmp(opt, lst+1, len) == 0) return i; lst += 1+*(const uint8_t *)lst; } lj_err_argv(L, narg, LJ_ERR_INVOPTM, opt); } return def; }
#include "lj_lib.h" /* ------------------------------------------------------------------------ */ #define LJLIB_MODULE_string LJLIB_LUA(string_len) /* function(s) CHECK_str(s) return #s end */ LJLIB_ASM(string_byte) LJLIB_REC(string_range 0) { GCstr *s = lj_lib_checkstr(L, 1); int32_t len = (int32_t)s->len; int32_t start = lj_lib_optint(L, 2, 1); int32_t stop = lj_lib_optint(L, 3, start); int32_t n, i; const unsigned char *p; if (stop < 0) stop += len+1; if (start < 0) start += len+1; if (start <= 0) start = 1; if (stop > len) stop = len; if (start > stop) return FFH_RES(0); /* Empty interval: return no results. */ start--; n = stop - start; if ((uint32_t)n > LUAI_MAXCSTACK) lj_err_caller(L, LJ_ERR_STRSLC); lj_state_checkstack(L, (MSize)n);
GCstr *lj_lib_optstr(lua_State *L, int narg) { TValue *o = L->base + narg-1; return (o < L->top && !tvisnil(o)) ? lj_lib_checkstr(L, narg) : NULL; }