/lua/src/lobject.c

https://github.com/Fleurer/lua_5.1.4_commentary · C · 214 lines · 175 code · 26 blank · 13 comment · 41 complexity · f265ed7accf6cc3a0559bfe20c3add73 MD5 · raw file

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