lobject.c 6.1 KB

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