lobject.c 2.9 KB

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