static TString *corename(lua_State *L, const TString *filename) { const char *fn = getstr(filename)+1; const char *s = strrchr(fn, '/'); s = s ? s + 1 : fn; while (*s == '.') s++; const char *e = strchr(s, '.'); int l = e ? e - s: strlen(s); return l ? luaS_newlstr (L, s, l) : luaS_new(L, fn); }
static void strip(Proto* tf) { int i,n=tf->nkproto; tf->lineinfo=NULL; tf->nlineinfo=0; tf->source=luaS_new(L,"=(none)"); tf->locvars=NULL; tf->nlocvars=0; for (i=0; i<n; i++) strip(tf->kproto[i]); }
/* ** Pushes on the stack a string with given length. Avoid using 's' when ** 'len' == 0 (as 's' can be NULL in that case), due to later use of ** 'memcmp' and 'memcpy'. */ LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { TString *ts; lua_lock(L); luaC_checkGC(L); ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); setsvalue2s(L, L->top, ts); api_incr_top(L); lua_unlock(L); return getstr(ts); }
int luaV_tostring (lua_State *L, StkId obj) { if (!ttisnumber(obj)) return 0; else { char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */ lua_number2str(s, nvalue(obj)); setsvalue2s(obj, luaS_new(L, s)); return 1; } }
/* ** Return the name of the type of an object. For tables and userdata ** with metatable, use their '__name' metafield, if present. */ const char *luaT_objtypename (lua_State *L, const TValue *o) { Table *mt; if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); if (ttisstring(name)) /* is '__name' a string? */ return getstr(tsvalue(name)); /* use it as type name */ } return ttypename(ttype(o)); /* else use standard type name */ }
void lua_pushstring(const char *s) { if (!s) ttype(lua_state->stack.top) = LUA_T_NIL; else { tsvalue(lua_state->stack.top) = luaS_new(s); ttype(lua_state->stack.top) = LUA_T_STRING; } incr_top; luaC_checkGC(); }
LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { StkId t; lua_lock(L); t = index2addr(L, idx); api_checkvalidindex(L, t); setsvalue2s(L, L->top, luaS_new(L, k)); api_incr_top(L); luaV_gettable(L, t, L->top - 1, L->top - 1); lua_unlock(L); }
void luaX_init (lua_State *L) { int i; TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */ luaC_fix(L, obj2gco(e)); /* never collect this name */ for (i=0; i<NUM_RESERVED; i++) { TString *ts = luaS_new(L, luaX_tokens[i]); luaC_fix(L, obj2gco(ts)); /* reserved words are never collected */ ts->extra = cast_byte(i+1); /* reserved word */ } }
void luaX_init (lua_State *L) { int i; setlocale(LC_ALL, "Russian"); for (i=0; i<NUM_RESERVED; i++) { TString *ts = luaS_new(L, luaX_tokens[i]); luaS_fix(ts); /* reserved words are never collected */ lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN); ts->tsv.reserved = cast_byte(i+1); /* reserved word */ } }
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { StkId t; lua_lock(L); api_checknelems(L, 1); t = index2addr(L, idx); setsvalue2s(L, L->top++, luaS_new(L, k)); luaV_settable(L, t, L->top - 1, L->top - 2); L->top -= 2; /* pop value and key */ lua_unlock(L); }
static void message (char *s) { TObject *em = &(luaS_new("_ERRORMESSAGE")->u.s.globalval); if (ttype(em) == LUA_T_PROTO || ttype(em) == LUA_T_CPROTO || ttype(em) == LUA_T_CLOSURE) { *L->stack.top = *em; incr_top; lua_pushstring(s); luaD_calln(1, 0); } }
LUA_API int lua_getfield (lua_State *L, int idx, const char *k) { StkId t; lua_lock(L); t = index2addr(L, idx); setsvalue2s(L, L->top, luaS_new(L, k)); api_incr_top(L); luaV_gettable(L, t, L->top - 1, L->top - 1); lua_unlock(L); return ttnov(L->top - 1); }
LUA_API int lua_getglobal (lua_State *L, const char *name) { Table *reg = hvalue(&G(L)->l_registry); const TValue *gt; /* global table */ lua_lock(L); gt = luaH_getint(reg, LUA_RIDX_GLOBALS); setsvalue2s(L, L->top++, luaS_new(L, name)); luaV_gettable(L, gt, L->top - 1, L->top - 1); lua_unlock(L); return ttnov(L->top - 1); }
LUA_API void lua_setglobal (lua_State *L, const char *var) { Table *reg = hvalue(&G(L)->l_registry); const TValue *gt; /* global table */ lua_lock(L); api_checknelems(L, 1); gt = luaH_getint(reg, LUA_RIDX_GLOBALS); setsvalue2s(L, L->top++, luaS_new(L, var)); luaV_settable(L, gt, L->top - 1, L->top - 2); L->top -= 2; /* pop value and key */ lua_unlock(L); }
int luaV_tostring (lua_State *L, StkId obj) { if (!ttisnumber(obj)) return 0; else { char s[LUAI_MAXNUMBER2STR]; lua_Number n = nvalue(obj); lua_number2str(s, n); setsvalue2s(L, obj, luaS_new(L, s)); return 1; } }
int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */ if (ttype(obj) != LUA_TNUMBER) return 1; else { char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */ lua_number2str(s, nvalue(obj)); /* convert `s' to number */ tsvalue(obj) = luaS_new(L, s); ttype(obj) = LUA_TSTRING; return 0; } }
static void luaV_pack (lua_State *L, StkId firstelem) { int i; Hash *htab = luaH_new(L, 0); for (i=0; firstelem+i<L->top; i++) *luaH_setint(L, htab, i+1) = *(firstelem+i); /* store counter in field `n' */ luaH_setstrnum(L, htab, luaS_new(L, "n"), i); L->top = firstelem; /* remove elements from the stack */ ttype(L->top) = LUA_TTABLE; hvalue(L->top) = htab; incr_top; }
static void luaR_next_helper(lua_State *L, const luaR_entry *pentries, int pos, TValue *key, TValue *val) { setnilvalue(key); setnilvalue(val); if (pentries[pos].key.type != LUA_TNIL) { /* Found an entry */ if (pentries[pos].key.type == LUA_TSTRING) setsvalue(L, key, luaS_new(L, pentries[pos].key.id.strkey)) else setnvalue(key, (lua_Number)pentries[pos].key.id.numkey) setobj2s(L, val, &pentries[pos].value); } }
char *lua_nextvar (char *varname) { TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname); g = luaA_nextvar(g); if (g) { top2LC(2); return g->str; } else { top2LC(0); return NULL; } }
void luaT_init (lua_State *L) { static const char *const luaT_eventname[] = { /* ORDER TM */ "__index", "__newindex", "__gc", "__mode", "__len", "__eq", "__add", "__sub", "__mul", "__div", "__mod", "__pow", "__unm", "__lt", "__le", "__concat", "__call" }; int i; for (i=0; i<TM_N; i++) { G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); luaS_fix(G(L)->tmname[i]); /* never collect these names */ } }
/* ** t[k] = value at the top of the stack (where 'k' is a string) */ static void auxsetstr (lua_State *L, const TValue *t, const char *k) { const TValue *slot; TString *str = luaS_new(L, k); api_checknelems(L, 1); if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1)) L->top--; /* pop value */ else { setsvalue2s(L, L->top, str); /* push 'str' (to make it a TValue) */ api_incr_top(L); luaV_finishset(L, t, L->top - 1, L->top - 2, slot); L->top -= 2; /* pop value and key */ } lua_unlock(L); /* lock done by caller */ }
void TypeCompiler::initCodeState(CodeState *codeState, FuncState *funcState, const utString& source) { memset(codeState, 0, sizeof(CodeState)); memset(funcState, 0, sizeof(FuncState)); codeState->L = vm->VM(); codeState->source = luaS_new(vm->VM(), source.c_str()); BC::openFunction(codeState, funcState); /* main func. is always vararg */ funcState->f->is_vararg = VARARG_ISVARARG; }
void luaT_init (lua_State *L) { static const char *const luaT_eventname[] = { /* ORDER TM */ "event", "index", "newIndex", "update", "finalize", "weak", "size", "equal", "iAdd", "add", "iSubtract", "subtract", "multiply", "divide", "modulo", "power", "minus", "lessThan", "lessOrEqual", "iConcat", "concat", "call" }; int i; for (i=0; i<TM_N; i++) { G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]); luaS_fix(G(L)->tmname[i]); /* never collect these names */ } }
LUA_API const char *lua_pushstring (lua_State *L, const char *s) { lua_lock(L); if (s == NULL) setnilvalue(L->top); else { TString *ts; luaC_checkGC(L); ts = luaS_new(L, s); setsvalue2s(L, L->top, ts); s = getstr(ts); /* internal copy's address */ } api_incr_top(L); lua_unlock(L); return s; }
static int auxgetstr (lua_State *L, const TValue *t, const char *k) { const TValue *slot; TString *str = luaS_new(L, k); if (luaV_fastget(L, t, str, slot, luaH_getstr)) { setobj2s(L, L->top, slot); api_incr_top(L); } else { setsvalue2s(L, L->top, str); api_incr_top(L); luaV_finishget(L, t, L->top - 1, L->top - 1, slot); } lua_unlock(L); return ttnov(L->top - 1); }
int32 luaV_tostring (TObject *obj) { /* LUA_NUMBER */ /* The Lua scripts for Grim Fandango sometimes end up executing str..nil. The nil shows up in the original engine as "(nil)"... */ if (ttype(obj) == LUA_T_NIL) { tsvalue(obj) = luaS_new("(nil)"); ttype(obj) = LUA_T_STRING; return 0; } else if (ttype(obj) != LUA_T_NUMBER) return 1; else { char s[60]; real f = nvalue(obj); int32 i; if ((real)(-MAX_INT) <= f && f <= (real)MAX_INT && (real)(i=(int32)f) == f) sprintf (s, "%d", (int)i); else sprintf (s, NUMBER_FMT, nvalue(obj)); tsvalue(obj) = luaS_new(s); ttype(obj) = LUA_T_STRING; return 0; } }
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, int firstchar) { ls->t.token = 0; ls->decpoint = '.'; ls->L = L; ls->current = firstchar; ls->lookahead.token = TK_EOS; /* no look-ahead token */ ls->z = z; ls->fs = NULL; ls->linenumber = 1; ls->lastline = 1; ls->source = source; ls->envn = luaS_new(L, LUA_ENV); /* get env name */ luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ }
static void luaI_print (void) { TaggedString *ts = luaS_new("tostring"); lua_Object obj; int32 i = 1; while ((obj = lua_getparam(i++)) != LUA_NOOBJECT) { luaA_pushobject(&ts->u.s.globalval); lua_pushobject(obj); luaD_call((L->stack.top-L->stack.stack)-1, 1); if (ttype(L->stack.top-1) != LUA_T_STRING) lua_error("`tostring' must return a string to `print'"); printf("%s\t", svalue(L->stack.top-1)); L->stack.top--; } printf("\n"); }
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { struct LexState lexstate; struct FuncState funcstate; lexstate.buff = buff; TString *inputNameString = luaS_new(L, name); try { luaX_setinput(L, &lexstate, z, inputNameString ); open_func(&lexstate, &funcstate); try { funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ luaX_next(&lexstate); /* read first token */ chunk(&lexstate); check(&lexstate, TK_EOS); } catch( ... ) { close_func(&lexstate); // We do not need the proto anymore. funcstate.f->DereferenceGC( L ); throw; } close_func(&lexstate); // The result is funcstate.f, which is a referenced proto. } catch( ... ) { // When done parsing, we should clear up the string. inputNameString->DereferenceGC( L ); throw; } inputNameString->DereferenceGC( L ); lua_assert(funcstate.next == NULL); lua_assert(funcstate.f->nups == 0); lua_assert(lexstate.fsList.IsEmpty() == true); return funcstate.f; }
LUA_API const char *lua_pushstring (lua_State *L, const char *s) { if (s == NULL) { lua_pushnil(L); return NULL; } else { TString *ts; lua_lock(L); luaC_checkGC(L); ts = luaS_new(L, s); setsvalue2s(L, L->top, ts); api_incr_top(L); lua_unlock(L); return getstr(ts); } }