lobject.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #endif
  22. const TValue luaO_nilobject_ = {LUA_TVALUE_NIL};
  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] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR = {
  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. #ifdef LUA_CROSS_COMPILER
  57. return l + log_2[x];
  58. #else
  59. return l + byte_of_aligned_array(log_2,x);
  60. #endif
  61. }
  62. int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
  63. if (ttype(t1) != ttype(t2)) return 0;
  64. else switch (ttype(t1)) {
  65. case LUA_TNIL:
  66. return 1;
  67. case LUA_TNUMBER:
  68. return luai_numeq(nvalue(t1), nvalue(t2));
  69. case LUA_TBOOLEAN:
  70. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  71. case LUA_TLIGHTUSERDATA:
  72. case LUA_TROTABLE:
  73. case LUA_TLIGHTFUNCTION:
  74. return pvalue(t1) == pvalue(t2);
  75. default:
  76. lua_assert(iscollectable(t1));
  77. return gcvalue(t1) == gcvalue(t2);
  78. }
  79. }
  80. int luaO_str2d (const char *s, lua_Number *result) {
  81. char *endptr;
  82. *result = lua_str2number(s, &endptr);
  83. if (endptr == s) return 0; /* conversion failed */
  84. if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
  85. *result = cast_num(c_strtoul(s, &endptr, 16));
  86. if (*endptr == '\0') return 1; /* most common case */
  87. while (isspace(cast(unsigned char, *endptr))) endptr++;
  88. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  89. return 1;
  90. }
  91. static void pushstr (lua_State *L, const char *str) {
  92. setsvalue2s(L, L->top, luaS_new(L, str));
  93. incr_top(L);
  94. }
  95. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  96. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  97. int n = 1;
  98. pushstr(L, "");
  99. for (;;) {
  100. const char *e = c_strchr(fmt, '%');
  101. if (e == NULL) break;
  102. setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
  103. incr_top(L);
  104. switch (*(e+1)) {
  105. case 's': {
  106. const char *s = va_arg(argp, char *);
  107. if (s == NULL) s = "(null)";
  108. pushstr(L, s);
  109. break;
  110. }
  111. case 'c': {
  112. char buff[2];
  113. buff[0] = cast(char, va_arg(argp, int));
  114. buff[1] = '\0';
  115. pushstr(L, buff);
  116. break;
  117. }
  118. case 'd': {
  119. setnvalue(L->top, cast_num(va_arg(argp, int)));
  120. incr_top(L);
  121. break;
  122. }
  123. case 'f': {
  124. setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
  125. incr_top(L);
  126. break;
  127. }
  128. case 'p': {
  129. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  130. c_sprintf(buff, "%p", va_arg(argp, void *));
  131. pushstr(L, buff);
  132. break;
  133. }
  134. case '%': {
  135. pushstr(L, "%");
  136. break;
  137. }
  138. default: {
  139. char buff[3];
  140. buff[0] = '%';
  141. buff[1] = *(e+1);
  142. buff[2] = '\0';
  143. pushstr(L, buff);
  144. break;
  145. }
  146. }
  147. n += 2;
  148. fmt = e+2;
  149. }
  150. pushstr(L, fmt);
  151. luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
  152. L->top -= n;
  153. return svalue(L->top - 1);
  154. }
  155. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  156. const char *msg;
  157. va_list argp;
  158. va_start(argp, fmt);
  159. msg = luaO_pushvfstring(L, fmt, argp);
  160. va_end(argp);
  161. return msg;
  162. }
  163. void luaO_chunkid (char *out, const char *source, size_t bufflen) {
  164. if (*source == '=') {
  165. c_strncpy(out, source+1, bufflen); /* remove first char */
  166. out[bufflen-1] = '\0'; /* ensures null termination */
  167. }
  168. else { /* out = "source", or "...source" */
  169. if (*source == '@') {
  170. size_t l;
  171. source++; /* skip the `@' */
  172. bufflen -= sizeof(" '...' ");
  173. l = c_strlen(source);
  174. c_strcpy(out, "");
  175. if (l > bufflen) {
  176. source += (l-bufflen); /* get last part of file name */
  177. c_strcat(out, "...");
  178. }
  179. c_strcat(out, source);
  180. }
  181. else { /* out = [string "string"] */
  182. size_t len = c_strcspn(source, "\n\r"); /* stop at first newline */
  183. bufflen -= sizeof(" [string \"...\"] ");
  184. if (len > bufflen) len = bufflen;
  185. c_strcpy(out, "[string \"");
  186. if (source[len] != '\0') { /* must truncate? */
  187. c_strncat(out, source, len);
  188. c_strcat(out, "...");
  189. }
  190. else
  191. c_strcat(out, source);
  192. c_strcat(out, "\"]");
  193. }
  194. }
  195. }