/runtime/object.d

http://github.com/wilkie/djehuty · D · 63 lines · 34 code · 12 blank · 17 comment · 0 complexity · 1f40afdda2dd674bdb8cb30b5fcefe31 MD5 · raw file

  1. /*
  2. * object.d
  3. *
  4. * This module implements the Object class.
  5. *
  6. */
  7. module object;
  8. // Imports necessary routines used by the runtime
  9. import runtime.util;
  10. import runtime.dstatic;
  11. import runtime.dstubs;
  12. import runtime.exception;
  13. import runtime.error;
  14. public import runtime.types;
  15. public import runtime.classinfo;
  16. public import runtime.typeinfo;
  17. // Description: The base class inherited by all classes.
  18. class Object {
  19. // Description: Returns a string representing this object.
  20. char[] toString() {
  21. return this.classinfo.name;
  22. }
  23. // Description: Computes a hash representing this object
  24. hash_t toHash() {
  25. string hashStr = (toStr(&this) ~ this.classinfo.name);
  26. hash_t hash = 0;
  27. foreach(chr; hashStr) {
  28. hash *= 9;
  29. hash += cast(ubyte)chr;
  30. }
  31. }
  32. // Will compare two Object classes
  33. // Returns: 0 if equal, -1 if o is greater, 1 if o is smaller.
  34. int opCmp(Object o) {
  35. // BUG: this prevents a compacting GC from working, needs to be fixed
  36. //return cast(int)cast(void *)this - cast(int)cast(void *)o;
  37. throw new Error("need opCmp for class " ~ this.classinfo.name);
  38. }
  39. // Will compare two Object classes for equality.
  40. // Returns: 0 if not equal.
  41. int opEquals(Object o) {
  42. return cast(int)(this is o);
  43. }
  44. }
  45. // Description: This is the information stored for an interface.
  46. struct Interface {
  47. ClassInfo classinfo; // .classinfo for this interface (not for containing class)
  48. void *[] vtbl;
  49. int offset; // offset to Interface 'this' from Object 'this'
  50. }
  51. public import djehuty;