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; }
private static int GetBoolField(LuaState L, CharPtr key) { int res; LuaGetField(L, -1, key); res = LuaIsNil(L, -1) ? -1 : LuaToBoolean(L, -1); LuaPop(L, 1); return res; }
private static int getboolfield(lua_State L, CharPtr key) { int res; lua_getfield(L, -1, key); res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); lua_pop(L, 1); return res; }
public static void LuaXLexError(LexState ls, CharPtr msg, int token) { CharPtr buff = new char[MAXSRC]; LuaOChunkID(buff, GetStr(ls.source), MAXSRC); msg = LuaOPushFString(ls.L, "%s:%d: %s", buff, ls.linenumber, msg); if (token != 0) LuaOPushFString(ls.L, "%s near " + LUA_QS, msg, TextToken(ls, token)); LuaDThrow(ls.L, LUA_ERRSYNTAX); }
private static void TreatStackOption (LuaState L, LuaState L1, CharPtr fname) { if (L == L1) { LuaPushValue(L, -2); LuaRemove(L, -3); } else LuaXMove(L1, L, 1); LuaSetField(L, -2, fname); }
private static void treatstackoption (lua_State L, lua_State L1, CharPtr fname) { if (L == L1) { lua_pushvalue(L, -2); lua_remove(L, -3); } else lua_xmove(L1, L, 1); lua_setfield(L, -2, fname); }
private static void DumpBlock(CharPtr b, uint size, DumpState D) { if (D.status==0) { LuaUnlock(D.L); D.status=D.writer(D.L,b,size,D.data); LuaLock(D.L); } }
private static int os_pushresult (lua_State L, int i, CharPtr filename) { int en = errno(); /* calls to Lua API may change this value */ if (i != 0) { lua_pushboolean(L, 1); return 1; } else { lua_pushnil(L); lua_pushfstring(L, "%s: %s", filename, strerror(en)); lua_pushinteger(L, en); return 3; } }
private static int OSPushResult (LuaState L, int i, CharPtr filename) { int en = errno(); /* calls to Lua API may change this value */ if (i != 0) { LuaPushBoolean(L, 1); return 1; } else { LuaPushNil(L); LuaPushFString(L, "%s: %s", filename, strerror(en)); LuaPushInteger(L, en); return 3; } }
private static int GetField(LuaState L, CharPtr key, int d) { int res; LuaGetField(L, -1, key); if (LuaIsNumber(L, -1) != 0) res = (int)LuaToInteger(L, -1); else { if (d < 0) return LuaLError(L, "field " + LUA_QS + " missing in date table", key); res = d; } LuaPop(L, 1); return res; }
private static int getfield(lua_State L, CharPtr key, int d) { int res; lua_getfield(L, -1, key); if (lua_isnumber(L, -1) != 0) res = (int)lua_tointeger(L, -1); else { if (d < 0) return luaL_error(L, "field " + LUA_QS + " missing in date table", key); res = d; } lua_pop(L, 1); return res; }
public static TString luaS_newlstr(lua_State L, CharPtr str, uint l) { GCObject o; uint h = (uint)l; /* seed */ uint step = (l>>5)+1; /* if string is too long, don't hash all its chars */ uint l1; for (l1=l; l1>=step; l1-=step) /* compute hash */ h = h ^ ((h<<5)+(h>>2)+(byte)str[l1-1]); for (o = G(L).strt.hash[lmod(h, G(L).strt.size)]; o != null; o = o.gch.next) { TString ts = rawgco2ts(o); if (ts.tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { /* string may be dead */ if (isdead(G(L), o)) changewhite(o); return ts; } } //return newlstr(L, str, l, h); /* not found */ TString res = newlstr(L, str, l, h); return res; }
/* * make header */ public static void luaU_header(CharPtr h) { h = new CharPtr(h); int x=1; memcpy(h, LUA_SIGNATURE, LUA_SIGNATURE.Length); h = h.add(LUA_SIGNATURE.Length); h[0] = (char)LUAC_VERSION; h.inc(); h[0] = (char)LUAC_FORMAT; h.inc(); //*h++=(char)*(char*)&x; /* endianness */ h[0] = (char)x; /* endianness */ h.inc(); h[0] = (char)sizeof(int); h.inc(); h[0] = (char)sizeof(uint); h.inc(); h[0] = (char)sizeof(Instruction); h.inc(); h[0] = (char)sizeof(lua_Number); h.inc(); //(h++)[0] = ((lua_Number)0.5 == 0) ? 0 : 1; /* is lua_Number integral? */ h[0] = (char)0; // always 0 on this build }
private static void read_long_string(LexState ls, SemInfo seminfo, int sep) { int line = ls.linenumber; /* initial line (for error message) */ save_and_next(ls); /* skip 2nd `[' */ if (currIsNewline(ls)) /* string starts with a newline? */ { inclinenumber(ls); /* skip it */ } for (;;) { switch (ls.current) { case EOZ: { /* error */ CharPtr what = (seminfo != null ? "string" : "comment"); CharPtr msg = luaO_pushfstring(ls.L, "unfinished long %s (starting at line %d)", what, line); lexerror(ls, msg, (int)RESERVED.TK_EOS); break; /* to avoid warnings */ } case ']': { if (skip_sep(ls) == sep) { save_and_next(ls); /* skip 2nd `]' */ goto endloop; } break; } case '\n': case '\r': { save(ls, '\n'); inclinenumber(ls); if (seminfo == null) { luaZ_resetbuffer(ls.buff); /* avoid wasting space */ } break; } default: { if (seminfo != null) { save_and_next(ls); } else { next(ls); } } break; } } endloop: if (seminfo != null) { seminfo.ts = luaX_newstring(ls, luaZ_buffer(ls.buff) + (2 + sep), (uint)(luaZ_bufflen(ls.buff) - 2 * (2 + sep))); } }
/* * @@ luai_writestring/luai_writeline define how 'print' prints its results. ** They are only used in libraries and the stand-alone program. */ public static void luai_writestring(CharPtr s, uint l) { fwrite(s, 1 /*sizeof(char)*/, (int)l, stdout); }
public static void /*l_noret*/ error(LoadState S, CharPtr why) { luaO_pushfstring(S.L, "%s: %s precompiled chunk", S.name, why); luaD_throw(S.L, LUA_ERRSYNTAX); }
public luaL_Reg(CharPtr name, lua_CFunction func) { this.name = name; this.func = func; }
public static CharPtr LuaLOptLString(LuaState L, int narg, CharPtr def) { uint len; return(LuaLOptLString(L, narg, def, out len)); }
public static void LuaLRegister(LuaState L, CharPtr libname, LuaLReg[] l) { LuaIOpenLib(L, libname, l, 0); }
/* ** {====================================================== ** Time/Date operations ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, ** wday=%w+1, yday=%j, isdst=? } ** ======================================================= */ private static void setfield(lua_State L, CharPtr key, int value) { lua_pushinteger(L, value); lua_setfield(L, -2, key); }
//#define luaL_dofile(L, fn) \ // (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) //#define luaL_dostring(L, s) \ // (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) public static void LuaLGetMetatable(LuaState L, CharPtr n) { LuaGetField(L, LUA_REGISTRYINDEX, n); }
public static int lua_number2str(CharPtr s, lua_Number n) { return(sprintf(s, LUA_NUMBER_FMT, n)); }
//#define l_mathop(op) op public static double lua_str2number(CharPtr s, out CharPtr p) { return(strtod(s, out p)); }
/* * @@ luai_writestringerror defines how to print error messages. ** (A format string with one argument is enough for Lua...) */ public static void luai_writestringerror(CharPtr s, object p) { fprintf(stderr, s, p); fflush(stderr); }
static void error(LoadState S, CharPtr why) { luaO_pushfstring(S.L,"%s: %s in precompiled chunk",S.name,why); luaD_throw(S.L,LUA_ERRSYNTAX); }
public LuaLReg(CharPtr name, LuaNativeFunction func) { this.name = name; this.func = func; }
public static TString luaS_newliteral(lua_State L, CharPtr s) { return luaS_newlstr(L, s, (uint)strlen(s)); }
public static void LuaLAddString(LuaLBuffer B, CharPtr s) { LuaLAddLString(B, s, (uint)strlen(s)); }
public static CharPtr lua_pushvfstring(lua_State L, CharPtr fmt, object[] argp) { CharPtr ret; lua_lock(L); luaC_checkGC(L); ret = luaO_pushvfstring(L, fmt, argp); lua_unlock(L); return ret; }
public static CharPtr LuaLOptString(LuaState L, int n, CharPtr d) { uint len; return(LuaLOptLString(L, n, d, out len)); }
} //FIXME: ???(int) public static CharPtr lua_pushliteral(lua_State L, CharPtr s) { return(lua_pushlstring(L, "" + s, (uint)strlen(s))); } //FIXME: changed???
public static int luaL_loadfile(lua_State L, CharPtr f) { return(luaL_loadfilex(L, f, null)); }
public static CharPtr luaL_optstring(lua_State L, int n, CharPtr d) { uint len; return(luaL_optlstring(L, n, d, out len)); }
public static void LuaGRunError(LuaState L, CharPtr fmt, params object[] argp) { AddInfo(L, LuaOPushVFString(L, fmt, argp)); LuaGErrorMsg(L); }
public static int LuaOStr2d(CharPtr s, out LuaNumberType result) { CharPtr endptr; result = lua_str2number(s, out endptr); if (endptr == s) return 0; /* conversion failed */ if (endptr[0] == 'x' || endptr[0] == 'X') /* maybe an hexadecimal constant? */ result = CastNum(strtoul(s, out endptr, 16)); if (endptr[0] == '\0') return 1; /* most common case */ while (isspace(endptr[0])) endptr = endptr.next(); if (endptr[0] != '\0') return 0; /* invalid trailing characters? */ return 1; }
private static int luaB_loadfile(lua_State L) { CharPtr fname = luaL_optstring(L, 1, null); return(load_aux(L, luaL_loadfile(L, fname))); }
/* ** {====================================================== ** Time/Date operations ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, ** wday=%w+1, yday=%j, isdst=? } ** ======================================================= */ private static void setfield (lua_State L, CharPtr key, int value) { lua_pushinteger(L, value); lua_setfield(L, -2, key); }
private static int os_date (lua_State L) { CharPtr s = new CharPtr(luaL_optstring(L, 1, "%c")); DateTime stm; if (s[0] == '!') { /* UTC? */ stm = DateTime.UtcNow; s.inc(); /* skip `!' */ } else stm = DateTime.Now; if (strcmp(s, "*t") == 0) { lua_createtable(L, 0, 9); /* 9 = number of fields */ setfield(L, "sec", stm.Second); setfield(L, "min", stm.Minute); setfield(L, "hour", stm.Hour); setfield(L, "day", stm.Day); setfield(L, "month", stm.Month); setfield(L, "year", stm.Year); setfield(L, "wday", (int)stm.DayOfWeek); setfield(L, "yday", stm.DayOfYear); setboolfield(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0); } else { luaL_error(L, "strftime not implemented yet"); // todo: implement this - mjf #if false CharPtr cc = new char[3]; luaL_Buffer b; cc[0] = '%'; cc[2] = '\0'; luaL_buffinit(L, b); for (; s[0] != 0; s.inc()) { if (s[0] != '%' || s[1] == '\0') /* no conversion specifier? */ luaL_addchar(b, s[0]); else { uint reslen; CharPtr buff = new char[200]; /* should be big enough for any conversion result */ s.inc(); cc[1] = s[0]; reslen = strftime(buff, buff.Length, cc, stm); luaL_addlstring(b, buff, reslen); } } luaL_pushresult(b); #endif // #if 0 } return 1; }
public static int LuaLLoadFile(LuaState L, CharPtr filename) { LoadF lf = new LoadF(); int status, readstatus; int c; int fnameindex = LuaGetTop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; if (filename == null) { LuaPushLiteral(L, "=stdin"); lf.f = stdin; } else { LuaPushFString(L, "@%s", filename); lf.f = fopen(filename, "r"); if (lf.f == null) { return(ErrFile(L, "open", fnameindex)); } } c = getc(lf.f); if (c == '#') /* Unix exec. file? */ { lf.extraline = 1; while ((c = getc(lf.f)) != EOF && c != '\n') { ; /* skip first line */ } if (c == '\n') { c = getc(lf.f); } } if (c == LUA_SIGNATURE[0] && (filename != null)) /* binary file? */ { lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == null) { return(ErrFile(L, "reopen", fnameindex)); } /* skip eventual `#!...' */ while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) { ; } lf.extraline = 0; } if (c == UTF8_SIGNATURE[0] && (filename != null)) { int c1 = getc(lf.f); if (c1 != UTF8_SIGNATURE[1]) { ungetc(c1, lf.f); } else { int c2 = getc(lf.f); if (c2 != UTF8_SIGNATURE[2]) { ungetc(c2, lf.f); ungetc(c1, lf.f); } else { c = getc(lf.f); } } } ungetc(c, lf.f); status = LuaLoad(L, GetF, lf, LuaToString(L, -1)); readstatus = ferror(lf.f); if (filename != null) { fclose(lf.f); /* close file (even in case of errors) */ } if (readstatus != 0) { LuaSetTop(L, fnameindex); /* ignore results from `lua_load' */ return(ErrFile(L, "read", fnameindex)); } LuaRemove(L, fnameindex); return(status); }
public static void /*l_noret*/ luaX_syntaxerror(LexState ls, CharPtr msg) { lexerror(ls, msg, ls.t.token); }
private static void PushStr(LuaState L, CharPtr str) { SetSValue2S(L, L.top, luaS_new(L, str)); IncrTop(L); }
public static int LuaLLoadString(LuaState L, CharPtr s) { return(LuaLLoadBuffer(L, s, (uint)strlen(s), s)); }
private static void setboolfield (lua_State L, CharPtr key, int value) { if (value < 0) /* undefined? */ return; /* does not set field */ lua_pushboolean(L, value); lua_setfield(L, -2, key); }
private static int isneg (ref CharPtr s) { if (s[0] == '-') { s.inc(); return 1; } else if (s[0] == '+') s.inc(); return 0; }
private static void pushstr (lua_State L, CharPtr str, uint l) { setsvalue2s(L, L.top, luaS_newlstr(L, str, l)); StkId.inc(ref L.top); }
public static uint luaZ_read(ZIO z, CharPtr b, uint n) { b = new CharPtr(b); while (n != 0) { uint m; if (luaZ_lookahead(z) == EOZ) return n; // return number of missing bytes m = (n <= z.n) ? n : z.n; // min. between n and z.n memcpy(b, z.p, m); z.n -= m; z.p += m; b = b + m; n -= m; } return 0; }
/* ** load precompiled chunk */ public static Proto luaU_undump(lua_State L, ZIO Z, Mbuffer buff, CharPtr name) { LoadState S = new LoadState(); if (name[0] == '@' || name[0] == '=') S.name = name+1; else if (name[0]==LUA_SIGNATURE[0]) S.name="binary string"; else S.name=name; S.L=L; S.Z=Z; S.b=buff; LoadHeader(S); return LoadFunction(S,luaS_newliteral(L,"=?")); }
public static int lua_load(lua_State L, lua_Reader reader, object data, CharPtr chunkname) { ZIO z = new ZIO(); int status; lua_lock(L); if (chunkname == null) chunkname = "?"; luaZ_init(L, z, reader, data); status = luaD_protectedparser(L, z, chunkname); lua_unlock(L); return status; }
private static void LoadBlock(LoadState S, CharPtr b, int size) { uint r=luaZ_read(S.Z, b, (uint)size); IF (r!=0, "unexpected end"); }
public static void lua_pushlstring(lua_State L, CharPtr s, uint len) { lua_lock(L); luaC_checkGC(L); setsvalue2s(L, L.top, luaS_newlstr(L, s, len)); api_incr_top(L); lua_unlock(L); }
/* this function handles only '%d', '%c', '%f', '%p', and '%s' conventional formats, plus Lua-specific '%L' and '%U' */ public static CharPtr luaO_pushvfstring (lua_State L, CharPtr fmt, params object[] argp) { int parm_index = 0; //FIXME: added, for emulating va_arg(argp, xxx) int n = 0; for (;;) { CharPtr e = strchr(fmt, '%'); if (e == null) break; luaD_checkstack(L, 2); /* fmt + item */ pushstr(L, fmt, (uint)(e - fmt)); switch (e[1]) { case 's': { object o = argp[parm_index++]; //FIXME: changed CharPtr s = o as CharPtr; //FIXME: changed if (s == null) //FIXME: changed s = (string)o; //FIXME: changed if (s == null) s = "(null)"; pushstr(L, s, (uint)strlen(s)); //FIXME:changed, (uint) break; } case 'c': { CharPtr buff = new char[1]; //FIXME:???char->CharPtr buff[0] = (char)(int)argp[parm_index++]; pushstr(L, buff, 1); //FIXME:???&buff break; } case 'd': { setivalue(L.top, (int)argp[parm_index++]); StkId.inc(ref L.top); luaV_tostring(L, L.top - 1); break; } case 'I': { setivalue(L.top, (lua_Integer)((l_uacInt)argp[parm_index++])); StkId.inc(ref L.top); luaV_tostring(L, L.top - 1); break; } case 'f': { setfltvalue(L.top, cast_num((l_uacNumber)argp[parm_index++])); StkId.inc(ref L.top); luaV_tostring(L, L.top - 1); break; } case 'p': { CharPtr buff = new char[32]; /* should be enough space for a `%p' */ //FIXME: changed, char buff[4*sizeof(void *) + 8]; uint l = (uint)sprintf(buff, "0x%08x", argp[parm_index++].GetHashCode()); //FIXME: changed, %p->%08x //FIXME:changed, (uint) pushstr(L, buff, l); break; } case 'U': { CharPtr buff = new CharPtr(new char[UTF8BUFFSZ]); int l = luaO_utf8esc(buff, (uint)(int)argp[parm_index++]); pushstr(L, buff + UTF8BUFFSZ - l, (uint)l); break; } case '%': { pushstr(L, "%", 1); break; } default: { luaG_runerror(L, "invalid option " + LUA_QL("%%%c") + " to " + LUA_QL("lua_pushfstring"), (e + 1).ToString()); //FIXME: changed, *(e+1) break; //FIXME:added } } n += 2; fmt = e+2; } luaD_checkstack(L, 1); pushstr(L, fmt, (uint)strlen(fmt)); //FIXME:changed, (uint) if (n > 0) luaV_concat(L, n+1); return svalue(L.top - 1); }
public static int lua_integer2str(CharPtr s, lua_Integer n) { return(sprintf(s, LUA_INTEGER_FMT, n)); }
public static void lua_getfield(lua_State L, int idx, CharPtr k) { StkId t; TValue key = new TValue(); lua_lock(L); t = index2adr(L, idx); api_checkvalidindex(L, t); setsvalue(L, key, luaS_new(L, k)); luaV_gettable(L, t, key, L.top); api_incr_top(L); lua_unlock(L); }
public static void addstr(CharPtr a, CharPtr b, uint l) { memcpy(a,b,l * 1); a += l; } //FIXME: * sizeof(char)
public static CharPtr lua_pushfstring(lua_State L, CharPtr fmt) { CharPtr ret; lua_lock(L); luaC_checkGC(L); ret = luaO_pushvfstring(L, fmt, null); lua_unlock(L); return ret; }
private static void fileerror(lua_State L, int arg, CharPtr filename) { lua_pushfstring(L, "%s: %s", filename, strerror(errno)); luaL_argerror(L, arg, lua_tostring(L, -1)); }
public static void lua_pushstring(lua_State L, CharPtr s) { if (s == null) lua_pushnil(L); else lua_pushlstring(L, s, (uint)strlen(s)); }
public static void luaG_runerror(lua_State L, CharPtr fmt, params object[] argp) { addinfo(L, luaO_pushvfstring(L, fmt, argp)); luaG_errormsg(L); }
public static void lua_setfield(lua_State L, int idx, CharPtr k) { StkId t; TValue key = new TValue(); lua_lock(L); api_checknelems(L, 1); t = index2adr(L, idx); api_checkvalidindex(L, t); setsvalue(L, key, luaS_new(L, k)); luaV_settable(L, t, key, L.top - 1); StkId.dec(ref L.top); /* pop value */ lua_unlock(L); }
public static void lua_register(lua_State L, CharPtr n, lua_CFunction f) { lua_pushcfunction(L, f); lua_setglobal(L, n); }