Browse Source

native lua_Number may be neither float nor double (in pack/unpackfloat)

pull/9/head
Roberto Ierusalimschy 11 years ago
parent
commit
2d5e2212d4
  1. 39
      lstrlib.c

39
lstrlib.c

@ -1,5 +1,5 @@
/* /*
** $Id: lstrlib.c,v 1.187 2014/03/12 18:09:06 roberto Exp roberto $ ** $Id: lstrlib.c,v 1.188 2014/03/21 13:52:33 roberto Exp roberto $
** Standard library for string operations and pattern-matching ** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -1088,12 +1088,9 @@ static void correctendianess (lua_State *L, char *b, int size, int endianarg) {
} }
#define DEFAULTFLOATSIZE \
(sizeof(lua_Number) == sizeof(float) ? "f" : "d")
static int getfloatsize (lua_State *L, int arg) { static int getfloatsize (lua_State *L, int arg) {
const char *size = luaL_optstring(L, arg, "n"); const char *size = luaL_optstring(L, arg, "n");
if (*size == 'n') size = DEFAULTFLOATSIZE; if (*size == 'n') return sizeof(lua_Number);
luaL_argcheck(L, *size == 'd' || *size == 'f', arg, luaL_argcheck(L, *size == 'd' || *size == 'f', arg,
"size must be 'f'/'d'/'n'"); "size must be 'f'/'d'/'n'");
return (*size == 'd' ? sizeof(double) : sizeof(float)); return (*size == 'd' ? sizeof(double) : sizeof(float));
@ -1105,13 +1102,16 @@ static int packfloat_l (lua_State *L) {
char *pn; /* pointer to number */ char *pn; /* pointer to number */
lua_Number n = luaL_checknumber(L, 1); lua_Number n = luaL_checknumber(L, 1);
int size = getfloatsize(L, 2); int size = getfloatsize(L, 2);
if (size == sizeof(double)) { if (size == sizeof(lua_Number))
d = (double)n; pn = (char*)&n;
pn = (char*)&d; else if (size == sizeof(float)) {
}
else {
f = (float)n; f = (float)n;
pn = (char*)&f; pn = (char*)&f;
}
else { /* native lua_Number may be neither float nor double */
lua_assert(size == sizeof(double));
d = (double)n;
pn = (char*)&d;
} }
correctendianess(L, pn, size, 3); correctendianess(L, pn, size, 3);
lua_pushlstring(L, pn, size); lua_pushlstring(L, pn, size);
@ -1127,17 +1127,22 @@ static int unpackfloat_l (lua_State *L) {
int size = getfloatsize(L, 3); int size = getfloatsize(L, 3);
luaL_argcheck(L, 1 <= pos && (size_t)pos + size - 1 <= len, 1, luaL_argcheck(L, 1 <= pos && (size_t)pos + size - 1 <= len, 1,
"string too short"); "string too short");
if (size == sizeof(double)) { if (size == sizeof(lua_Number)) {
double d; memcpy(&res, s + pos - 1, size);
memcpy(&d, s + pos - 1, size); correctendianess(L, (char*)&res, size, 4);
correctendianess(L, (char*)&d, size, 4); }
res = (lua_Number)d; else if (size == sizeof(float)) {
}
else {
float f; float f;
memcpy(&f, s + pos - 1, size); memcpy(&f, s + pos - 1, size);
correctendianess(L, (char*)&f, size, 4); correctendianess(L, (char*)&f, size, 4);
res = (lua_Number)f; res = (lua_Number)f;
}
else { /* native lua_Number may be neither float nor double */
double d;
lua_assert(size == sizeof(double));
memcpy(&d, s + pos - 1, size);
correctendianess(L, (char*)&d, size, 4);
res = (lua_Number)d;
} }
lua_pushnumber(L, res); lua_pushnumber(L, res);
return 1; return 1;

Loading…
Cancel
Save