lobject.c 13 KB

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