lobject.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. ** $Id: lobject.c,v 1.19 1999/04/13 19:28:49 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. void luaO_insertlist (GCnode *root, GCnode *node) {
  44. node->next = root->next;
  45. root->next = node;
  46. node->marked = 0;
  47. }
  48. #ifdef OLD_ANSI
  49. void luaO_memup (void *dest, void *src, int size) {
  50. while (size--)
  51. ((char *)dest)[size]=((char *)src)[size];
  52. }
  53. void luaO_memdown (void *dest, void *src, int size) {
  54. int i;
  55. for (i=0; i<size; i++)
  56. ((char *)dest)[i]=((char *)src)[i];
  57. }
  58. #endif
  59. static double expten (unsigned int e) {
  60. double exp = 10.0;
  61. double res = 1.0;
  62. for (; e; e>>=1) {
  63. if (e & 1) res *= exp;
  64. exp *= exp;
  65. }
  66. return res;
  67. }
  68. double luaO_str2d (const char *s) { /* LUA_NUMBER */
  69. double a = 0.0;
  70. int point = 0;
  71. while (isdigit((unsigned char)*s)) {
  72. a = 10.0*a + (*(s++)-'0');
  73. }
  74. if (*s == '.') {
  75. s++;
  76. while (isdigit((unsigned char)*s)) {
  77. a = 10.0*a + (*(s++)-'0');
  78. point++;
  79. }
  80. }
  81. if (toupper((unsigned char)*s) == 'E') {
  82. int e = 0;
  83. int sig = 1;
  84. s++;
  85. if (*s == '-') {
  86. s++;
  87. sig = -1;
  88. }
  89. else if (*s == '+') s++;
  90. if (!isdigit((unsigned char)*s)) return -1; /* no digit in the exponent? */
  91. do {
  92. e = 10*e + (*(s++)-'0');
  93. } while (isdigit((unsigned char)*s));
  94. point -= sig*e;
  95. }
  96. while (isspace((unsigned char)*s)) s++;
  97. if (*s != '\0') return -1; /* invalid trailing characters? */
  98. if (point > 0)
  99. a /= expten(point);
  100. else if (point < 0)
  101. a *= expten(-point);
  102. return a;
  103. }