lobject.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. ** $Id: lobject.c,v 2.31 2009/06/17 17:48:34 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. 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. default:
  70. lua_assert(iscollectable(t1));
  71. return gcvalue(t1) == gcvalue(t2);
  72. }
  73. }
  74. lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
  75. switch (op) {
  76. case LUA_OPADD: return luai_numadd(NULL, v1, v2);
  77. case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
  78. case LUA_OPMUL: return luai_nummul(NULL, v1, v2);
  79. case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);
  80. case LUA_OPMOD: return luai_nummod(NULL, v1, v2);
  81. case LUA_OPPOW: return luai_numpow(NULL, v1, v2);
  82. case LUA_OPUNM: return luai_numunm(N, v1);
  83. default: lua_assert(0); return 0;
  84. }
  85. }
  86. int luaO_str2d (const char *s, lua_Number *result) {
  87. char *endptr;
  88. *result = lua_str2number(s, &endptr);
  89. if (endptr == s) return 0; /* conversion failed */
  90. if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
  91. *result = cast_num(strtoul(s, &endptr, 16));
  92. if (*endptr == '\0') return 1; /* most common case */
  93. while (lisspace(cast(unsigned char, *endptr))) endptr++;
  94. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  95. return 1;
  96. }
  97. static void pushstr (lua_State *L, const char *str) {
  98. setsvalue2s(L, L->top, luaS_new(L, str));
  99. incr_top(L);
  100. }
  101. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  102. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  103. int n = 1;
  104. pushstr(L, "");
  105. for (;;) {
  106. const char *e = strchr(fmt, '%');
  107. if (e == NULL) break;
  108. setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
  109. incr_top(L);
  110. switch (*(e+1)) {
  111. case 's': {
  112. const char *s = va_arg(argp, char *);
  113. if (s == NULL) s = "(null)";
  114. pushstr(L, s);
  115. break;
  116. }
  117. case 'c': {
  118. char buff[2];
  119. buff[0] = cast(char, va_arg(argp, int));
  120. buff[1] = '\0';
  121. pushstr(L, buff);
  122. break;
  123. }
  124. case 'd': {
  125. setnvalue(L->top, cast_num(va_arg(argp, int)));
  126. incr_top(L);
  127. break;
  128. }
  129. case 'f': {
  130. setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
  131. incr_top(L);
  132. break;
  133. }
  134. case 'p': {
  135. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  136. sprintf(buff, "%p", va_arg(argp, void *));
  137. pushstr(L, buff);
  138. break;
  139. }
  140. case '%': {
  141. pushstr(L, "%");
  142. break;
  143. }
  144. default: {
  145. luaG_runerror(L,
  146. "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
  147. *(e + 1));
  148. break;
  149. }
  150. }
  151. n += 2;
  152. fmt = e+2;
  153. }
  154. pushstr(L, fmt);
  155. luaV_concat(L, n+1);
  156. return svalue(L->top - 1);
  157. }
  158. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  159. const char *msg;
  160. va_list argp;
  161. va_start(argp, fmt);
  162. msg = luaO_pushvfstring(L, fmt, argp);
  163. va_end(argp);
  164. return msg;
  165. }
  166. #define LL(x) (sizeof(x) - 1)
  167. #define RETS "..."
  168. #define PRE "[string \""
  169. #define POS "\"]"
  170. #define addstr(a,b,l) ( memcpy(a,b,l), a += (l) )
  171. void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  172. size_t l = strlen(source);
  173. if (*source == '=') { /* 'literal' source */
  174. if (l <= bufflen) /* small enough? */
  175. memcpy(out, source + 1, l);
  176. else { /* truncate it */
  177. addstr(out, source + 1, bufflen - 1);
  178. *out = '\0';
  179. }
  180. }
  181. else if (*source == '@') { /* file name */
  182. if (l <= bufflen) /* small enough? */
  183. memcpy(out, source + 1, l);
  184. else { /* add '...' before rest of name */
  185. addstr(out, RETS, LL(RETS));
  186. bufflen -= LL(RETS);
  187. memcpy(out, source + 1 + l - bufflen, bufflen);
  188. }
  189. }
  190. else { /* string; format as [string "source"] */
  191. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  192. addstr(out, PRE, LL(PRE)); /* add prefix */
  193. bufflen -= LL(PRE RETS POS); /* save space for prefix+sufix */
  194. if (l < bufflen && nl == NULL) { /* small one-line source? */
  195. addstr(out, source, l); /* keep it */
  196. }
  197. else {
  198. if (nl != NULL) l = nl - source; /* stop at first newline */
  199. if (l > bufflen) l = bufflen;
  200. addstr(out, source, l);
  201. addstr(out, RETS, LL(RETS));
  202. }
  203. memcpy(out, POS, LL(POS) + 1);
  204. }
  205. }