blitz_object.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "blitz.h"
  2. #define REG_GROW 256
  3. static BBClass **reg_base,**reg_put,**reg_end;
  4. static BBDebugScope debugScope={
  5. BBDEBUGSCOPE_USERTYPE,
  6. "Object",
  7. BBDEBUGDECL_END
  8. };
  9. BBClass bbObjectClass={
  10. 0, //super
  11. bbObjectFree, //free
  12. &debugScope, //debug_scope
  13. 8, //instance_size
  14. bbObjectCtor,
  15. bbObjectDtor,
  16. bbObjectToString,
  17. bbObjectCompare,
  18. bbObjectSendMessage,
  19. bbObjectReserved,
  20. bbObjectReserved,
  21. bbObjectReserved,
  22. };
  23. BBObject bbNullObject={
  24. 0, //clas
  25. BBGC_MANYREFS //refs
  26. };
  27. BBObject *bbObjectNew( BBClass *clas ){
  28. int flags=( clas->dtor!=bbObjectDtor ) ? BBGC_FINALIZE : 0;
  29. BBObject *o=(BBObject*)bbGCAllocObject( clas->instance_size,clas,flags );
  30. clas->ctor( o );
  31. return o;
  32. }
  33. void bbObjectFree( BBObject *o ){
  34. BBClass *clas=o->clas;
  35. #ifdef BB_GC_RC
  36. if( o==&bbNullObject ){
  37. o->refs=BBGC_MANYREFS;
  38. return;
  39. }
  40. clas->dtor( o );
  41. bbGCDeallocObject( o,clas->instance_size );
  42. #else
  43. clas->dtor( o );
  44. #endif
  45. }
  46. void bbObjectCtor( BBObject *o ){
  47. o->clas=&bbObjectClass;
  48. }
  49. void bbObjectDtor( BBObject *o ){
  50. o->clas=0;
  51. }
  52. BBString *bbObjectToString( BBObject *o ){
  53. char buf[32];
  54. sprintf( buf,"%p",o );
  55. return bbStringFromCString( buf );
  56. }
  57. int bbObjectCompare( BBObject *x,BBObject *y ){
  58. return (char*)x-(char*)y;
  59. }
  60. BBObject *bbObjectSendMessage( BBObject *m,BBObject *s ){
  61. return &bbNullObject;
  62. }
  63. void bbObjectReserved(){
  64. bbExThrowCString( "Illegal call to reserved method" );
  65. }
  66. BBObject *bbObjectDowncast( BBObject *o,BBClass *t ){
  67. BBClass *p=o->clas;
  68. while( p && p!=t ) p=p->super;
  69. return p ? o : &bbNullObject;
  70. }
  71. void bbObjectRegisterType( BBClass *clas ){
  72. if( reg_put==reg_end ){
  73. int len=reg_put-reg_base,new_len=len+REG_GROW;
  74. reg_base=(BBClass**)bbMemExtend( reg_base,len*sizeof(BBClass*),new_len*sizeof(BBClass*) );
  75. reg_end=reg_base+new_len;
  76. reg_put=reg_base+len;
  77. }
  78. *reg_put++=clas;
  79. }
  80. BBClass **bbObjectRegisteredTypes( int *count ){
  81. *count=reg_put-reg_base;
  82. return reg_base;
  83. }