lobject.c 4.6 KB

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