lobject.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #if 0
  7. #include <ctype.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #endif
  13. #define lobject_c
  14. #define LUA_CORE
  15. #include "lua.h"
  16. #include "ldo.h"
  17. #include "lmem.h"
  18. #include "lobject.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "lvm.h"
  22. const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
  23. /*
  24. ** converts an integer to a "floating point byte", represented as
  25. ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
  26. ** eeeee != 0 and (xxx) otherwise.
  27. */
  28. int luaO_int2fb (unsigned int x) {
  29. int e = 0; /* expoent */
  30. while (x >= 16) {
  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. int e = (x >> 3) & 31;
  40. if (e == 0) return x;
  41. else return ((x & 7)+8) << (e - 1);
  42. }
  43. int luaO_log2 (unsigned int x) {
  44. static const lu_byte log_2[256] = {
  45. 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,
  46. 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,
  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. 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,
  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. 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. };
  54. int l = -1;
  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. snprintf(buff, sizeof (buff), "%p", va_arg(argp, void *));
  125. pushstr(L, buff);
  126. break;
  127. }
  128. case '%': {
  129. pushstr(L, "%");
  130. break;
  131. }
  132. default: {
  133. char buff[3];
  134. buff[0] = '%';
  135. buff[1] = *(e+1);
  136. buff[2] = '\0';
  137. pushstr(L, buff);
  138. break;
  139. }
  140. }
  141. n += 2;
  142. fmt = e+2;
  143. }
  144. pushstr(L, fmt);
  145. luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
  146. L->top -= n;
  147. return svalue(L->top - 1);
  148. }
  149. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  150. const char *msg;
  151. va_list argp;
  152. va_start(argp, fmt);
  153. msg = luaO_pushvfstring(L, fmt, argp);
  154. va_end(argp);
  155. return msg;
  156. }
  157. void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  158. if (*source == '=') {
  159. strncpy(out, source+1, bufflen); /* remove first char */
  160. out[bufflen-1] = '\0'; /* ensures null termination */
  161. }
  162. else { /* out = "source", or "...source" */
  163. if (*source == '@') {
  164. size_t l;
  165. source++; /* skip the `@' */
  166. bufflen -= sizeof(" '...' ");
  167. l = strlen(source);
  168. strcpy(out, "");
  169. if (l > bufflen) {
  170. source += (l-bufflen); /* get last part of file name */
  171. strcat(out, "...");
  172. }
  173. strcat(out, source);
  174. }
  175. else { /* out = [string "string"] */
  176. size_t len = strcspn(source, "\n\r"); /* stop at first newline */
  177. bufflen -= sizeof(" [string \"...\"] ");
  178. if (len > bufflen) len = bufflen;
  179. strcpy(out, "[string \"");
  180. if (source[len] != '\0') { /* must truncate? */
  181. strncat(out, source, len);
  182. strcat(out, "...");
  183. }
  184. else
  185. strcat(out, source);
  186. strcat(out, "\"]");
  187. }
  188. }
  189. }