public static TString newlstr (LuaState 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 void DumpBlock(CharPtr b, uint size, DumpState D) { if (D.status == 0) { lua_unlock(D.L); D.status = D.writer(D.L, b, size, D.data); lua_lock(D.L); } }
private static int os_pushresult (LuaState 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; } }
public static CharPtr luaL_optlstring(lua_State L, int narg, CharPtr def) { uint len; return(luaL_optlstring(L, narg, def, out len)); }
public static void luaL_addstring(luaL_Buffer B, CharPtr s) { luaL_addlstring(B, s, (uint)strlen(s)); }
//#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 luaL_getmetatable(LuaState L, CharPtr n) { lua_getfield(L, LUA_REGISTRYINDEX, n); }
public static CharPtr lua_pushfstring(LuaState L, CharPtr fmt, params object[] p) { CharPtr ret; lua_lock(L); luaC_checkGC(L); ret = luaO_pushvfstring(L, fmt, p); lua_unlock(L); return ret; }
public static void luaL_addlstring(luaL_Buffer B, CharPtr s, uint l) { while (l-- != 0) { char c = s[0]; s = s.next(); luaL_addchar(B, c); } }
private static int errfile(LuaState L, CharPtr what, int fnameindex) { CharPtr serr = strerror(errno()); CharPtr filename = lua_tostring(L, fnameindex) + 1; lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); lua_remove(L, fnameindex); return LUA_ERRFILE; }
static bool luaL_dostring(lua_State L, CharPtr s) { return(luaL_loadstring(L, s) == 0 || lua_pcall(L, 0, LUA_MULTRET, 0) == 0); }
public static void luaI_openlib(LuaState L, CharPtr libname, luaL_Reg[] l, int nup) { if (libname != null) { int size = libsize(l); /* check whether lib already exists */ luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); lua_getfield(L, -1, libname); /* get _LOADED[libname] */ if (!lua_istable(L, -1)) { /* not found? */ lua_pop(L, 1); /* remove previous result */ /* try global variable (and create one if it does not exist) */ if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != null) luaL_error(L, "name conflict for module " + LUA_QS, libname); lua_pushvalue(L, -1); lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ } lua_remove(L, -2); /* remove _LOADED table */ lua_insert(L, -(nup + 1)); /* move library table to below upvalues */ } int reg_num = 0; for (; l[reg_num].name != null; reg_num++) { int i; for (i = 0; i < nup; i++) /* copy upvalues to the top */ lua_pushvalue(L, -nup); lua_pushcclosure(L, l[reg_num].func, nup); lua_setfield(L, -(nup + 2), l[reg_num].name); } lua_pop(L, nup); /* remove upvalues */ }
public static void lua_register(LuaState L, CharPtr n, lua_CFunction f) { lua_pushcfunction(L, f); lua_setglobal(L, n); }
public static void lua_getglobal(LuaState L, CharPtr s) { lua_getfield(L, LUA_GLOBALSINDEX, s); }
public static int luaL_loadstring(lua_State L, CharPtr s) { return(luaL_loadbuffer(L, s, (uint)strlen(s), s)); }
public static int luaL_loadfile(lua_State L, CharPtr filename) { LoadF lf = new LoadF(); int status, readstatus; int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; if (filename == null) { lua_pushliteral(L, "=stdin"); lf.f = stdin; } else { lua_pushfstring(L, "@%s", filename); lf.f = fopen(filename, "r"); if (lf.f == null) { return(errfile(L, "open", fnameindex)); } } #if PocketPC fseek(lf.f, 3, 0); #endif 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; } ungetc(c, lf.f); status = lua_load(L, getF, lf, lua_tostring(L, -1)); readstatus = ferror(lf.f); if (filename != null) { fclose(lf.f); /* close file (even in case of errors) */ } if (readstatus != 0) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return(errfile(L, "read", fnameindex)); } lua_remove(L, fnameindex); return(status); }
public static CharPtr luaL_optstring(lua_State L, int n, CharPtr d) { uint len; return(luaL_optlstring(L, n, d, out len)); }
public static CharPtr luaL_optlstring(LuaState L, int narg, CharPtr def, out uint len) { if (lua_isnoneornil(L, narg)) { len = (uint)((def != null) ? strlen(def) : 0); return def; } else return luaL_checklstring(L, narg, out len); }
static int dostring(lua_State L, CharPtr s, CharPtr name) { int status = (luaL_loadbuffer(L, s, (uint)strlen(s), name) != 0) || (docall(L, 0, 1) != 0) ? 1 : 0; return(report(L, status)); }
public static int luaL_callmeta(LuaState L, int obj, CharPtr event_) { obj = abs_index(L, obj); if (luaL_getmetafield(L, obj, event_) == 0) /* no metafield? */ return 0; lua_pushvalue(L, obj); lua_call(L, 1, 1); return 1; }
private static void pushstr(lua_State L, CharPtr str) { setsvalue2s(L, L.top, luaS_new(L, str)); incr_top(L); }
/* }====================================================== */ public static CharPtr luaL_gsub(LuaState L, CharPtr s, CharPtr p, CharPtr r) { CharPtr wild; uint l = (uint)strlen(p); luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); while ((wild = strstr(s, p)) != null) { luaL_addlstring(b, s, (uint)(wild - s)); /* push prefix */ luaL_addstring(b, r); /* push replacement in place of pattern */ s = wild + l; /* continue after `p' */ } luaL_addstring(b, s); /* push last suffix */ luaL_pushresult(b); return lua_tostring(L, -1); }
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ public static CharPtr luaO_pushvfstring(lua_State L, CharPtr fmt, params object[] argp) { int parm_index = 0; int n = 1; pushstr(L, ""); for (;;) { CharPtr e = strchr(fmt, '%'); if (e == null) { break; } setsvalue2s(L, L.top, luaS_newlstr(L, fmt, (uint)(e - fmt))); incr_top(L); switch (e[1]) { case 's': { object o = argp[parm_index++]; CharPtr s = o as CharPtr; if (s == null) { s = (string)o; } if (s == null) { s = "(null)"; } pushstr(L, s); break; } case 'c': { CharPtr buff = new char[2]; buff[0] = (char)(int)argp[parm_index++]; buff[1] = '\0'; pushstr(L, buff); break; } case 'd': { setnvalue(L.top, (int)argp[parm_index++]); incr_top(L); break; } case 'f': { setnvalue(L.top, (l_uacNumber)argp[parm_index++]); incr_top(L); break; } case 'p': { //CharPtr buff = new char[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ CharPtr buff = new char[32]; sprintf(buff, "0x%08x", argp[parm_index++].GetHashCode()); pushstr(L, buff); break; } case '%': { pushstr(L, "%"); break; } default: { CharPtr buff = new char[3]; buff[0] = '%'; buff[1] = e[1]; buff[2] = '\0'; pushstr(L, buff); break; } } n += 2; fmt = e + 2; } pushstr(L, fmt); luaV_concat(L, n + 1, cast_int(L.top - L.base_) - 1); L.top -= n; return(svalue(L.top - 1)); }
public static void luaL_addvalue(luaL_Buffer B) { LuaState L = B.L; uint vl; CharPtr s = lua_tolstring(L, -1, out vl); if (vl <= bufffree(B)) { /* fit into buffer? */ CharPtr dst = new CharPtr(B.buffer.chars, B.buffer.index + B.p); CharPtr src = new CharPtr(s.chars, s.index); for (uint i = 0; i < vl; i++) dst[i] = src[i]; B.p += (int)vl; lua_pop(L, 1); /* remove from stack */ } else { if (emptybuffer(B) != 0) lua_insert(L, -2); /* put buffer before new value */ B.lvl++; /* add new value into B stack */ adjuststack(B); } }
public static CharPtr luaO_pushfstring(lua_State L, CharPtr fmt, params object[] args) { return(luaO_pushvfstring(L, fmt, args)); }
public static int luaL_loadbuffer(LuaState L, CharPtr buff, uint size, CharPtr name) { LoadS ls = new LoadS(); ls.s = new CharPtr(buff); ls.size = size; return lua_load(L, getS, ls, name); }
public TString(CharPtr str) { this.str = str; }
public static void lua_pushstring(LuaState L, CharPtr s) { if (s == null) lua_pushnil(L); else lua_pushlstring(L, s, (uint)strlen(s)); }
public static void luaL_register(lua_State L, CharPtr libname, luaL_Reg[] l) { luaI_openlib(L, libname, l, 0); }
public static void lua_setfield(LuaState 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 luaL_checkstack(LuaState L, int space, CharPtr mes) { if (lua_checkstack(L, space) == 0) luaL_error(L, "stack overflow (%s)", mes); }
public static int MYINT(CharPtr s) { return(s[0] - '0'); }
public luaL_Reg(CharPtr name, lua_CFunction func) { this.name = name; this.func = func; }
public static object luaL_checkudata(LuaState L, int ud, CharPtr tname) { object p = lua_touserdata(L, ud); if (p != null) { /* value is a userdata? */ if (lua_getmetatable(L, ud) != 0) { /* does it have a metatable? */ lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ if (lua_rawequal(L, -1, -2) != 0) { /* does it have the correct mt? */ lua_pop(L, 2); /* remove both metatables */ return p; } } } luaL_typerror(L, ud, tname); /* else error */ return null; /* to avoid warnings */ }
private static int luaB_loadfile(LuaState L) { CharPtr fname = luaL_optstring(L, 1, null); return(load_aux(L, luaL_loadfile(L, fname))); }
public static CharPtr luaL_optlstring(LuaState L, int narg, CharPtr def) { uint len; return luaL_optlstring(L, narg, def, out len); }
private static int db_getinfo(LuaState L) { lua_Debug ar = new lua_Debug(); int arg; LuaState L1 = getthread(L, out arg); CharPtr options = luaL_optstring(L, arg + 2, "flnSu"); if (lua_isnumber(L, arg + 1) != 0) { if (lua_getstack(L1, (int)lua_tointeger(L, arg + 1), ar) == 0) { lua_pushnil(L); /* level out of range */ return(1); } } else if (lua_isfunction(L, arg + 1)) { lua_pushfstring(L, ">%s", options); options = lua_tostring(L, -1); lua_pushvalue(L, arg + 1); lua_xmove(L, L1, 1); } else { return(luaL_argerror(L, arg + 1, "function or level expected")); } if (lua_getinfo(L1, options, ar) == 0) { return(luaL_argerror(L, arg + 2, "invalid option")); } lua_createtable(L, 0, 2); if (strchr(options, 'S') != null) { settabss(L, "source", ar.source); settabss(L, "short_src", ar.short_src); settabsi(L, "linedefined", ar.linedefined); settabsi(L, "lastlinedefined", ar.lastlinedefined); settabss(L, "what", ar.what); } if (strchr(options, 'l') != null) { settabsi(L, "currentline", ar.currentline); } if (strchr(options, 'u') != null) { settabsi(L, "nups", ar.nups); } if (strchr(options, 'n') != null) { settabss(L, "name", ar.name); settabss(L, "namewhat", ar.namewhat); } if (strchr(options, 'L') != null) { treatstackoption(L, L1, "activelines"); } if (strchr(options, 'f') != null) { treatstackoption(L, L1, "func"); } return(1); /* return table */ }
public static int luaL_getmetafield(LuaState L, int obj, CharPtr event_) { if (lua_getmetatable(L, obj) == 0) /* no metatable? */ return 0; lua_pushstring(L, event_); lua_rawget(L, -2); if (lua_isnil(L, -1)) { lua_pop(L, 2); /* remove metatable and metafield */ return 0; } else { lua_remove(L, -2); /* remove only metatable */ return 1; } }
private static void settabss(LuaState L, CharPtr i, CharPtr v) { lua_pushstring(L, v); lua_setfield(L, -2, i); }
public static void luaL_register(LuaState L, CharPtr libname, luaL_Reg[] l) { luaI_openlib(L, libname, l, 0); }
private static void settabsi(LuaState L, CharPtr i, int v) { lua_pushinteger(L, v); lua_setfield(L, -2, i); }
public static void luaM_growvector <T>(LuaState L, ref T[] v, int nelems, ref int size, int limit, CharPtr e) { if (nelems + 1 > size) { v = (T[])luaM_growaux_(L, ref v, ref size, limit, e); } }
public static CharPtr luaL_findtable(LuaState L, int idx, CharPtr fname, int szhint) { CharPtr e; lua_pushvalue(L, idx); do { e = strchr(fname, '.'); if (e == null) e = fname + strlen(fname); lua_pushlstring(L, fname, (uint)(e - fname)); lua_rawget(L, -2); if (lua_isnil(L, -1)) { /* no such field? */ lua_pop(L, 1); /* remove this nil */ lua_createtable(L, 0, (e == '.' ? 1 : szhint)); /* new table for field */ lua_pushlstring(L, fname, (uint)(e - fname)); lua_pushvalue(L, -2); lua_settable(L, -4); /* set new table into field */ } else if (!lua_istable(L, -1)) { /* field has a non-table value? */ lua_pop(L, 2); /* remove table and value */ return fname; /* return problematic part of the name */ } lua_remove(L, -2); /* remove previous table */ fname = e + 1; } while (e == '.'); return null; }
//extern Symbol *lua_table; //extern Word lua_ntable; //extern sbyte **lua_constant; //extern Word lua_nconstant; //extern sbyte **lua_string; //extern Word lua_nstring; //extern Hash **lua_array; //extern Word lua_narray; //extern sbyte *lua_file[]; //extern int lua_nfile; //#define lua_markstring(s) (*((s)-1)) public static char lua_markstring(CharPtr s) { return(s[-1]); }
public static void lua_markstring(CharPtr s, char ch) { s[-1] = ch; }
public static CharPtr luaL_optstring(LuaState L, int n, CharPtr d) { uint len; return luaL_optlstring(L, n, d, out len); }
public static void luaX_syntaxerror(LexState ls, CharPtr msg) { luaX_lexerror(ls, msg, ls.t.token); }
public static int luaL_loadfile(LuaState L, CharPtr filename) { LoadF lf = new LoadF(); int status, readstatus; int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; if (filename == null) { lua_pushliteral(L, "=stdin"); lf.f = stdin; } else { lua_pushfstring(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]) ; // do nothing here lf.extraline = 0; } ungetc(c, lf.f); status = lua_load(L, getF, lf, lua_tostring(L, -1)); readstatus = ferror(lf.f); if (filename != null) fclose(lf.f); /* close file (even in case of errors) */ if (readstatus != 0) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return errfile(L, "read", fnameindex); } lua_remove(L, fnameindex); return status; }
public static int str_format(LuaState L) { int top = LuaGetTop(L); int arg = 1; uint sfl; CharPtr strfrmt = LuaLCheckLString(L, arg, out sfl); CharPtr strfrmt_end = strfrmt + sfl; LuaLBuffer b = new LuaLBuffer(L); LuaLBuffInit(L, b); while (strfrmt < strfrmt_end) { if (strfrmt[0] != L_ESC) { LuaLAddChar(b, strfrmt[0]); strfrmt = strfrmt.next(); } else if (strfrmt[1] == L_ESC) { LuaLAddChar(b, strfrmt[0]); /* %% */ strfrmt = strfrmt + 2; } else { /* format item */ strfrmt = strfrmt.next(); CharPtr form = new char[MAX_FORMAT]; /* to store the format (`%...') */ CharPtr buff = new char[MAX_ITEM]; /* to store the formatted item */ if (++arg > top) { LuaLArgError(L, arg, "no value"); } strfrmt = scanformat(L, strfrmt, form); char ch = strfrmt[0]; strfrmt = strfrmt.next(); switch (ch) { case 'c': { sprintf(buff, form, (int)LuaLCheckNumber(L, arg)); break; } case 'd': case 'i': { addintlen(form); sprintf(buff, form, (LUA_INTFRM_T)LuaLCheckNumber(L, arg)); break; } case 'o': case 'u': case 'x': case 'X': { addintlen(form); sprintf(buff, form, (UNSIGNED_LUA_INTFRM_T)LuaLCheckNumber(L, arg)); break; } case 'e': case 'E': case 'f': case 'g': case 'G': { sprintf(buff, form, (double)LuaLCheckNumber(L, arg)); break; } case 'q': { addquoted(L, b, arg); continue; /* skip the 'addsize' at the end */ } case 's': { uint l; CharPtr s = LuaLCheckLString(L, arg, out l); if ((strchr(form, '.') == null) && l >= 100) { /* no precision and string is too long to be formatted; * keep original string */ LuaPushValue(L, arg); LuaLAddValue(b); continue; /* skip the `addsize' at the end */ } else { sprintf(buff, form, s); break; } } default: { /* also treat cases `pnLlh' */ return(LuaLError(L, "invalid option " + LUA_QL("%" + ch) + " to " + LUA_QL("format"), strfrmt[-1])); } } LuaLAddLString(b, buff, (uint)strlen(buff)); } } LuaLPushResult(b); return(1); }
public static int luaL_loadstring(LuaState L, CharPtr s) { return luaL_loadbuffer(L, s, (uint)strlen(s), s); }
private static CharPtr match(MatchState ms, CharPtr s, CharPtr p) { s = new CharPtr(s); p = new CharPtr(p); if (ms.matchdepth-- == 0) { LuaLError(ms.L, "pattern too complex"); } init: /* using goto's to optimize tail recursion */ switch (p[0]) { case '(': { /* start capture */ if (p[1] == ')') /* position capture? */ { return(start_capture(ms, s, p + 2, CAP_POSITION)); } else { return(start_capture(ms, s, p + 1, CAP_UNFINISHED)); } } case ')': { /* end capture */ return(end_capture(ms, s, p + 1)); } case L_ESC: { switch (p[1]) { case 'b': { /* balanced string? */ s = matchbalance(ms, s, p + 2); if (s == null) { return(null); } p += 4; goto init; /* else return match(ms, s, p+4); */ } case 'f': { /* frontier? */ CharPtr ep; char previous; p += 2; if (p[0] != '[') { LuaLError(ms.L, "missing " + LUA_QL("[") + " after " + LUA_QL("%f") + " in pattern"); } ep = classend(ms, p); /* points to what is next */ previous = (s == ms.src_init) ? '\0' : s[-1]; if ((matchbracketclass((previous), p, ep - 1) != 0) || (matchbracketclass((s[0]), p, ep - 1) == 0)) { return(null); } p = ep; goto init; /* else return match(ms, s, ep); */ } default: { if (isdigit((char)(p[1]))) { /* capture results (%0-%9)? */ s = match_capture(ms, s, (p[1])); if (s == null) { return(null); } p += 2; goto init; /* else return match(ms, s, p+2) */ } //ismeretlen hiba miatt lett ide átmásolva { /* it is a pattern item */ CharPtr ep = classend(ms, p); /* points to what is next */ int m = (s < ms.src_end) && (singlematch((s[0]), p, ep) != 0) ? 1 : 0; switch (ep[0]) { case '?': { /* optional */ CharPtr res; if ((m != 0) && ((res = match(ms, s + 1, ep + 1)) != null)) { return(res); } p = ep + 1; goto init; /* else return match(ms, s, ep+1); */ } case '*': { /* 0 or more repetitions */ return(max_expand(ms, s, p, ep)); } case '+': { /* 1 or more repetitions */ return((m != 0) ? max_expand(ms, s + 1, p, ep) : null); } case '-': { /* 0 or more repetitions (minimum) */ return(min_expand(ms, s, p, ep)); } default: { if (m == 0) { return(null); } s = s.next(); p = ep; goto init; /* else return match(ms, s+1, ep); */ } } } //goto dflt; /* case default */ } } } case '\0': { /* end of pattern */ return(s); /* match succeeded */ } case '$': { if (p[1] == '\0') /* is the `$' the last char in pattern? */ { return((s == ms.src_end) ? s : null); /* check end of string */ } else { goto dflt; } } default: dflt: { /* it is a pattern item */ CharPtr ep = classend(ms, p); /* points to what is next */ int m = (s < ms.src_end) && (singlematch((s[0]), p, ep) != 0) ? 1 : 0; switch (ep[0]) { case '?': { /* optional */ CharPtr res; if ((m != 0) && ((res = match(ms, s + 1, ep + 1)) != null)) { return(res); } p = ep + 1; goto init; /* else return match(ms, s, ep+1); */ } case '*': { /* 0 or more repetitions */ return(max_expand(ms, s, p, ep)); } case '+': { /* 1 or more repetitions */ return((m != 0) ? max_expand(ms, s + 1, p, ep) : null); } case '-': { /* 0 or more repetitions (minimum) */ return(min_expand(ms, s, p, ep)); } default: { if (m == 0) { return(null); } s = s.next(); p = ep; goto init; /* else return match(ms, s+1, ep); */ } } } } }
public static void lua_pushlstring(LuaState 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); }
private static int str_find_aux(LuaState L, int find) { uint l1, l2; CharPtr s = LuaLCheckLString(L, 1, out l1); CharPtr p = PatchPattern(LuaLCheckLString(L, 2, out l2)); ptrdiff_t init = posrelat(LuaLOptInteger(L, 3, 1), l1) - 1; if (init < 0) { init = 0; } else if ((uint)(init) > l1) { init = (ptrdiff_t)l1; } if ((find != 0) && ((LuaToBoolean(L, 4) != 0) || /* explicit request? */ strpbrk(p, SPECIALS) == null)) { /* or no special characters? */ /* do a plain search */ CharPtr s2 = lmemfind(s + init, (uint)(l1 - init), p, (uint)(l2)); if (s2 != null) { LuaPushInteger(L, s2 - s + 1); LuaPushInteger(L, (int)(s2 - s + l2)); return(2); } } else { MatchState ms = new MatchState(); int anchor = 0; if (p[0] == '^') { p = p.next(); anchor = 1; } CharPtr s1 = s + init; ms.L = L; ms.matchdepth = MAXCCALLS; ms.src_init = s; ms.src_end = s + l1; do { CharPtr res; ms.level = 0; // LuaAssert(ms.matchdepth == MAXCCALLS); ms.matchdepth = MAXCCALLS; if ((res = match(ms, s1, p)) != null) { if (find != 0) { LuaPushInteger(L, s1 - s + 1); /* start */ LuaPushInteger(L, res - s); /* end */ return(push_captures(ms, null, null) + 2); } else { return(push_captures(ms, s1, res)); } } } while (((s1 = s1.next()) <= ms.src_end) && (anchor == 0)); } LuaPushNil(L); /* not found */ return(1); }
public static CharPtr lua_pushfstring(LuaState L, CharPtr fmt) { CharPtr ret; lua_lock(L); luaC_checkGC(L); ret = luaO_pushvfstring(L, fmt, null); lua_unlock(L); return ret; }
public static int str_gsub(LuaState L) { uint srcl; CharPtr src = LuaLCheckLString(L, 1, out srcl); CharPtr p = PatchPattern(LuaLCheckStringStr(L, 2)); int tr = LuaType(L, 3); int max_s = LuaLOptInt(L, 4, (int)(srcl + 1)); int anchor = 0; if (p[0] == '^') { p = p.next(); anchor = 1; } int n = 0; MatchState ms = new MatchState(); LuaLBuffer b = new LuaLBuffer(L); LuaLArgCheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || tr == LUA_TFUNCTION || tr == LUA_TTABLE || tr == LUA_TUSERDATA, 3, "string/function/table expected"); LuaLBuffInit(L, b); ms.L = L; ms.matchdepth = MAXCCALLS; ms.src_init = src; ms.src_end = src + srcl; while (n < max_s) { CharPtr e; ms.level = 0; //LuaAssert(ms.matchdepth == MAXCCALLS); ms.matchdepth = MAXCCALLS; e = match(ms, src, p); if (e != null) { n++; add_value(ms, b, src, e); } if ((e != null) && e > src) /* non empty match? */ { src = e; /* skip it */ } else if (src < ms.src_end) { char c = src[0]; src = src.next(); LuaLAddChar(b, c); } else { break; } if (anchor != 0) { break; } } LuaLAddLString(b, src, (uint)(ms.src_end - src)); LuaLPushResult(b); LuaPushInteger(L, n); /* number of substitutions */ return(2); }
public static void lua_getfield(LuaState 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 luaG_runerror(lua_State L, CharPtr fmt, params object[] argp) { addinfo(L, luaO_pushvfstring(L, fmt, argp)); luaG_errormsg(L); }
public static int lua_load(LuaState L, lua_Reader reader, object data, CharPtr chunkname) { ZIO z = new ZIO(); int status; lua_lock(L); if (chunkname == null) chunkname = "?"; #if OVERRIDE_LOAD || true //#if false if (data is LoadS) { LoadS d = data as LoadS; if (d.size > 0 && d.s.chars[0] != LUA_SIGNATURE[0]) // if its not binary { Lexer l = new Lexer(); try { //Console.WriteLine(d.s); TokenReader tr = l.Lex(d.s); Parser p = new Parser(tr); Ast.Chunk c = p.Parse(); Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput(); string s = lco.Format(c); d.s = s; d.size = (lu_mem)s.Length; } catch (LuaSourceException ex) { throw ex; } } else { d.s.index = 0; // Why isn't the size equal to the chars.Length? Debug.WriteLine("Binary data: d.size=" + d.size + " d.s.chars.Length=" + d.s.chars.Length); Debug.WriteLine("Equal: " + (d.size == d.s.chars.Length)); //Debug.Assert(d.size == d.s.chars.Length); d.size = (uint)d.s.chars.Length; } } else if (data is LoadF) { LoadF lf = data as LoadF; if (lf.f.ReadByte() != LUA_SIGNATURE[0]) // if its not binary { lf.f.Position = 0; MemoryStream ms = new MemoryStream(); while (lf.f.Position < lf.f.Length) ms.WriteByte((byte)lf.f.ReadByte()); ms.Position = 0; // not binary file ms.Position = 0; StringBuilder sb = new StringBuilder(); while (ms.Position < ms.Length) sb.Append((char)ms.ReadByte()); try { Lexer l = new Lexer(); TokenReader tr = l.Lex(sb.ToString()); Parser p = new Parser(tr); Ast.Chunk c = p.Parse(); Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput(); string s = lco.Format(c); ms = new MemoryStream(); // TODO: there HAS to be a better way... foreach (char c2 in s) ms.WriteByte((byte)c2); ms.Position = 0; lf.f = ms; } catch (LuaSourceException ex) { lua_pushstring(L, ex.GenerateMessage(chunkname)); return 1; //throw ex; } } else { lf.f.Position = 0; // reset the read character } } #endif luaZ_init(L, z, reader, data); status = luaD_protectedparser(L, z, chunkname); lua_unlock(L); if (data is LoadF) { LoadF f = data as LoadF; if (f.f != null) { f.f.Close(); f.f.Dispose(); } } return status; }
public static void luaI_free_CharPtr(ref CharPtr block) { block = null; //*((int *)block) = -1; /* to catch errors */ //free(block); }