lobject.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. ** $Id: lobject.c,v 2.70 2013/12/18 14:12:03 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(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(^, cast_integer(-1), v1);
  74. default: lua_assert(0); return 0;
  75. }
  76. }
  77. static lua_Number numarith (int op, lua_Number v1, lua_Number v2) {
  78. switch (op) {
  79. case LUA_OPADD: return luai_numadd(NULL, v1, v2);
  80. case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
  81. case LUA_OPMUL: return luai_nummul(NULL, v1, v2);
  82. case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
  83. case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
  84. case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
  85. case LUA_OPUNM: return luai_numunm(NULL, v1);
  86. default: lua_assert(0); return 0;
  87. }
  88. }
  89. void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
  90. TValue *res) {
  91. switch (op) {
  92. case LUA_OPIDIV: case LUA_OPBAND: case LUA_OPBOR:
  93. case LUA_OPBXOR: case LUA_OPSHL: case LUA_OPSHR:
  94. case LUA_OPBNOT: { /* operates only on integers */
  95. lua_Integer i1; lua_Integer i2;
  96. if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
  97. setivalue(res, intarith(L, op, i1, i2));
  98. return;
  99. }
  100. else break; /* go to the end */
  101. }
  102. case LUA_OPDIV: { /* operates only on floats */
  103. lua_Number n1; lua_Number n2;
  104. if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
  105. setnvalue(res, numarith(op, n1, n2));
  106. return;
  107. }
  108. else break; /* go to the end */
  109. }
  110. default: { /* other operations */
  111. lua_Number n1; lua_Number n2;
  112. if (ttisinteger(p1) && ttisinteger(p2)) {
  113. setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
  114. return;
  115. }
  116. else if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
  117. setnvalue(res, numarith(op, n1, n2));
  118. return;
  119. }
  120. else break; /* go to the end */
  121. }
  122. }
  123. /* could not perform raw operation; try metmethod */
  124. lua_assert(L != NULL); /* should not fail when folding (compile time) */
  125. luaT_trybinTM(L, p1, p2, res, cast(TMS, op - LUA_OPADD + TM_ADD));
  126. }
  127. int luaO_hexavalue (int c) {
  128. if (lisdigit(c)) return c - '0';
  129. else return ltolower(c) - 'a' + 10;
  130. }
  131. static int isneg (const char **s) {
  132. if (**s == '-') { (*s)++; return 1; }
  133. else if (**s == '+') (*s)++;
  134. return 0;
  135. }
  136. /*
  137. ** lua_strx2number converts an hexadecimal numeric string to a number.
  138. ** In C99, 'strtod' does both conversions. C89, however, has no function
  139. ** to convert floating hexadecimal strings to numbers. For these
  140. ** systems, you can leave 'lua_strx2number' undefined and Lua will
  141. ** provide its own implementation.
  142. */
  143. #if defined(LUA_USE_STRTODHEX)
  144. #define lua_strx2number(s,p) lua_str2number(s,p)
  145. #endif
  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 zero? */
  174. nosigdig++;
  175. if (dot) e--; /* zero after dot? correct exponent */
  176. }
  177. else {
  178. if (++sigdig <= MAXSIGDIG) { /* can read it without overflow? */
  179. r = (r * cast_num(16.0)) + luaO_hexavalue(cast_uchar(*s));
  180. if (dot) e--; /* decimal digit */
  181. }
  182. else /* too many digits; ignore */
  183. if (!dot) e++; /* still count it for exponent */
  184. }
  185. }
  186. else break; /* neither a dot nor a digit */
  187. }
  188. if (nosigdig + sigdig == 0) /* no digits? */
  189. return 0.0; /* invalid format */
  190. *endptr = cast(char *, s); /* valid up to here */
  191. e *= 4; /* each digit multiplies/divides value by 2^4 */
  192. if (*s == 'p' || *s == 'P') { /* exponent part? */
  193. int exp1 = 0; /* exponent value */
  194. int neg1; /* exponent signal */
  195. s++; /* skip 'p' */
  196. neg1 = isneg(&s); /* signal */
  197. if (!lisdigit(cast_uchar(*s)))
  198. return 0.0; /* invalid; must have at least one digit */
  199. while (lisdigit(cast_uchar(*s))) /* read exponent */
  200. exp1 = exp1 * 10 + *(s++) - '0';
  201. if (neg1) exp1 = -exp1;
  202. e += exp1;
  203. *endptr = cast(char *, s); /* valid up to here */
  204. }
  205. if (neg) r = -r;
  206. return l_mathop(ldexp)(r, e);
  207. }
  208. #endif
  209. int luaO_str2d (const char *s, size_t len, lua_Number *result) {
  210. char *endptr;
  211. if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
  212. return 0;
  213. else if (strpbrk(s, "xX")) /* hexa? */
  214. *result = lua_strx2number(s, &endptr);
  215. else
  216. *result = lua_str2number(s, &endptr);
  217. if (endptr == s) return 0; /* nothing recognized */
  218. while (lisspace(cast_uchar(*endptr))) endptr++;
  219. return (endptr == s + len); /* OK if no trailing characters */
  220. }
  221. int luaO_str2int (const char *s, size_t len, lua_Integer *result) {
  222. const char *ends = s + len;
  223. lua_Unsigned a = 0;
  224. int empty = 1;
  225. int neg;
  226. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  227. neg = isneg(&s);
  228. if (s[0] == '0' &&
  229. (s[1] == 'x' || s[1] == 'X')) { /* hexa? */
  230. s += 2; /* skip '0x' */
  231. for (; lisxdigit(cast_uchar(*s)); s++) {
  232. a = a * 16 + luaO_hexavalue(cast_uchar(*s));
  233. empty = 0;
  234. }
  235. }
  236. else { /* decimal */
  237. for (; lisdigit(cast_uchar(*s)); s++) {
  238. a = a * 10 + luaO_hexavalue(cast_uchar(*s));
  239. empty = 0;
  240. }
  241. }
  242. while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
  243. if (empty || s != ends) return 0; /* something wrong in the numeral */
  244. else {
  245. *result = cast_integer((neg) ? 0u - a : a);
  246. return 1;
  247. }
  248. }
  249. static void pushstr (lua_State *L, const char *str, size_t l) {
  250. setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
  251. }
  252. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  253. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  254. int n = 0;
  255. for (;;) {
  256. const char *e = strchr(fmt, '%');
  257. if (e == NULL) break;
  258. luaD_checkstack(L, 2); /* fmt + item */
  259. pushstr(L, fmt, e - fmt);
  260. switch (*(e+1)) {
  261. case 's': {
  262. const char *s = va_arg(argp, char *);
  263. if (s == NULL) s = "(null)";
  264. pushstr(L, s, strlen(s));
  265. break;
  266. }
  267. case 'c': {
  268. char buff;
  269. buff = cast(char, va_arg(argp, int));
  270. pushstr(L, &buff, 1);
  271. break;
  272. }
  273. case 'd': {
  274. setivalue(L->top++, cast_int(va_arg(argp, int)));
  275. break;
  276. }
  277. case 'I': {
  278. setivalue(L->top++, cast_integer(va_arg(argp, lua_Integer)));
  279. break;
  280. }
  281. case 'f': {
  282. setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
  283. break;
  284. }
  285. case 'p': {
  286. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  287. int l = sprintf(buff, "%p", va_arg(argp, void *));
  288. pushstr(L, buff, l);
  289. break;
  290. }
  291. case '%': {
  292. pushstr(L, "%", 1);
  293. break;
  294. }
  295. default: {
  296. luaG_runerror(L,
  297. "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
  298. *(e + 1));
  299. }
  300. }
  301. n += 2;
  302. fmt = e+2;
  303. }
  304. luaD_checkstack(L, 1);
  305. pushstr(L, fmt, strlen(fmt));
  306. if (n > 0) luaV_concat(L, n + 1);
  307. return svalue(L->top - 1);
  308. }
  309. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  310. const char *msg;
  311. va_list argp;
  312. va_start(argp, fmt);
  313. msg = luaO_pushvfstring(L, fmt, argp);
  314. va_end(argp);
  315. return msg;
  316. }
  317. /* number of chars of a literal string without the ending \0 */
  318. #define LL(x) (sizeof(x)/sizeof(char) - 1)
  319. #define RETS "..."
  320. #define PRE "[string \""
  321. #define POS "\"]"
  322. #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
  323. void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  324. size_t l = strlen(source);
  325. if (*source == '=') { /* 'literal' source */
  326. if (l <= bufflen) /* small enough? */
  327. memcpy(out, source + 1, l * sizeof(char));
  328. else { /* truncate it */
  329. addstr(out, source + 1, bufflen - 1);
  330. *out = '\0';
  331. }
  332. }
  333. else if (*source == '@') { /* file name */
  334. if (l <= bufflen) /* small enough? */
  335. memcpy(out, source + 1, l * sizeof(char));
  336. else { /* add '...' before rest of name */
  337. addstr(out, RETS, LL(RETS));
  338. bufflen -= LL(RETS);
  339. memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
  340. }
  341. }
  342. else { /* string; format as [string "source"] */
  343. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  344. addstr(out, PRE, LL(PRE)); /* add prefix */
  345. bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
  346. if (l < bufflen && nl == NULL) { /* small one-line source? */
  347. addstr(out, source, l); /* keep it */
  348. }
  349. else {
  350. if (nl != NULL) l = nl - source; /* stop at first newline */
  351. if (l > bufflen) l = bufflen;
  352. addstr(out, source, l);
  353. addstr(out, RETS, LL(RETS));
  354. }
  355. memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
  356. }
  357. }