lobject.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. ** $Id: lobject.c,v 2.26 2007/11/09 18:54:25 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 (unsigned int 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_rawequalObj (const TValue *t1, const TValue *t2) {
  43. if (ttype(t1) != ttype(t2)) return 0;
  44. else switch (ttype(t1)) {
  45. case LUA_TNIL:
  46. return 1;
  47. case LUA_TNUMBER:
  48. return luai_numeq(nvalue(t1), nvalue(t2));
  49. case LUA_TBOOLEAN:
  50. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  51. case LUA_TLIGHTUSERDATA:
  52. return pvalue(t1) == pvalue(t2);
  53. default:
  54. lua_assert(iscollectable(t1));
  55. return gcvalue(t1) == gcvalue(t2);
  56. }
  57. }
  58. int luaO_str2d (const char *s, lua_Number *result) {
  59. char *endptr;
  60. *result = lua_str2number(s, &endptr);
  61. if (endptr == s) return 0; /* conversion failed */
  62. if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
  63. *result = cast_num(strtoul(s, &endptr, 16));
  64. if (*endptr == '\0') return 1; /* most common case */
  65. while (isspace(cast(unsigned char, *endptr))) endptr++;
  66. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  67. return 1;
  68. }
  69. static void pushstr (lua_State *L, const char *str) {
  70. setsvalue2s(L, L->top, luaS_new(L, str));
  71. incr_top(L);
  72. }
  73. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  74. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  75. int n = 1;
  76. pushstr(L, "");
  77. for (;;) {
  78. const char *e = strchr(fmt, '%');
  79. if (e == NULL) break;
  80. setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
  81. incr_top(L);
  82. switch (*(e+1)) {
  83. case 's': {
  84. const char *s = va_arg(argp, char *);
  85. if (s == NULL) s = "(null)";
  86. pushstr(L, s);
  87. break;
  88. }
  89. case 'c': {
  90. char buff[2];
  91. buff[0] = cast(char, va_arg(argp, int));
  92. buff[1] = '\0';
  93. pushstr(L, buff);
  94. break;
  95. }
  96. case 'd': {
  97. setnvalue(L->top, cast_num(va_arg(argp, int)));
  98. incr_top(L);
  99. break;
  100. }
  101. case 'f': {
  102. setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
  103. incr_top(L);
  104. break;
  105. }
  106. case 'p': {
  107. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  108. sprintf(buff, "%p", va_arg(argp, void *));
  109. pushstr(L, buff);
  110. break;
  111. }
  112. case '%': {
  113. pushstr(L, "%");
  114. break;
  115. }
  116. default: {
  117. luaG_runerror(L,
  118. "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
  119. *(e + 1));
  120. break;
  121. }
  122. }
  123. n += 2;
  124. fmt = e+2;
  125. }
  126. pushstr(L, fmt);
  127. luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
  128. L->top -= n;
  129. return svalue(L->top - 1);
  130. }
  131. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  132. const char *msg;
  133. va_list argp;
  134. va_start(argp, fmt);
  135. msg = luaO_pushvfstring(L, fmt, argp);
  136. va_end(argp);
  137. return msg;
  138. }
  139. #define LL(x) (sizeof(x) - 1)
  140. #define RETS "..."
  141. #define PRE "[string \""
  142. #define POS "\"]"
  143. #define addstr(a,b,l) ( memcpy(a,b,l), a += (l) )
  144. void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  145. size_t l = strlen(source);
  146. if (*source == '=') { /* 'literal' source */
  147. if (l <= bufflen) /* small enough? */
  148. memcpy(out, source + 1, l);
  149. else { /* truncate it */
  150. addstr(out, source + 1, bufflen - 1);
  151. *out = '\0';
  152. }
  153. }
  154. else if (*source == '@') { /* file name */
  155. if (l <= bufflen) /* small enough? */
  156. memcpy(out, source + 1, l);
  157. else { /* add '...' before rest of name */
  158. addstr(out, RETS, LL(RETS));
  159. bufflen -= LL(RETS);
  160. memcpy(out, source + 1 + l - bufflen, bufflen);
  161. }
  162. }
  163. else { /* string; format as [string "source"] */
  164. const char *nl = strchr(source, '\n'); /* find first new line (if any) */
  165. addstr(out, PRE, LL(PRE)); /* add prefix */
  166. bufflen -= LL(PRE RETS POS); /* save space for prefix+sufix */
  167. if (l < bufflen && nl == NULL) { /* small one-line source? */
  168. addstr(out, source, l); /* keep it */
  169. }
  170. else {
  171. if (nl != NULL) l = nl - source; /* stop at first newline */
  172. if (l > bufflen) l = bufflen;
  173. addstr(out, source, l);
  174. addstr(out, RETS, LL(RETS));
  175. }
  176. memcpy(out, POS, LL(POS) + 1);
  177. }
  178. }