lobject.c 6.4 KB

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