lobject.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. ** $Id: lobject.c,v 2.62 2013/05/02 12:37:24 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_OPUNM: return -v1;
  67. default: lua_assert(0); return 0;
  68. }
  69. }
  70. static lua_Number numarith (int op, lua_Number v1, lua_Number v2) {
  71. switch (op) {
  72. case LUA_OPADD: return luai_numadd(NULL, v1, v2);
  73. case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
  74. case LUA_OPMUL: return luai_nummul(NULL, v1, v2);
  75. case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
  76. case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
  77. case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
  78. case LUA_OPUNM: return luai_numunm(NULL, v1);
  79. default: lua_assert(0); return 0;
  80. }
  81. }
  82. void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
  83. TValue *res) {
  84. if (op == LUA_OPIDIV) { /* operates only on integers */
  85. lua_Integer i1; lua_Integer i2;
  86. if (tointeger(p1, &i1) && tointeger(p2, &i2)) {
  87. setivalue(res, luaV_div(L, i1, i2));
  88. return;
  89. }
  90. /* else go to the end */
  91. }
  92. else { /* other operations */
  93. lua_Number n1; lua_Number n2;
  94. if (ttisinteger(p1) && ttisinteger(p2) && op != LUA_OPDIV &&
  95. (op != LUA_OPPOW || ivalue(p2) >= 0)) {
  96. setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
  97. return;
  98. }
  99. else if (tonumber(p1, &n1) && tonumber(p2, &n2)) {
  100. setnvalue(res, numarith(op, n1, n2));
  101. return;
  102. }
  103. /* else go to the end */
  104. }
  105. /* could not perform raw operation; try metmethod */
  106. lua_assert(L != NULL); /* cannot fail when folding (compile time) */
  107. luaT_trybinTM(L, p1, p2, res, cast(TMS, op - LUA_OPADD + TM_ADD));
  108. }
  109. int luaO_hexavalue (int c) {
  110. if (lisdigit(c)) return c - '0';
  111. else return ltolower(c) - 'a' + 10;
  112. }
  113. static int isneg (const char **s) {
  114. if (**s == '-') { (*s)++; return 1; }
  115. else if (**s == '+') (*s)++;
  116. return 0;
  117. }
  118. #if !defined(lua_strx2number)
  119. #include <math.h>
  120. static lua_Number readhexa (const char **s, lua_Number r, int *count) {
  121. for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
  122. r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));
  123. (*count)++;
  124. }
  125. return r;
  126. }
  127. /*
  128. ** convert an hexadecimal numeric string to a number, following
  129. ** C99 specification for 'strtod'
  130. */
  131. static lua_Number lua_strx2number (const char *s, char **endptr) {
  132. lua_Number r = 0.0;
  133. int e = 0, i = 0;
  134. int neg = 0; /* 1 if number is negative */
  135. *endptr = cast(char *, s); /* nothing is valid yet */
  136. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  137. neg = isneg(&s); /* check signal */
  138. if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
  139. return 0.0; /* invalid format (no '0x') */
  140. s += 2; /* skip '0x' */
  141. r = readhexa(&s, r, &i); /* read integer part */
  142. if (*s == '.') {
  143. s++; /* skip dot */
  144. r = readhexa(&s, r, &e); /* read fractional part */
  145. }
  146. if (i == 0 && e == 0)
  147. return 0.0; /* invalid format (no digit) */
  148. e *= -4; /* each fractional digit divides value by 2^-4 */
  149. *endptr = cast(char *, s); /* valid up to here */
  150. if (*s == 'p' || *s == 'P') { /* exponent part? */
  151. int exp1 = 0;
  152. int neg1;
  153. s++; /* skip 'p' */
  154. neg1 = isneg(&s); /* signal */
  155. if (!lisdigit(cast_uchar(*s)))
  156. goto ret; /* must have at least one digit */
  157. while (lisdigit(cast_uchar(*s))) /* read exponent */
  158. exp1 = exp1 * 10 + *(s++) - '0';
  159. if (neg1) exp1 = -exp1;
  160. e += exp1;
  161. }
  162. *endptr = cast(char *, s); /* valid up to here */
  163. ret:
  164. if (neg) r = -r;
  165. return l_mathop(ldexp)(r, e);
  166. }
  167. #endif
  168. int luaO_str2d (const char *s, size_t len, lua_Number *result) {
  169. char *endptr;
  170. if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
  171. return 0;
  172. else if (strpbrk(s, "xX")) /* hexa? */
  173. *result = lua_strx2number(s, &endptr);
  174. else
  175. *result = lua_str2number(s, &endptr);
  176. if (endptr == s) return 0; /* nothing recognized */
  177. while (lisspace(cast_uchar(*endptr))) endptr++;
  178. return (endptr == s + len); /* OK if no trailing characters */
  179. }
  180. int luaO_str2int (const char *s, size_t len, lua_Integer *result) {
  181. const char *ends = s + len;
  182. lua_Unsigned a = 0;
  183. int empty = 1;
  184. int neg;
  185. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  186. neg = isneg(&s);
  187. if (s[0] == '0' &&
  188. (s[1] == 'x' || s[1] == 'X')) { /* hexa? */
  189. s += 2; /* skip '0x' */
  190. for (; lisxdigit(cast_uchar(*s)); s++) {
  191. a = a * 16 + luaO_hexavalue(cast_uchar(*s));
  192. empty = 0;
  193. }
  194. }
  195. else { /* decimal */
  196. for (; lisdigit(cast_uchar(*s)); s++) {
  197. a = a * 10 + luaO_hexavalue(cast_uchar(*s));
  198. empty = 0;
  199. }
  200. }
  201. while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
  202. if (empty || s != ends) return 0; /* something wrong in the numeral */
  203. else {
  204. if (neg) *result = -cast(lua_Integer, a);
  205. else *result = cast(lua_Integer, a);
  206. return 1;
  207. }
  208. }
  209. static void pushstr (lua_State *L, const char *str, size_t l) {
  210. setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
  211. }
  212. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  213. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  214. int n = 0;
  215. for (;;) {
  216. const char *e = strchr(fmt, '%');
  217. if (e == NULL) break;
  218. luaD_checkstack(L, 2); /* fmt + item */
  219. pushstr(L, fmt, e - fmt);
  220. switch (*(e+1)) {
  221. case 's': {
  222. const char *s = va_arg(argp, char *);
  223. if (s == NULL) s = "(null)";
  224. pushstr(L, s, strlen(s));
  225. break;
  226. }
  227. case 'c': {
  228. char buff;
  229. buff = cast(char, va_arg(argp, int));
  230. pushstr(L, &buff, 1);
  231. break;
  232. }
  233. case 'd': {
  234. setnvalue(L->top++, cast_num(va_arg(argp, int)));
  235. break;
  236. }
  237. case 'I': {
  238. char buff[LUA_MAXINTEGER2STR];
  239. lua_Integer i = cast(lua_Integer, va_arg(argp, lua_Integer));
  240. int l = lua_integer2str(buff, i);
  241. pushstr(L, buff, l);
  242. break;
  243. }
  244. case 'f': {
  245. setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
  246. break;
  247. }
  248. case 'p': {
  249. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  250. int l = sprintf(buff, "%p", va_arg(argp, void *));
  251. pushstr(L, buff, l);
  252. break;
  253. }
  254. case '%': {
  255. pushstr(L, "%", 1);
  256. break;
  257. }
  258. default: {
  259. luaG_runerror(L,
  260. "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
  261. *(e + 1));
  262. }
  263. }
  264. n += 2;
  265. fmt = e+2;
  266. }
  267. luaD_checkstack(L, 1);
  268. pushstr(L, fmt, strlen(fmt));
  269. if (n > 0) luaV_concat(L, n + 1);
  270. return svalue(L->top - 1);
  271. }
  272. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  273. const char *msg;
  274. va_list argp;
  275. va_start(argp, fmt);
  276. msg = luaO_pushvfstring(L, fmt, argp);
  277. va_end(argp);
  278. return msg;
  279. }
  280. /* number of chars of a literal string without the ending \0 */
  281. #define LL(x) (sizeof(x)/sizeof(char) - 1)
  282. #define RETS "..."
  283. #define PRE "[string \""
  284. #define POS "\"]"
  285. #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
  286. void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  287. size_t l = strlen(source);
  288. if (*source == '=') { /* 'literal' source */
  289. if (l <= bufflen) /* small enough? */
  290. memcpy(out, source + 1, l * sizeof(char));
  291. else { /* truncate it */
  292. addstr(out, source + 1, bufflen - 1);
  293. *out = '\0';
  294. }
  295. }
  296. else if (*source == '@') { /* file name */
  297. if (l <= bufflen) /* small enough? */
  298. memcpy(out, source + 1, l * sizeof(char));
  299. else { /* add '...' before rest of name */
  300. addstr(out, RETS, LL(RETS));
  301. bufflen -= LL(RETS);
  302. memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
  303. }
  304. }
  305. else { /* string; format as [string "source"] */
  306. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  307. addstr(out, PRE, LL(PRE)); /* add prefix */
  308. bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
  309. if (l < bufflen && nl == NULL) { /* small one-line source? */
  310. addstr(out, source, l); /* keep it */
  311. }
  312. else {
  313. if (nl != NULL) l = nl - source; /* stop at first newline */
  314. if (l > bufflen) l = bufflen;
  315. addstr(out, source, l);
  316. addstr(out, RETS, LL(RETS));
  317. }
  318. memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
  319. }
  320. }