lobject.c 4.9 KB

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