2
0

lobject.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. ** $Id: lobject.c,v 2.1 2003/12/10 12:13:36 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, 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. case 'c': {
  90. char buff[2];
  91. buff[0] = cast(char, va_arg(argp, int));
  92. buff[1] = '\0';
  93. pushstr(L, buff);
  94. break;
  95. }
  96. case 'd':
  97. setnvalue(L->top, cast(lua_Number, va_arg(argp, int)));
  98. incr_top(L);
  99. break;
  100. case 'f':
  101. setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber)));
  102. incr_top(L);
  103. break;
  104. case '%':
  105. pushstr(L, "%");
  106. break;
  107. default: {
  108. char buff[3];
  109. buff[0] = '%';
  110. buff[1] = *(e+1);
  111. buff[2] = '\0';
  112. pushstr(L, buff);
  113. break;
  114. }
  115. }
  116. n += 2;
  117. fmt = e+2;
  118. }
  119. pushstr(L, fmt);
  120. luaV_concat(L, n+1, L->top - L->base - 1);
  121. L->top -= n;
  122. return svalue(L->top - 1);
  123. }
  124. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  125. const char *msg;
  126. va_list argp;
  127. va_start(argp, fmt);
  128. msg = luaO_pushvfstring(L, fmt, argp);
  129. va_end(argp);
  130. return msg;
  131. }
  132. void luaO_chunkid (char *out, const char *source, int bufflen) {
  133. if (*source == '=') {
  134. strncpy(out, source+1, bufflen); /* remove first char */
  135. out[bufflen-1] = '\0'; /* ensures null termination */
  136. }
  137. else { /* out = "source", or "...source" */
  138. if (*source == '@') {
  139. int l;
  140. source++; /* skip the `@' */
  141. bufflen -= sizeof(" `...' ");
  142. l = strlen(source);
  143. strcpy(out, "");
  144. if (l>bufflen) {
  145. source += (l-bufflen); /* get last part of file name */
  146. strcat(out, "...");
  147. }
  148. strcat(out, source);
  149. }
  150. else { /* out = [string "string"] */
  151. int len = strcspn(source, "\n"); /* stop at first newline */
  152. bufflen -= sizeof(" [string \"...\"] ");
  153. if (len > bufflen) len = bufflen;
  154. strcpy(out, "[string \"");
  155. if (source[len] != '\0') { /* must truncate? */
  156. strncat(out, source, len);
  157. strcat(out, "...");
  158. }
  159. else
  160. strcat(out, source);
  161. strcat(out, "\"]");
  162. }
  163. }
  164. }