diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-06-29 12:06:44 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-06-29 12:06:44 -0300 |
| commit | f96497397addca22f22a6ba6eeabc906be43f16b (patch) | |
| tree | af8d27b9af36dfe0b0b6e0f765ea90b95b110efc /lobject.c | |
| parent | 5a1c8d8ef343bf0157851a4832c2c937b812b64f (diff) | |
| download | lua-f96497397addca22f22a6ba6eeabc906be43f16b.tar.gz lua-f96497397addca22f22a6ba6eeabc906be43f16b.tar.bz2 lua-f96497397addca22f22a6ba6eeabc906be43f16b.zip | |
new type 'StackValue' for stack elements
(we may want to put extra info there in the future)
Diffstat (limited to 'lobject.c')
| -rw-r--r-- | lobject.c | 45 |
1 files changed, 26 insertions, 19 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 2.114 2017/04/19 16:34:35 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.115 2017/05/24 13:47:11 roberto Exp roberto $ |
| 3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -120,8 +120,8 @@ static lua_Number numarith (lua_State *L, int op, lua_Number v1, | |||
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | 122 | ||
| 123 | void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, | 123 | int luaO_rawarith (lua_State *L, int op, const TValue *p1, const TValue *p2, |
| 124 | TValue *res) { | 124 | TValue *res) { |
| 125 | switch (op) { | 125 | switch (op) { |
| 126 | case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: | 126 | case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: |
| 127 | case LUA_OPSHL: case LUA_OPSHR: | 127 | case LUA_OPSHL: case LUA_OPSHR: |
| @@ -129,33 +129,40 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, | |||
| 129 | lua_Integer i1; lua_Integer i2; | 129 | lua_Integer i1; lua_Integer i2; |
| 130 | if (tointeger(p1, &i1) && tointeger(p2, &i2)) { | 130 | if (tointeger(p1, &i1) && tointeger(p2, &i2)) { |
| 131 | setivalue(res, intarith(L, op, i1, i2)); | 131 | setivalue(res, intarith(L, op, i1, i2)); |
| 132 | return; | 132 | return 1; |
| 133 | } | 133 | } |
| 134 | else break; /* go to the end */ | 134 | else return 0; /* fail */ |
| 135 | } | 135 | } |
| 136 | case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ | 136 | case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ |
| 137 | lua_Number n1; lua_Number n2; | 137 | lua_Number n1; lua_Number n2; |
| 138 | if (tonumber(p1, &n1) && tonumber(p2, &n2)) { | 138 | if (tonumber(p1, &n1) && tonumber(p2, &n2)) { |
| 139 | setfltvalue(res, numarith(L, op, n1, n2)); | 139 | setfltvalue(res, numarith(L, op, n1, n2)); |
| 140 | return; | 140 | return 1; |
| 141 | } | 141 | } |
| 142 | else break; /* go to the end */ | 142 | else return 0; /* fail */ |
| 143 | } | 143 | } |
| 144 | default: { /* other operations */ | 144 | default: { /* other operations */ |
| 145 | lua_Number n1; lua_Number n2; | 145 | lua_Number n1; lua_Number n2; |
| 146 | if (ttisinteger(p1) && ttisinteger(p2)) { | 146 | if (ttisinteger(p1) && ttisinteger(p2)) { |
| 147 | setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); | 147 | setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); |
| 148 | return; | 148 | return 1; |
| 149 | } | 149 | } |
| 150 | else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { | 150 | else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { |
| 151 | setfltvalue(res, numarith(L, op, n1, n2)); | 151 | setfltvalue(res, numarith(L, op, n1, n2)); |
| 152 | return; | 152 | return 1; |
| 153 | } | 153 | } |
| 154 | else break; /* go to the end */ | 154 | else return 0; /* fail */ |
| 155 | } | 155 | } |
| 156 | } | 156 | } |
| 157 | /* could not perform raw operation; try metamethod */ | 157 | } |
| 158 | luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); | 158 | |
| 159 | |||
| 160 | void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, | ||
| 161 | StkId res) { | ||
| 162 | if (!luaO_rawarith(L, op, p1, p2, s2v(res))) { | ||
| 163 | /* could not perform raw operation; try metamethod */ | ||
| 164 | luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); | ||
| 165 | } | ||
| 159 | } | 166 | } |
| 160 | 167 | ||
| 161 | 168 | ||
| @@ -367,7 +374,7 @@ int luaO_utf8esc (char *buff, unsigned long x) { | |||
| 367 | /* | 374 | /* |
| 368 | ** Convert a number object to a string | 375 | ** Convert a number object to a string |
| 369 | */ | 376 | */ |
| 370 | void luaO_tostring (lua_State *L, StkId obj) { | 377 | void luaO_tostring (lua_State *L, TValue *obj) { |
| 371 | char buff[MAXNUMBER2STR]; | 378 | char buff[MAXNUMBER2STR]; |
| 372 | size_t len; | 379 | size_t len; |
| 373 | lua_assert(ttisnumber(obj)); | 380 | lua_assert(ttisnumber(obj)); |
| @@ -382,7 +389,7 @@ void luaO_tostring (lua_State *L, StkId obj) { | |||
| 382 | } | 389 | } |
| 383 | #endif | 390 | #endif |
| 384 | } | 391 | } |
| 385 | setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); | 392 | setsvalue(L, obj, luaS_newlstr(L, buff, len)); |
| 386 | } | 393 | } |
| 387 | 394 | ||
| 388 | 395 | ||
| @@ -418,18 +425,18 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | |||
| 418 | break; | 425 | break; |
| 419 | } | 426 | } |
| 420 | case 'd': { /* an 'int' */ | 427 | case 'd': { /* an 'int' */ |
| 421 | setivalue(L->top, va_arg(argp, int)); | 428 | setivalue(s2v(L->top), va_arg(argp, int)); |
| 422 | goto top2str; | 429 | goto top2str; |
| 423 | } | 430 | } |
| 424 | case 'I': { /* a 'lua_Integer' */ | 431 | case 'I': { /* a 'lua_Integer' */ |
| 425 | setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt))); | 432 | setivalue(s2v(L->top), cast(lua_Integer, va_arg(argp, l_uacInt))); |
| 426 | goto top2str; | 433 | goto top2str; |
| 427 | } | 434 | } |
| 428 | case 'f': { /* a 'lua_Number' */ | 435 | case 'f': { /* a 'lua_Number' */ |
| 429 | setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); | 436 | setfltvalue(s2v(L->top), cast_num(va_arg(argp, l_uacNumber))); |
| 430 | top2str: /* convert the top element to a string */ | 437 | top2str: /* convert the top element to a string */ |
| 431 | luaD_inctop(L); | 438 | luaD_inctop(L); |
| 432 | luaO_tostring(L, L->top - 1); | 439 | luaO_tostring(L, s2v(L->top - 1)); |
| 433 | break; | 440 | break; |
| 434 | } | 441 | } |
| 435 | case 'p': { /* a pointer */ | 442 | case 'p': { /* a pointer */ |
| @@ -460,7 +467,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | |||
| 460 | luaD_checkstack(L, 1); | 467 | luaD_checkstack(L, 1); |
| 461 | pushstr(L, fmt, strlen(fmt)); | 468 | pushstr(L, fmt, strlen(fmt)); |
| 462 | if (n > 0) luaV_concat(L, n + 1); | 469 | if (n > 0) luaV_concat(L, n + 1); |
| 463 | return svalue(L->top - 1); | 470 | return svalue(s2v(L->top - 1)); |
| 464 | } | 471 | } |
| 465 | 472 | ||
| 466 | 473 | ||
