diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-06-13 11:13:06 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-06-13 11:13:06 -0300 |
| commit | fd80e63468f0c08fedd8dbf944fa4954b72d7384 (patch) | |
| tree | e074a257d1eb265eaaafeb930e94282a72d27f88 | |
| parent | f62565abea4e56f6bd064df83e5b0f3818b99d82 (diff) | |
| download | lua-fd80e63468f0c08fedd8dbf944fa4954b72d7384.tar.gz lua-fd80e63468f0c08fedd8dbf944fa4954b72d7384.tar.bz2 lua-fd80e63468f0c08fedd8dbf944fa4954b72d7384.zip | |
configuration for NaN trick big-endian + macro 'luai_checknum' to
ensure numbers comming from C are not "improper" (some kinds of
signaling NaNs)
| -rw-r--r-- | lapi.c | 4 | ||||
| -rw-r--r-- | lobject.h | 21 | ||||
| -rw-r--r-- | luaconf.h | 32 |
3 files changed, 50 insertions, 7 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 2.147 2011/05/31 18:27:56 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.148 2011/06/02 19:31:40 roberto Exp roberto $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -463,6 +463,8 @@ LUA_API void lua_pushnil (lua_State *L) { | |||
| 463 | LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { | 463 | LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { |
| 464 | lua_lock(L); | 464 | lua_lock(L); |
| 465 | setnvalue(L->top, n); | 465 | setnvalue(L->top, n); |
| 466 | luai_checknum(L, L->top, | ||
| 467 | luaG_runerror(L, "C API - attempt to push a signaling NaN")); | ||
| 466 | api_incr_top(L); | 468 | api_incr_top(L); |
| 467 | lua_unlock(L); | 469 | lua_unlock(L); |
| 468 | } | 470 | } |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.h,v 2.58 2011/06/07 19:02:33 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 2.59 2011/06/09 18:21:25 roberto Exp roberto $ |
| 3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -262,7 +262,7 @@ typedef struct lua_TValue TValue; | |||
| 262 | ** ======================================================= | 262 | ** ======================================================= |
| 263 | */ | 263 | */ |
| 264 | 264 | ||
| 265 | #if defined(LUA_NANTRICK) | 265 | #if defined(LUA_NANTRICKLE) || defined(LUA_NANTRICKBE) |
| 266 | 266 | ||
| 267 | /* | 267 | /* |
| 268 | ** numbers are represented in the 'd_' field. All other values have the | 268 | ** numbers are represented in the 'd_' field. All other values have the |
| @@ -270,11 +270,20 @@ typedef struct lua_TValue TValue; | |||
| 270 | ** a "signaled NaN", which is never generated by regular operations by | 270 | ** a "signaled NaN", which is never generated by regular operations by |
| 271 | ** the CPU (nor by 'strtod') | 271 | ** the CPU (nor by 'strtod') |
| 272 | */ | 272 | */ |
| 273 | #if !defined(NNMARK) | ||
| 273 | #define NNMARK 0x7FF7A500 | 274 | #define NNMARK 0x7FF7A500 |
| 275 | #endif | ||
| 274 | 276 | ||
| 275 | #undef TValuefields | 277 | #undef TValuefields |
| 278 | #if defined(LUA_NANTRICKLE) | ||
| 279 | /* little endian */ | ||
| 276 | #define TValuefields \ | 280 | #define TValuefields \ |
| 277 | union { struct { Value v_; int tt_; } i; double d_; } u | 281 | union { struct { Value v_; int tt_; } i; double d_; } u |
| 282 | #else | ||
| 283 | /* big endian */ | ||
| 284 | #define TValuefields \ | ||
| 285 | union { struct { int tt_; Value v_; } i; double d_; } u | ||
| 286 | #endif | ||
| 278 | 287 | ||
| 279 | #undef numfield | 288 | #undef numfield |
| 280 | #define numfield /* no such field; numbers are the entire struct */ | 289 | #define numfield /* no such field; numbers are the entire struct */ |
| @@ -322,6 +331,14 @@ typedef struct lua_TValue TValue; | |||
| 322 | (ttisnumber(o1) ? ttisnumber(o2) : ((o1)->u.i.tt_ == (o2)->u.i.tt_)) | 331 | (ttisnumber(o1) ? ttisnumber(o2) : ((o1)->u.i.tt_ == (o2)->u.i.tt_)) |
| 323 | 332 | ||
| 324 | 333 | ||
| 334 | |||
| 335 | #define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; } | ||
| 336 | |||
| 337 | |||
| 338 | #else | ||
| 339 | |||
| 340 | #define luai_checknum(L,o,c) { /* empty */ } | ||
| 341 | |||
| 325 | #endif | 342 | #endif |
| 326 | /* }====================================================== */ | 343 | /* }====================================================== */ |
| 327 | 344 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: luaconf.h,v 1.157 2011/04/29 13:56:28 roberto Exp roberto $ | 2 | ** $Id: luaconf.h,v 1.158 2011/05/26 16:09:40 roberto Exp roberto $ |
| 3 | ** Configuration file for Lua | 3 | ** Configuration file for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -465,11 +465,11 @@ | |||
| 465 | 465 | ||
| 466 | /* | 466 | /* |
| 467 | @@ LUA_IEEEENDIAN is the endianness of doubles in your machine | 467 | @@ LUA_IEEEENDIAN is the endianness of doubles in your machine |
| 468 | @@ (0 for little endian, 1 for big endian); if not defined, Lua will | 468 | ** (0 for little endian, 1 for big endian); if not defined, Lua will |
| 469 | @@ check it dynamically. | 469 | ** check it dynamically. |
| 470 | */ | 470 | */ |
| 471 | /* check for known architectures */ | 471 | /* check for known architectures */ |
| 472 | #if defined(__i386__) || defined(__i386) || defined(i386) || \ | 472 | #if defined(__i386__) || defined(__i386) || defined(__X86__) || \ |
| 473 | defined (__x86_64) | 473 | defined (__x86_64) |
| 474 | #define LUA_IEEEENDIAN 0 | 474 | #define LUA_IEEEENDIAN 0 |
| 475 | #elif defined(__POWERPC__) || defined(__ppc__) | 475 | #elif defined(__POWERPC__) || defined(__ppc__) |
| @@ -485,6 +485,30 @@ | |||
| 485 | /* }================================================================== */ | 485 | /* }================================================================== */ |
| 486 | 486 | ||
| 487 | 487 | ||
| 488 | /* | ||
| 489 | @@ LUA_NANTRICKLE/LUA_NANTRICKBE controls the use of a trick to pack all | ||
| 490 | ** types into a single double value, using NaN values to represent | ||
| 491 | ** non-number values. The trick only works on 32-bit machines (ints and | ||
| 492 | ** pointers are 32-bit values) with numbers represented as IEEE 754-2008 | ||
| 493 | ** doubles with conventional endianess (12345678 or 87654321), in CPUs | ||
| 494 | ** that do not produce signaling NaN values (all NaNs are quiet). | ||
| 495 | */ | ||
| 496 | #if defined(LUA_CORE) /* { */ | ||
| 497 | |||
| 498 | #if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */ | ||
| 499 | |||
| 500 | /* little-endian architectures that satisfy those conditions */ | ||
| 501 | #if defined(__i386__) || defined(__i386) || defined(__X86__) | ||
| 502 | |||
| 503 | #define LUA_NANTRICKLE | ||
| 504 | |||
| 505 | #endif | ||
| 506 | |||
| 507 | #endif /* } */ | ||
| 508 | |||
| 509 | #endif /* } */ | ||
| 510 | |||
| 511 | |||
| 488 | 512 | ||
| 489 | 513 | ||
| 490 | /* =================================================================== */ | 514 | /* =================================================================== */ |
