lobject.c 8.2 KB

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