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