lobject.c 14 KB

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