//#define Sizeof(x) ((int)sizeof(x)) //#define VOID(p) ((const void*)(p)) public static void PrintString(TString ts) { CharPtr s=GetStr(ts); uint i,n=ts.tsv.len; putchar('"'); for (i=0; i<n; i++) { int c=s[i]; switch (c) { case '"': printf("\\\""); break; case '\\': printf("\\\\"); break; case '\a': printf("\\a"); break; case '\b': printf("\\b"); break; case '\f': printf("\\f"); break; case '\n': printf("\\n"); break; case '\r': printf("\\r"); break; case '\t': printf("\\t"); break; case '\v': printf("\\v"); break; default: if (isprint((byte)c)) putchar(c); else printf("\\%03u",(byte)c); break; } } putchar('"'); }
public static TString newlstr (lua_State L, CharPtr str, uint l, uint h) { TString ts; stringtable tb; if (l+1 > MAX_SIZET /GetUnmanagedSize(typeof(char))) luaM_toobig(L); ts = new TString(new char[l+1]); AddTotalBytes(L, (int)(l + 1) * GetUnmanagedSize(typeof(char)) + GetUnmanagedSize(typeof(TString))); ts.tsv.len = l; ts.tsv.hash = h; ts.tsv.marked = luaC_white(G(L)); ts.tsv.tt = LUA_TSTRING; ts.tsv.reserved = 0; //memcpy(ts+1, str, l*GetUnmanagedSize(typeof(char))); memcpy(ts.str.chars, str.chars, str.index, (int)l); ts.str[l] = '\0'; /* ending 0 */ tb = G(L).strt; h = (uint)lmod(h, tb.size); ts.tsv.next = tb.hash[h]; /* chain new entry */ tb.hash[h] = obj2gco(ts); tb.nuse++; if ((tb.nuse > (int)tb.size) && (tb.size <= MAX_INT/2)) luaS_resize(L, tb.size*2); /* too crowded */ return ts; }
/* ** function to be used with macro "fasttm": optimized for absence of ** tag methods */ public static TValue luaT_gettm (Table events, TMS event_, TString ename) { /*const*/ TValue tm = luaH_getstr(events, ename); LuaAssert(event_ <= TMS.TM_EQ); if (TTIsNil(tm)) { /* no tag method? */ events.flags |= (byte)(1<<(int)event_); /* cache this fact */ return null; } else return tm; }
public static void StringMark(TString s) { Reset2Bits(ref s.tsv.marked, WHITE0BIT, WHITE1BIT); }
private static expkind singlevaraux(FuncState fs, TString n, expdesc var, int base_) { if (fs == null) { /* no more levels? */ init_exp(var, expkind.VGLOBAL, NO_REG); /* default is global variable */ return expkind.VGLOBAL; } else { int v = searchvar(fs, n); /* look up at current level */ if (v >= 0) { init_exp(var, expkind.VLOCAL, v); if (base_==0) markupval(fs, v); /* local will be used as an upval */ return expkind.VLOCAL; } else { /* not found at current level; try upper one */ if (singlevaraux(fs.prev, n, var, 0) == expkind.VGLOBAL) return expkind.VGLOBAL; var.u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */ var.k = expkind.VUPVAL; /* upvalue in this level */ return expkind.VUPVAL; } } }
private static Proto LoadFunction(LoadState S, TString p) { Proto f; if (++S.L.nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); f=luaF_newproto(S.L); setptvalue2s(S.L,S.L.top,f); incr_top(S.L); f.source=LoadString(S); if (f.source==null) f.source=p; f.linedefined=LoadInt(S); f.lastlinedefined=LoadInt(S); f.nups=LoadByte(S); f.numparams=LoadByte(S); f.is_vararg=LoadByte(S); f.maxstacksize=LoadByte(S); LoadCode(S,f); LoadConstants(S,f); LoadDebug(S,f); IF (luaG_checkcode(f)==0 ? 1 : 0, "bad code"); StkId.dec(ref S.L.top); S.L.nCcalls--; return f; }
public static void luaS_fix(TString s) { lu_byte marked = s.tsv.marked; // can't pass properties in as ref l_setbit(ref marked, FIXEDBIT); s.tsv.marked = marked; }
private static void new_localvar(LexState ls, TString name, int n) { FuncState fs = ls.fs; luaY_checklimit(fs, fs.nactvar+n+1, LUAI_MAXVARS, "local variables"); fs.actvar[fs.nactvar+n] = (ushort)registerlocalvar(ls, name); }
public static CharPtr GetStr(TString ts) { return ts.str; }
//#define setsvalue2s setsvalue internal static void setsvalue2s(lua_State L, TValue obj, TString x) { setsvalue(L, obj, x); }
private static void fornum(LexState ls, TString varname, int line) { /* fornum . NAME = exp1,exp1[,exp1] forbody */ FuncState fs = ls.fs; int base_ = fs.freereg; new_localvarliteral(ls, "(for index)", 0); new_localvarliteral(ls, "(for limit)", 1); new_localvarliteral(ls, "(for step)", 2); new_localvar(ls, varname, 3); checknext(ls, '='); exp1(ls); /* initial value */ checknext(ls, ','); exp1(ls); /* limit */ if (testnext(ls, ',') != 0) exp1(ls); /* optional step */ else { /* default step = 1 */ LuaKCodeABx(fs, OpCode.OP_LOADK, fs.freereg, LuaKNumberK(fs, 1)); LuaKReserveRegs(fs, 1); } forbody(ls, base_, line, 1, 1); }
public static Node hashstr(Table t, TString str) { return hashpow2(t, str.tsv.hash); }
public static void stringmark(TString s) { reset2bits(ref s.tsv.marked, WHITE0BIT, WHITE1BIT); }
public static TValue luaH_setstr(lua_State L, Table t, TString key) { TValue p = luaH_getstr(t, key); if (p != luaO_nilobject) return (TValue)p; else { TValue k = new TValue(); setsvalue(L, k, key); return newkey(L, t, k); } }
/* ** search function for strings */ public static TValue luaH_getstr(Table t, TString key) { Node n = hashstr(t, key); do { /* check whether `key' is somewhere in the chain */ if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key) return gval(n); /* that's it */ else n = gnext(n); } while (n != null); return luaO_nilobject; }
private static int l_strcmp(TString ls, TString rs) { CharPtr l = getstr(ls); uint ll = ls.tsv.len; CharPtr r = getstr(rs); uint lr = rs.tsv.len; for (;;) { //int temp = strcoll(l, r); int temp = String.Compare(l.ToString(), r.ToString()); if (temp != 0) return temp; else { /* strings are equal up to a `\0' */ uint len = (uint)l.ToString().Length; /* index of first `\0' in both strings */ if (len == lr) /* r is finished? */ return (len == ll) ? 0 : 1; else if (len == ll) /* l is finished? */ return -1; /* l is smaller than r (because r is not finished) */ /* both strings longer than `len'; go on comparing (after the `\0') */ len++; l += len; ll -= len; r += len; lr -= len; } } }
private static void codestring(LexState ls, expdesc e, TString s) { init_exp(e, expkind.VK, LuaKStringK(ls.fs, s)); }
private static void DumpFunction(Proto f, TString p, DumpState D) { DumpString( ((f.source==p) || (D.strip!=0)) ? null : f.source, D); DumpInt(f.linedefined,D); DumpInt(f.lastlinedefined,D); DumpChar(f.nups,D); DumpChar(f.numparams,D); DumpChar(f.is_vararg,D); DumpChar(f.maxstacksize,D); DumpCode(f,D); DumpConstants(f,D); DumpDebug(f,D); }
private static void forlist(LexState ls, TString indexname) { /* forlist . NAME {,NAME} IN explist1 forbody */ FuncState fs = ls.fs; expdesc e = new expdesc(); int nvars = 0; int line; int base_ = fs.freereg; /* create control variables */ new_localvarliteral(ls, "(for generator)", nvars++); new_localvarliteral(ls, "(for state)", nvars++); new_localvarliteral(ls, "(for control)", nvars++); /* create declared variables */ new_localvar(ls, indexname, nvars++); while (testnext(ls, ',') != 0) new_localvar(ls, str_checkname(ls), nvars++); checknext(ls, (int)RESERVED.TK_IN); line = ls.linenumber; adjust_assign(ls, 3, explist1(ls, e), e); LuaKCheckStack(fs, 3); /* extra space to call generator */ forbody(ls, base_, line, nvars - 3, 0); }
private static void DumpString(TString s, DumpState D) { if (s==null || GetStr(s)==null) { uint size=0; DumpVar(size,D); } else { uint size=s.tsv.len+1; /* include trailing '\0' */ DumpVar(size,D); DumpBlock(GetStr(s),size,D); } }
private static int indexupvalue(FuncState fs, TString name, expdesc v) { int i; Proto f = fs.f; int oldsize = f.sizeupvalues; for (i=0; i<f.nups; i++) { if ((int)fs.upvalues[i].k == (int)v.k && fs.upvalues[i].info == v.u.s.info) { LuaAssert(f.upvalues[i] == name); return i; } } /* new one */ luaY_checklimit(fs, f.nups + 1, LUAI_MAXUPVALUES, "upvalues"); LuaMGrowVector(fs.L, ref f.upvalues, f.nups, ref f.sizeupvalues, MAXINT, ""); while (oldsize < f.sizeupvalues) f.upvalues[oldsize++] = null; f.upvalues[f.nups] = name; LuaCObjBarrier(fs.L, f, name); LuaAssert(v.k == expkind.VLOCAL || v.k == expkind.VUPVAL); fs.upvalues[f.nups].k = CastByte(v.k); fs.upvalues[f.nups].info = CastByte(v.u.s.info); return f.nups++; }
//#define setsvalue2n setsvalue internal static void SetSValue2N(LuaState L, Lua.LuaTypeValue obj, TString x) { SetSValue(L, obj, x); }
//#define setsvalue2s setsvalue internal static void SetSValue2S(LuaState L, TValue obj, TString x) { SetSValue(L, obj, x); }
public static int luaK_stringK (FuncState fs, TString s) { TValue o = new TValue(); setsvalue(fs.L, o, s); return addk(fs, o, o); }
public static int LuaKStringK (FuncState fs, TString s) { TValue o = new LuaTypeValue(); SetSValue(fs.L, o, s); return AddK(fs, o, o); }
protected virtual Node_String parseString(TString node) { return new Node_String( node.Text, getSource(node)); }
public static int sizestring(TString s) {return ((int)s.len + 1) * GetUnmanagedSize(typeof(char)); }
private static int searchvar(FuncState fs, TString n) { int i; for (i=fs.nactvar-1; i >= 0; i--) { if (n == getlocvar(fs, i).varname) return i; } return -1; /* not found */ }
private static int registerlocalvar(LexState ls, TString varname) { FuncState fs = ls.fs; Proto f = fs.f; int oldsize = f.sizelocvars; LuaMGrowVector(ls.L, ref f.locvars, fs.nlocvars, ref f.sizelocvars, (int)SHRT_MAX, "too many local variables"); while (oldsize < f.sizelocvars) f.locvars[oldsize++].varname = null; f.locvars[fs.nlocvars].varname = varname; LuaCObjBarrier(ls.L, f, varname); return fs.nlocvars++; }
public static TValue luaH_setstr (LuaState L, Table t, TString key) { TValue p = luaH_getstr(t, key); if (p != LuaONilObject) return (TValue)p; else { TValue k = new LuaTypeValue(); SetSValue(L, k, key); return newkey(L, t, k); } }