MCPcopy Create free account
hub / github.com/DFHack/dfhack / luaV_tointeger

Function luaV_tointeger

depends/lua/src/lvm.c:94–117  ·  view source on GitHub ↗

** try to convert a value to an integer, rounding according to 'mode': ** mode == 0: accepts only integral values ** mode == 1: takes the floor of the number ** mode == 2: takes the ceil of the number */

Source from the content-addressed store, hash-verified

92** mode == 2: takes the ceil of the number
93*/
94int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) {
95 TValue v;
96 again:
97 if (ttisfloat(obj)) {
98 lua_Number n = fltvalue(obj);
99 lua_Number f = l_floor(n);
100 if (n != f) { /* not an integral value? */
101 if (mode == 0) return 0; /* fails if mode demands integral value */
102 else if (mode > 1) /* needs ceil? */
103 f += 1; /* convert floor to ceil (remember: n != f) */
104 }
105 return lua_numbertointeger(f, p);
106 }
107 else if (ttisinteger(obj)) {
108 *p = ivalue(obj);
109 return 1;
110 }
111 else if (cvt2num(obj) &&
112 luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) {
113 obj = &v;
114 goto again; /* convert result from 'luaO_str2num' to an integer */
115 }
116 return 0; /* conversion failed */
117}
118
119
120/*

Callers 3

forlimitFunction · 0.85
luaH_newkeyFunction · 0.85
luaH_getFunction · 0.85

Calls 1

luaO_str2numFunction · 0.85

Tested by

no test coverage detected