lobject.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. ** $Id: lobject.c,v 2.43 2010/10/29 15:54:55 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. static int checkend (const char *s, const char *e, const char *endptr) {
  91. if (endptr == s) return 0; /* no characters converted */
  92. while (lisspace(cast(unsigned char, *endptr))) endptr++;
  93. return (endptr == e); /* OK if no trailing characters */
  94. }
  95. int luaO_str2d (const char *s, size_t len, lua_Number *result) {
  96. char *endptr;
  97. const char *e = s + len; /* string 's' ends here */
  98. *result = lua_str2number(s, &endptr);
  99. if (checkend(s, e, endptr)) return 1; /* conversion OK? */
  100. *result = cast_num(strtoul(s, &endptr, 0)); /* try hexadecimal */
  101. return checkend(s, e, endptr);
  102. }
  103. static void pushstr (lua_State *L, const char *str, size_t l) {
  104. setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
  105. incr_top(L);
  106. }
  107. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  108. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  109. int n = 0;
  110. for (;;) {
  111. const char *e = strchr(fmt, '%');
  112. if (e == NULL) break;
  113. setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
  114. incr_top(L);
  115. switch (*(e+1)) {
  116. case 's': {
  117. const char *s = va_arg(argp, char *);
  118. if (s == NULL) s = "(null)";
  119. pushstr(L, s, strlen(s));
  120. break;
  121. }
  122. case 'c': {
  123. char buff;
  124. buff = cast(char, va_arg(argp, int));
  125. pushstr(L, &buff, 1);
  126. break;
  127. }
  128. case 'd': {
  129. setnvalue(L->top, cast_num(va_arg(argp, int)));
  130. incr_top(L);
  131. break;
  132. }
  133. case 'f': {
  134. setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
  135. incr_top(L);
  136. break;
  137. }
  138. case 'p': {
  139. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  140. int l = sprintf(buff, "%p", va_arg(argp, void *));
  141. pushstr(L, buff, l);
  142. break;
  143. }
  144. case '%': {
  145. pushstr(L, "%", 1);
  146. break;
  147. }
  148. default: {
  149. luaG_runerror(L,
  150. "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
  151. *(e + 1));
  152. break;
  153. }
  154. }
  155. n += 2;
  156. fmt = e+2;
  157. }
  158. pushstr(L, fmt, strlen(fmt));
  159. if (n > 0) luaV_concat(L, n + 1);
  160. return svalue(L->top - 1);
  161. }
  162. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  163. const char *msg;
  164. va_list argp;
  165. va_start(argp, fmt);
  166. msg = luaO_pushvfstring(L, fmt, argp);
  167. va_end(argp);
  168. return msg;
  169. }
  170. #define LL(x) (sizeof(x) - 1)
  171. #define RETS "..."
  172. #define PRE "[string \""
  173. #define POS "\"]"
  174. #define addstr(a,b,l) ( memcpy(a,b,l), a += (l) )
  175. void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  176. size_t l = strlen(source);
  177. if (*source == '=') { /* 'literal' source */
  178. if (l <= bufflen) /* small enough? */
  179. memcpy(out, source + 1, l);
  180. else { /* truncate it */
  181. addstr(out, source + 1, bufflen - 1);
  182. *out = '\0';
  183. }
  184. }
  185. else if (*source == '@') { /* file name */
  186. if (l <= bufflen) /* small enough? */
  187. memcpy(out, source + 1, l);
  188. else { /* add '...' before rest of name */
  189. addstr(out, RETS, LL(RETS));
  190. bufflen -= LL(RETS);
  191. memcpy(out, source + 1 + l - bufflen, bufflen);
  192. }
  193. }
  194. else { /* string; format as [string "source"] */
  195. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  196. addstr(out, PRE, LL(PRE)); /* add prefix */
  197. bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */
  198. if (l < bufflen && nl == NULL) { /* small one-line source? */
  199. addstr(out, source, l); /* keep it */
  200. }
  201. else {
  202. if (nl != NULL) l = nl - source; /* stop at first newline */
  203. if (l > bufflen) l = bufflen;
  204. addstr(out, source, l);
  205. addstr(out, RETS, LL(RETS));
  206. }
  207. memcpy(out, POS, LL(POS) + 1);
  208. }
  209. }