lobject.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. ** $Id: lobject.c,v 1.13 1998/06/19 16:14:09 roberto Exp $
  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. char *luaO_typenames[] = { /* ORDER LUA_T */
  11. "userdata", "number", "string", "table", "function", "function",
  12. "nil", "function", "mark", "mark", "mark", "line", NULL
  13. };
  14. TObject luaO_nilobject = {LUA_T_NIL, {NULL}};
  15. /* hash dimensions values */
  16. static 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. {
  22. int i;
  23. for (i=0; dimensions[i]<MAX_INT; i++) {
  24. if (dimensions[i] > oldsize)
  25. return dimensions[i];
  26. }
  27. lua_error("table overflow");
  28. return 0; /* to avoid warnings */
  29. }
  30. int luaO_equalObj (TObject *t1, TObject *t2)
  31. {
  32. if (ttype(t1) != ttype(t2)) return 0;
  33. switch (ttype(t1)) {
  34. case LUA_T_NIL: return 1;
  35. case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
  36. case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2);
  37. case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
  38. case LUA_T_PROTO: return tfvalue(t1) == tfvalue(t2);
  39. case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2);
  40. case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl;
  41. default:
  42. LUA_INTERNALERROR("invalid type");
  43. return 0; /* UNREACHABLE */
  44. }
  45. }
  46. void luaO_insertlist (GCnode *root, GCnode *node)
  47. {
  48. node->next = root->next;
  49. root->next = node;
  50. node->marked = 0;
  51. }
  52. #ifdef OLD_ANSI
  53. void luaO_memup (void *dest, void *src, int size) {
  54. while (size--)
  55. ((char *)dest)[size]=((char *)src)[size];
  56. }
  57. void luaO_memdown (void *dest, void *src, int size) {
  58. int i;
  59. for (i=0; i<size; i++)
  60. ((char *)dest)[i]=((char *)src)[i];
  61. }
  62. #endif
  63. static double expten (unsigned int e) {
  64. double exp = 10.0;
  65. double res = 1.0;
  66. for (; e; e>>=1) {
  67. if (e & 1) res *= exp;
  68. exp *= exp;
  69. }
  70. return res;
  71. }
  72. double luaO_str2d (char *s) {
  73. double a = 0.0;
  74. int point = 0;
  75. if (!isdigit((unsigned char)*s) && !isdigit((unsigned char)*(s+1)))
  76. return -1; /* no digit before or after decimal point */
  77. while (isdigit((unsigned char)*s)) {
  78. a = 10.0*a + (*(s++)-'0');
  79. }
  80. if (*s == '.') s++;
  81. while (isdigit((unsigned char)*s)) {
  82. a = 10.0*a + (*(s++)-'0');
  83. point++;
  84. }
  85. if (toupper((unsigned char)*s) == 'E') {
  86. int e = 0;
  87. int sig = 1;
  88. s++;
  89. if (*s == '+') s++;
  90. else if (*s == '-') {
  91. s++;
  92. sig = -1;
  93. }
  94. if (!isdigit((unsigned char)*s)) return -1; /* no digit in expoent part? */
  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 -1; /* invalid trailing characters? */
  102. if (point > 0)
  103. a /= expten(point);
  104. else if (point < 0)
  105. a *= expten(-point);
  106. return a;
  107. }