lobject.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. ** $Id: lobject.c,v 1.4 1997/10/23 16:26:37 roberto Exp roberto $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "lobject.h"
  9. #include "lua.h"
  10. char *luaO_typenames[] = { /* ORDER LUA_T */
  11. "userdata", "number", "string", "table", "prototype", "cprototype",
  12. "nil", "function", "mark", "cmark", "line", NULL
  13. };
  14. unsigned long luaO_nblocks = 0;
  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_FUNCTION: return t1->value.cl == t2->value.cl;
  39. default:
  40. lua_error("internal error in `lua_equalObj'");
  41. return 0; /* UNREACHEABLE */
  42. }
  43. }
  44. int luaO_findstring (char *name, char *list[])
  45. {
  46. int i;
  47. for (i=0; list[i]; i++)
  48. if (strcmp(list[i], name) == 0)
  49. return i;
  50. return -1; /* name not found */
  51. }
  52. void luaO_insertlist (GCnode *root, GCnode *node)
  53. {
  54. node->next = root->next;
  55. root->next = node;
  56. node->marked = 0;
  57. }