private static CharPtr l_str2int (CharPtr s, ref lua_Integer result) { s = new CharPtr(s); //FIXME:added lua_Unsigned a = 0; int empty = 1; int neg; while (lisspace((byte)(s[0]))!=0) s.inc(); /* skip initial spaces */ neg = isneg(ref s); if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) { /* hexa? */ s += 2; /* skip '0x' */ for (; lisxdigit((byte)(s[0]))!=0; s.inc()) { a = (uint)(a * 16 + luaO_hexavalue(cast_uchar(s[0]))); empty = 0; } } else { /* decimal */ for (; lisdigit(cast_uchar(s[0]))!=0; s.inc()) { a = (uint)(a * 10 + luaO_hexavalue(cast_uchar(s[0]))); empty = 0; } } while (lisspace((byte)(s[0]))!=0) s.inc(); /* skip trailing spaces */ if (empty!=0 || s[0] != '\0') return null; /* something wrong in the numeral */ else { result = l_castU2S((neg!=0) ? 0u - a : a); return s; } }
//#endif private static int tinsert(lua_State L) { lua_Integer e = aux_getn(L, 1) + 1; /* first empty element */ lua_Integer pos; /* where to insert new element */ switch (lua_gettop(L)) { case 2: { /* called with only 2 arguments */ pos = e; /* insert new element at the end */ break; } case 3: { lua_Integer i; pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); for (i = e; i > pos; i--) /* move up elements */ { lua_rawgeti(L, 1, i - 1); lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ } break; } default: { return(luaL_error(L, "wrong number of arguments to " + LUA_QL("insert"))); } } lua_rawseti(L, 1, pos); /* t[pos] = v */ return(0); }
public static void LuaPushInteger(LuaState L, lua_Integer n) { LuaLock(L); SetNValue(L.top, CastNum(n)); IncrementTop(L); LuaUnlock(L); }
/* ** return false if folding can raise an error */ private static int validop(int op, TValue v1, TValue v2) { lua_Number a = 0, b = 0; lua_Integer i = 0; cast_void(a); cast_void(b); /* macro may not use its arguments */ cast_void(tonumber(ref v1, ref a)); cast_void(tonumber(ref v2, ref b)); if (0 != luai_numinvalidop(op, a, b)) { return(0); } switch (op) { case LUA_OPIDIV: /* division by 0 and conversion errors */ return((0 != tointeger(ref v1, ref i) && 0 != tointeger(ref v2, ref i) && i != 0)?1:0); case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: /* conversion errors */ return((0 != tointeger(ref v1, ref i) && 0 != tointeger(ref v2, ref i))?1:0); case LUA_OPMOD: /* integer module by 0 */ return((ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0)?0:1); case LUA_OPPOW: /* negative integer exponentiation */ return((ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) < 0)?0:1); default: return(1); /* everything else is valid */ } }
public static void luaO_arith(lua_State L, int op, TValue p1, TValue p2, TValue res) { if (op == LUA_OPIDIV) /* operates only on integers */ { lua_Integer i1 = 0; lua_Integer i2 = 0; if (tointeger(ref p1, ref i1) != 0 && tointeger(ref p2, ref i2) != 0) { setivalue(res, luaV_div(L, i1, i2)); return; } /* else go to the end */ } else /* other operations */ { lua_Number n1 = 0; lua_Number n2 = 0; if (ttisinteger(p1) && ttisinteger(p2) && op != LUA_OPDIV && (op != LUA_OPPOW || ivalue(p2) >= 0)) { setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); return; } else if (tonumber(ref p1, ref n1) != 0 && tonumber(ref p2, ref n2) != 0) { setnvalue(res, numarith(op, n1, n2)); return; } /* else go to the end */ } /* could not perform raw operation; try metmethod */ lua_assert(L != null); /* cannot fail when folding (compile time) */ luaT_trybinTM(L, p1, p2, res, (TMS)(op - LUA_OPADD + TMS.TM_ADD)); }
/* ** utf8len(s, [i]) --> number of codepoints in 's' after 'i'; ** nil if 's' not well formed */ private static int utflen(lua_State L) { int n = 0; CharPtr ends; uint len; CharPtr s = luaL_checklstring(L, 1, out len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), 1); luaL_argcheck(L, 1 <= posi && posi <= (lua_Integer)len, 1, "initial position out of string"); ends = s + len; s += posi - 1; int null_ = 0; while (s < ends && (s = utf8_decode(s, ref null_)) != null) { n++; } if (s == ends) { lua_pushinteger(L, n); } else { lua_pushnil(L); } return(1); }
private static int str_sub(lua_State L) { uint l; //FIXME:???l ? 1 ? CharPtr s = luaL_checklstring(L, 1, out l); lua_Integer start = posrelat(luaL_checkinteger(L, 2), l); lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l); if (start < 1) { start = 1; } if (end > (lua_Integer)l) { end = (lua_Integer)l; } if (start <= end) { lua_pushlstring(L, s + start - 1, (uint)(end - start + 1)); } else { lua_pushliteral(L, ""); } return(1); }
/* ** return false if folding can raise an error */ private static int validop(int op, TValue v1, TValue v2) { lua_Integer i = 0; if (0 != luai_numinvalidop(op, nvalue(v1), nvalue(v2))) { return(0); } switch (op) { case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: /* conversion errors */ return((0 != tointeger(ref v1, ref i) && 0 != tointeger(ref v2, ref i))?1:0); case LUA_OPIDIV: case LUA_OPMOD: /* integer division by 0 */ return((ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0)?0:1); default: return(1); /* everything else is valid */ } }
private static int str_byte(lua_State L) { uint l; CharPtr s = luaL_checklstring(L, 1, out l); lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l); lua_Integer pose = posrelat(luaL_optinteger(L, 3, (int)posi), l); int n, i; if (posi < 1) { posi = 1; } if (pose > (lua_Integer)l) { pose = (lua_Integer)l; } if (posi > pose) { return(0); /* empty interval; return no values */ } n = (int)(pose - posi + 1); if (posi + n <= pose) /* arithmetic overflow? */ { return(luaL_error(L, "string slice too long")); } luaL_checkstack(L, n, "string slice too long"); for (i = 0; i < n; i++) { lua_pushinteger(L, (byte)(s[posi + i - 1])); } return(n); }
public const uint MAXSIZE = (uint)(int.MaxValue / 2); //FIXME: //((size_t)(INT_MAX / 2)); //#else //#define MAXSIZE ((size_t)0x10000000) //#endif private static int str_rep(lua_State L) { uint l, lsep; CharPtr s = luaL_checklstring(L, 1, out l); lua_Integer n = luaL_checkinteger(L, 2); CharPtr sep = luaL_optlstring(L, 3, "", out lsep); if (n <= 0) { lua_pushliteral(L, ""); } else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */ { return(luaL_error(L, "resulting string too large")); } else { uint totallen = (uint)(n * l + (n - 1) * lsep); //FIXME:changed, (uint) luaL_Buffer b = new luaL_Buffer(); CharPtr p = luaL_buffinitsize(L, b, totallen); while (n-- > 1) /* first n-1 copies (followed by separator) */ { memcpy(p, s, l * 1); p += l; //FIXME:changed, sizeof(char) if (lsep > 0) /* avoid empty 'memcpy' (may be expensive) */ { memcpy(p, sep, lsep * 1); p += lsep; //FIXME:changed, sizeof(char) } } memcpy(p, s, l * 1 /*sizeof(char)*/); /* last copy (not followed by separator) */ luaL_pushresultsize(b, totallen); } return(1); }
private readonly static CharPtr[] f_setvbuf_modenames = {"no", "full", "line", null}; //FIXME: ???static const??? private static int f_setvbuf (lua_State L) { StreamProxy f = tofile(L); int op = luaL_checkoption(L, 2, null, f_setvbuf_modenames); lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); int res = setvbuf(f, null, f_setvbuf_mode[op], (uint)sz); return luaL_fileresult(L, (res == 0) ? 1 : 0, null); }
private static int gmatch_aux(LuaState L) { MatchState ms = new MatchState(); uint ls; CharPtr s = LuaToLString(L, LuaUpValueIndex(1), out ls); CharPtr p = LuaToString(L, LuaUpValueIndex(2)); CharPtr src; ms.L = L; ms.matchdepth = MAXCCALLS; ms.src_init = s; ms.src_end = s + ls; for (src = s + (uint)LuaToInteger(L, LuaUpValueIndex(3)); src <= ms.src_end; src = src.next()) { CharPtr e; ms.level = 0; LuaAssert(ms.matchdepth == MAXCCALLS); if ((e = match(ms, src, p)) != null) { lua_Integer newstart = e - s; if (e == src) { newstart++; /* empty match? go at least one position */ } LuaPushInteger(L, newstart); LuaReplace(L, LuaUpValueIndex(3)); return(push_captures(ms, src, e)); } } return(0); /* not found */ }
public static void luaO_arith(lua_State L, int op, TValue p1, TValue p2, TValue res) { switch (op) { case LUA_OPIDIV: case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* operates only on integers */ lua_Integer i1 = 0; lua_Integer i2 = 0; if (0 != tointeger(ref p1, ref i1) && 0 != tointeger(ref p2, ref i2)) { setivalue(res, intarith(L, op, i1, i2)); return; } else { break; /* go to the end */ } } case LUA_OPDIV: case LUA_OPPOW: { /* operates only on floats */ lua_Number n1 = 0; lua_Number n2 = 0; if (0 != tonumber(ref p1, ref n1) && 0 != tonumber(ref p2, ref n2)) { setfltvalue(res, numarith(L, op, n1, n2)); return; } else { break; /* go to the end */ } } default: { /* other operations */ lua_Number n1 = 0; lua_Number n2 = 0; if (ttisinteger(p1) && ttisinteger(p2)) { setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); return; } else if (0 != tonumber(ref p1, ref n1) && 0 != tonumber(ref p2, ref n2)) { setfltvalue(res, numarith(L, op, n1, n2)); return; } else { break; /* go to the end */ } } } /* could not perform raw operation; try metamethod */ lua_assert(L != null); /* should not fail when folding (compile time) */ luaT_trybinTM(L, p1, p2, res, (TMS)(op - LUA_OPADD + TMS.TM_ADD)); }
private static int iter_aux(lua_State L) { uint len; CharPtr s = luaL_checklstring(L, 1, out len); lua_Integer n = lua_tointeger(L, 2) - 1; if (n < 0) /* first iteration? */ { n = 0; /* start from here */ } else if (n < (lua_Integer)len) { n++; /* skip current byte */ while (iscont(s + n)) { n++; /* and its continuations */ } } if (n >= (lua_Integer)len) { return(0); /* no more codepoints */ } else { int code = 0; CharPtr next = utf8_decode(s + n, ref code); if (next == null || iscont(next)) { return(luaL_error(L, "invalid UTF-8 code")); } lua_pushinteger(L, n + 1); lua_pushinteger(L, code); return(2); } }
private static lua_Integer intarith(lua_State L, int op, lua_Integer v1, lua_Integer v2) { switch (op) { case LUA_OPADD: return(intop_plus(v1, v2)); case LUA_OPSUB: return(intop_minus(v1, v2)); case LUA_OPMUL: return(intop_mul(v1, v2)); case LUA_OPMOD: return(luaV_mod(L, v1, v2)); case LUA_OPIDIV: return(luaV_div(L, v1, v2)); case LUA_OPBAND: return(intop_and(v1, v2)); case LUA_OPBOR: return(intop_or(v1, v2)); case LUA_OPBXOR: return(intop_xor(v1, v2)); case LUA_OPSHL: return(luaV_shiftl(v1, v2)); case LUA_OPSHR: return(luaV_shiftl(v1, -v2)); case LUA_OPUNM: return(intop_minus(0, v1)); case LUA_OPBNOT: return(intop_xor((int)(~l_castS2U(0)), v1)); default: lua_assert(0); return(0); } }
/* ** Traversal function for 'ipairs' for tables with metamethods */ private static int ipairsaux(lua_State L) { lua_Integer i = luaL_checkinteger(L, 2) + 1; lua_pushinteger(L, i); return((lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2); }
private static CharPtr b_str2int(CharPtr s, int base_, ref lua_Integer pn) { s = new CharPtr(s); //FIXME:??? lua_Unsigned n = 0; int neg = 0; s += strspn(s, SPACECHARS); /* skip initial spaces */ if (s[0] == '-') { s.inc(); neg = 1; } /* handle signal */ else if (s[0] == '+') { s.inc(); } if (!isalnum((byte)s[0])) /* no digit? */ { return(null); } do { int digit = (isdigit((byte)s[0])) ? s[0] - '0' : (toupper((byte)s[0]) - 'A') + 10; if (digit >= base_) { return(null); /* invalid numeral */ } n = (lua_Unsigned)(n * base_ + digit); s.inc(); } while (isalnum((byte)s[0])); s += strspn(s, SPACECHARS); /* skip trailing spaces */ pn = (lua_Integer)((neg != 0) ? (0u - n) : n); return(s); }
/* ** utf8len(s [, i [, j]]) --> number of characters that start in the ** range [i,j], or nil + current position if 's' is not well formed in ** that interval */ private static int utflen(lua_State L) { int n = 0; uint len; CharPtr s = luaL_checklstring(L, 1, out len); lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len); lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2, "initial position out of string"); luaL_argcheck(L, --posj < (lua_Integer)len, 3, "final position out of string"); while (posi <= posj) { int null_ = 0; CharPtr s1 = utf8_decode(s + posi, ref null_); if (s1 == null) /* conversion error? */ { lua_pushnil(L); /* return nil ... */ lua_pushinteger(L, posi + 1); /* ... and current position */ return(2); } posi = s1 - s; n++; } lua_pushinteger(L, n); return(1); }
public static void lua_pushinteger(lua_State L, lua_Integer n) { lua_lock(L); setnvalue(L.top, cast_num(n)); api_incr_top(L); lua_unlock(L); }
private static int gmatch_aux(lua_State L) { MatchState ms = new MatchState(); uint ls; CharPtr s = lua_tolstring(L, lua_upvalueindex(1), out ls); CharPtr p = lua_tostring(L, lua_upvalueindex(2)); CharPtr src; ms.L = L; ms.src_init = s; ms.src_end = s + ls; for (src = s + (uint)lua_tointeger(L, lua_upvalueindex(3)); src <= ms.src_end; src = src.next()) { CharPtr e; ms.level = 0; if ((e = match(ms, src, p)) != null) { lua_Integer newstart = e - s; if (e == src) { newstart++; /* empty match? go at least one position */ } lua_pushinteger(L, newstart); lua_replace(L, lua_upvalueindex(3)); return(push_captures(ms, src, e)); } } return(0); /* not found */ }
private static void pushutfchar(lua_State L, int arg) { lua_Integer code = luaL_checkinteger(L, arg); luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range"); lua_pushfstring(L, "%U", (long)code); }
//#endif private static int getfield(lua_State L, CharPtr key, int d, int delta) { int isnum = 0; int t = lua_getfield(L, -1, key); /* get field and its type */ lua_Integer res = lua_tointegerx(L, -1, ref isnum); if (0 == isnum) /* field is not an integer? */ { if (t != LUA_TNIL) /* some other value? */ { return(luaL_error(L, "field '%s' is not an integer", key)); } else if (d < 0) /* absent field; no default? */ { return(luaL_error(L, "field '%s' missing in date table", key)); } res = d; } else { if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD)) { return(luaL_error(L, "field '%s' is out-of-bound", key)); } res -= delta; } lua_pop(L, 1); return((int)res); }
/* ** Integers use userdata as keys to avoid collision with floats with same ** value; conversion to 'void*' used only for hashing, no "precision" ** problems */ public static int luaK_intK(FuncState fs, lua_Integer n) { TValue k = new TValue(); TValue o = new TValue(); setpvalue(k, (object)((uint)n)); setivalue(o, n); return(addk(fs, k, o)); }
/* ** Traversal function for 'ipairs' for raw tables */ private static int ipairsaux_raw(lua_State L) { lua_Integer i = luaL_checkinteger(L, 2) + 1; luaL_checktype(L, 1, LUA_TTABLE); lua_pushinteger(L, i); return((lua_rawgeti(L, 1, i) == LUA_TNIL) ? 1 : 2); }
private static int math_ult(lua_State L) { lua_Integer a = luaL_checkinteger(L, 1); lua_Integer b = luaL_checkinteger(L, 2); lua_pushboolean(L, ((lua_Unsigned)a < (lua_Unsigned)b)?1:0); return(1); }
/* ** offset(s, n, [i]) -> index where n-th character counting from ** position 'i' starts; 0 means character at 'i'. */ private static int byteoffset(lua_State L) { uint len; CharPtr s = luaL_checklstring(L, 1, out len); lua_Integer n = luaL_checkinteger(L, 2); lua_Integer posi = (int)((n >= 0) ? 1 : len + 1); posi = u_posrelat(luaL_optinteger(L, 3, posi), len); luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3, "position out of range"); if (n == 0) { /* find beginning of current byte sequence */ while (posi > 0 && iscont(s + posi)) { posi--; } } else { if (iscont(s + posi)) { luaL_error(L, "initial position is a continuation byte"); } if (n < 0) { while (n < 0 && posi > 0) /* move back */ { do /* find beginning of previous character */ { posi--; } while (posi > 0 && iscont(s + posi)); n++; } } else { n--; /* do not move for 1st character */ while (n > 0 && posi < (lua_Integer)len) { do /* find beginning of next character */ { posi++; } while (iscont(s + posi)); /* (cannot pass final '\0') */ n--; } } } if (n == 0) /* did it find given character? */ { lua_pushinteger(L, posi + 1); } else /* no such character */ { lua_pushnil(L); } return(1); }
/* * @@ lua_numbertointeger converts a float number to an integer, or ** returns 0 if float is not within the range of a lua_Integer. ** (The range comparisons are tricky because of rounding. The tests ** here assume a two-complement representation, where MININTEGER always ** has an exact representation as a float; MAXINTEGER may not have one, ** and therefore its conversion to float may have an ill-defined value.) */ public static int lua_numbertointeger(double n, ref lua_Integer p) { if ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && (n) < -(LUA_NUMBER)(LUA_MININTEGER)) { p = (LUA_INTEGER)(n); return(1); } return(0); }
public static lua_Integer luaL_checkinteger(lua_State L, int narg) { lua_Integer d = lua_tointeger(L, narg); if (d == 0 && lua_isnumber(L, narg) == 0) /* avoid extra test when d is not 0 */ { tag_error(L, narg, LUA_TNUMBER); } return(d); }
private static void addfield(lua_State L, luaL_Buffer b, lua_Integer i) { lua_rawgeti(L, 1, i); if (lua_isstring(L, -1) == 0) { luaL_error(L, "invalid value (%s) at index %d in table for " + LUA_QL("concat"), luaL_typename(L, -1), i); } luaL_addvalue(b); }
/* ** Error when both values are convertible to numbers, but not to integers */ public static void /*l_noret*/ luaG_tointerror(lua_State L, /*const*/ TValue p1, /*const*/ TValue p2) { lua_Integer temp = 0; if (0 == tointeger(ref p1, ref temp)) { p2 = p1; } luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); }
public static void LuaPushInteger (LuaState L, lua_Integer n) { LuaLock(L); SetNValue(L.top, CastNum(n)); IncrementTop(L); LuaUnlock(L); }
public extern static void lua_pushinteger(lua_State L, lua_Integer n);
public static lua_Integer luaL_optinteger(LuaState L, int narg, lua_Integer def) { return luaL_opt_integer(L, luaL_checkinteger, narg, def); }
public static int luaL_optint(LuaState L, int n, lua_Integer d) { return (int)luaL_optinteger(L, n, d); }
public static long luaL_optlong(LuaState L, int n, lua_Integer d) { return luaL_optinteger(L, n, d); }
public static void lua_pushinteger(LuaState L, lua_Integer n) { lua_lock(L); setnvalue(L.top, cast_num(n)); api_incr_top(L); lua_unlock(L); }
public extern static lua_Integer luaL_optinteger(lua_State L, int nArg, lua_Integer def);