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 | |
| 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
| -rw-r--r-- | lobject.h | 138 | ||||
| -rw-r--r-- | ltable.c | 63 | ||||
| -rw-r--r-- | lvm.c | 32 | ||||
| -rw-r--r-- | lvm.h | 12 |
4 files changed, 87 insertions, 158 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 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 2.71 2012/05/23 15:37:09 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.71 2012/05/23 15:37:09 roberto Exp $ |
| 3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -52,6 +52,7 @@ | |||
| 52 | 52 | ||
| 53 | #define hashstr(t,str) hashpow2(t, (str)->tsv.hash) | 53 | #define hashstr(t,str) hashpow2(t, (str)->tsv.hash) |
| 54 | #define hashboolean(t,p) hashpow2(t, p) | 54 | #define hashboolean(t,p) hashpow2(t, p) |
| 55 | #define hashint(t,i) hashpow2(t, i) | ||
| 55 | 56 | ||
| 56 | 57 | ||
| 57 | /* | 58 | /* |
| @@ -96,8 +97,12 @@ static Node *hashnum (const Table *t, lua_Number n) { | |||
| 96 | */ | 97 | */ |
| 97 | static Node *mainposition (const Table *t, const TValue *key) { | 98 | static Node *mainposition (const Table *t, const TValue *key) { |
| 98 | switch (ttype(key)) { | 99 | switch (ttype(key)) { |
| 99 | case LUA_TNUMBER: | 100 | case LUA_TNUMINT: |
| 100 | return hashnum(t, nvalue(key)); | 101 | return hashint(t, ivalue(key)); |
| 102 | case LUA_TNUMFLT: | ||
| 103 | return hashnum(t, fltvalue(key)); | ||
| 104 | case LUA_TSHRSTR: | ||
| 105 | return hashstr(t, rawtsvalue(key)); | ||
| 101 | case LUA_TLNGSTR: { | 106 | case LUA_TLNGSTR: { |
| 102 | TString *s = rawtsvalue(key); | 107 | TString *s = rawtsvalue(key); |
| 103 | if (s->tsv.extra == 0) { /* no hash? */ | 108 | if (s->tsv.extra == 0) { /* no hash? */ |
| @@ -106,8 +111,6 @@ static Node *mainposition (const Table *t, const TValue *key) { | |||
| 106 | } | 111 | } |
| 107 | return hashstr(t, rawtsvalue(key)); | 112 | return hashstr(t, rawtsvalue(key)); |
| 108 | } | 113 | } |
| 109 | case LUA_TSHRSTR: | ||
| 110 | return hashstr(t, rawtsvalue(key)); | ||
| 111 | case LUA_TBOOLEAN: | 114 | case LUA_TBOOLEAN: |
| 112 | return hashboolean(t, bvalue(key)); | 115 | return hashboolean(t, bvalue(key)); |
| 113 | case LUA_TLIGHTUSERDATA: | 116 | case LUA_TLIGHTUSERDATA: |
| @@ -120,19 +123,24 @@ static Node *mainposition (const Table *t, const TValue *key) { | |||
| 120 | } | 123 | } |
| 121 | 124 | ||
| 122 | 125 | ||
| 126 | static int numisint (lua_Number n, int *p) { | ||
| 127 | int k; | ||
| 128 | lua_number2int(k, n); | ||
| 129 | if (luai_numeq(cast_num(k), n)) { /* 'k' is int? */ | ||
| 130 | *p = k; | ||
| 131 | return 1; | ||
| 132 | } | ||
| 133 | return 0; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 123 | /* | 137 | /* |
| 124 | ** returns the index for `key' if `key' is an appropriate key to live in | 138 | ** returns the index for `key' if `key' is an appropriate key to live in |
| 125 | ** the array part of the table, -1 otherwise. | 139 | ** the array part of the table, -1 otherwise. |
| 126 | */ | 140 | */ |
| 127 | static int arrayindex (const TValue *key) { | 141 | static int arrayindex (const TValue *key) { |
| 128 | if (ttisnumber(key)) { | 142 | if (ttisinteger(key)) return ivalue(key); |
| 129 | lua_Number n = nvalue(key); | 143 | else return -1; /* `key' did not match some condition */ |
| 130 | int k; | ||
| 131 | lua_number2int(k, n); | ||
| 132 | if (luai_numeq(cast_num(k), n)) | ||
| 133 | return k; | ||
| 134 | } | ||
| 135 | return -1; /* `key' did not match some condition */ | ||
| 136 | } | 144 | } |
| 137 | 145 | ||
| 138 | 146 | ||
| @@ -170,7 +178,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) { | |||
| 170 | int i = findindex(L, t, key); /* find original element */ | 178 | int i = findindex(L, t, key); /* find original element */ |
| 171 | for (i++; i < t->sizearray; i++) { /* try first array part */ | 179 | for (i++; i < t->sizearray; i++) { /* try first array part */ |
| 172 | if (!ttisnil(&t->array[i])) { /* a non-nil value? */ | 180 | if (!ttisnil(&t->array[i])) { /* a non-nil value? */ |
| 173 | setnvalue(key, cast_num(i+1)); | 181 | setivalue(key, i + 1); |
| 174 | setobj2s(L, key+1, &t->array[i]); | 182 | setobj2s(L, key+1, &t->array[i]); |
| 175 | return 1; | 183 | return 1; |
| 176 | } | 184 | } |
| @@ -404,9 +412,18 @@ static Node *getfreepos (Table *t) { | |||
| 404 | */ | 412 | */ |
| 405 | TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { | 413 | TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { |
| 406 | Node *mp; | 414 | Node *mp; |
| 415 | TValue aux; | ||
| 407 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); | 416 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); |
| 408 | else if (ttisnumber(key) && luai_numisnan(L, nvalue(key))) | 417 | else if (ttisfloat(key)) { |
| 409 | luaG_runerror(L, "table index is NaN"); | 418 | lua_Number n = fltvalue(key); |
| 419 | int k; | ||
| 420 | if (luai_numisnan(L, n)) | ||
| 421 | luaG_runerror(L, "table index is NaN"); | ||
| 422 | if (numisint(n, &k)) { /* index is int? */ | ||
| 423 | setivalue(&aux, k); | ||
| 424 | key = &aux; /* insert it as an integer */ | ||
| 425 | } | ||
| 426 | } | ||
| 410 | mp = mainposition(t, key); | 427 | mp = mainposition(t, key); |
| 411 | if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ | 428 | if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ |
| 412 | Node *othern; | 429 | Node *othern; |
| @@ -448,10 +465,9 @@ const TValue *luaH_getint (Table *t, int key) { | |||
| 448 | if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) | 465 | if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) |
| 449 | return &t->array[key-1]; | 466 | return &t->array[key-1]; |
| 450 | else { | 467 | else { |
| 451 | lua_Number nk = cast_num(key); | 468 | Node *n = hashint(t, key); |
| 452 | Node *n = hashnum(t, nk); | ||
| 453 | do { /* check whether `key' is somewhere in the chain */ | 469 | do { /* check whether `key' is somewhere in the chain */ |
| 454 | if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) | 470 | if (ttisinteger(gkey(n)) && ivalue(gkey(n)) == key) |
| 455 | return gval(n); /* that's it */ | 471 | return gval(n); /* that's it */ |
| 456 | else n = gnext(n); | 472 | else n = gnext(n); |
| 457 | } while (n); | 473 | } while (n); |
| @@ -481,12 +497,11 @@ const TValue *luaH_getstr (Table *t, TString *key) { | |||
| 481 | const TValue *luaH_get (Table *t, const TValue *key) { | 497 | const TValue *luaH_get (Table *t, const TValue *key) { |
| 482 | switch (ttype(key)) { | 498 | switch (ttype(key)) { |
| 483 | case LUA_TSHRSTR: return luaH_getstr(t, rawtsvalue(key)); | 499 | case LUA_TSHRSTR: return luaH_getstr(t, rawtsvalue(key)); |
| 500 | case LUA_TNUMINT: return luaH_getint(t, ivalue(key)); | ||
| 484 | case LUA_TNIL: return luaO_nilobject; | 501 | case LUA_TNIL: return luaO_nilobject; |
| 485 | case LUA_TNUMBER: { | 502 | case LUA_TNUMFLT: { |
| 486 | int k; | 503 | int k; |
| 487 | lua_Number n = nvalue(key); | 504 | if (numisint(fltvalue(key), &k)) /* index is int? */ |
| 488 | lua_number2int(k, n); | ||
| 489 | if (luai_numeq(cast_num(k), n)) /* index is int? */ | ||
| 490 | return luaH_getint(t, k); /* use specialized version */ | 505 | return luaH_getint(t, k); /* use specialized version */ |
| 491 | /* else go through */ | 506 | /* else go through */ |
| 492 | } | 507 | } |
| @@ -522,7 +537,7 @@ void luaH_setint (lua_State *L, Table *t, int key, TValue *value) { | |||
| 522 | cell = cast(TValue *, p); | 537 | cell = cast(TValue *, p); |
| 523 | else { | 538 | else { |
| 524 | TValue k; | 539 | TValue k; |
| 525 | setnvalue(&k, cast_num(key)); | 540 | setivalue(&k, key); |
| 526 | cell = luaH_newkey(L, t, &k); | 541 | cell = luaH_newkey(L, t, &k); |
| 527 | } | 542 | } |
| 528 | setobj2t(L, cell, value); | 543 | setobj2t(L, cell, value); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 2.155 2013/03/16 21:10:18 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.154 2012/08/16 17:34:28 roberto Exp $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -34,7 +34,11 @@ | |||
| 34 | 34 | ||
| 35 | const TValue *luaV_tonumber (const TValue *obj, TValue *n) { | 35 | const TValue *luaV_tonumber (const TValue *obj, TValue *n) { |
| 36 | lua_Number num; | 36 | lua_Number num; |
| 37 | if (ttisnumber(obj)) return obj; | 37 | if (ttisfloat(obj)) return obj; |
| 38 | if (ttisinteger(obj)) { | ||
| 39 | setnvalue(n, cast_num(ivalue(obj))); | ||
| 40 | return n; | ||
| 41 | } | ||
| 38 | if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) { | 42 | if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) { |
| 39 | setnvalue(n, num); | 43 | setnvalue(n, num); |
| 40 | return n; | 44 | return n; |
| @@ -257,12 +261,19 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { | |||
| 257 | /* | 261 | /* |
| 258 | ** equality of Lua values. L == NULL means raw equality (no metamethods) | 262 | ** equality of Lua values. L == NULL means raw equality (no metamethods) |
| 259 | */ | 263 | */ |
| 260 | int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) { | 264 | int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { |
| 261 | const TValue *tm; | 265 | const TValue *tm; |
| 262 | lua_assert(ttisequal(t1, t2)); | 266 | if (ttype(t1) != ttype(t2)) { |
| 267 | if (ttnov(t1) != ttnov(t2) || ttnov(t1) != LUA_TNUMBER) | ||
| 268 | return 0; /* only numbers can be equal with different variants */ | ||
| 269 | else /* two numbers with different variants */ | ||
| 270 | return luai_numeq(nvalue(t1), nvalue(t2)); | ||
| 271 | } | ||
| 272 | /* values have same type and same variant */ | ||
| 263 | switch (ttype(t1)) { | 273 | switch (ttype(t1)) { |
| 264 | case LUA_TNIL: return 1; | 274 | case LUA_TNIL: return 1; |
| 265 | case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); | 275 | case LUA_TNUMINT: return (ivalue(t1) == ivalue(t2)); |
| 276 | case LUA_TNUMFLT: return luai_numeq(fltvalue(t1), fltvalue(t2)); | ||
| 266 | case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ | 277 | case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ |
| 267 | case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); | 278 | case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); |
| 268 | case LUA_TLCF: return fvalue(t1) == fvalue(t2); | 279 | case LUA_TLCF: return fvalue(t1) == fvalue(t2); |
| @@ -281,7 +292,6 @@ int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) { | |||
| 281 | break; /* will try TM */ | 292 | break; /* will try TM */ |
| 282 | } | 293 | } |
| 283 | default: | 294 | default: |
| 284 | lua_assert(iscollectable(t1)); | ||
| 285 | return gcvalue(t1) == gcvalue(t2); | 295 | return gcvalue(t1) == gcvalue(t2); |
| 286 | } | 296 | } |
| 287 | if (tm == NULL) return 0; /* no TM? */ | 297 | if (tm == NULL) return 0; /* no TM? */ |
| @@ -615,7 +625,13 @@ void luaV_execute (lua_State *L) { | |||
| 615 | Protect(luaV_gettable(L, rb, RKC(i), ra)); | 625 | Protect(luaV_gettable(L, rb, RKC(i), ra)); |
| 616 | ) | 626 | ) |
| 617 | vmcase(OP_ADD, | 627 | vmcase(OP_ADD, |
| 618 | arith_op(luai_numadd, TM_ADD); | 628 | TValue *rb1 = RKB(i); |
| 629 | TValue *rc1 = RKC(i); | ||
| 630 | if (ttisinteger(rb1) && ttisinteger(rc1)) { | ||
| 631 | lua_Integer ib = ivalue(rb1); lua_Integer ic = ivalue(rc1); | ||
| 632 | setivalue(ra, ib + ic); | ||
| 633 | } | ||
| 634 | else arith_op(luai_numadd, TM_ADD); | ||
| 619 | ) | 635 | ) |
| 620 | vmcase(OP_SUB, | 636 | vmcase(OP_SUB, |
| 621 | arith_op(luai_numsub, TM_SUB); | 637 | arith_op(luai_numsub, TM_SUB); |
| @@ -669,7 +685,7 @@ void luaV_execute (lua_State *L) { | |||
| 669 | TValue *rb = RKB(i); | 685 | TValue *rb = RKB(i); |
| 670 | TValue *rc = RKC(i); | 686 | TValue *rc = RKC(i); |
| 671 | Protect( | 687 | Protect( |
| 672 | if (cast_int(equalobj(L, rb, rc)) != GETARG_A(i)) | 688 | if (cast_int(luaV_equalobj(L, rb, rc)) != GETARG_A(i)) |
| 673 | ci->u.l.savedpc++; | 689 | ci->u.l.savedpc++; |
| 674 | else | 690 | else |
| 675 | donextjump(ci); | 691 | donextjump(ci); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.h,v 2.17 2011/05/31 18:27:56 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 2.17 2011/05/31 18:27:56 roberto Exp $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -15,17 +15,13 @@ | |||
| 15 | 15 | ||
| 16 | #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) | 16 | #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) |
| 17 | 17 | ||
| 18 | #define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) | 18 | #define tonumber(o,n) (ttisfloat(o) || (((o) = luaV_tonumber(o,n)) != NULL)) |
| 19 | 19 | ||
| 20 | #define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) | ||
| 21 | 20 | ||
| 22 | #define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2) | 21 | #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) |
| 23 | |||
| 24 | |||
| 25 | /* not to called directly */ | ||
| 26 | LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); | ||
| 27 | 22 | ||
| 28 | 23 | ||
| 24 | LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); | ||
| 29 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); | 25 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); |
| 30 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); | 26 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); |
| 31 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); | 27 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); |
