lobject.c 4.9 KB

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