blitz_object.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #include "blitz.h"
  2. #define REG_GROW 256
  3. static BBClass **reg_base,**reg_put,**reg_end;
  4. static BBInterface **ireg_base,**ireg_put,**ireg_end;
  5. static BBDebugScope debugScope={
  6. BBDEBUGSCOPE_USERTYPE,
  7. "Object",
  8. BBDEBUGDECL_END
  9. };
  10. BBClass bbObjectClass={
  11. 0, //super
  12. bbObjectFree, //free
  13. &debugScope, //debug_scope
  14. 8, //instance_size
  15. bbObjectCtor,
  16. bbObjectDtor,
  17. bbObjectToString,
  18. bbObjectCompare,
  19. bbObjectSendMessage,
  20. 0, //interface
  21. 0, //extra
  22. 0 //obj_size
  23. };
  24. BBObject bbNullObject={
  25. 0 //clas
  26. //BBGC_MANYREFS //refs
  27. };
  28. BBObject *bbObjectNew( BBClass *clas ){
  29. int flags=( clas->dtor!=bbObjectDtor ) ? BBGC_FINALIZE : 0;
  30. BBObject *o=(BBObject*)bbGCAllocObject( clas->instance_size,clas,flags );
  31. clas->ctor( o );
  32. return o;
  33. }
  34. BBObject *bbObjectAtomicNew( BBClass *clas ){
  35. int flags=( clas->dtor!=bbObjectDtor ) ? BBGC_FINALIZE | BBGC_ATOMIC : BBGC_ATOMIC;
  36. BBObject *o=(BBObject*)bbGCAllocObject( clas->instance_size,clas,flags );
  37. clas->ctor( o );
  38. return o;
  39. }
  40. BBObject *bbObjectNewNC( BBClass *clas ){
  41. int flags=( clas->dtor!=bbObjectDtor ) ? BBGC_FINALIZE : 0;
  42. BBObject *o=(BBObject*)bbGCAllocObject( clas->instance_size,clas,flags );
  43. return o;
  44. }
  45. BBObject *bbObjectAtomicNewNC( BBClass *clas ){
  46. int flags=( clas->dtor!=bbObjectDtor ) ? BBGC_FINALIZE | BBGC_ATOMIC : BBGC_ATOMIC;
  47. BBObject *o=(BBObject*)bbGCAllocObject( clas->instance_size,clas,flags );
  48. return o;
  49. }
  50. void bbObjectFree( BBObject *o ){
  51. BBClass *clas=o->clas;
  52. #ifdef BB_GC_RC
  53. if( o==&bbNullObject ){
  54. //o->refs=BBGC_MANYREFS;
  55. return;
  56. }
  57. clas->dtor( o );
  58. bbGCDeallocObject( o,clas->instance_size );
  59. #else
  60. clas->dtor( o );
  61. #endif
  62. }
  63. void bbObjectCtor( BBObject *o ){
  64. o->clas=&bbObjectClass;
  65. }
  66. void bbObjectDtor( BBObject *o ){
  67. }
  68. BBString *bbObjectToString( BBObject *o ){
  69. char buf[32];
  70. sprintf( buf,"%p",o );
  71. return bbStringFromCString( buf );
  72. }
  73. int bbObjectCompare( BBObject *x,BBObject *y ){
  74. return (char*)x-(char*)y;
  75. }
  76. BBObject *bbObjectSendMessage( BBObject * o, BBObject *m,BBObject *s ){
  77. return &bbNullObject;
  78. }
  79. void bbObjectReserved(){
  80. bbExThrowCString( "Illegal call to reserved method" );
  81. }
  82. BBObject *bbObjectStringcast( BBObject *o ){
  83. if (o->clas == &bbStringClass) {
  84. return o;
  85. } else {
  86. return (BBObject *)&bbEmptyString;
  87. }
  88. }
  89. BBObject *bbObjectArraycast( BBObject *o ){
  90. if (o->clas == &bbArrayClass) {
  91. return o;
  92. } else {
  93. return (BBObject *)&bbEmptyArray;
  94. }
  95. }
  96. BBObject *bbObjectDowncast( BBObject *o,BBClass *t ){
  97. BBClass *p=o->clas;
  98. while( p && p!=t ) p=p->super;
  99. return p ? o : (t==&bbStringClass) ? (BBObject *)&bbEmptyString : (t==&bbArrayClass) ? (BBObject *)&bbEmptyArray : &bbNullObject;
  100. }
  101. void bbObjectRegisterType( BBClass *clas ){
  102. if( reg_put==reg_end ){
  103. int len=reg_put-reg_base,new_len=len+REG_GROW;
  104. reg_base=(BBClass**)bbMemExtend( reg_base,len*sizeof(BBClass*),new_len*sizeof(BBClass*) );
  105. reg_end=reg_base+new_len;
  106. reg_put=reg_base+len;
  107. }
  108. *reg_put++=clas;
  109. }
  110. BBClass **bbObjectRegisteredTypes( int *count ){
  111. *count=reg_put-reg_base;
  112. return reg_base;
  113. }
  114. void bbObjectRegisterInterface( BBInterface * ifc ){
  115. if( ireg_put==ireg_end ){
  116. int len=ireg_put-ireg_base,new_len=len+REG_GROW;
  117. ireg_base=(BBInterface**)bbMemExtend( ireg_base,len*sizeof(BBInterface*),new_len*sizeof(BBInterface*) );
  118. ireg_end=ireg_base+new_len;
  119. ireg_put=ireg_base+len;
  120. }
  121. *ireg_put++=ifc;
  122. }
  123. BBInterface **bbObjectRegisteredInterfaces( int *count ){
  124. *count=ireg_put-ireg_base;
  125. return ireg_base;
  126. }
  127. BBObject * bbInterfaceDowncast(BBOBJECT o, BBINTERFACE ifc) {
  128. int i;
  129. BBCLASS superclas = o->clas;
  130. while (superclas) {
  131. BBCLASS clas = superclas;
  132. superclas = clas->super;
  133. BBINTERFACETABLE table = clas->itable;
  134. if (table) {
  135. BBINTERFACEOFFSETS offsets = table->ifc_offsets;
  136. for (i = table->ifc_size; i; i--) {
  137. if (offsets->ifc == ifc) {
  138. return o;
  139. }
  140. offsets++;
  141. }
  142. }
  143. }
  144. return &bbNullObject;
  145. }
  146. void * bbObjectInterface(BBOBJECT o, BBINTERFACE ifc) {
  147. int i;
  148. BBCLASS superclas = o->clas;
  149. while (superclas) {
  150. BBCLASS clas = superclas;
  151. superclas = clas->super;
  152. BBINTERFACETABLE table = clas->itable;
  153. if (table) {
  154. BBINTERFACEOFFSETS offsets = table->ifc_offsets;
  155. for (i = table->ifc_size; i; i--) {
  156. if (offsets->ifc == ifc) {
  157. return (char*) table->ifc_vtable + offsets->offset;
  158. }
  159. offsets++;
  160. }
  161. }
  162. }
  163. return &bbNullObject;
  164. }
  165. static struct avl_root *struct_root = 0;
  166. int struct_node_compare(const void *x, const void *y) {
  167. struct struct_node * node_x = (struct struct_node *)x;
  168. struct struct_node * node_y = (struct struct_node *)y;
  169. return strcmp(node_x->scope->name, node_y->scope->name);
  170. }
  171. void bbObjectRegisterStruct( BBDebugScope *p ) {
  172. struct struct_node * node = (struct struct_node *)malloc(sizeof(struct struct_node));
  173. node->scope = p;
  174. struct struct_node * old_node = (struct struct_node *)avl_map(&node->link, struct_node_compare, &struct_root);
  175. if (&node->link != &old_node->link) {
  176. // this object already exists here...
  177. // delete the new node, since we don't need it
  178. // note : should never happen as structs should only ever be registered once.
  179. free(node);
  180. }
  181. }
  182. BBDebugScope * bbObjectStructInfo( char * name ) {
  183. // create something to look up
  184. struct struct_node node;
  185. BBDebugScope scope;
  186. scope.name = name;
  187. node.scope = &scope;
  188. struct struct_node * found = (struct struct_node *)tree_search((struct tree_root_np *)&node, struct_node_compare, (struct tree_root_np *)struct_root);
  189. if (found) {
  190. return found->scope;
  191. }
  192. return 0;
  193. }
  194. BBObject * bbNullObjectTest( BBObject *o ) {
  195. if (o == &bbNullObject) brl_blitz_NullObjectError();
  196. return o;
  197. }
  198. static struct avl_root *enum_root = 0;
  199. int enum_node_compare(const void *x, const void *y) {
  200. struct enum_node * node_x = (struct enum_node *)x;
  201. struct enum_node * node_y = (struct enum_node *)y;
  202. return strcmp(node_x->scope->name, node_y->scope->name);
  203. }
  204. void bbObjectRegisterEnum( BBDebugScope *p ) {
  205. struct enum_node * node = (struct enum_node *)malloc(sizeof(struct enum_node));
  206. node->scope = p;
  207. struct enum_node * old_node = (struct enum_node *)avl_map(&node->link, enum_node_compare, &enum_root);
  208. if (&node->link != &old_node->link) {
  209. // this object already exists here...
  210. // delete the new node, since we don't need it
  211. // note : should never happen as structs should only ever be registered once.
  212. free(node);
  213. }
  214. }
  215. BBDebugScope * bbObjectEnumInfo( char * name ) {
  216. // create something to look up
  217. struct enum_node node;
  218. BBDebugScope scope;
  219. scope.name = name;
  220. node.scope = &scope;
  221. struct enum_node * found = (struct enum_node *)tree_search((struct tree_root_np *)&node, enum_node_compare, (struct tree_root_np *)enum_root);
  222. if (found) {
  223. return found->scope;
  224. }
  225. return 0;
  226. }