lobject.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. ** $Id: lobject.c,v 2.27 2007/12/19 17:24:38 roberto Exp roberto $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #define lobject_c
  12. #define LUA_CORE
  13. #include "lua.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_ = {{NULL}, LUA_TNIL};
  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. int luaO_str2d (const char *s, lua_Number *result) {
  75. char *endptr;
  76. *result = lua_str2number(s, &endptr);
  77. if (endptr == s) return 0; /* conversion failed */
  78. if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
  79. *result = cast_num(strtoul(s, &endptr, 16));
  80. if (*endptr == '\0') return 1; /* most common case */
  81. while (isspace(cast(unsigned char, *endptr))) endptr++;
  82. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  83. return 1;
  84. }
  85. static void pushstr (lua_State *L, const char *str) {
  86. setsvalue2s(L, L->top, luaS_new(L, str));
  87. incr_top(L);
  88. }
  89. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  90. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  91. int n = 1;
  92. pushstr(L, "");
  93. for (;;) {
  94. const char *e = strchr(fmt, '%');
  95. if (e == NULL) break;
  96. setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
  97. incr_top(L);
  98. switch (*(e+1)) {
  99. case 's': {
  100. const char *s = va_arg(argp, char *);
  101. if (s == NULL) s = "(null)";
  102. pushstr(L, s);
  103. break;
  104. }
  105. case 'c': {
  106. char buff[2];
  107. buff[0] = cast(char, va_arg(argp, int));
  108. buff[1] = '\0';
  109. pushstr(L, buff);
  110. break;
  111. }
  112. case 'd': {
  113. setnvalue(L->top, cast_num(va_arg(argp, int)));
  114. incr_top(L);
  115. break;
  116. }
  117. case 'f': {
  118. setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
  119. incr_top(L);
  120. break;
  121. }
  122. case 'p': {
  123. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  124. sprintf(buff, "%p", va_arg(argp, void *));
  125. pushstr(L, buff);
  126. break;
  127. }
  128. case '%': {
  129. pushstr(L, "%");
  130. break;
  131. }
  132. default: {
  133. luaG_runerror(L,
  134. "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
  135. *(e + 1));
  136. break;
  137. }
  138. }
  139. n += 2;
  140. fmt = e+2;
  141. }
  142. pushstr(L, fmt);
  143. luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
  144. L->top -= n;
  145. return svalue(L->top - 1);
  146. }
  147. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  148. const char *msg;
  149. va_list argp;
  150. va_start(argp, fmt);
  151. msg = luaO_pushvfstring(L, fmt, argp);
  152. va_end(argp);
  153. return msg;
  154. }
  155. #define LL(x) (sizeof(x) - 1)
  156. #define RETS "..."
  157. #define PRE "[string \""
  158. #define POS "\"]"
  159. #define addstr(a,b,l) ( memcpy(a,b,l), a += (l) )
  160. void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  161. size_t l = strlen(source);
  162. if (*source == '=') { /* 'literal' source */
  163. if (l <= bufflen) /* small enough? */
  164. memcpy(out, source + 1, l);
  165. else { /* truncate it */
  166. addstr(out, source + 1, bufflen - 1);
  167. *out = '\0';
  168. }
  169. }
  170. else if (*source == '@') { /* file name */
  171. if (l <= bufflen) /* small enough? */
  172. memcpy(out, source + 1, l);
  173. else { /* add '...' before rest of name */
  174. addstr(out, RETS, LL(RETS));
  175. bufflen -= LL(RETS);
  176. memcpy(out, source + 1 + l - bufflen, bufflen);
  177. }
  178. }
  179. else { /* string; format as [string "source"] */
  180. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  181. addstr(out, PRE, LL(PRE)); /* add prefix */
  182. bufflen -= LL(PRE RETS POS); /* save space for prefix+sufix */
  183. if (l < bufflen && nl == NULL) { /* small one-line source? */
  184. addstr(out, source, l); /* keep it */
  185. }
  186. else {
  187. if (nl != NULL) l = nl - source; /* stop at first newline */
  188. if (l > bufflen) l = bufflen;
  189. addstr(out, source, l);
  190. addstr(out, RETS, LL(RETS));
  191. }
  192. memcpy(out, POS, LL(POS) + 1);
  193. }
  194. }