lobject.c 4.7 KB

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