lobject.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. ** $Id: lobject.c,v 1.85 2002/07/17 16:25:13 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. switch (ttype(t1)) {
  50. case LUA_TNUMBER:
  51. return nvalue(t1) == nvalue(t2);
  52. case LUA_TNIL:
  53. return 1;
  54. case LUA_TSTRING:
  55. return tsvalue(t1) == tsvalue(t2);
  56. case LUA_TBOOLEAN:
  57. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  58. case LUA_TLIGHTUSERDATA:
  59. return pvalue(t1) == pvalue(t2);
  60. case LUA_TUSERDATA:
  61. return uvalue(t1) == uvalue(t2);
  62. case LUA_TTABLE:
  63. return hvalue(t1) == hvalue(t2);
  64. case LUA_TFUNCTION:
  65. return clvalue(t1) == clvalue(t2);
  66. }
  67. lua_assert(0);
  68. return 0; /* to avoid warnings */
  69. }
  70. void *luaO_openspaceaux (lua_State *L, size_t n) {
  71. if (n > G(L)->Mbuffsize) {
  72. G(L)->Mbuffer = luaM_realloc(L, G(L)->Mbuffer, G(L)->Mbuffsize, n);
  73. G(L)->Mbuffsize = n;
  74. }
  75. return G(L)->Mbuffer;
  76. }
  77. int luaO_str2d (const char *s, lua_Number *result) {
  78. char *endptr;
  79. lua_Number res = lua_str2number(s, &endptr);
  80. if (endptr == s) return 0; /* no conversion */
  81. while (isspace((unsigned char)(*endptr))) endptr++;
  82. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  83. *result = res;
  84. return 1;
  85. }
  86. static void pushstr (lua_State *L, const char *str) {
  87. setsvalue(L->top, luaS_new(L, str));
  88. incr_top(L);
  89. }
  90. /* this function handles only `%d', `%c', %f, and `%s' formats */
  91. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  92. int n = 1;
  93. pushstr(L, "");
  94. for (;;) {
  95. const char *e = strchr(fmt, '%');
  96. if (e == NULL) break;
  97. setsvalue(L->top, luaS_newlstr(L, fmt, e-fmt));
  98. incr_top(L);
  99. switch (*(e+1)) {
  100. case 's':
  101. pushstr(L, va_arg(argp, char *));
  102. break;
  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, va_arg(argp, int));
  112. incr_top(L);
  113. break;
  114. case 'f':
  115. setnvalue(L->top, va_arg(argp, lua_Number));
  116. incr_top(L);
  117. break;
  118. case '%':
  119. pushstr(L, "%");
  120. break;
  121. default: lua_assert(0);
  122. }
  123. n += 2;
  124. fmt = e+2;
  125. }
  126. pushstr(L, fmt);
  127. luaV_concat(L, n+1, L->top - L->ci->base - 1);
  128. L->top -= n;
  129. return svalue(L->top - 1);
  130. }
  131. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  132. const char *msg;
  133. va_list argp;
  134. va_start(argp, fmt);
  135. msg = luaO_pushvfstring(L, fmt, argp);
  136. va_end(argp);
  137. return msg;
  138. }
  139. void luaO_chunkid (char *out, const char *source, int bufflen) {
  140. if (*source == '=') {
  141. strncpy(out, source+1, bufflen); /* remove first char */
  142. out[bufflen-1] = '\0'; /* ensures null termination */
  143. }
  144. else { /* out = "source", or "...source" */
  145. if (*source == '@') {
  146. int l;
  147. source++; /* skip the `@' */
  148. bufflen -= sizeof(" `...' ");
  149. l = strlen(source);
  150. strcpy(out, "");
  151. if (l>bufflen) {
  152. source += (l-bufflen); /* get last part of file name */
  153. strcat(out, "...");
  154. }
  155. strcat(out, source);
  156. }
  157. else { /* out = [string "string"] */
  158. int len = strcspn(source, "\n"); /* stop at first newline */
  159. bufflen -= sizeof(" [string \"...\"] ");
  160. if (len > bufflen) len = bufflen;
  161. strcpy(out, "[string \"");
  162. if (source[len] != '\0') { /* must truncate? */
  163. strncat(out, source, len);
  164. strcat(out, "...");
  165. }
  166. else
  167. strcat(out, source);
  168. strcat(out, "\"]");
  169. }
  170. }
  171. }