lua

A copy of the Lua development repository
Log | Files | Refs | README

commit c98f195eb930422be2829f78696fb4bf79b93677
parent 4d696c45b9710e4b3d2eb65a0ebef79766937a4a
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Mon, 26 May 2014 14:09:57 -0300

function 'luaV_numtointeger' changed to a global macro
'lua_numtointeger' (tricky, small, and useful in several places)

Diffstat:
Mltable.c | 21+++++++++++++--------
Mluaconf.h | 20+++++++++++++++++++-
Mlvm.c | 20++------------------
Mlvm.h | 3+--
4 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/ltable.c b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.87 2014/04/15 14:28:20 roberto Exp roberto $ +** $Id: ltable.c,v 2.88 2014/04/15 16:32:49 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -67,12 +67,6 @@ #define hashpointer(t,p) hashmod(t, IntPoint(p)) -/* checks whether a float has a value representable as a lua_Integer - (and does the conversion if so) */ -#define numisinteger(x,i) \ - (((x) == l_floor(x)) && luaV_numtointeger(x, i)) - - #define dummynode (&dummynode_) #define isdummy(n) ((n) == dummynode) @@ -84,6 +78,17 @@ static const Node dummynode_ = { /* +** Checks whether a float has a value representable as a lua_Integer +** (and does the conversion if so) +*/ +static int numisinteger (lua_Number x, lua_Integer *p) { + if ((x) == l_floor(x)) /* integral value? */ + return lua_numtointeger(x, p); /* try as an integer */ + else return 0; +} + + +/* ** hash for floating-point numbers */ static Node *hashfloat (const Table *t, lua_Number n) { @@ -424,7 +429,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { if (luai_numisnan(n)) luaG_runerror(L, "table index is NaN"); if (numisinteger(n, &k)) { /* index is int? */ - setivalue(&aux, k); + setivalue(&aux, k); key = &aux; /* insert it as an integer */ } } diff --git a/luaconf.h b/luaconf.h @@ -1,5 +1,5 @@ /* -** $Id: luaconf.h,v 1.202 2014/05/15 15:24:32 roberto Exp roberto $ +** $Id: luaconf.h,v 1.203 2014/05/21 15:24:21 roberto Exp roberto $ ** Configuration file for Lua ** See Copyright Notice in lua.h */ @@ -506,6 +506,24 @@ /* +@@ lua_numtointeger converts a float number to an integer, or +** return 0 if float is not within the range of a lua_Integer. +** (The comparisons are tricky because of rounding, which can or +** not occur depending on the relative sizes of floats and integers. +** The tests here assume a two-complement representation, where +** MININTEGER always has an exact representation as a float, +** while if LUA_MAXINTEGER has an exact representation, so does +** (LUA_MAXINTEGER + 1); otherwise, LUA_MAXINTEGER is equal to +** (LUA_MAXINTEGER + 1) when converted to a float.) +** This macro should be used only when 'n' has an integral value. +*/ +#define lua_numtointeger(n,p) \ + (((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ + (n) < (LUA_NUMBER)(LUA_MAXINTEGER) + 1) && \ + (*p = (LUA_INTEGER)(n), 1)) + + +/* @@ The luai_num* macros define the primitive operations over numbers. ** They should work for any size of floating numbers. */ diff --git a/lvm.c b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.212 2014/05/20 14:12:59 roberto Exp roberto $ +** $Id: lvm.c,v 2.213 2014/05/23 18:32:21 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -80,22 +80,6 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { /* -** Check whether a float number is within the range of a lua_Integer. -** (The comparisons are tricky because of rounding, which can or -** not occur depending on the relative sizes of floats and integers.) -** This function should be called only when 'n' has an integral value. -*/ -int luaV_numtointeger (lua_Number n, lua_Integer *p) { - if (cast_num(LUA_MININTEGER) <= n && n < (LUA_MAXINTEGER + cast_num(1))) { - *p = cast(lua_Integer, n); - lua_assert(cast_num(*p) == n); - return 1; - } - return 0; /* number is outside integer limits */ -} - - -/* ** try to convert a value to an integer, rounding up if 'up' is true */ static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) { @@ -104,7 +88,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) { if (ttisfloat(obj)) { lua_Number n = fltvalue(obj); n = (up ? -l_floor(-n) : l_floor(n)); - return luaV_numtointeger(n, p); + return lua_numtointeger(n, p); } else if (ttisinteger(obj)) { *p = ivalue(obj); diff --git a/lvm.h b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.29 2014/04/27 14:41:11 roberto Exp roberto $ +** $Id: lvm.h,v 2.30 2014/05/12 21:22:05 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -29,7 +29,6 @@ LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p); -LUAI_FUNC int luaV_numtointeger (lua_Number n, lua_Integer *p); LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val);