Showing the changes to src/lobject.c (1 of 60 files). Show all
Unified · Split
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 2.67 2013/06/25 18:58:32 roberto Exp $ | 2 | ** $Id: lobject.c,v 2.76 2014/03/21 13:52:33 roberto Exp $ |
| 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 | */ |
| @@ -77,22 +77,30 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, · 71 unmodified lines | |||
| 77 | case LUA_OPSUB:return intop(-, v1, v2); | 77 | case LUA_OPSUB:return intop(-, v1, v2); |
| 78 | case LUA_OPMUL:return intop(*, v1, v2); | 78 | case LUA_OPMUL:return intop(*, v1, v2); |
| 79 | case LUA_OPMOD: return luaV_mod(L, v1, v2); | 79 | case LUA_OPMOD: return luaV_mod(L, v1, v2); |
| 80 | case LUA_OPPOW: return luaV_pow(v1, v2); | 80 | case LUA_OPPOW: return luaV_pow(L, v1, v2); |
| 81 | case LUA_OPUNM: return -v1; | 81 | case LUA_OPIDIV: return luaV_div(L, v1, v2); |
| 82 | case LUA_OPBAND: return intop(&, v1, v2); | ||
| 83 | case LUA_OPBOR: return intop(|, v1, v2); | ||
| 84 | case LUA_OPBXOR: return intop(^, v1, v2); | ||
| 85 | case LUA_OPSHL: return luaV_shiftl(v1, v2); | ||
| 86 | case LUA_OPSHR: return luaV_shiftl(v1, -v2); | ||
| 87 | case LUA_OPUNM: return intop(-, 0, v1); | ||
| 88 | case LUA_OPBNOT: return intop(^, cast_integer(-1), v1); | ||
| 82 | default: lua_assert(0); return 0; | 89 | default: lua_assert(0); return 0; |
| 83 | } | 90 | } |
| 84 | } | 91 | } |
| 85 | 92 | ||
| 86 | 93 | ||
| 87 | static lua_Number numarith (int op, lua_Number v1, lua_Number v2) { | 94 | static lua_Number numarith (lua_State *L, int op, lua_Number v1, |
| 95 | lua_Number v2) { | ||
| 88 | switch (op) { | 96 | switch (op) { |
| 89 | case LUA_OPADD: return luai_numadd(NULL, v1, v2); | 97 | case LUA_OPADD: return luai_numadd(L, v1, v2); |
| 90 | case LUA_OPSUB: return luai_numsub(NULL, v1, v2); | 98 | case LUA_OPSUB: return luai_numsub(L, v1, v2); |
| 91 | case LUA_OPMUL: return luai_nummul(NULL, v1, v2); | 99 | case LUA_OPMUL: return luai_nummul(L, v1, v2); |
| 92 | case LUA_OPDIV: return luai_numdiv(NULL, v1, v2); | 100 | case LUA_OPDIV: return luai_numdiv(L, v1, v2); |
| 93 | case LUA_OPMOD: return luai_nummod(NULL, v1, v2); | 101 | case LUA_OPMOD: return luai_nummod(L, v1, v2); |
| 94 | case LUA_OPPOW: return luai_numpow(NULL, v1, v2); | 102 | case LUA_OPPOW: return luai_numpow(L, v1, v2); |
| 95 | case LUA_OPUNM: return luai_numunm(NULL, v1); | 103 | case LUA_OPUNM: return luai_numunm(L, v1); |
| 96 | default: lua_assert(0); return 0; | 104 | default: lua_assert(0); return 0; |
| 97 | } | 105 | } |
| 98 | } | 106 | } |
| @@ -100,29 +108,40 @@ static lua_Number numarith (int op, lua_Number v1, lua_Number v2) { · 1 unmodified line | |||
| 100 | 108 | ||
| 101 | void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, | 109 | void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, |
| 102 | TValue *res) { | 110 | TValue *res) { |
| 103 | if (op == LUA_OPIDIV) { /* operates only on integers */ | 111 | switch (op) { |
| 104 | lua_Integer i1; lua_Integer i2; | 112 | case LUA_OPIDIV: case LUA_OPBAND: case LUA_OPBOR: |
| 105 | if (tointeger(p1, &i1) && tointeger(p2, &i2)) { | 113 | case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR: |
| 106 | setivalue(res, luaV_div(L, i1, i2)); | 114 | case LUA_OPBNOT: { /* operates only on integers */ |
| 107 | return; | 115 | lua_Integer i1; lua_Integer i2; |
| 116 | if (tointeger(p1, &i1) && tointeger(p2, &i2)) { | ||
| 117 | setivalue(res, intarith(L, op, i1, i2)); | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | else break; /* go to the end */ | ||
| 108 | } | 121 | } |
| 109 | /* else go to the end */ | 122 | case LUA_OPDIV: { /* operates only on floats */ |
| 110 | } | 123 | lua_Number n1; lua_Number n2; |
| 111 | else { /* other operations */ | 124 | if (tonumber(p1, &n1) && tonumber(p2, &n2)) { |
| 112 | lua_Number n1; lua_Number n2; | 125 | setnvalue(res, numarith(L, op, n1, n2)); |
| 113 | if (ttisinteger(p1) && ttisinteger(p2) && op != LUA_OPDIV && | 126 | return; |
| 114 | (op != LUA_OPPOW || ivalue(p2) >= 0)) { | 127 | } |
| 115 | setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); | 128 | else break; /* go to the end */ |
| 116 | return; | ||
| 117 | } | 129 | } |
| 118 | else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { | 130 | default: { /* other operations */ |
| 119 | setnvalue(res, numarith(op, n1, n2)); | 131 | lua_Number n1; lua_Number n2; |
| 120 | return; | 132 | if (ttisinteger(p1) && ttisinteger(p2)) { |
| 133 | setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { | ||
| 137 | setnvalue(res, numarith(L, op, n1, n2)); | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | else break; /* go to the end */ | ||
| 121 | } | 141 | } |
| 122 | /* else go to the end */ | ||
| 123 | } | 142 | } |
| 124 | /* could not perform raw operation; try metmethod */ | 143 | /* could not perform raw operation; try metamethod */ |
| 125 | lua_assert(L != NULL); /* cannot fail when folding (compile time) */ | 144 | lua_assert(L != NULL); /* should not fail when folding (compile time) */ |
| 126 | luaT_trybinTM(L, p1, p2, res, cast(TMS, op - LUA_OPADD + TM_ADD)); | 145 | luaT_trybinTM(L, p1, p2, res, cast(TMS, op - LUA_OPADD + TM_ADD)); |
| 127 | } | 146 | } |
| 128 | 147 | ||
| @@ -141,22 +160,26 @@ static int isneg (const char **s) { · 12 unmodified lines | |||
| 141 | 160 | ||
| 142 | 161 | ||
| 143 | /* | 162 | /* |
| 163 | ** {====================================================== | ||
| 144 | ** lua_strx2number converts an hexadecimal numeric string to a number. | 164 | ** lua_strx2number converts an hexadecimal numeric string to a number. |
| 145 | ** In C99, 'strtod' does both conversions. C89, however, has no function | 165 | ** In C99, 'strtod' does both conversions. C89, however, has no function |
| 146 | ** to convert floating hexadecimal strings to numbers. For these | 166 | ** to convert floating hexadecimal strings to numbers. For these |
| 147 | ** systems, you can leave 'lua_strx2number' undefined and Lua will | 167 | ** systems, you can leave 'lua_strx2number' undefined and Lua will |
| 148 | ** provide its own implementation. | 168 | ** provide its own implementation. |
| 169 | ** ======================================================= | ||
| 149 | */ | 170 | */ |
| 150 | #if defined(LUA_USE_STRTODHEX) | 171 | #if !defined(lua_strx2number) /* { */ |
| 172 | |||
| 173 | #if defined(LUA_USE_C99) /* { */ | ||
| 174 | |||
| 151 | #define lua_strx2number(s,p) lua_str2number(s,p) | 175 | #define lua_strx2number(s,p) lua_str2number(s,p) |
| 152 | #endif | ||
| 153 | 176 | ||
| 177 | #else /* }{ */ | ||
| 154 | 178 | ||
| 155 | #if !defined(lua_strx2number) | 179 | /* Lua's implementation for 'lua_strx2number' */ |
| 156 | 180 | ||
| 157 | #include <math.h> | 181 | #include <math.h> |
| 158 | 182 | ||
| 159 | |||
| 160 | /* maximum number of significant digits to read (to avoid overflows | 183 | /* maximum number of significant digits to read (to avoid overflows |
| 161 | even with single floats) */ | 184 | even with single floats) */ |
| 162 | #define MAXSIGDIG 30 | 185 | #define MAXSIGDIG 30 |
| @@ -219,7 +242,11 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { · 56 unmodified lines | |||
| 219 | return l_mathop(ldexp)(r, e); | 242 | return l_mathop(ldexp)(r, e); |
| 220 | } | 243 | } |
| 221 | 244 | ||
| 222 | #endif | 245 | #endif /* } */ |
| 246 | |||
| 247 | #endif /* } */ | ||
| 248 | |||
| 249 | /* }====================================================== */ | ||
| 223 | 250 | ||
| 224 | 251 | ||
| 225 | int luaO_str2d (const char *s, size_t len, lua_Number *result) { | 252 | int luaO_str2d (const char *s, size_t len, lua_Number *result) { |
| @@ -260,19 +287,36 @@ int luaO_str2int (const char *s, size_t len, lua_Integer *result) { · 34 unmodified lines | |||
| 260 | while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ | 287 | while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ |
| 261 | if (empty || s != ends) return 0; /* something wrong in the numeral */ | 288 | if (empty || s != ends) return 0; /* something wrong in the numeral */ |
| 262 | else { | 289 | else { |
| 263 | if (neg) *result = -cast(lua_Integer, a); | 290 | *result = cast_integer((neg) ? 0u - a : a); |
| 264 | else *result = cast(lua_Integer, a); | ||
| 265 | return 1; | 291 | return 1; |
| 266 | } | 292 | } |
| 267 | } | 293 | } |
| 268 | 294 | ||
| 269 | 295 | ||
| 296 | int luaO_utf8esc (char *buff, unsigned int x) { | ||
| 297 | int n = 1; /* number of bytes put in buffer (backwards) */ | ||
| 298 | if (x < 0x80) /* ascii? */ | ||
| 299 | buff[UTF8BUFFSZ - 1] = x; | ||
| 300 | else { /* need continuation bytes */ | ||
| 301 | unsigned int mfb = 0x3f; /* maximum that fits in first byte */ | ||
| 302 | do { | ||
| 303 | buff[UTF8BUFFSZ - (n++)] = 0x80 | (x & 0x3f); /* add continuation byte */ | ||
| 304 | x >>= 6; /* remove added bits */ | ||
| 305 | mfb >>= 1; /* now there is one less bit available in first byte */ | ||
| 306 | } while (x > mfb); /* still needs continuation byte? */ | ||
| 307 | buff[UTF8BUFFSZ - n] = (~mfb << 1) | x; /* add first byte */ | ||
| 308 | } | ||
| 309 | return n; | ||
| 310 | } | ||
| 311 | |||
| 312 | |||
| 270 | static void pushstr (lua_State *L, const char *str, size_t l) { | 313 | static void pushstr (lua_State *L, const char *str, size_t l) { |
| 271 | setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); | 314 | setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); |
| 272 | } | 315 | } |
| 273 | 316 | ||
| 274 | 317 | ||
| 275 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ | 318 | /* this function handles only '%d', '%c', '%f', '%p', and '%s' |
| 319 | conventional formats, plus Lua-specific '%L' and '%U' */ | ||
| 276 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | 320 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { |
| 277 | int n = 0; | 321 | int n = 0; |
| 278 | for (;;) { | 322 | for (;;) { |
| @@ -311,6 +355,12 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { · 32 unmodified lines | |||
| 311 | pushstr(L, buff, l); | 355 | pushstr(L, buff, l); |
| 312 | break; | 356 | break; |
| 313 | } | 357 | } |
| 358 | case 'U': { | ||
| 359 | char buff[UTF8BUFFSZ]; | ||
| 360 | int l = luaO_utf8esc(buff, va_arg(argp, int)); | ||
| 361 | pushstr(L, buff + UTF8BUFFSZ - l, l); | ||
| 362 | break; | ||
| 363 | } | ||
| 314 | case '%': { | 364 | case '%': { |
| 315 | pushstr(L, "%", 1); | 365 | pushstr(L, "%", 1); |
| 316 | break; | 366 | break; |