blitz_debug.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "blitz.h"
  2. void bbCAssertEx(){
  3. bbExThrowCString( "C Assert failed" );
  4. }
  5. static void debugNop(){
  6. }
  7. static void debugUnhandledEx( BBObject *ex ){
  8. bbWriteStderr( ex->clas->ToString( ex ) );
  9. exit(-1);
  10. }
  11. void (*bbOnDebugStop)()=debugNop;
  12. void (*bbOnDebugLog)( BBString *str )=debugNop;
  13. void (*bbOnDebugEnterStm)( BBDebugStm *stm )=debugNop;
  14. void (*bbOnDebugEnterScope)( BBDebugScope *scope )=debugNop;
  15. void (*bbOnDebugLeaveScope)()=debugNop;
  16. void (*bbOnDebugPushExState)()=debugNop;
  17. void (*bbOnDebugPopExState)()=debugNop;
  18. void (*bbOnDebugUnhandledEx)( BBObject *ex )=debugUnhandledEx;
  19. static unsigned int bpCount = 0;
  20. static unsigned int bpSize = 0;
  21. static BBSource * sources = 0;
  22. static void swap(BBSource* a, BBSource* b) {
  23. BBSource s = *a;
  24. *a = *b;
  25. *b = s;
  26. }
  27. static int partition (BBSource arr[], int low, int high) {
  28. BBULONG pivot = arr[high].id;
  29. int i = (low - 1);
  30. int j;
  31. for (j = low; j <= high- 1; j++) {
  32. if (arr[j].id < pivot) {
  33. i++;
  34. swap(&arr[i], &arr[j]);
  35. }
  36. }
  37. swap(&arr[i + 1], &arr[high]);
  38. return (i + 1);
  39. }
  40. static void sort(BBSource arr[], int low, int high) {
  41. if (low < high) {
  42. int part = partition(arr, low, high);
  43. sort(arr, low, part - 1);
  44. sort(arr, part + 1, high);
  45. }
  46. }
  47. void bbRegisterSource(BBULONG sourceId, const char * source) {
  48. if (sources == 0) {
  49. bpSize = 32;
  50. sources = calloc(bpSize, sizeof(BBSource));
  51. } else {
  52. if (bpCount == bpSize) {
  53. BBSource * bp = calloc(bpSize * 2, sizeof(BBSource));
  54. memcpy(bp, sources, bpSize * sizeof(BBSource));
  55. BBSource * old = sources;
  56. sources = bp;
  57. free(old);
  58. bpSize *= 2;
  59. }
  60. }
  61. sources[bpCount].id = sourceId;
  62. sources[bpCount].file = source;
  63. bpCount++;
  64. if (bpCount > 1) {
  65. sort(sources, 0, bpCount - 1);
  66. }
  67. }
  68. BBSource * bbSourceForId(BBULONG id) {
  69. if (bpCount > 0) {
  70. unsigned int first = 0;
  71. unsigned int last = bpCount - 1;
  72. unsigned int index = 0;
  73. while (first <= last) {
  74. index = (first + last) / 2;
  75. if (sources[index].id == id) {
  76. return &sources[index];
  77. } else {
  78. if (sources[index].id < id) {
  79. first = index + 1;
  80. } else {
  81. if (index == 0) {
  82. return 0;
  83. }
  84. last = index - 1;
  85. }
  86. }
  87. }
  88. }
  89. return 0;
  90. }
  91. BBSource * bbSourceForName(BBString * filename) {
  92. if (bpCount > 0) {
  93. char path[512];
  94. size_t len = 512;
  95. bbStringToUTF8StringBuffer(filename, path, &len);
  96. path[len] = 0;
  97. int i;
  98. for (i = 0; i < bpCount; i++) {
  99. if (strcmp(path, sources[i].file) == 0) {
  100. return &sources[i];
  101. }
  102. }
  103. }
  104. return 0;
  105. }