lobject.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. ** $Id: lobject.c,v 1.18 1999/02/26 15:48:30 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. 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("tableEM");
  28. return 0; /* to avoid warnings */
  29. }
  30. int luaO_equalval (TObject *t1, TObject *t2) {
  31. switch (ttype(t1)) {
  32. case LUA_T_NIL: return 1;
  33. case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
  34. case LUA_T_STRING: case LUA_T_USERDATA: return svalue(t1) == svalue(t2);
  35. case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
  36. case LUA_T_PROTO: return tfvalue(t1) == tfvalue(t2);
  37. case LUA_T_CPROTO: return fvalue(t1) == fvalue(t2);
  38. case LUA_T_CLOSURE: return t1->value.cl == t2->value.cl;
  39. default:
  40. LUA_INTERNALERROR("invalid type");
  41. return 0; /* UNREACHABLE */
  42. }
  43. }
  44. void luaO_insertlist (GCnode *root, GCnode *node)
  45. {
  46. node->next = root->next;
  47. root->next = node;
  48. node->marked = 0;
  49. }
  50. #ifdef OLD_ANSI
  51. void luaO_memup (void *dest, void *src, int size) {
  52. while (size--)
  53. ((char *)dest)[size]=((char *)src)[size];
  54. }
  55. void luaO_memdown (void *dest, void *src, int size) {
  56. int i;
  57. for (i=0; i<size; i++)
  58. ((char *)dest)[i]=((char *)src)[i];
  59. }
  60. #endif
  61. static double expten (unsigned int e) {
  62. double exp = 10.0;
  63. double res = 1.0;
  64. for (; e; e>>=1) {
  65. if (e & 1) res *= exp;
  66. exp *= exp;
  67. }
  68. return res;
  69. }
  70. double luaO_str2d (char *s) { /* LUA_NUMBER */
  71. double a = 0.0;
  72. int point = 0;
  73. while (isdigit((unsigned char)*s)) {
  74. a = 10.0*a + (*(s++)-'0');
  75. }
  76. if (*s == '.') {
  77. s++;
  78. while (isdigit((unsigned char)*s)) {
  79. a = 10.0*a + (*(s++)-'0');
  80. point++;
  81. }
  82. }
  83. if (toupper((unsigned char)*s) == 'E') {
  84. int e = 0;
  85. int sig = 1;
  86. s++;
  87. if (*s == '-') {
  88. s++;
  89. sig = -1;
  90. }
  91. else if (*s == '+') s++;
  92. if (!isdigit((unsigned char)*s)) return -1; /* no digit in the exponent? */
  93. do {
  94. e = 10*e + (*(s++)-'0');
  95. } while (isdigit((unsigned char)*s));
  96. point -= sig*e;
  97. }
  98. while (isspace((unsigned char)*s)) s++;
  99. if (*s != '\0') return -1; /* invalid trailing characters? */
  100. if (point > 0)
  101. a /= expten(point);
  102. else if (point < 0)
  103. a *= expten(-point);
  104. return a;
  105. }