diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-15 12:44:46 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-04-15 12:44:46 -0300 |
| commit | 07f861385eaa0073af98261ae2919ad9bbce813c (patch) | |
| tree | 8781cfcd099606f53b692807e4078ace358b5dc4 /lobject.h | |
| parent | 7a543cfae613d7fa5f0cc82ef462310e5d3a4413 (diff) | |
| download | lua-07f861385eaa0073af98261ae2919ad9bbce813c.tar.gz lua-07f861385eaa0073af98261ae2919ad9bbce813c.tar.bz2 lua-07f861385eaa0073af98261ae2919ad9bbce813c.zip | |
first steps in the support of integers: basic representation + table indexing + basic API ops (tointeger/pushinteger) + equality + a few extra stuff
Diffstat (limited to 'lobject.h')
| -rw-r--r-- | lobject.h | 138 |
1 files changed, 20 insertions, 118 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.h,v 2.71 2012/09/11 18:21:44 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 2.70 2012/05/11 14:10:50 roberto Exp $ |
| 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 | */ |
| @@ -57,6 +57,11 @@ | |||
| 57 | #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ | 57 | #define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */ |
| 58 | 58 | ||
| 59 | 59 | ||
| 60 | /* Variant tags for numbers */ | ||
| 61 | #define LUA_TNUMFLT (LUA_TNUMBER | (0 << 4)) /* float numbers */ | ||
| 62 | #define LUA_TNUMINT (LUA_TNUMBER | (1 << 4)) /* integer numbers */ | ||
| 63 | |||
| 64 | |||
| 60 | /* Bit mark for collectable types */ | 65 | /* Bit mark for collectable types */ |
| 61 | #define BIT_ISCOLLECTABLE (1 << 6) | 66 | #define BIT_ISCOLLECTABLE (1 << 6) |
| 62 | 67 | ||
| @@ -92,8 +97,6 @@ typedef struct GCheader { | |||
| 92 | typedef union Value Value; | 97 | typedef union Value Value; |
| 93 | 98 | ||
| 94 | 99 | ||
| 95 | #define numfield lua_Number n; /* numbers */ | ||
| 96 | |||
| 97 | 100 | ||
| 98 | 101 | ||
| 99 | /* | 102 | /* |
| @@ -111,7 +114,6 @@ typedef struct lua_TValue TValue; | |||
| 111 | 114 | ||
| 112 | 115 | ||
| 113 | #define val_(o) ((o)->value_) | 116 | #define val_(o) ((o)->value_) |
| 114 | #define num_(o) (val_(o).n) | ||
| 115 | 117 | ||
| 116 | 118 | ||
| 117 | /* raw type tag of a TValue */ | 119 | /* raw type tag of a TValue */ |
| @@ -130,7 +132,9 @@ typedef struct lua_TValue TValue; | |||
| 130 | /* Macros to test type */ | 132 | /* Macros to test type */ |
| 131 | #define checktag(o,t) (rttype(o) == (t)) | 133 | #define checktag(o,t) (rttype(o) == (t)) |
| 132 | #define checktype(o,t) (ttnov(o) == (t)) | 134 | #define checktype(o,t) (ttnov(o) == (t)) |
| 133 | #define ttisnumber(o) checktag((o), LUA_TNUMBER) | 135 | #define ttisnumber(o) checktype((o), LUA_TNUMBER) |
| 136 | #define ttisfloat(o) checktag((o), LUA_TNUMFLT) | ||
| 137 | #define ttisinteger(o) checktag((o), LUA_TNUMINT) | ||
| 134 | #define ttisnil(o) checktag((o), LUA_TNIL) | 138 | #define ttisnil(o) checktag((o), LUA_TNIL) |
| 135 | #define ttisboolean(o) checktag((o), LUA_TBOOLEAN) | 139 | #define ttisboolean(o) checktag((o), LUA_TBOOLEAN) |
| 136 | #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) | 140 | #define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA) |
| @@ -147,10 +151,13 @@ typedef struct lua_TValue TValue; | |||
| 147 | #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) | 151 | #define ttisthread(o) checktag((o), ctb(LUA_TTHREAD)) |
| 148 | #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY) | 152 | #define ttisdeadkey(o) checktag((o), LUA_TDEADKEY) |
| 149 | 153 | ||
| 150 | #define ttisequal(o1,o2) (rttype(o1) == rttype(o2)) | ||
| 151 | 154 | ||
| 152 | /* Macros to access values */ | 155 | /* Macros to access values */ |
| 153 | #define nvalue(o) check_exp(ttisnumber(o), num_(o)) | 156 | #define nvalue(o) \ |
| 157 | check_exp(ttisnumber(o), (ttisfloat(o) ? val_(o).n : cast_num(val_(o).i))) | ||
| 158 | |||
| 159 | #define ivalue(o) check_exp(ttisinteger(o), val_(o).i) | ||
| 160 | #define fltvalue(o) check_exp(ttisfloat(o), val_(o).n) | ||
| 154 | #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) | 161 | #define gcvalue(o) check_exp(iscollectable(o), val_(o).gc) |
| 155 | #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) | 162 | #define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p) |
| 156 | #define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts) | 163 | #define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts) |
| @@ -185,7 +192,10 @@ typedef struct lua_TValue TValue; | |||
| 185 | #define settt_(o,t) ((o)->tt_=(t)) | 192 | #define settt_(o,t) ((o)->tt_=(t)) |
| 186 | 193 | ||
| 187 | #define setnvalue(obj,x) \ | 194 | #define setnvalue(obj,x) \ |
| 188 | { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); } | 195 | { TValue *io=(obj); val_(io).n=(x); settt_(io, LUA_TNUMFLT); } |
| 196 | |||
| 197 | #define setivalue(obj,x) \ | ||
| 198 | { TValue *io=(obj); val_(io).i=(x); settt_(io, LUA_TNUMINT); } | ||
| 189 | 199 | ||
| 190 | #define setnilvalue(obj) settt_(obj, LUA_TNIL) | 200 | #define setnilvalue(obj) settt_(obj, LUA_TNIL) |
| 191 | 201 | ||
| @@ -267,115 +277,6 @@ typedef struct lua_TValue TValue; | |||
| 267 | #define luai_checknum(L,o,c) { /* empty */ } | 277 | #define luai_checknum(L,o,c) { /* empty */ } |
| 268 | 278 | ||
| 269 | 279 | ||
| 270 | /* | ||
| 271 | ** {====================================================== | ||
| 272 | ** NaN Trick | ||
| 273 | ** ======================================================= | ||
| 274 | */ | ||
| 275 | #if defined(LUA_NANTRICK) | ||
| 276 | |||
| 277 | /* | ||
| 278 | ** numbers are represented in the 'd_' field. All other values have the | ||
| 279 | ** value (NNMARK | tag) in 'tt__'. A number with such pattern would be | ||
| 280 | ** a "signaled NaN", which is never generated by regular operations by | ||
| 281 | ** the CPU (nor by 'strtod') | ||
| 282 | */ | ||
| 283 | |||
| 284 | /* allows for external implementation for part of the trick */ | ||
| 285 | #if !defined(NNMARK) /* { */ | ||
| 286 | |||
| 287 | |||
| 288 | #if !defined(LUA_IEEEENDIAN) | ||
| 289 | #error option 'LUA_NANTRICK' needs 'LUA_IEEEENDIAN' | ||
| 290 | #endif | ||
| 291 | |||
| 292 | |||
| 293 | #define NNMARK 0x7FF7A500 | ||
| 294 | #define NNMASK 0x7FFFFF00 | ||
| 295 | |||
| 296 | #undef TValuefields | ||
| 297 | #undef NILCONSTANT | ||
| 298 | |||
| 299 | #if (LUA_IEEEENDIAN == 0) /* { */ | ||
| 300 | |||
| 301 | /* little endian */ | ||
| 302 | #define TValuefields \ | ||
| 303 | union { struct { Value v__; int tt__; } i; double d__; } u | ||
| 304 | #define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}} | ||
| 305 | /* field-access macros */ | ||
| 306 | #define v_(o) ((o)->u.i.v__) | ||
| 307 | #define d_(o) ((o)->u.d__) | ||
| 308 | #define tt_(o) ((o)->u.i.tt__) | ||
| 309 | |||
| 310 | #else /* }{ */ | ||
| 311 | |||
| 312 | /* big endian */ | ||
| 313 | #define TValuefields \ | ||
| 314 | union { struct { int tt__; Value v__; } i; double d__; } u | ||
| 315 | #define NILCONSTANT {{tag2tt(LUA_TNIL), {NULL}}} | ||
| 316 | /* field-access macros */ | ||
| 317 | #define v_(o) ((o)->u.i.v__) | ||
| 318 | #define d_(o) ((o)->u.d__) | ||
| 319 | #define tt_(o) ((o)->u.i.tt__) | ||
| 320 | |||
| 321 | #endif /* } */ | ||
| 322 | |||
| 323 | #endif /* } */ | ||
| 324 | |||
| 325 | |||
| 326 | /* correspondence with standard representation */ | ||
| 327 | #undef val_ | ||
| 328 | #define val_(o) v_(o) | ||
| 329 | #undef num_ | ||
| 330 | #define num_(o) d_(o) | ||
| 331 | |||
| 332 | |||
| 333 | #undef numfield | ||
| 334 | #define numfield /* no such field; numbers are the entire struct */ | ||
| 335 | |||
| 336 | /* basic check to distinguish numbers from non-numbers */ | ||
| 337 | #undef ttisnumber | ||
| 338 | #define ttisnumber(o) ((tt_(o) & NNMASK) != NNMARK) | ||
| 339 | |||
| 340 | #define tag2tt(t) (NNMARK | (t)) | ||
| 341 | |||
| 342 | #undef rttype | ||
| 343 | #define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : tt_(o) & 0xff) | ||
| 344 | |||
| 345 | #undef settt_ | ||
| 346 | #define settt_(o,t) (tt_(o) = tag2tt(t)) | ||
| 347 | |||
| 348 | #undef setnvalue | ||
| 349 | #define setnvalue(obj,x) \ | ||
| 350 | { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); } | ||
| 351 | |||
| 352 | #undef setobj | ||
| 353 | #define setobj(L,obj1,obj2) \ | ||
| 354 | { const TValue *o2_=(obj2); TValue *o1_=(obj1); \ | ||
| 355 | o1_->u = o2_->u; \ | ||
| 356 | checkliveness(G(L),o1_); } | ||
| 357 | |||
| 358 | |||
| 359 | /* | ||
| 360 | ** these redefinitions are not mandatory, but these forms are more efficient | ||
| 361 | */ | ||
| 362 | |||
| 363 | #undef checktag | ||
| 364 | #undef checktype | ||
| 365 | #define checktag(o,t) (tt_(o) == tag2tt(t)) | ||
| 366 | #define checktype(o,t) (ctb(tt_(o) | VARBITS) == ctb(tag2tt(t) | VARBITS)) | ||
| 367 | |||
| 368 | #undef ttisequal | ||
| 369 | #define ttisequal(o1,o2) \ | ||
| 370 | (ttisnumber(o1) ? ttisnumber(o2) : (tt_(o1) == tt_(o2))) | ||
| 371 | |||
| 372 | |||
| 373 | #undef luai_checknum | ||
| 374 | #define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; } | ||
| 375 | |||
| 376 | #endif | ||
| 377 | /* }====================================================== */ | ||
| 378 | |||
| 379 | 280 | ||
| 380 | 281 | ||
| 381 | /* | 282 | /* |
| @@ -390,7 +291,8 @@ union Value { | |||
| 390 | void *p; /* light userdata */ | 291 | void *p; /* light userdata */ |
| 391 | int b; /* booleans */ | 292 | int b; /* booleans */ |
| 392 | lua_CFunction f; /* light C functions */ | 293 | lua_CFunction f; /* light C functions */ |
| 393 | numfield /* numbers */ | 294 | lua_Integer i; /* integer numbers */ |
| 295 | lua_Number n; /* float numbers */ | ||
| 394 | }; | 296 | }; |
| 395 | 297 | ||
| 396 | 298 | ||
