lobject.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. ** $Id: lobject.c,v 1.27 1999/12/14 18:31:20 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. #define LUA_REENTRANT
  9. #include "lobject.h"
  10. #include "lua.h"
  11. const char *const luaO_typenames[] = { /* ORDER LUA_T */
  12. "userdata", "number", "string", "table", "function", "function", "nil",
  13. "function", "function", "function", "function", "function", "function",
  14. "line", NULL
  15. };
  16. const TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
  17. /*
  18. ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
  19. */
  20. unsigned long luaO_power2 (unsigned long n) {
  21. unsigned long p = MINPOWER2;
  22. while (p<=n) p<<=1;
  23. return p;
  24. }
  25. int luaO_equalval (const TObject *t1, const TObject *t2) {
  26. switch (ttype(t1)) {
  27. case LUA_T_NIL:
  28. return 1;
  29. case LUA_T_NUMBER:
  30. return nvalue(t1) == nvalue(t2);
  31. case LUA_T_STRING: case LUA_T_USERDATA:
  32. return svalue(t1) == svalue(t2);
  33. case LUA_T_ARRAY:
  34. return avalue(t1) == avalue(t2);
  35. case LUA_T_LPROTO:
  36. return tfvalue(t1) == tfvalue(t2);
  37. case LUA_T_CPROTO:
  38. return fvalue(t1) == fvalue(t2);
  39. case LUA_T_CCLOSURE: case LUA_T_LCLOSURE:
  40. return t1->value.cl == t2->value.cl;
  41. default:
  42. LUA_INTERNALERROR(L, "invalid type");
  43. return 0; /* UNREACHABLE */
  44. }
  45. }
  46. #ifdef OLD_ANSI
  47. void luaO_memup (void *dest, void *src, int size) {
  48. while (size--)
  49. ((char *)dest)[size]=((char *)src)[size];
  50. }
  51. void luaO_memdown (void *dest, void *src, int size) {
  52. int i;
  53. for (i=0; i<size; i++)
  54. ((char *)dest)[i]=((char *)src)[i];
  55. }
  56. #endif
  57. static double expten (unsigned int e) {
  58. double exp = 10.0;
  59. double res = 1.0;
  60. for (; e; e>>=1) {
  61. if (e & 1) res *= exp;
  62. exp *= exp;
  63. }
  64. return res;
  65. }
  66. int luaO_str2d (const char *s, real *result) { /* LUA_NUMBER */
  67. double a = 0.0;
  68. int point = 0; /* number of decimal digits */
  69. int sig;
  70. while (isspace((unsigned char)*s)) s++;
  71. sig = 1;
  72. switch (*s) {
  73. case '-': sig = -1; /* go through */
  74. case '+': s++;
  75. }
  76. if (! (isdigit((unsigned char)*s) ||
  77. (*s == '.' && isdigit((unsigned char)*(s+1)))))
  78. return 0; /* not (at least one digit before or after the point) */
  79. while (isdigit((unsigned char)*s))
  80. a = 10.0*a + (*(s++)-'0');
  81. if (*s == '.') {
  82. s++;
  83. while (isdigit((unsigned char)*s)) {
  84. a = 10.0*a + (*(s++)-'0');
  85. point++;
  86. }
  87. }
  88. a *= sig;
  89. if (toupper((unsigned char)*s) == 'E') {
  90. int e = 0;
  91. s++;
  92. sig = 1;
  93. switch (*s) {
  94. case '-': sig = -1; /* go through */
  95. case '+': s++;
  96. }
  97. if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
  98. do {
  99. e = 10*e + (*(s++)-'0');
  100. } while (isdigit((unsigned char)*s));
  101. point -= sig*e;
  102. }
  103. while (isspace((unsigned char)*s)) s++;
  104. if (*s != '\0') return 0; /* invalid trailing characters? */
  105. if (point != 0) {
  106. if (point > 0) a /= expten(point);
  107. else a *= expten(-point);
  108. }
  109. *result = a;
  110. return 1;
  111. }