public static lua_Integer luaL_opt_integer(lua_State L, luaL_opt_delegate_integer f, int n, lua_Number d) { return((lua_Integer)(lua_isnoneornil(L, n) ? d : f(L, (n)))); }
public static void setnvalue(TValue obj, lua_Number x) { TValue io = obj; io.value_.n = x; settt_(io, LUA_TNUMFLT); }
public static lua_Number luaL_optnumber(lua_State L, int narg, lua_Number def) { return(luaL_opt(L, luaL_checknumber, narg, def)); }
public static lua_Number luaL_opt(LuaState L, luaL_opt_delegate f, int n, lua_Number d) { return lua_isnoneornil(L, (n != 0) ? d : f(L, n)) ? 1 : 0; }
public static int luaK_numberK (FuncState fs, lua_Number r) { TValue o = new TValue(); setnvalue(o, r); return addk(fs, o, o); }
public static bool lua_isnoneornil(lua_State L, lua_Number n) { return lua_type(L, (int)n) <= 0; }
public static lu_byte cast_byte(lua_Number i) { return (lu_byte)i; }
public static lua_Number modf(lua_Number a, out lua_Number b) { b = Math.Floor(a); return(a - Math.Floor(a)); }
public static long lmod(lua_Number a, lua_Number b) { return((long)a % (long)b); }
public static bool lua_isnoneornil(lua_State L, lua_Number n) { return(lua_type(L, (int)n) <= 0); }
public static lua_Number fmod(lua_Number a, lua_Number b) { float quotient = (int)Math.Floor(a / b); return(a - quotient * b); }
public static int cast_int(lua_Number i) { return((int)i); }
internal static int CastInt(lua_Number i) { return (int)i; }
public static void LuaPushNumber (LuaState L, lua_Number n) { LuaLock(L); SetNValue(L.top, n); IncrementTop(L); LuaUnlock(L); }
//public static Node gnode(Table t, int i) {return t.node[i];} public static Node hashpow2(Table t, lua_Number n) { return(gnode(t, (int)lmod(n, sizenode(t)))); }
public static lua_Number luai_numadd(lua_Number a, lua_Number b) { return((a) + (b)); }
internal static void setnvalue(TValue obj, lua_Number x) { obj.value.n = x; obj.tt = LUA_TNUMBER; }
public static lua_Number luai_numsub(lua_Number a, lua_Number b) { return((a) - (b)); }
private static void DumpNumber(lua_Number x, DumpState D) { DumpVar(x, D); }
public static lua_Number luai_nummul(lua_Number a, lua_Number b) { return((a) * (b)); }
/* ** hash for lua_Numbers */ private static Node hashnum (Table t, lua_Number n) { byte[] a = BitConverter.GetBytes(n); for (int i = 1; i < a.Length; i++) a[0] += a[i]; return hashmod(t, (int)a[0]); }
public static lua_Number luai_numdiv(lua_Number a, lua_Number b) { return((a) / (b)); }
internal static int cast_int(lua_Number i) { return (int)i; }
public static lua_Number luai_nummod(lua_Number a, lua_Number b) { return((a) - Math.Floor((a) / (b)) * (b)); }
public extern static void lua_pushnumber(lua_State L, lua_Number n);
public static lua_Number luai_numpow(lua_Number a, lua_Number b) { return(Math.Pow(a, b)); }
public static void luaV_execute(lua_State L, int nexeccalls) { LClosure cl; StkId base_; TValue[] k; /*const*/ InstructionPtr pc; reentry: /* entry point */ lua_assert(isLua(L.ci)); pc = InstructionPtr.Assign(L.savedpc); cl = clvalue(L.ci.func).l; base_ = L.base_; k = cl.p.k; /* main loop of interpreter */ for (;;) { /*const*/ Instruction i = InstructionPtr.inc(ref pc)[0]; StkId ra; if (((L.hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) != 0) && (((--L.hookcount) == 0) || ((L.hookmask & LUA_MASKLINE) != 0))) { traceexec(L, pc); if (L.status == LUA_YIELD) /* did hook yield? */ { L.savedpc = new InstructionPtr(pc.codes, pc.pc - 1); return; } base_ = L.base_; } /* warning!! several calls may realloc the stack and invalidate `ra' */ ra = RA(L, base_, i); lua_assert(base_ == L.base_ && L.base_ == L.ci.base_); lua_assert(base_ <= L.top && ((L.top - L.stack) <= L.stacksize)); lua_assert(L.top == L.ci.top || (luaG_checkopenop(i) != 0)); //Dump(pc.pc, i); switch (GET_OPCODE(i)) { case OpCode.OP_MOVE: { setobjs2s(L, ra, RB(L, base_, i)); continue; } case OpCode.OP_LOADK: { setobj2s(L, ra, KBx(L, i, k)); continue; } case OpCode.OP_LOADBOOL: { setbvalue(ra, GETARG_B(i)); if (GETARG_C(i) != 0) { InstructionPtr.inc(ref pc); /* skip next instruction (if C) */ } continue; } case OpCode.OP_LOADNIL: { TValue rb = RB(L, base_, i); do { setnilvalue(StkId.dec(ref rb)); } while (rb >= ra); continue; } case OpCode.OP_GETUPVAL: { int b = GETARG_B(i); setobj2s(L, ra, cl.upvals[b].v); continue; } case OpCode.OP_GETGLOBAL: { TValue g = new TValue(); TValue rb = KBx(L, i, k); sethvalue(L, g, cl.env); lua_assert(ttisstring(rb)); //Protect( L.savedpc = InstructionPtr.Assign(pc); luaV_gettable(L, g, rb, ra); base_ = L.base_; //); L.savedpc = InstructionPtr.Assign(pc); continue; } case OpCode.OP_GETTABLE: { //Protect( L.savedpc = InstructionPtr.Assign(pc); luaV_gettable(L, RB(L, base_, i), RKC(L, base_, i, k), ra); base_ = L.base_; //); L.savedpc = InstructionPtr.Assign(pc); continue; } case OpCode.OP_SETGLOBAL: { TValue g = new TValue(); sethvalue(L, g, cl.env); lua_assert(ttisstring(KBx(L, i, k))); //Protect( L.savedpc = InstructionPtr.Assign(pc); luaV_settable(L, g, KBx(L, i, k), ra); base_ = L.base_; //); L.savedpc = InstructionPtr.Assign(pc); continue; } case OpCode.OP_SETUPVAL: { UpVal uv = cl.upvals[GETARG_B(i)]; setobj(L, uv.v, ra); luaC_barrier(L, uv, ra); continue; } case OpCode.OP_SETTABLE: { //Protect( L.savedpc = InstructionPtr.Assign(pc); luaV_settable(L, ra, RKB(L, base_, i, k), RKC(L, base_, i, k)); base_ = L.base_; //); L.savedpc = InstructionPtr.Assign(pc); continue; } case OpCode.OP_NEWTABLE: { int b = GETARG_B(i); int c = GETARG_C(i); sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); //Protect( L.savedpc = InstructionPtr.Assign(pc); luaC_checkGC(L); base_ = L.base_; //); L.savedpc = InstructionPtr.Assign(pc); continue; } case OpCode.OP_SELF: { StkId rb = RB(L, base_, i); setobjs2s(L, ra + 1, rb); //Protect( L.savedpc = InstructionPtr.Assign(pc); luaV_gettable(L, rb, RKC(L, base_, i, k), ra); base_ = L.base_; //); L.savedpc = InstructionPtr.Assign(pc); continue; } case OpCode.OP_ADD: { arith_op(L, luai_numadd, TMS.TM_ADD, base_, i, k, ra, pc); continue; } case OpCode.OP_SUB: { arith_op(L, luai_numsub, TMS.TM_SUB, base_, i, k, ra, pc); continue; } case OpCode.OP_MUL: { arith_op(L, luai_nummul, TMS.TM_MUL, base_, i, k, ra, pc); continue; } case OpCode.OP_DIV: { arith_op(L, luai_numdiv, TMS.TM_DIV, base_, i, k, ra, pc); continue; } case OpCode.OP_MOD: { arith_op(L, luai_nummod, TMS.TM_MOD, base_, i, k, ra, pc); continue; } case OpCode.OP_POW: { arith_op(L, luai_numpow, TMS.TM_POW, base_, i, k, ra, pc); continue; } case OpCode.OP_UNM: { TValue rb = RB(L, base_, i); if (ttisnumber(rb)) { lua_Number nb = nvalue(rb); setnvalue(ra, luai_numunm(nb)); } else { //Protect( L.savedpc = InstructionPtr.Assign(pc); Arith(L, ra, rb, rb, TMS.TM_UNM); base_ = L.base_; //); L.savedpc = InstructionPtr.Assign(pc); } continue; } case OpCode.OP_NOT: { int res = l_isfalse(RB(L, base_, i)) == 0 ? 0 : 1; /* next assignment may change this value */ setbvalue(ra, res); continue; } case OpCode.OP_LEN: { TValue rb = RB(L, base_, i); switch (ttype(rb)) { case LUA_TTABLE: { setnvalue(ra, (lua_Number)luaH_getn(hvalue(rb))); break; } case LUA_TSTRING: { setnvalue(ra, (lua_Number)tsvalue(rb).len); break; } default: { /* try metamethod */ //Protect( L.savedpc = InstructionPtr.Assign(pc); if (call_binTM(L, rb, luaO_nilobject, ra, TMS.TM_LEN) == 0) { luaG_typeerror(L, rb, "get length of"); } base_ = L.base_; //) break; } } continue; } case OpCode.OP_CONCAT: { int b = GETARG_B(i); int c = GETARG_C(i); //Protect( L.savedpc = InstructionPtr.Assign(pc); luaV_concat(L, c - b + 1, c); luaC_checkGC(L); base_ = L.base_; //); setobjs2s(L, RA(L, base_, i), base_ + b); continue; } case OpCode.OP_JMP: { dojump(L, pc, GETARG_sBx(i)); continue; } case OpCode.OP_EQ: { TValue rb = RKB(L, base_, i, k); TValue rc = RKC(L, base_, i, k); //Protect( L.savedpc = InstructionPtr.Assign(pc); if (equalobj(L, rb, rc) == GETARG_A(i)) { dojump(L, pc, GETARG_sBx(pc[0])); } base_ = L.base_; //); InstructionPtr.inc(ref pc); continue; } case OpCode.OP_LT: { //Protect( L.savedpc = InstructionPtr.Assign(pc); if (luaV_lessthan(L, RKB(L, base_, i, k), RKC(L, base_, i, k)) == GETARG_A(i)) { dojump(L, pc, GETARG_sBx(pc[0])); } base_ = L.base_; //); InstructionPtr.inc(ref pc); continue; } case OpCode.OP_LE: { //Protect( L.savedpc = InstructionPtr.Assign(pc); if (lessequal(L, RKB(L, base_, i, k), RKC(L, base_, i, k)) == GETARG_A(i)) { dojump(L, pc, GETARG_sBx(pc[0])); } base_ = L.base_; //); InstructionPtr.inc(ref pc); continue; } case OpCode.OP_TEST: { if (l_isfalse(ra) != GETARG_C(i)) { dojump(L, pc, GETARG_sBx(pc[0])); } InstructionPtr.inc(ref pc); continue; } case OpCode.OP_TESTSET: { TValue rb = RB(L, base_, i); if (l_isfalse(rb) != GETARG_C(i)) { setobjs2s(L, ra, rb); dojump(L, pc, GETARG_sBx(pc[0])); } InstructionPtr.inc(ref pc); continue; } case OpCode.OP_CALL: { int b = GETARG_B(i); int nresults = GETARG_C(i) - 1; if (b != 0) { L.top = ra + b; /* else previous instruction set top */ } L.savedpc = InstructionPtr.Assign(pc); switch (luaD_precall(L, ra, nresults)) { case PCRLUA: { nexeccalls++; goto reentry; /* restart luaV_execute over new Lua function */ } case PCRC: { /* it was a C function (`precall' called it); adjust results */ if (nresults >= 0) { L.top = L.ci.top; } base_ = L.base_; continue; } default: { return; /* yield */ } } } case OpCode.OP_TAILCALL: { int b = GETARG_B(i); if (b != 0) { L.top = ra + b; /* else previous instruction set top */ } L.savedpc = InstructionPtr.Assign(pc); lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); switch (luaD_precall(L, ra, LUA_MULTRET)) { case PCRLUA: { /* tail call: put new frame in place of previous one */ CallInfo ci = L.ci - 1; /* previous frame */ int aux; StkId func = ci.func; StkId pfunc = (ci + 1).func; /* previous function index */ if (L.openupval != null) { luaF_close(L, ci.base_); } L.base_ = ci.base_ = ci.func + (ci[1].base_ - pfunc); for (aux = 0; pfunc + aux < L.top; aux++) /* move frame down */ { setobjs2s(L, func + aux, pfunc + aux); } ci.top = L.top = func + aux; /* correct top */ lua_assert(L.top == L.base_ + clvalue(func).l.p.maxstacksize); ci.savedpc = InstructionPtr.Assign(L.savedpc); ci.tailcalls++; /* one more call lost */ CallInfo.dec(ref L.ci); /* remove new frame */ goto reentry; } case PCRC: { /* it was a C function (`precall' called it) */ base_ = L.base_; continue; } default: { return; /* yield */ } } } case OpCode.OP_RETURN: { int b = GETARG_B(i); if (b != 0) { L.top = ra + b - 1; } if (L.openupval != null) { luaF_close(L, base_); } L.savedpc = InstructionPtr.Assign(pc); b = luaD_poscall(L, ra); if (--nexeccalls == 0) /* was previous function running `here'? */ { return; /* no: return */ } else /* yes: continue its execution */ { if (b != 0) { L.top = L.ci.top; } lua_assert(isLua(L.ci)); lua_assert(GET_OPCODE(L.ci.savedpc[-1]) == OpCode.OP_CALL); goto reentry; } } case OpCode.OP_FORLOOP: { lua_Number step = nvalue(ra + 2); lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */ lua_Number limit = nvalue(ra + 1); if (luai_numlt(0, step) ? luai_numle(idx, limit) : luai_numle(limit, idx)) { dojump(L, pc, GETARG_sBx(i)); /* jump back */ setnvalue(ra, idx); /* update internal index... */ setnvalue(ra + 3, idx); /* ...and external index */ } continue; } case OpCode.OP_FORPREP: { TValue init = ra; TValue plimit = ra + 1; TValue pstep = ra + 2; L.savedpc = InstructionPtr.Assign(pc); /* next steps may throw errors */ if (tonumber(ref init, ra) == 0) { luaG_runerror(L, LUA_QL("for") + " initial value must be a number"); } else if (tonumber(ref plimit, ra + 1) == 0) { luaG_runerror(L, LUA_QL("for") + " limit must be a number"); } else if (tonumber(ref pstep, ra + 2) == 0) { luaG_runerror(L, LUA_QL("for") + " step must be a number"); } setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep))); dojump(L, pc, GETARG_sBx(i)); continue; } case OpCode.OP_TFORLOOP: { StkId cb = ra + 3; /* call base */ setobjs2s(L, cb + 2, ra + 2); setobjs2s(L, cb + 1, ra + 1); setobjs2s(L, cb, ra); L.top = cb + 3; /* func. + 2 args (state and index) */ //Protect( L.savedpc = InstructionPtr.Assign(pc); luaD_call(L, cb, GETARG_C(i)); base_ = L.base_; //); L.top = L.ci.top; cb = RA(L, base_, i) + 3; /* previous call may change the stack */ if (!ttisnil(cb)) /* continue loop? */ { setobjs2s(L, cb - 1, cb); /* save control variable */ dojump(L, pc, GETARG_sBx(pc[0])); /* jump back */ } InstructionPtr.inc(ref pc); continue; } case OpCode.OP_SETLIST: { int n = GETARG_B(i); int c = GETARG_C(i); int last; Table h; if (n == 0) { n = cast_int(L.top - ra) - 1; L.top = L.ci.top; } if (c == 0) { c = cast_int(pc[0]); InstructionPtr.inc(ref pc); } runtime_check(L, ttistable(ra)); h = hvalue(ra); last = ((c - 1) * LFIELDS_PER_FLUSH) + n; if (last > h.sizearray) /* needs more space? */ { luaH_resizearray(L, h, last); /* pre-alloc it at once */ } for (; n > 0; n--) { TValue val = ra + n; setobj2t(L, luaH_setnum(L, h, last--), val); luaC_barriert(L, h, val); } continue; } case OpCode.OP_CLOSE: { luaF_close(L, ra); continue; } case OpCode.OP_CLOSURE: { Proto p; Closure ncl; int nup, j; p = cl.p.p[GETARG_Bx(i)]; nup = p.nups; ncl = luaF_newLclosure(L, nup, cl.env); ncl.l.p = p; for (j = 0; j < nup; j++, InstructionPtr.inc(ref pc)) { if (GET_OPCODE(pc[0]) == OpCode.OP_GETUPVAL) { ncl.l.upvals[j] = cl.upvals[GETARG_B(pc[0])]; } else { lua_assert(GET_OPCODE(pc[0]) == OpCode.OP_MOVE); ncl.l.upvals[j] = luaF_findupval(L, base_ + GETARG_B(pc[0])); } } setclvalue(L, ra, ncl); //Protect( L.savedpc = InstructionPtr.Assign(pc); luaC_checkGC(L); base_ = L.base_; //); continue; } case OpCode.OP_VARARG: { int b = GETARG_B(i) - 1; int j; CallInfo ci = L.ci; int n = cast_int(ci.base_ - ci.func) - cl.p.numparams - 1; if (b == LUA_MULTRET) { //Protect( L.savedpc = InstructionPtr.Assign(pc); luaD_checkstack(L, n); base_ = L.base_; //); ra = RA(L, base_, i); /* previous call may change the stack */ b = n; L.top = ra + n; } for (j = 0; j < b; j++) { if (j < n) { setobjs2s(L, ra + j, ci.base_ - n + j); } else { setnilvalue(ra + j); } } continue; } } } }
public static lua_Number luai_numunm(lua_Number a) { return(-(a)); }
public static lua_Number luaL_opt(lua_State L, luaL_opt_delegate f, int n, lua_Number d) { return(lua_isnoneornil(L, (n != 0) ? d : f(L, n)) ? 1 : 0); }
public static bool luai_numeq(lua_Number a, lua_Number b) { return((a) == (b)); }
/* ** convert an hexadecimal numeric string to a number, following ** C99 specification for 'strtod' */ private static lua_Number lua_strx2number(CharPtr s, out CharPtr endptr) { s = new CharPtr(s); //FIXME: added lua_Number r = 0.0; /* result (accumulator) */ int sigdig = 0; /* number of significant digits */ int nosigdig = 0; /* number of non-significant digits */ int e = 0; /* exponent correction */ int neg = 0; /* 1 if number is negative */ int dot = 0; /* true after seen a dot */ endptr = (CharPtr)(s); /* nothing is valid yet */ while (lisspace((byte)(s[0])) != 0) { s.inc(); /* skip initial spaces */ } neg = isneg(ref s); /* check signal */ if (!(s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))) /* check '0x' */ { return(0.0); /* invalid format (no '0x') */ } for (s += 2; ; s.inc()) /* skip '0x' and read numeral */ { if (s[0] == '.') { if (dot != 0) { break; /* second dot? stop loop */ } else { dot = 1; } } else if (lisxdigit((byte)(s[0])) != 0) { if (sigdig == 0 && s[0] == '0') /* non-significant zero? */ { nosigdig++; if (dot != 0) { e--; /* zero after dot? correct exponent */ } } else { if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ { r = (r * cast_num(16.0)) + luaO_hexavalue((byte)(s[0])); if (dot != 0) { e--; /* decimal digit */ } } else /* too many digits; ignore */ if (dot == 0) { e++; /* still count it for exponent */ } } } else { break; /* neither a dot nor a digit */ } } if (nosigdig + sigdig == 0) /* no digits? */ { return(0.0); /* invalid format */ } endptr = (CharPtr)(s); /* valid up to here */ e *= 4; /* each digit multiplies/divides value by 2^4 */ if (s[0] == 'p' || s[0] == 'P') /* exponent part? */ { int exp1 = 0; /* exponent value */ int neg1; /* exponent signal */ s.inc(); /* skip 'p' */ neg1 = isneg(ref s); /* signal */ if (0 == lisdigit((byte)(s[0]))) { return(0.0); /* invalid; must have at least one digit */ } while (lisdigit((byte)(s[0])) != 0) /* read exponent */ { exp1 = exp1 * 10 + s[0] - '0'; s.inc(); } if (neg1 != 0) { exp1 = -exp1; } e += exp1; endptr = (CharPtr)(s); /* valid up to here */ } if (neg != 0) { r = -r; } return(ldexp(r, e)); }
public static bool luai_numle(lua_Number a, lua_Number b) { return((a) <= (b)); }
public static void setnvalue(TValue obj, lua_Number x) { obj.value.n = x; obj.tt = LUA_TNUMBER; }
public static bool luai_numisnan(lua_Number a) { return(lua_Number.IsNaN(a)); }
public static int luaO_str2d (CharPtr s, out lua_Number 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 = cast_num(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; }
/* * @@ lua_number2int is a macro to convert lua_Number to int. * @@ lua_number2integer is a macro to convert lua_Number to lua_Integer. ** CHANGE them if you know a faster way to convert a lua_Number to ** int (with any rounding method and without throwing errors) in your ** system. In Pentium machines, a naive typecast from double to int ** in C is extremely slow, so any alternative is worth trying. */ /* On a Pentium, resort to a trick */ //#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \ // (defined(__i386) || defined (_M_IX86) || defined(__i386__)) /* On a Microsoft compiler, use assembler */ //#if defined(_MSC_VER) //#define lua_number2int(i,d) __asm fld d __asm fistp i //#define lua_number2integer(i,n) lua_number2int(i, n) /* the next trick should work on any Pentium, but sometimes clashes * with a DirectX idiosyncrasy */ //#else //union luai_Cast { double l_d; long l_l; }; //#define lua_number2int(i,d) \ // { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; } //#define lua_number2integer(i,n) lua_number2int(i, n) //#endif /* this option always works, but may be slow */ //#else //#define lua_number2int(i,d) ((i)=(int)(d)) //#define lua_number2integer(i,d) ((i)=(lua_Integer)(d)) //#endif private static void lua_number2int(out int i, lua_Number d) { i = (int)d; }
private static void DumpNumber(lua_Number x, DumpState D) { DumpMem(BitConverter.GetBytes(x), D); }
private static void lua_number2integer(out int i, lua_Number n) { i = (int)n; }
public static int cast_int(lua_Number i) { return (int)i; }
public static extern void lua_pushnumber(lua_StatePtr L, lua_Number n);
//public static Node gnode(Table t, int i) {return t.node[i];} internal static Node hashpow2(Table t, lua_Number n) { return gnode(t, (int)lmod(n, SizeNode(t))); }
private static int str_format(lua_State L) { int top = lua_gettop(L); int arg = 1; uint sfl; CharPtr strfrmt = luaL_checklstring(L, arg, out sfl); CharPtr strfrmt_end = strfrmt + sfl; luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); while (strfrmt < strfrmt_end) { if (strfrmt[0] != L_ESC) { luaL_addchar(b, strfrmt[0]); strfrmt = strfrmt.next(); } else if (strfrmt[1] == L_ESC) { luaL_addchar(b, strfrmt[0]); /* %% */ strfrmt = strfrmt + 2; } else /* format item */ { strfrmt = strfrmt.next(); CharPtr form = new char[MAX_FORMAT]; /* to store the format (`%...') */ CharPtr buff = luaL_prepbuffsize(b, MAX_ITEM); /* to put formatted item */ int nb = 0; /* number of bytes in added item */ if (++arg > top) { luaL_argerror(L, arg, "no value"); } strfrmt = scanformat(L, strfrmt, form); char ch = strfrmt[0]; //FIXME:added, move here strfrmt = strfrmt.next(); //FIXME:added, move here switch (ch) { case 'c': { nb = sprintf(buff, form, luaL_checkint(L, arg)); break; } case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { lua_Number n = luaL_checknumber(L, arg); LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n : (LUA_INTFRM_T)(UInt64)n; //FIXME: changed here addlenmod(form, LUA_INTFRMLEN); nb = sprintf(buff, form, r); break; } case 'e': case 'E': case 'f': case 'g': case 'G': { addlenmod(form, LUA_FLTFRMLEN); nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg)); break; } case 'q': { addquoted(L, b, arg); break; } case 's': { uint l; CharPtr s = luaL_checklstring(L, arg, out l); if ((strchr(form, '.') == null) && l >= 100) { /* no precision and string is too long to be formatted; * keep original string */ lua_pushvalue(L, arg); luaL_addvalue(b); break; } else { nb = sprintf(buff, form, s); break; } } default: { /* also treat cases `pnLlh' */ return(luaL_error(L, "invalid option " + LUA_QL("%%%c") + " to " + LUA_QL("format"), strfrmt[-1])); } } luaL_addsize(b, (uint)nb); //FIXME:changed, (uint) } } luaL_pushresult(b); return(1); }
public static lua_Number luaL_optnumber(LuaState L, int narg, lua_Number def) { return luaL_opt(L, luaL_checknumber, narg, def); }
/* ** inserts a new key into a hash table; first, check whether key's main ** position is free. If not, check whether colliding node is in its main ** position or not: if it is not, move colliding node to an empty place and ** put new key in its main position; otherwise (colliding node is in its main ** position), new key goes to an empty position. */ private static TValue luaH_newkey(lua_State L, Table t, TValue key) { Node mp; TValue aux = new TValue(); if (ttisnil(key)) { luaG_runerror(L, "table index is nil"); } else if (ttisfloat(key)) { lua_Number n = fltvalue(key); lua_Integer k = 0; if (luai_numisnan(n)) { luaG_runerror(L, "table index is NaN"); } if (0 != numisinteger(n, ref k)) /* index is int? */ { setivalue(aux, k); key = aux; /* insert it as an integer */ } } mp = mainposition(t, key); if (!ttisnil(gval(mp)) || isdummy(mp)) /* main position is taken? */ { Node othern; Node f = getfreepos(t); /* get a free place */ if (f == null) /* cannot find a free place? */ { rehash(L, t, key); /* grow table */ /* whatever called 'newkey' take care of TM cache and GC barrier */ return(luaH_set(L, t, key)); /* insert key into grown table */ } lua_assert(!isdummy(f)); othern = mainposition(t, gkey(mp)); if (othern != mp) /* is colliding node out of its main position? */ //Debug.WriteLine("othern != mp, " + gnext(othern)); /* yes; move colliding node into free position */ { while (Node.plus(othern, gnext(othern)) != mp) /* find previous */ { Node.inc(ref othern, gnext(othern)); } gnext_set(othern, cast_int(f - othern)); /* re-chain with 'f' in place of 'mp' */ f.Assign(mp); /* copy colliding node into free pos. (mp->next also goes) */ if (gnext(mp) != 0) { //if (mp - f == 0) //{ // Debug.WriteLine("???"); //} gnext_inc(f, cast_int(mp - f)); /* correct 'next' */ gnext_set(mp, 0); /* now 'mp' is free */ } setnilvalue(gval(mp)); } else /* colliding node is in its own main position */ /* new node will go into free position */ { if (gnext(mp) != 0) { gnext_set(f, cast_int(Node.plus(mp, gnext(mp)) - f)); /* chain new position */ } else { lua_assert(gnext(f) == 0); } gnext_set(mp, cast_int(f - mp)); mp = f; } } setobj2t(L, gkey(mp), key); luaC_barrierback(L, t, key); lua_assert(ttisnil(gval(mp))); return(gval(mp)); }
public static lua_Integer luaL_opt_integer(LuaState L, luaL_opt_delegate_integer f, int n, lua_Number d) { return (lua_Integer)(lua_isnoneornil(L, n) ? d : f(L, (n))); }
internal static int cast_int(lua_Number i) { return((int)i); }
internal static lu_byte cast_byte(lua_Number i) { return (lu_byte)i; }
internal static lu_byte cast_byte(lua_Number i) { return((lu_byte)i); }
public static void lua_pushnumber(LuaState L, lua_Number n) { lua_lock(L); setnvalue(L.top, n); api_incr_top(L); lua_unlock(L); }
public extern static lua_Number luaL_optnumber(lua_State L, int nArg, lua_Number def);