lua-users home
lua-l archive

Re: [ANN] Lua 5.3.0 (beta) now available

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> In lvm.h:
> 
> #define tonumber(o,n) \
> (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
> 
> Yet `luaV_tonumber_` already handles this case.
> We could probably get rid of the #define altogether and just use
> `luaV_tonumber_`.
> 
> I'm also wondering why we gained the "_" suffix.
The define saves a function call in what we expect to be a common case
(using a float as a float). The suffix is a convention in Lua that a
function should not be called directly, only thourgh its corresponding
macro. The fact that luaV_tonumber_ handles floats is an implementation
detail, a consequence of its way of converting "integer strings" to
floats. It could as well be implemented like this:
int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
 TValue v;
 if (ttisinteger(obj)) {
 *n = cast_num(ivalue(obj));
 return 1;
 }
 else if (cvt2num(obj) && /* string convertible to number? */
 luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
 if (ttisinteger(&v))
 *n = cast_num(ivalue(&v));
 else
 *n = fltvalue(&v);
 return 1;
 }
 return 0; /* conversion failed */
}
-- Roberto

AltStyle によって変換されたページ (->オリジナル) /