lobject.c 4.6 KB

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