lobject.c 4.8 KB

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