lobject.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. ** $Id: lobject.c,v 1.23 1999/09/08 20:45:18 roberto Exp roberto $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdlib.h>
  8. #include "lobject.h"
  9. #include "lua.h"
  10. const char *const luaO_typenames[] = { /* ORDER LUA_T */
  11. "userdata", "number", "string", "table", "function", "function",
  12. "nil", "function", "mark", "mark", "mark", "line", NULL
  13. };
  14. const TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
  15. /* hash dimensions values */
  16. static const long dimensions[] =
  17. {5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
  18. 12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
  19. 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
  20. int luaO_redimension (int oldsize) {
  21. int i;
  22. for (i=0; dimensions[i]<MAX_INT; i++) {
  23. if (dimensions[i] > oldsize)
  24. return dimensions[i];
  25. }
  26. lua_error("tableEM");
  27. return 0; /* to avoid warnings */
  28. }
  29. int luaO_equalval (const TObject *t1, const TObject *t2) {
  30. switch (ttype(t1)) {
  31. case LUA_T_NIL: return 1;
  32. case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
  33. case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2);
  34. case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
  35. case LUA_T_PROTO: return tfvalue(t1) == tfvalue(t2);
  36. case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2);
  37. case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl;
  38. default:
  39. LUA_INTERNALERROR("invalid type");
  40. return 0; /* UNREACHABLE */
  41. }
  42. }
  43. #ifdef OLD_ANSI
  44. void luaO_memup (void *dest, void *src, int size) {
  45. while (size--)
  46. ((char *)dest)[size]=((char *)src)[size];
  47. }
  48. void luaO_memdown (void *dest, void *src, int size) {
  49. int i;
  50. for (i=0; i<size; i++)
  51. ((char *)dest)[i]=((char *)src)[i];
  52. }
  53. #endif
  54. static double expten (unsigned int e) {
  55. double exp = 10.0;
  56. double res = 1.0;
  57. for (; e; e>>=1) {
  58. if (e & 1) res *= exp;
  59. exp *= exp;
  60. }
  61. return res;
  62. }
  63. int luaO_str2d (const char *s, real *result) { /* LUA_NUMBER */
  64. double a = 0.0;
  65. int point = 0; /* number of decimal digits */
  66. int sig;
  67. while (isspace((unsigned char)*s)) s++;
  68. sig = 1;
  69. switch (*s) {
  70. case '-': sig = -1; /* go through */
  71. case '+': s++;
  72. }
  73. if (! (isdigit((unsigned char)*s) ||
  74. (*s == '.' && isdigit((unsigned char)*(s+1)))))
  75. return 0; /* not (at least one digit before or after the point) */
  76. while (isdigit((unsigned char)*s))
  77. a = 10.0*a + (*(s++)-'0');
  78. if (*s == '.') {
  79. s++;
  80. while (isdigit((unsigned char)*s)) {
  81. a = 10.0*a + (*(s++)-'0');
  82. point++;
  83. }
  84. }
  85. a *= sig;
  86. if (toupper((unsigned char)*s) == 'E') {
  87. int e = 0;
  88. s++;
  89. sig = 1;
  90. switch (*s) {
  91. case '-': sig = -1; /* go through */
  92. case '+': s++;
  93. }
  94. if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
  95. do {
  96. e = 10*e + (*(s++)-'0');
  97. } while (isdigit((unsigned char)*s));
  98. point -= sig*e;
  99. }
  100. while (isspace((unsigned char)*s)) s++;
  101. if (*s != '\0') return 0; /* invalid trailing characters? */
  102. if (point != 0) {
  103. if (point > 0) a /= expten(point);
  104. else a *= expten(-point);
  105. }
  106. *result = a;
  107. return 1;
  108. }