lobject.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. ** $Id: lobject.c,v 2.58 2013/02/20 14:08:56 roberto Exp $
  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. lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
  59. switch (op) {
  60. case LUA_OPADD: return luai_numadd(NULL, v1, v2);
  61. case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
  62. case LUA_OPMUL: return luai_nummul(NULL, v1, v2);
  63. case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
  64. case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
  65. case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
  66. case LUA_OPUNM: return luai_numunm(NULL, v1);
  67. default: lua_assert(0); return 0;
  68. }
  69. }
  70. int luaO_hexavalue (int c) {
  71. if (lisdigit(c)) return c - '0';
  72. else return ltolower(c) - 'a' + 10;
  73. }
  74. #if !defined(lua_strx2number)
  75. #include <math.h>
  76. static int isneg (const char **s) {
  77. if (**s == '-') { (*s)++; return 1; }
  78. else if (**s == '+') (*s)++;
  79. return 0;
  80. }
  81. static lua_Number readhexa (const char **s, lua_Number r, int *count) {
  82. for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
  83. r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));
  84. (*count)++;
  85. }
  86. return r;
  87. }
  88. /*
  89. ** convert an hexadecimal numeric string to a number, following
  90. ** C99 specification for 'strtod'
  91. */
  92. static lua_Number lua_strx2number (const char *s, char **endptr) {
  93. lua_Number r = 0.0;
  94. int e = 0, i = 0;
  95. int neg = 0; /* 1 if number is negative */
  96. *endptr = cast(char *, s); /* nothing is valid yet */
  97. while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
  98. neg = isneg(&s); /* check signal */
  99. if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
  100. return 0.0; /* invalid format (no '0x') */
  101. s += 2; /* skip '0x' */
  102. r = readhexa(&s, r, &i); /* read integer part */
  103. if (*s == '.') {
  104. s++; /* skip dot */
  105. r = readhexa(&s, r, &e); /* read fractional part */
  106. }
  107. if (i == 0 && e == 0)
  108. return 0.0; /* invalid format (no digit) */
  109. e *= -4; /* each fractional digit divides value by 2^-4 */
  110. *endptr = cast(char *, s); /* valid up to here */
  111. if (*s == 'p' || *s == 'P') { /* exponent part? */
  112. int exp1 = 0;
  113. int neg1;
  114. s++; /* skip 'p' */
  115. neg1 = isneg(&s); /* signal */
  116. if (!lisdigit(cast_uchar(*s)))
  117. goto ret; /* must have at least one digit */
  118. while (lisdigit(cast_uchar(*s))) /* read exponent */
  119. exp1 = exp1 * 10 + *(s++) - '0';
  120. if (neg1) exp1 = -exp1;
  121. e += exp1;
  122. }
  123. *endptr = cast(char *, s); /* valid up to here */
  124. ret:
  125. if (neg) r = -r;
  126. return l_mathop(ldexp)(r, e);
  127. }
  128. #endif
  129. int luaO_str2d (const char *s, size_t len, lua_Number *result) {
  130. char *endptr;
  131. if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
  132. return 0;
  133. else if (strpbrk(s, "xX")) /* hexa? */
  134. *result = lua_strx2number(s, &endptr);
  135. else
  136. *result = lua_str2number(s, &endptr);
  137. if (endptr == s) return 0; /* nothing recognized */
  138. while (lisspace(cast_uchar(*endptr))) endptr++;
  139. return (endptr == s + len); /* OK if no trailing characters */
  140. }
  141. static void pushstr (lua_State *L, const char *str, size_t l) {
  142. setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
  143. }
  144. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  145. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  146. int n = 0;
  147. for (;;) {
  148. const char *e = strchr(fmt, '%');
  149. if (e == NULL) break;
  150. luaD_checkstack(L, 2); /* fmt + item */
  151. pushstr(L, fmt, e - fmt);
  152. switch (*(e+1)) {
  153. case 's': {
  154. const char *s = va_arg(argp, char *);
  155. if (s == NULL) s = "(null)";
  156. pushstr(L, s, strlen(s));
  157. break;
  158. }
  159. case 'c': {
  160. char buff;
  161. buff = cast(char, va_arg(argp, int));
  162. pushstr(L, &buff, 1);
  163. break;
  164. }
  165. case 'd': {
  166. setnvalue(L->top++, cast_num(va_arg(argp, int)));
  167. break;
  168. }
  169. case 'f': {
  170. setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
  171. break;
  172. }
  173. case 'p': {
  174. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  175. int l = sprintf(buff, "%p", va_arg(argp, void *));
  176. pushstr(L, buff, l);
  177. break;
  178. }
  179. case '%': {
  180. pushstr(L, "%", 1);
  181. break;
  182. }
  183. default: {
  184. luaG_runerror(L,
  185. "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
  186. *(e + 1));
  187. }
  188. }
  189. n += 2;
  190. fmt = e+2;
  191. }
  192. luaD_checkstack(L, 1);
  193. pushstr(L, fmt, strlen(fmt));
  194. if (n > 0) luaV_concat(L, n + 1);
  195. return svalue(L->top - 1);
  196. }
  197. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  198. const char *msg;
  199. va_list argp;
  200. va_start(argp, fmt);
  201. msg = luaO_pushvfstring(L, fmt, argp);
  202. va_end(argp);
  203. return msg;
  204. }
  205. /* number of chars of a literal string without the ending \0 */
  206. #define LL(x) (sizeof(x)/sizeof(char) - 1)
  207. #define RETS "..."
  208. #define PRE "[string \""
  209. #define POS "\"]"
  210. #define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
  211. void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  212. size_t l = strlen(source);
  213. if (*source == '=') { /* 'literal' source */
  214. if (l <= bufflen) /* small enough? */
  215. memcpy(out, source + 1, l * sizeof(char));
  216. else { /* truncate it */
  217. addstr(out, source + 1, bufflen - 1);
  218. *out = '\0';
  219. }
  220. }
  221. else if (*source == '@') { /* file name */
  222. if (l <= bufflen) /* small enough? */
  223. memcpy(out, source + 1, l * sizeof(char));
  224. else { /* add '...' before rest of name */
  225. addstr(out, RETS, LL(RETS));
  226. bufflen -= LL(RETS);
  227. memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
  228. }
  229. }
  230. else { /* string; format as [string "source"] */
  231. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  232. addstr(out, PRE, LL(PRE)); /* add prefix */
  233. bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
  234. if (l < bufflen && nl == NULL) { /* small one-line source? */
  235. addstr(out, source, l); /* keep it */
  236. }
  237. else {
  238. if (nl != NULL) l = nl - source; /* stop at first newline */
  239. if (l > bufflen) l = bufflen;
  240. addstr(out, source, l);
  241. addstr(out, RETS, LL(RETS));
  242. }
  243. memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
  244. }
  245. }