lobject.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #define lobject_c
  7. #define LUA_CORE
  8. #define LUAC_CROSS_FILE
  9. #include "lua.h"
  10. #include C_HEADER_STDIO
  11. #include C_HEADER_STRING
  12. #include C_HEADER_STDLIB
  13. #include "ldo.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "lvm.h"
  19. #ifndef LUA_CROSS_COMPILER
  20. #include "flash_api.h"
  21. #else
  22. #include <limits.h>
  23. #endif
  24. const TValue luaO_nilobject_ = {LUA_TVALUE_NIL};
  25. /*
  26. ** converts an integer to a "floating point byte", represented as
  27. ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
  28. ** eeeee != 0 and (xxx) otherwise.
  29. */
  30. int luaO_int2fb (unsigned int x) {
  31. int e = 0; /* expoent */
  32. while (x >= 16) {
  33. x = (x+1) >> 1;
  34. e++;
  35. }
  36. if (x < 8) return x;
  37. else return ((e+1) << 3) | (cast_int(x) - 8);
  38. }
  39. /* converts back */
  40. int luaO_fb2int (int x) {
  41. int e = (x >> 3) & 31;
  42. if (e == 0) return x;
  43. else return ((x & 7)+8) << (e - 1);
  44. }
  45. int luaO_log2 (unsigned int x) {
  46. static const lu_byte log_2[256] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR = {
  47. 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,
  48. 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,
  49. 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,
  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. 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. 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. };
  56. int l = -1;
  57. while (x >= 256) { l += 8; x >>= 8; }
  58. #ifdef LUA_CROSS_COMPILER
  59. return l + log_2[x];
  60. #else
  61. return l + byte_of_aligned_array(log_2,x);
  62. #endif
  63. }
  64. int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
  65. if (ttype(t1) != ttype(t2)) return 0;
  66. else switch (ttype(t1)) {
  67. case LUA_TNIL:
  68. return 1;
  69. case LUA_TNUMBER:
  70. return luai_numeq(nvalue(t1), nvalue(t2));
  71. case LUA_TBOOLEAN:
  72. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  73. case LUA_TLIGHTUSERDATA:
  74. return pvalue(t1) == pvalue(t2);
  75. case LUA_TROTABLE:
  76. return rvalue(t1) == rvalue(t2);
  77. case LUA_TLIGHTFUNCTION:
  78. return fvalue(t1) == fvalue(t2);
  79. default:
  80. lua_assert(iscollectable(t1));
  81. return gcvalue(t1) == gcvalue(t2);
  82. }
  83. }
  84. int luaO_str2d (const char *s, lua_Number *result) {
  85. char *endptr;
  86. *result = lua_str2number(s, &endptr);
  87. if (endptr == s) return 0; /* conversion failed */
  88. if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
  89. #if defined(LUA_CROSS_COMPILER)
  90. {
  91. long lres = strtoul(s, &endptr, 16);
  92. #if LONG_MAX != 2147483647L
  93. if (lres & ~0xffffffffL)
  94. *result = cast_num(-1);
  95. else if (lres & 0x80000000L)
  96. *result = cast_num(lres | ~0x7fffffffL);
  97. else
  98. #endif
  99. *result = cast_num(lres);
  100. }
  101. #else
  102. *result = cast_num(c_strtoul(s, &endptr, 16));
  103. #endif
  104. if (*endptr == '\0') return 1; /* most common case */
  105. while (isspace(cast(unsigned char, *endptr))) endptr++;
  106. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  107. return 1;
  108. }
  109. static void pushstr (lua_State *L, const char *str) {
  110. setsvalue2s(L, L->top, luaS_new(L, str));
  111. incr_top(L);
  112. }
  113. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  114. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  115. int n = 1;
  116. pushstr(L, "");
  117. for (;;) {
  118. const char *e = c_strchr(fmt, '%');
  119. if (e == NULL) break;
  120. setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
  121. incr_top(L);
  122. switch (*(e+1)) {
  123. case 's': {
  124. const char *s = va_arg(argp, char *);
  125. if (s == NULL) s = "(null)";
  126. pushstr(L, s);
  127. break;
  128. }
  129. case 'c': {
  130. char buff[2];
  131. buff[0] = cast(char, va_arg(argp, int));
  132. buff[1] = '\0';
  133. pushstr(L, buff);
  134. break;
  135. }
  136. case 'd': {
  137. setnvalue(L->top, cast_num(va_arg(argp, int)));
  138. incr_top(L);
  139. break;
  140. }
  141. case 'f': {
  142. setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
  143. incr_top(L);
  144. break;
  145. }
  146. case 'p': {
  147. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  148. c_sprintf(buff, "%p", va_arg(argp, void *));
  149. pushstr(L, buff);
  150. break;
  151. }
  152. case '%': {
  153. pushstr(L, "%");
  154. break;
  155. }
  156. default: {
  157. char buff[3];
  158. buff[0] = '%';
  159. buff[1] = *(e+1);
  160. buff[2] = '\0';
  161. pushstr(L, buff);
  162. break;
  163. }
  164. }
  165. n += 2;
  166. fmt = e+2;
  167. }
  168. pushstr(L, fmt);
  169. luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
  170. L->top -= n;
  171. return svalue(L->top - 1);
  172. }
  173. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  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. if (*source == '=') {
  183. c_strncpy(out, source+1, bufflen); /* remove first char */
  184. out[bufflen-1] = '\0'; /* ensures null termination */
  185. }
  186. else { /* out = "source", or "...source" */
  187. if (*source == '@') {
  188. size_t l;
  189. source++; /* skip the `@' */
  190. bufflen -= sizeof(" '...' ");
  191. l = c_strlen(source);
  192. c_strcpy(out, "");
  193. if (l > bufflen) {
  194. source += (l-bufflen); /* get last part of file name */
  195. c_strcat(out, "...");
  196. }
  197. c_strcat(out, source);
  198. }
  199. else { /* out = [string "string"] */
  200. size_t len = c_strcspn(source, "\n\r"); /* stop at first newline */
  201. bufflen -= sizeof(" [string \"...\"] ");
  202. if (len > bufflen) len = bufflen;
  203. c_strcpy(out, "[string \"");
  204. if (source[len] != '\0') { /* must truncate? */
  205. c_strncat(out, source, len);
  206. c_strcat(out, "...");
  207. }
  208. else
  209. c_strcat(out, source);
  210. c_strcat(out, "\"]");
  211. }
  212. }
  213. }