Example #1
0
lua_Integer lua_tointeger(lua_State *L, int index)
{
    lua_Number  d = lua_tonumber(L, index);
    lua_Integer i;
    lua_number2integer(i, d);
    return i;
}
 // - LuaValue::asInteger() --------------------------------------------------
 lua_Integer LuaValue::asInteger() const
 {
    if (dataType_ == LUA_TNUMBER)
    {
       lua_Number num = (*reinterpret_cast<const lua_Number*>(&data_));
       lua_Integer res;
       lua_number2integer (res, num);
       return res;
    }
    else
    {
       throw TypeMismatchError ("number", typeName());
    }
 }
Example #3
0
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
  TValue n;
  const TValue *o = index2addr(L, idx);
  if (tonumber(o, &n)) {
    lua_Integer res;
    lua_Number num = nvalue(o);
    lua_number2integer(res, num);
    if (isnum) *isnum = 1;
    return res;
  }
  else {
    if (isnum) *isnum = 0;
    return 0;
  }
}
Example #4
0
/*
* If a LUA_TNUMBER has integer value, give it.
*/
int /*bool*/ tt_integer_valued( const TValue *o, lua_Integer *ref ) {
  lua_Number d;
  lua_Integer i;

  lua_assert( ttype(o)==LUA_TNUMBER );
  lua_assert( ref );
#ifdef LNUM_COMPLEX
  if (nvalue_img_fast(o)!=0) return 0;
#endif
  d= nvalue_fast(o);
  lua_number2integer(i, d);
  if (cast_num(i) == d) {
    *ref= i; return 1;
  }
  return 0;
}
Example #5
0
static int read_number (lua_State *L, FILE *f) {
  lua_Number d;
  if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {
#ifdef LUA_TINT
    lua_Integer tmp;
    lua_number2integer(tmp, d);
    if (cast_num(tmp) == d)
        lua_pushinteger(L, tmp);
    else
#endif
    lua_pushnumber(L, d);
    return 1;
  }
  else {
    lua_pushnil(L);  /* "result" to be removed */
    return 0;  /* read fails */
  }
}