lobject.c 4.7 KB

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