2
0

lobject.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. ** $Id: lobject.c,v 2.6 2004/11/01 15:06:50 roberto Exp roberto $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdarg.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define lobject_c
  11. #define LUA_CORE
  12. #include "lua.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. const TValue luaO_nilobject = {{NULL}, LUA_TNIL};
  20. /*
  21. ** converts an integer to a "floating point byte", represented as
  22. ** (mmmmmxxx), where the real value is (xxx) * 2^(mmmmm)
  23. */
  24. int luaO_int2fb (unsigned int x) {
  25. int e = 0; /* expoent */
  26. while (x >= 16) {
  27. x = (x+1) >> 1;
  28. e++;
  29. }
  30. if (x < 8) return x;
  31. else return ((e+1) << 3) | (cast(int, x) - 8);
  32. }
  33. /* converts back */
  34. int luaO_fb2int (int x) {
  35. int e = (x >> 3) & 31;
  36. if (e == 0) return x;
  37. else return ((x & 7)+8) << (e - 1);
  38. }
  39. int luaO_log2 (unsigned int x) {
  40. static const lu_byte log_2[256] = {
  41. 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,
  42. 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,
  43. 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,
  44. 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,
  45. 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,
  46. 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,
  47. 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,
  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. };
  50. int l = -1;
  51. while (x >= 256) { l += 8; x >>= 8; }
  52. return l + log_2[x];
  53. }
  54. int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
  55. if (ttype(t1) != ttype(t2)) return 0;
  56. else switch (ttype(t1)) {
  57. case LUA_TNIL:
  58. return 1;
  59. case LUA_TNUMBER:
  60. return nvalue(t1) == nvalue(t2);
  61. case LUA_TBOOLEAN:
  62. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  63. case LUA_TLIGHTUSERDATA:
  64. return pvalue(t1) == pvalue(t2);
  65. default:
  66. lua_assert(iscollectable(t1));
  67. return gcvalue(t1) == gcvalue(t2);
  68. }
  69. }
  70. int luaO_str2d (const char *s, lua_Number *result) {
  71. char *endptr;
  72. lua_Number res = lua_str2number(s, &endptr);
  73. if (endptr == s) return 0; /* no conversion */
  74. while (isspace(cast(unsigned char, *endptr))) endptr++;
  75. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  76. *result = res;
  77. return 1;
  78. }
  79. static void pushstr (lua_State *L, const char *str) {
  80. setsvalue2s(L, L->top, luaS_new(L, str));
  81. incr_top(L);
  82. }
  83. /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
  84. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  85. int n = 1;
  86. pushstr(L, "");
  87. for (;;) {
  88. const char *e = strchr(fmt, '%');
  89. if (e == NULL) break;
  90. setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
  91. incr_top(L);
  92. switch (*(e+1)) {
  93. case 's': {
  94. pushstr(L, va_arg(argp, char *));
  95. break;
  96. }
  97. case 'c': {
  98. char buff[2];
  99. buff[0] = cast(char, va_arg(argp, int));
  100. buff[1] = '\0';
  101. pushstr(L, buff);
  102. break;
  103. }
  104. case 'd': {
  105. setnvalue(L->top, cast(lua_Number, va_arg(argp, int)));
  106. incr_top(L);
  107. break;
  108. }
  109. case 'f': {
  110. setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber)));
  111. incr_top(L);
  112. break;
  113. }
  114. case 'p': {
  115. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  116. sprintf(buff, "%p", va_arg(argp, void *));
  117. pushstr(L, buff);
  118. break;
  119. }
  120. case '%': {
  121. pushstr(L, "%");
  122. break;
  123. }
  124. default: {
  125. char buff[3];
  126. buff[0] = '%';
  127. buff[1] = *(e+1);
  128. buff[2] = '\0';
  129. pushstr(L, buff);
  130. break;
  131. }
  132. }
  133. n += 2;
  134. fmt = e+2;
  135. }
  136. pushstr(L, fmt);
  137. luaV_concat(L, n+1, L->top - L->base - 1);
  138. L->top -= n;
  139. return svalue(L->top - 1);
  140. }
  141. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  142. const char *msg;
  143. va_list argp;
  144. va_start(argp, fmt);
  145. msg = luaO_pushvfstring(L, fmt, argp);
  146. va_end(argp);
  147. return msg;
  148. }
  149. void luaO_chunkid (char *out, const char *source, int bufflen) {
  150. if (*source == '=') {
  151. strncpy(out, source+1, bufflen); /* remove first char */
  152. out[bufflen-1] = '\0'; /* ensures null termination */
  153. }
  154. else { /* out = "source", or "...source" */
  155. if (*source == '@') {
  156. int l;
  157. source++; /* skip the `@' */
  158. bufflen -= sizeof(" `...' ");
  159. l = strlen(source);
  160. strcpy(out, "");
  161. if (l>bufflen) {
  162. source += (l-bufflen); /* get last part of file name */
  163. strcat(out, "...");
  164. }
  165. strcat(out, source);
  166. }
  167. else { /* out = [string "string"] */
  168. int len = strcspn(source, "\n\r"); /* stop at first newline */
  169. bufflen -= sizeof(" [string \"...\"] ");
  170. if (len > bufflen) len = bufflen;
  171. strcpy(out, "[string \"");
  172. if (source[len] != '\0') { /* must truncate? */
  173. strncat(out, source, len);
  174. strcat(out, "...");
  175. }
  176. else
  177. strcat(out, source);
  178. strcat(out, "\"]");
  179. }
  180. }
  181. }