lobject.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. ** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $
  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 "ldo.h"
  15. #include "lmem.h"
  16. #include "lobject.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "lvm.h"
  20. const TValue luaO_nilobject_ = {LUA_TVALUE_NIL};
  21. /*
  22. ** converts an integer to a "floating point byte", represented as
  23. ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
  24. ** eeeee != 0 and (xxx) otherwise.
  25. */
  26. int luaO_int2fb(unsigned int x)
  27. {
  28. int e = 0; /* expoent */
  29. while (x >= 16)
  30. {
  31. x = (x + 1) >> 1;
  32. e++;
  33. }
  34. if (x < 8) return x;
  35. else return ((e + 1) << 3) | (cast_int(x) - 8);
  36. }
  37. /* converts back */
  38. int luaO_fb2int(int x)
  39. {
  40. int e = (x >> 3) & 31;
  41. if (e == 0) return x;
  42. else return ((x & 7) + 8) << (e - 1);
  43. }
  44. int luaO_log2(unsigned int x)
  45. {
  46. static const lu_byte log_2[256] =
  47. {
  48. 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,
  49. 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,
  50. 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,
  51. 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,
  52. 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,
  53. 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,
  54. 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,
  55. 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
  56. };
  57. int l = -1;
  58. while (x >= 256)
  59. {
  60. l += 8;
  61. x >>= 8;
  62. }
  63. return l + log_2[x];
  64. }
  65. int luaO_rawequalObj(const TValue *t1, const TValue *t2)
  66. {
  67. if (ttype(t1) != ttype(t2)) return 0;
  68. else switch (ttype(t1))
  69. {
  70. case LUA_TNIL:
  71. return 1;
  72. case LUA_TNUMBER:
  73. return luai_numeq(nvalue(t1), nvalue(t2));
  74. case LUA_TBOOLEAN:
  75. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  76. case LUA_TLIGHTUSERDATA:
  77. case LUA_TROTABLE:
  78. case LUA_TLIGHTFUNCTION:
  79. return pvalue(t1) == pvalue(t2);
  80. default:
  81. lua_assert(iscollectable(t1));
  82. return gcvalue(t1) == gcvalue(t2);
  83. }
  84. }
  85. int luaO_str2d(const char *s, lua_Number *result)
  86. {
  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 (isspace(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. {
  99. setsvalue2s(L, L->top, luaS_new(L, str));
  100. incr_top(L);
  101. }
  102. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  103. const char *luaO_pushvfstring(lua_State *L, const char *fmt, va_list argp)
  104. {
  105. int n = 1;
  106. pushstr(L, "");
  107. for (;;)
  108. {
  109. const char *e = strchr(fmt, '%');
  110. if (e == NULL) break;
  111. setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e - fmt));
  112. incr_top(L);
  113. switch (*(e + 1))
  114. {
  115. case 's':
  116. {
  117. const char *s = va_arg(argp, char *);
  118. if (s == NULL) s = "(null)";
  119. pushstr(L, s);
  120. break;
  121. }
  122. case 'c':
  123. {
  124. char buff[2];
  125. buff[0] = cast(char, va_arg(argp, int));
  126. buff[1] = '\0';
  127. pushstr(L, buff);
  128. break;
  129. }
  130. case 'd':
  131. {
  132. setnvalue(L->top, cast_num(va_arg(argp, int)));
  133. incr_top(L);
  134. break;
  135. }
  136. case 'f':
  137. {
  138. setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
  139. incr_top(L);
  140. break;
  141. }
  142. case 'p':
  143. {
  144. char buff[4 * sizeof(void *) + 8]; /* should be enough space for a `%p' */
  145. sprintf(buff, "%p", va_arg(argp, void *));
  146. pushstr(L, buff);
  147. break;
  148. }
  149. case '%':
  150. {
  151. pushstr(L, "%");
  152. break;
  153. }
  154. default:
  155. {
  156. char buff[3];
  157. buff[0] = '%';
  158. buff[1] = *(e + 1);
  159. buff[2] = '\0';
  160. pushstr(L, buff);
  161. break;
  162. }
  163. }
  164. n += 2;
  165. fmt = e + 2;
  166. }
  167. pushstr(L, fmt);
  168. luaV_concat(L, n + 1, cast_int(L->top - L->base) - 1);
  169. L->top -= n;
  170. return svalue(L->top - 1);
  171. }
  172. const char *luaO_pushfstring(lua_State *L, const char *fmt, ...)
  173. {
  174. const char *msg;
  175. va_list argp;
  176. va_start(argp, fmt);
  177. msg = luaO_pushvfstring(L, fmt, argp);
  178. va_end(argp);
  179. return msg;
  180. }
  181. void luaO_chunkid(char *out, const char *source, size_t bufflen)
  182. {
  183. if (*source == '=')
  184. {
  185. strncpy(out, source + 1, bufflen); /* remove first char */
  186. out[bufflen - 1] = '\0'; /* ensures null termination */
  187. }
  188. else /* out = "source", or "...source" */
  189. {
  190. if (*source == '@')
  191. {
  192. size_t l;
  193. source++; /* skip the `@' */
  194. bufflen -= sizeof(" '...' ");
  195. l = strlen(source);
  196. strcpy(out, "");
  197. if (l > bufflen)
  198. {
  199. source += (l - bufflen); /* get last part of file name */
  200. strcat(out, "...");
  201. }
  202. strcat(out, source);
  203. }
  204. else /* out = [string "string"] */
  205. {
  206. size_t len = strcspn(source, "\n\r"); /* stop at first newline */
  207. bufflen -= sizeof(" [string \"...\"] ");
  208. if (len > bufflen) len = bufflen;
  209. strcpy(out, "[string \"");
  210. if (source[len] != '\0') /* must truncate? */
  211. {
  212. strncat(out, source, len);
  213. strcat(out, "...");
  214. }
  215. else
  216. strcat(out, source);
  217. strcat(out, "\"]");
  218. }
  219. }
  220. }