lobject.c 5.3 KB

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