You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
7.8 KiB

  1. /*
  2. ** $Id: lobject.c,v 2.55 2011/11/30 19:30:16 roberto Exp $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #if defined HAVE_CONFIG_H // LOL BEGIN
  7. # include "config.h"
  8. #endif // LOL END
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #define lobject_c
  14. #define LUA_CORE
  15. #include "lua.h"
  16. #include "lctype.h"
  17. #include "ldebug.h"
  18. #include "ldo.h"
  19. #include "lmem.h"
  20. #include "lobject.h"
  21. #include "lstate.h"
  22. #include "lstring.h"
  23. #include "lvm.h"
  24. LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
  25. /*
  26. ** converts an integer to a "floating point byte", represented as
  27. ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
  28. ** eeeee != 0 and (xxx) otherwise.
  29. */
  30. int luaO_int2fb (unsigned int x) {
  31. int e = 0; /* exponent */
  32. if (x < 8) return x;
  33. while (x >= 0x10) {
  34. x = (x+1) >> 1;
  35. e++;
  36. }
  37. return ((e+1) << 3) | (cast_int(x) - 8);
  38. }
  39. /* converts back */
  40. int luaO_fb2int (int x) {
  41. int e = (x >> 3) & 0x1f;
  42. if (e == 0) return x;
  43. else return ((x & 7) + 8) << (e - 1);
  44. }
  45. int luaO_ceillog2 (unsigned int x) {
  46. static const lu_byte log_2[256] = {
  47. 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,
  48. 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,
  49. 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,
  50. 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,
  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. 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,
  54. 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
  55. };
  56. int l = 0;
  57. x--;
  58. while (x >= 256) { l += 8; x >>= 8; }
  59. return l + log_2[x];
  60. }
  61. lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
  62. switch (op) {
  63. case LUA_OPADD: return luai_numadd(NULL, v1, v2);
  64. case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
  65. case LUA_OPMUL: return luai_nummul(NULL, v1, v2);
  66. case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
  67. case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
  68. case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
  69. case LUA_OPUNM: return luai_numunm(NULL, v1);
  70. default: lua_assert(0); return 0;
  71. }
  72. }
  73. int luaO_hexavalue (int c) {
  74. if (lisdigit(c)) return c - '0';
  75. else return ltolower(c) - 'a' + 10;
  76. }
  77. #if !defined(lua_strx2number)
  78. #include <math.h>
  79. static int isneg (const char **s) {
  80. if (**s == '-') { (*s)++; return 1; }
  81. else if (**s == '+') (*s)++;
  82. return 0;
  83. }
  84. static lua_Number readhexa (const char **s, lua_Number r, int *count) {
  85. for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
  86. r = (r * 16.0) + cast_num(luaO_hexavalue(cast_uchar(**s)));
  87. (*count)++;
  88. }
  89. return r;
  90. }
  91. /*
  92. ** convert an hexadecimal numeric string to a number, following
  93. ** C99 specification for 'strtod'
  94. */
  95. static lua_Number lua_strx2number (const char *s, char **endptr) {
  96. lua_Number r = 0.0;
  97. int e = 0, i = 0;
  98. int neg = 0; /* 1 if number is negative */
  99. *endptr = cast(char *, s); /* nothing is valid yet */
  100. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  101. neg = isneg(&s); /* check signal */
  102. if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
  103. return 0.0; /* invalid format (no '0x') */
  104. s += 2; /* skip '0x' */
  105. r = readhexa(&s, r, &i); /* read integer part */
  106. if (*s == '.') {
  107. s++; /* skip dot */
  108. r = readhexa(&s, r, &e); /* read fractional part */
  109. }
  110. if (i == 0 && e == 0)
  111. return 0.0; /* invalid format (no digit) */
  112. e *= -4; /* each fractional digit divides value by 2^-4 */
  113. *endptr = cast(char *, s); /* valid up to here */
  114. if (*s == 'p' || *s == 'P') { /* exponent part? */
  115. int exp1 = 0;
  116. int neg1;
  117. s++; /* skip 'p' */
  118. neg1 = isneg(&s); /* signal */
  119. if (!lisdigit(cast_uchar(*s)))
  120. goto ret; /* must have at least one digit */
  121. while (lisdigit(cast_uchar(*s))) /* read exponent */
  122. exp1 = exp1 * 10 + *(s++) - '0';
  123. if (neg1) exp1 = -exp1;
  124. e += exp1;
  125. }
  126. *endptr = cast(char *, s); /* valid up to here */
  127. ret:
  128. if (neg) r = -r;
  129. return ldexp(r, e);
  130. }
  131. #endif
  132. int luaO_str2d (const char *s, size_t len, lua_Number *result) {
  133. char *endptr;
  134. if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
  135. return 0;
  136. else if (strpbrk(s, "xX")) /* hexa? */
  137. *result = lua_strx2number(s, &endptr);
  138. else
  139. *result = lua_str2number(s, &endptr);
  140. if (endptr == s) return 0; /* nothing recognized */
  141. while (lisspace(cast_uchar(*endptr))) endptr++;
  142. return (endptr == s + len); /* OK if no trailing characters */
  143. }
  144. static void pushstr (lua_State *L, const char *str, size_t l) {
  145. setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
  146. incr_top(L);
  147. }
  148. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  149. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  150. int n = 0;
  151. for (;;) {
  152. const char *e = strchr(fmt, '%');
  153. if (e == NULL) break;
  154. setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
  155. incr_top(L);
  156. switch (*(e+1)) {
  157. case 's': {
  158. const char *s = va_arg(argp, char *);
  159. if (s == NULL) s = "(null)";
  160. pushstr(L, s, strlen(s));
  161. break;
  162. }
  163. case 'c': {
  164. char buff;
  165. buff = cast(char, va_arg(argp, int));
  166. pushstr(L, &buff, 1);
  167. break;
  168. }
  169. case 'd': {
  170. setnvalue(L->top, cast_num(va_arg(argp, int)));
  171. incr_top(L);
  172. break;
  173. }
  174. case 'f': {
  175. setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
  176. incr_top(L);
  177. break;
  178. }
  179. case 'p': {
  180. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  181. int l = sprintf(buff, "%p", va_arg(argp, void *));
  182. pushstr(L, buff, l);
  183. break;
  184. }
  185. case '%': {
  186. pushstr(L, "%", 1);
  187. break;
  188. }
  189. default: {
  190. luaG_runerror(L,
  191. "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
  192. *(e + 1));
  193. }
  194. }
  195. n += 2;
  196. fmt = e+2;
  197. }
  198. pushstr(L, fmt, strlen(fmt));
  199. if (n > 0) luaV_concat(L, n + 1);
  200. return svalue(L->top - 1);
  201. }
  202. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  203. const char *msg;
  204. va_list argp;
  205. va_start(argp, fmt);
  206. msg = luaO_pushvfstring(L, fmt, argp);
  207. va_end(argp);
  208. return msg;
  209. }
  210. /* number of chars of a literal string without the ending \0 */
  211. #define LL(x) (sizeof(x)/sizeof(char) - 1)
  212. #define RETS "..."
  213. #define PRE "[string \""
  214. #define POS "\"]"
  215. #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
  216. void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  217. size_t l = strlen(source);
  218. if (*source == '=') { /* 'literal' source */
  219. if (l <= bufflen) /* small enough? */
  220. memcpy(out, source + 1, l * sizeof(char));
  221. else { /* truncate it */
  222. addstr(out, source + 1, bufflen - 1);
  223. *out = '\0';
  224. }
  225. }
  226. else if (*source == '@') { /* file name */
  227. if (l <= bufflen) /* small enough? */
  228. memcpy(out, source + 1, l * sizeof(char));
  229. else { /* add '...' before rest of name */
  230. addstr(out, RETS, LL(RETS));
  231. bufflen -= LL(RETS);
  232. memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
  233. }
  234. }
  235. else { /* string; format as [string "source"] */
  236. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  237. addstr(out, PRE, LL(PRE)); /* add prefix */
  238. bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
  239. if (l < bufflen && nl == NULL) { /* small one-line source? */
  240. addstr(out, source, l); /* keep it */
  241. }
  242. else {
  243. if (nl != NULL) l = nl - source; /* stop at first newline */
  244. if (l > bufflen) l = bufflen;
  245. addstr(out, source, l);
  246. addstr(out, RETS, LL(RETS));
  247. }
  248. memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
  249. }
  250. }