** 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 */
| 92 | ** mode == 2: takes the ceil of the number |
| 93 | */ |
| 94 | int 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 | /* |
no test coverage detected