lobject.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. ** $Id: lobject.c,v 1.89 2002/10/04 14:31: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 <stdlib.h>
  9. #include <string.h>
  10. #include "lua.h"
  11. #include "ldo.h"
  12. #include "lmem.h"
  13. #include "lobject.h"
  14. #include "lstate.h"
  15. #include "lstring.h"
  16. #include "lvm.h"
  17. /* function to convert a string to a lua_Number */
  18. #ifndef lua_str2number
  19. #define lua_str2number(s,p) strtod((s), (p))
  20. #endif
  21. const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
  22. int luaO_log2 (unsigned int x) {
  23. static const lu_byte log_8[255] = {
  24. 0,
  25. 1,1,
  26. 2,2,2,2,
  27. 3,3,3,3,3,3,3,3,
  28. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  29. 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,
  30. 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,
  31. 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,
  32. 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,
  33. 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,
  34. 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,
  35. 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
  36. };
  37. if (x >= 0x00010000) {
  38. if (x >= 0x01000000) return log_8[((x>>24) & 0xff) - 1]+24;
  39. else return log_8[((x>>16) & 0xff) - 1]+16;
  40. }
  41. else {
  42. if (x >= 0x00000100) return log_8[((x>>8) & 0xff) - 1]+8;
  43. else if (x) return log_8[(x & 0xff) - 1];
  44. return -1; /* special `log' for 0 */
  45. }
  46. }
  47. int luaO_rawequalObj (const TObject *t1, const TObject *t2) {
  48. if (ttype(t1) != ttype(t2)) return 0;
  49. if (iscollectable(t1)) return gcvalue(t1) == gcvalue(t2);
  50. else switch (ttype(t1)) {
  51. case LUA_TNIL:
  52. return 1;
  53. case LUA_TBOOLEAN:
  54. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  55. case LUA_TLIGHTUSERDATA:
  56. return pvalue(t1) == pvalue(t2);
  57. case LUA_TNUMBER:
  58. return nvalue(t1) == nvalue(t2);
  59. }
  60. lua_assert(0);
  61. return 0; /* to avoid warnings */
  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. setsvalue(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. setsvalue(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, va_arg(argp, int));
  98. incr_top(L);
  99. break;
  100. case 'f':
  101. setnvalue(L->top, va_arg(argp, lua_Number));
  102. incr_top(L);
  103. break;
  104. case '%':
  105. pushstr(L, "%");
  106. break;
  107. default: lua_assert(0);
  108. }
  109. n += 2;
  110. fmt = e+2;
  111. }
  112. pushstr(L, fmt);
  113. luaV_concat(L, n+1, L->top - L->ci->base - 1);
  114. L->top -= n;
  115. return svalue(L->top - 1);
  116. }
  117. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  118. const char *msg;
  119. va_list argp;
  120. va_start(argp, fmt);
  121. msg = luaO_pushvfstring(L, fmt, argp);
  122. va_end(argp);
  123. return msg;
  124. }
  125. void luaO_chunkid (char *out, const char *source, int bufflen) {
  126. if (*source == '=') {
  127. strncpy(out, source+1, bufflen); /* remove first char */
  128. out[bufflen-1] = '\0'; /* ensures null termination */
  129. }
  130. else { /* out = "source", or "...source" */
  131. if (*source == '@') {
  132. int l;
  133. source++; /* skip the `@' */
  134. bufflen -= sizeof(" `...' ");
  135. l = strlen(source);
  136. strcpy(out, "");
  137. if (l>bufflen) {
  138. source += (l-bufflen); /* get last part of file name */
  139. strcat(out, "...");
  140. }
  141. strcat(out, source);
  142. }
  143. else { /* out = [string "string"] */
  144. int len = strcspn(source, "\n"); /* stop at first newline */
  145. bufflen -= sizeof(" [string \"...\"] ");
  146. if (len > bufflen) len = bufflen;
  147. strcpy(out, "[string \"");
  148. if (source[len] != '\0') { /* must truncate? */
  149. strncat(out, source, len);
  150. strcat(out, "...");
  151. }
  152. else
  153. strcat(out, source);
  154. strcat(out, "\"]");
  155. }
  156. }
  157. }