1 /* $NetBSD: lobject.c,v 1.8 2016/01/28 14:41:39 lneto Exp $ */ 2 3 /* 4 ** Id: lobject.c,v 2.108 2015/11/02 16:09:30 roberto Exp 5 ** Some generic functions over Lua objects 6 ** See Copyright Notice in lua.h 7 */ 8 9 #define lobject_c 10 #define LUA_CORE 11 12 #include "lprefix.h" 13 14 15 #ifndef _KERNEL 16 #include <locale.h> 17 #include <math.h> 18 #include <stdarg.h> 19 #include <stdio.h> 20 #include <stdlib.h> 21 #include <string.h> 22 #endif /* _KERNEL */ 23 24 #include "lua.h" 25 26 #include "lctype.h" 27 #include "ldebug.h" 28 #include "ldo.h" 29 #include "lmem.h" 30 #include "lobject.h" 31 #include "lstate.h" 32 #include "lstring.h" 33 #include "lvm.h" 34 35 36 37 LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT}; 38 39 40 /* 41 ** converts an integer to a "floating point byte", represented as 42 ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if 43 ** eeeee != 0 and (xxx) otherwise. 44 */ 45 int luaO_int2fb (unsigned int x) { 46 int e = 0; /* exponent */ 47 if (x < 8) return x; 48 while (x >= (8 << 4)) { /* coarse steps */ 49 x = (x + 0xf) >> 4; /* x = ceil(x / 16) */ 50 e += 4; 51 } 52 while (x >= (8 << 1)) { /* fine steps */ 53 x = (x + 1) >> 1; /* x = ceil(x / 2) */ 54 e++; 55 } 56 return ((e+1) << 3) | (cast_int(x) - 8); 57 } 58 59 60 /* converts back */ 61 int luaO_fb2int (int x) { 62 return (x < 8) ? x : ((x & 7) + 8) << ((x >> 3) - 1); 63 } 64 65 66 /* 67 ** Computes ceil(log2(x)) 68 */ 69 int luaO_ceillog2 (unsigned int x) { 70 static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */ 71 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 72 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 73 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 74 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 75 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 76 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 77 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 78 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 79 }; 80 int l = 0; 81 x--; 82 while (x >= 256) { l += 8; x >>= 8; } 83 return l + log_2[x]; 84 } 85 86 87 static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, 88 lua_Integer v2) { 89 switch (op) { 90 case LUA_OPADD: return intop(+, v1, v2); 91 case LUA_OPSUB:return intop(-, v1, v2); 92 case LUA_OPMUL:return intop(*, v1, v2); 93 case LUA_OPMOD: return luaV_mod(L, v1, v2); 94 case LUA_OPIDIV: return luaV_div(L, v1, v2); 95 case LUA_OPBAND: return intop(&, v1, v2); 96 case LUA_OPBOR: return intop(|, v1, v2); 97 case LUA_OPBXOR: return intop(^, v1, v2); 98 case LUA_OPSHL: return luaV_shiftl(v1, v2); 99 case LUA_OPSHR: return luaV_shiftl(v1, -v2); 100 case LUA_OPUNM: return intop(-, 0, v1); 101 case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); 102 default: lua_assert(0); return 0; 103 } 104 } 105 106 107 #ifndef _KERNEL 108 static lua_Number numarith (lua_State *L, int op, lua_Number v1, 109 lua_Number v2) { 110 switch (op) { 111 case LUA_OPADD: return luai_numadd(L, v1, v2); 112 case LUA_OPSUB: return luai_numsub(L, v1, v2); 113 case LUA_OPMUL: return luai_nummul(L, v1, v2); 114 case LUA_OPDIV: return luai_numdiv(L, v1, v2); 115 case LUA_OPPOW: return luai_numpow(L, v1, v2); 116 case LUA_OPIDIV: return luai_numidiv(L, v1, v2); 117 case LUA_OPUNM: return luai_numunm(L, v1); 118 case LUA_OPMOD: { 119 lua_Number m; 120 luai_nummod(L, v1, v2, m); 121 return m; 122 } 123 default: lua_assert(0); return 0; 124 } 125 } 126 #endif /* _KERNEL */ 127 128 129 void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2, 130 TValue *res) { 131 switch (op) { 132 case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: 133 case LUA_OPSHL: case LUA_OPSHR: 134 case LUA_OPBNOT: { /* operate only on integers */ 135 lua_Integer i1; lua_Integer i2; 136 if (tointeger(p1, &i1) && tointeger(p2, &i2)) { 137 setivalue(res, intarith(L, op, i1, i2)); 138 return; 139 } 140 else break; /* go to the end */ 141 } 142 #ifndef _KERNEL 143 case LUA_OPDIV: case LUA_OPPOW: { /* operate only on floats */ 144 lua_Number n1; lua_Number n2; 145 if (tonumber(p1, &n1) && tonumber(p2, &n2)) { 146 setfltvalue(res, numarith(L, op, n1, n2)); 147 return; 148 } 149 else break; /* go to the end */ 150 } 151 #endif /* _KERNEL */ 152 default: { /* other operations */ 153 #ifndef _KERNEL 154 lua_Number n1; lua_Number n2; 155 if (ttisinteger(p1) && ttisinteger(p2)) { 156 setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2))); 157 return; 158 } 159 else if (tonumber(p1, &n1) && tonumber(p2, &n2)) { 160 setfltvalue(res, numarith(L, op, n1, n2)); 161 return; 162 } 163 #else /* _KERNEL */ 164 lua_Integer i1; lua_Integer i2; 165 if (tointeger(p1, &i1) && tointeger(p2, &i2)) { 166 setivalue(res, intarith(L, op, i1, i2)); 167 return; 168 } 169 #endif /* _KERNEL */ 170 else break; /* go to the end */ 171 } 172 } 173 /* could not perform raw operation; try metamethod */ 174 lua_assert(L != NULL); /* should not fail when folding (compile time) */ 175 luaT_trybinTM(L, p1, p2, res, cast(TMS, (op - LUA_OPADD) + TM_ADD)); 176 } 177 178 179 int luaO_hexavalue (int c) { 180 if (lisdigit(c)) return c - '0'; 181 else return (ltolower(c) - 'a') + 10; 182 } 183 184 185 static int isneg (const char **s) { 186 if (**s == '-') { (*s)++; return 1; } 187 else if (**s == '+') (*s)++; 188 return 0; 189 } 190 191 192 193 #ifndef _KERNEL 194 /* 195 ** {================================================================== 196 ** Lua's implementation for 'lua_strx2number' 197 ** =================================================================== 198 */ 199 200 #if !defined(lua_strx2number) 201 202 /* maximum number of significant digits to read (to avoid overflows 203 even with single floats) */ 204 #define MAXSIGDIG 30 205 206 /* 207 ** convert an hexadecimal numeric string to a number, following 208 ** C99 specification for 'strtod' 209 */ 210 static lua_Number lua_strx2number (const char *s, char **endptr) { 211 int dot = lua_getlocaledecpoint(); 212 lua_Number r = 0.0; /* result (accumulator) */ 213 int sigdig = 0; /* number of significant digits */ 214 int nosigdig = 0; /* number of non-significant digits */ 215 int e = 0; /* exponent correction */ 216 int neg; /* 1 if number is negative */ 217 int hasdot = 0; /* true after seen a dot */ 218 *endptr = cast(char *, s); /* nothing is valid yet */ 219 while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ 220 neg = isneg(&s); /* check signal */ 221 if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ 222 return 0.0; /* invalid format (no '0x') */ 223 for (s += 2; ; s++) { /* skip '0x' and read numeral */ 224 if (*s == dot) { 225 if (hasdot) break; /* second dot? stop loop */ 226 else hasdot = 1; 227 } 228 else if (lisxdigit(cast_uchar(*s))) { 229 if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ 230 nosigdig++; 231 else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ 232 r = (r * cast_num(16.0)) + luaO_hexavalue(*s); 233 else e++; /* too many digits; ignore, but still count for exponent */ 234 if (hasdot) e--; /* decimal digit? correct exponent */ 235 } 236 else break; /* neither a dot nor a digit */ 237 } 238 if (nosigdig + sigdig == 0) /* no digits? */ 239 return 0.0; /* invalid format */ 240 *endptr = cast(char *, s); /* valid up to here */ 241 e *= 4; /* each digit multiplies/divides value by 2^4 */ 242 if (*s == 'p' || *s == 'P') { /* exponent part? */ 243 int exp1 = 0; /* exponent value */ 244 int neg1; /* exponent signal */ 245 s++; /* skip 'p' */ 246 neg1 = isneg(&s); /* signal */ 247 if (!lisdigit(cast_uchar(*s))) 248 return 0.0; /* invalid; must have at least one digit */ 249 while (lisdigit(cast_uchar(*s))) /* read exponent */ 250 exp1 = exp1 * 10 + *(s++) - '0'; 251 if (neg1) exp1 = -exp1; 252 e += exp1; 253 *endptr = cast(char *, s); /* valid up to here */ 254 } 255 if (neg) r = -r; 256 return l_mathop(ldexp)(r, e); 257 } 258 259 #endif 260 /* }====================================================== */ 261 262 263 static const char *l_str2d (const char *s, lua_Number *result) { 264 char *endptr; 265 if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ 266 return NULL; 267 else if (strpbrk(s, "xX")) /* hex? */ 268 *result = lua_strx2number(s, &endptr); 269 else 270 *result = lua_str2number(s, &endptr); 271 if (endptr == s) return NULL; /* nothing recognized */ 272 while (lisspace(cast_uchar(*endptr))) endptr++; 273 return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */ 274 } 275 #endif /* _KERNEL */ 276 277 278 static const char *l_str2int (const char *s, lua_Integer *result) { 279 lua_Unsigned a = 0; 280 int empty = 1; 281 int neg; 282 while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ 283 neg = isneg(&s); 284 if (s[0] == '0' && 285 (s[1] == 'x' || s[1] == 'X')) { /* hex? */ 286 s += 2; /* skip '0x' */ 287 for (; lisxdigit(cast_uchar(*s)); s++) { 288 a = a * 16 + luaO_hexavalue(*s); 289 empty = 0; 290 } 291 } 292 else { /* decimal */ 293 for (; lisdigit(cast_uchar(*s)); s++) { 294 a = a * 10 + *s - '0'; 295 empty = 0; 296 } 297 } 298 while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ 299 if (empty || *s != '\0') return NULL; /* something wrong in the numeral */ 300 else { 301 *result = l_castU2S((neg) ? 0u - a : a); 302 return s; 303 } 304 } 305 306 307 size_t luaO_str2num (const char *s, TValue *o) { 308 #ifndef _KERNEL 309 lua_Integer i; lua_Number n; 310 #else /* _KERNEL */ 311 lua_Integer i; 312 #endif /* _KERNEL */ 313 const char *e; 314 if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */ 315 setivalue(o, i); 316 } 317 #ifndef _KERNEL 318 else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */ 319 setfltvalue(o, n); 320 } 321 #endif /* _KERNEL */ 322 else 323 return 0; /* conversion failed */ 324 return (e - s) + 1; /* success; return string size */ 325 } 326 327 328 int luaO_utf8esc (char *buff, unsigned long x) { 329 int n = 1; /* number of bytes put in buffer (backwards) */ 330 lua_assert(x <= 0x10FFFF); 331 if (x < 0x80) /* ascii? */ 332 buff[UTF8BUFFSZ - 1] = cast(char, x); 333 else { /* need continuation bytes */ 334 unsigned int mfb = 0x3f; /* maximum that fits in first byte */ 335 do { /* add continuation bytes */ 336 buff[UTF8BUFFSZ - (n++)] = cast(char, 0x80 | (x & 0x3f)); 337 x >>= 6; /* remove added bits */ 338 mfb >>= 1; /* now there is one less bit available in first byte */ 339 } while (x > mfb); /* still needs continuation byte? */ 340 buff[UTF8BUFFSZ - n] = cast(char, (~mfb << 1) | x); /* add first byte */ 341 } 342 return n; 343 } 344 345 346 /* maximum length of the conversion of a number to a string */ 347 #define MAXNUMBER2STR 50 348 349 350 /* 351 ** Convert a number object to a string 352 */ 353 void luaO_tostring (lua_State *L, StkId obj) { 354 char buff[MAXNUMBER2STR]; 355 size_t len; 356 lua_assert(ttisnumber(obj)); 357 #ifndef _KERNEL 358 if (ttisinteger(obj)) 359 len = lua_integer2str(buff, sizeof(buff), ivalue(obj)); 360 else { 361 len = lua_number2str(buff, sizeof(buff), fltvalue(obj)); 362 #if !defined(LUA_COMPAT_FLOATSTRING) 363 if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ 364 buff[len++] = lua_getlocaledecpoint(); 365 buff[len++] = '0'; /* adds '.0' to result */ 366 } 367 #endif 368 } 369 #else /* _KERNEL */ 370 lua_assert(ttisinteger(obj)); 371 len = lua_integer2str(buff, sizeof(buff), ivalue(obj)); 372 #endif /* _KERNEL */ 373 setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); 374 } 375 376 377 static void pushstr (lua_State *L, const char *str, size_t l) { 378 setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); 379 luaD_inctop(L); 380 } 381 382 383 /* this function handles only '%d', '%c', '%f', '%p', and '%s' 384 conventional formats, plus Lua-specific '%I' and '%U' */ 385 const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { 386 int n = 0; 387 for (;;) { 388 const char *e = strchr(fmt, '%'); 389 if (e == NULL) break; 390 pushstr(L, fmt, e - fmt); 391 switch (*(e+1)) { 392 case 's': { 393 const char *s = va_arg(argp, char *); 394 if (s == NULL) s = "(null)"; 395 pushstr(L, s, strlen(s)); 396 break; 397 } 398 case 'c': { 399 char buff = cast(char, va_arg(argp, int)); 400 if (lisprint(cast_uchar(buff))) 401 pushstr(L, &buff, 1); 402 else /* non-printable character; print its code */ 403 luaO_pushfstring(L, "<\\%d>", cast_uchar(buff)); 404 break; 405 } 406 case 'd': { 407 setivalue(L->top, va_arg(argp, int)); 408 goto top2str; 409 } 410 case 'I': { 411 setivalue(L->top, cast(lua_Integer, va_arg(argp, l_uacInt))); 412 #ifndef _KERNEL 413 goto top2str; 414 } 415 case 'f': { 416 setfltvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); 417 #endif /* _KERNEL */ 418 top2str: 419 luaD_inctop(L); 420 luaO_tostring(L, L->top - 1); 421 break; 422 } 423 case 'p': { 424 char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ 425 int l = l_sprintf(buff, sizeof(buff), "%p", va_arg(argp, void *)); 426 pushstr(L, buff, l); 427 break; 428 } 429 case 'U': { 430 char buff[UTF8BUFFSZ]; 431 int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long))); 432 pushstr(L, buff + UTF8BUFFSZ - l, l); 433 break; 434 } 435 case '%': { 436 pushstr(L, "%", 1); 437 break; 438 } 439 default: { 440 luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'", 441 *(e + 1)); 442 } 443 } 444 n += 2; 445 fmt = e+2; 446 } 447 luaD_checkstack(L, 1); 448 pushstr(L, fmt, strlen(fmt)); 449 if (n > 0) luaV_concat(L, n + 1); 450 return svalue(L->top - 1); 451 } 452 453 454 const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { 455 const char *msg; 456 va_list argp; 457 va_start(argp, fmt); 458 msg = luaO_pushvfstring(L, fmt, argp); 459 va_end(argp); 460 return msg; 461 } 462 463 464 /* number of chars of a literal string without the ending \0 */ 465 #define LL(x) (sizeof(x)/sizeof(char) - 1) 466 467 #define RETS "..." 468 #define PRE "[string \"" 469 #define POS "\"]" 470 471 #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) ) 472 473 void luaO_chunkid (char *out, const char *source, size_t bufflen) { 474 size_t l = strlen(source); 475 if (*source == '=') { /* 'literal' source */ 476 if (l <= bufflen) /* small enough? */ 477 memcpy(out, source + 1, l * sizeof(char)); 478 else { /* truncate it */ 479 addstr(out, source + 1, bufflen - 1); 480 *out = '\0'; 481 } 482 } 483 else if (*source == '@') { /* file name */ 484 if (l <= bufflen) /* small enough? */ 485 memcpy(out, source + 1, l * sizeof(char)); 486 else { /* add '...' before rest of name */ 487 addstr(out, RETS, LL(RETS)); 488 bufflen -= LL(RETS); 489 memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char)); 490 } 491 } 492 else { /* string; format as [string "source"] */ 493 const char *nl = strchr(source, '\n'); /* find first new line (if any) */ 494 addstr(out, PRE, LL(PRE)); /* add prefix */ 495 bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */ 496 if (l < bufflen && nl == NULL) { /* small one-line source? */ 497 addstr(out, source, l); /* keep it */ 498 } 499 else { 500 if (nl != NULL) l = nl - source; /* stop at first newline */ 501 if (l > bufflen) l = bufflen; 502 addstr(out, source, l); 503 addstr(out, RETS, LL(RETS)); 504 } 505 memcpy(out, POS, (LL(POS) + 1) * sizeof(char)); 506 } 507 } 508 509