lobject.c 14 KB

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