lobject.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. ** $Id: lobject.c,v 2.11 2005/03/09 16:28:07 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(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. lua_Number res = lua_str2number(s, &endptr);
  75. if (endptr == s) return 0; /* no conversion */
  76. while (isspace(cast(unsigned char, *endptr))) endptr++;
  77. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  78. *result = res;
  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. pushstr(L, va_arg(argp, char *));
  97. break;
  98. }
  99. case 'c': {
  100. char buff[2];
  101. buff[0] = cast(char, va_arg(argp, int));
  102. buff[1] = '\0';
  103. pushstr(L, buff);
  104. break;
  105. }
  106. case 'd': {
  107. setnvalue(L->top, cast(lua_Number, va_arg(argp, int)));
  108. incr_top(L);
  109. break;
  110. }
  111. case 'f': {
  112. setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber)));
  113. incr_top(L);
  114. break;
  115. }
  116. case 'p': {
  117. char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
  118. sprintf(buff, "%p", va_arg(argp, void *));
  119. pushstr(L, buff);
  120. break;
  121. }
  122. case '%': {
  123. pushstr(L, "%");
  124. break;
  125. }
  126. default: {
  127. char buff[3];
  128. buff[0] = '%';
  129. buff[1] = *(e+1);
  130. buff[2] = '\0';
  131. pushstr(L, buff);
  132. break;
  133. }
  134. }
  135. n += 2;
  136. fmt = e+2;
  137. }
  138. pushstr(L, fmt);
  139. luaV_concat(L, n+1, L->top - L->base - 1);
  140. L->top -= n;
  141. return svalue(L->top - 1);
  142. }
  143. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  144. const char *msg;
  145. va_list argp;
  146. va_start(argp, fmt);
  147. msg = luaO_pushvfstring(L, fmt, argp);
  148. va_end(argp);
  149. return msg;
  150. }
  151. void luaO_chunkid (char *out, const char *source, int bufflen) {
  152. if (*source == '=') {
  153. strncpy(out, source+1, bufflen); /* remove first char */
  154. out[bufflen-1] = '\0'; /* ensures null termination */
  155. }
  156. else { /* out = "source", or "...source" */
  157. if (*source == '@') {
  158. int l;
  159. source++; /* skip the `@' */
  160. bufflen -= sizeof(" `...' ");
  161. l = strlen(source);
  162. strcpy(out, "");
  163. if (l>bufflen) {
  164. source += (l-bufflen); /* get last part of file name */
  165. strcat(out, "...");
  166. }
  167. strcat(out, source);
  168. }
  169. else { /* out = [string "string"] */
  170. int len = strcspn(source, "\n\r"); /* stop at first newline */
  171. bufflen -= sizeof(" [string \"...\"] ");
  172. if (len > bufflen) len = bufflen;
  173. strcpy(out, "[string \"");
  174. if (source[len] != '\0') { /* must truncate? */
  175. strncat(out, source, len);
  176. strcat(out, "...");
  177. }
  178. else
  179. strcat(out, source);
  180. strcat(out, "\"]");
  181. }
  182. }
  183. }