lobject.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. ** $Id: lobject.c,v 1.86 2002/08/07 14:35:55 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_TNIL:
  51. return 1;
  52. case LUA_TBOOLEAN:
  53. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  54. case LUA_TLIGHTUSERDATA:
  55. return pvalue(t1) == pvalue(t2);
  56. case LUA_TNUMBER:
  57. return nvalue(t1) == nvalue(t2);
  58. default:
  59. return gcvalue(t1) == gcvalue(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_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  84. int n = 1;
  85. pushstr(L, "");
  86. for (;;) {
  87. const char *e = strchr(fmt, '%');
  88. if (e == NULL) break;
  89. setsvalue(L->top, luaS_newlstr(L, fmt, e-fmt));
  90. incr_top(L);
  91. switch (*(e+1)) {
  92. case 's':
  93. pushstr(L, va_arg(argp, char *));
  94. break;
  95. case 'c': {
  96. char buff[2];
  97. buff[0] = cast(char, va_arg(argp, int));
  98. buff[1] = '\0';
  99. pushstr(L, buff);
  100. break;
  101. }
  102. case 'd':
  103. setnvalue(L->top, va_arg(argp, int));
  104. incr_top(L);
  105. break;
  106. case 'f':
  107. setnvalue(L->top, va_arg(argp, lua_Number));
  108. incr_top(L);
  109. break;
  110. case '%':
  111. pushstr(L, "%");
  112. break;
  113. default: lua_assert(0);
  114. }
  115. n += 2;
  116. fmt = e+2;
  117. }
  118. pushstr(L, fmt);
  119. luaV_concat(L, n+1, L->top - L->ci->base - 1);
  120. L->top -= n;
  121. return svalue(L->top - 1);
  122. }
  123. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  124. const char *msg;
  125. va_list argp;
  126. va_start(argp, fmt);
  127. msg = luaO_pushvfstring(L, fmt, argp);
  128. va_end(argp);
  129. return msg;
  130. }
  131. void luaO_chunkid (char *out, const char *source, int bufflen) {
  132. if (*source == '=') {
  133. strncpy(out, source+1, bufflen); /* remove first char */
  134. out[bufflen-1] = '\0'; /* ensures null termination */
  135. }
  136. else { /* out = "source", or "...source" */
  137. if (*source == '@') {
  138. int l;
  139. source++; /* skip the `@' */
  140. bufflen -= sizeof(" `...' ");
  141. l = strlen(source);
  142. strcpy(out, "");
  143. if (l>bufflen) {
  144. source += (l-bufflen); /* get last part of file name */
  145. strcat(out, "...");
  146. }
  147. strcat(out, source);
  148. }
  149. else { /* out = [string "string"] */
  150. int len = strcspn(source, "\n"); /* stop at first newline */
  151. bufflen -= sizeof(" [string \"...\"] ");
  152. if (len > bufflen) len = bufflen;
  153. strcpy(out, "[string \"");
  154. if (source[len] != '\0') { /* must truncate? */
  155. strncat(out, source, len);
  156. strcat(out, "...");
  157. }
  158. else
  159. strcat(out, source);
  160. strcat(out, "\"]");
  161. }
  162. }
  163. }