** 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 */
| 105 | ** mode == 2: takes the ceil of the number |
| 106 | */ |
| 107 | int luaV_tointeger(const TValue *obj, lua_Integer *p, int mode) { |
| 108 | TValue v; |
| 109 | again: |
| 110 | if (ttisfloat(obj)) { |
| 111 | lua_Number n = fltvalue(obj); |
| 112 | lua_Number f = l_floor(n); |
| 113 | if (n != f) { /* not an integral value? */ |
| 114 | if (mode == 0) return 0; /* fails if mode demands integral value */ |
| 115 | else if (mode > 1) /* needs ceil? */ |
| 116 | f += 1; /* convert floor to ceil (remember: n != f) */ |
| 117 | } |
| 118 | return lua_numbertointeger(f, p); |
| 119 | } |
| 120 | else if (ttisinteger(obj)) { |
| 121 | *p = ivalue(obj); |
| 122 | return 1; |
| 123 | } |
| 124 | else if (cvt2num(obj) && |
| 125 | luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { |
| 126 | obj = &v; |
| 127 | goto again; /* convert result from 'luaO_str2num' to an integer */ |
| 128 | } |
| 129 | return 0; /* conversion failed */ |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /* |
no test coverage detected