lobject.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. ** $Id: lobject.c,v 1.78 2002/05/06 15:51:41 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 <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "ldo.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lstate.h"
  16. #include "lstring.h"
  17. #include "lvm.h"
  18. const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
  19. int luaO_log2 (unsigned int x) {
  20. static const lu_byte log_8[255] = {
  21. 0,
  22. 1,1,
  23. 2,2,2,2,
  24. 3,3,3,3,3,3,3,3,
  25. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  26. 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,
  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. 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,
  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. 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. };
  34. if (x >= 0x00010000) {
  35. if (x >= 0x01000000) return log_8[((x>>24) & 0xff) - 1]+24;
  36. else return log_8[((x>>16) & 0xff) - 1]+16;
  37. }
  38. else {
  39. if (x >= 0x00000100) return log_8[((x>>8) & 0xff) - 1]+8;
  40. else if (x) return log_8[(x & 0xff) - 1];
  41. return -1; /* special `log' for 0 */
  42. }
  43. }
  44. /*
  45. ** warning: this function must return 1 for true (see opcode OP_TESTEQ)
  46. */
  47. int luaO_equalObj (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_TBOOLEAN:
  55. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  56. case LUA_TUDATAVAL:
  57. return pvalue(t1) == pvalue(t2);
  58. default: /* other types are equal if struct pointers are equal */
  59. return tsvalue(t1) == tsvalue(t2);
  60. }
  61. }
  62. void *luaO_openspaceaux (lua_State *L, size_t n) {
  63. if (n > G(L)->Mbuffsize) {
  64. G(L)->Mbuffer = luaM_realloc(L, G(L)->Mbuffer, G(L)->Mbuffsize, n);
  65. G(L)->Mbuffsize = n;
  66. }
  67. return G(L)->Mbuffer;
  68. }
  69. int luaO_str2d (const char *s, lua_Number *result) {
  70. char *endptr;
  71. lua_Number res = lua_str2number(s, &endptr);
  72. if (endptr == s) return 0; /* no conversion */
  73. while (isspace((unsigned char)(*endptr))) endptr++;
  74. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  75. *result = res;
  76. return 1;
  77. }
  78. static void pushstr (lua_State *L, const char *str) {
  79. setsvalue(L->top, luaS_new(L, str));
  80. incr_top(L);
  81. }
  82. /* this function handles only `%d', `%c', %f, and `%s' formats */
  83. const char *luaO_vpushstr (lua_State *L, const char *fmt, va_list argp) {
  84. int n = 0;
  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_strconc(L, n+1, L->top - L->ci->base - 1);
  119. L->top -= n;
  120. return svalue(L->top - 1);
  121. }
  122. const char *luaO_pushstr (lua_State *L, const char *fmt, ...) {
  123. const char *msg;
  124. va_list argp;
  125. va_start(argp, fmt);
  126. msg = luaO_vpushstr(L, fmt, argp);
  127. va_end(argp);
  128. return msg;
  129. }
  130. void luaO_verror (lua_State *L, const char *fmt, ...) {
  131. const char *msg;
  132. va_list argp;
  133. va_start(argp, fmt);
  134. msg = luaO_vpushstr(L, fmt, argp);
  135. va_end(argp);
  136. luaD_runerror(L, msg);
  137. }
  138. void luaO_chunkid (char *out, const char *source, int bufflen) {
  139. if (*source == '=') {
  140. strncpy(out, source+1, bufflen); /* remove first char */
  141. out[bufflen-1] = '\0'; /* ensures null termination */
  142. }
  143. else { /* out = "file `source'", or "file `...source'" */
  144. if (*source == '@') {
  145. int l;
  146. source++; /* skip the `@' */
  147. bufflen -= sizeof(" file `...' ");
  148. l = strlen(source);
  149. strcpy(out, "file `");
  150. if (l>bufflen) {
  151. source += (l-bufflen); /* get last part of file name */
  152. strcat(out, "...");
  153. }
  154. strcat(out, source);
  155. strcat(out, "'");
  156. }
  157. else {
  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. }