env.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "__env.h"
  2. static af_Core *makeCore(void);
  3. static void freeCore(af_Core *core);
  4. static bool checkInheritAPI(af_ObjectData *od);
  5. static void checkInherit(af_Inherit **ih, af_Object *obj);
  6. static bool enableCore(af_Core *core);
  7. static af_Activity *makeActivity(af_ByteCode *bt,bool new_vs, af_VarSpaceListNode *vsl, af_Object *belong);
  8. static af_Activity *freeActivity(af_Activity *activity);
  9. static void freeAllActivity(af_Activity *activity);
  10. static af_Core *makeCore(void) {
  11. af_Core *core = calloc(sizeof(af_Core), 1);
  12. core->in_init = true;
  13. core->protect = makeVarSpace();
  14. return core;
  15. }
  16. static void freeCore(af_Core *core) {
  17. freeVarSpace(core->protect); // 无论是否gc接管都释放
  18. gc_freeAllValue(core);
  19. free(core);
  20. }
  21. /*
  22. * 函数名: getBaseObjectFromCore
  23. * 目标: 从VarSpace中获取一个量
  24. * 作用: 用于init初始化时在保护空间获得一些初始化对象
  25. */
  26. af_Object *getBaseObjectFromCore(char *name, af_Core *core) {
  27. af_Var *var = findVarFromVarSpace(name, core->protect);
  28. if (var != NULL)
  29. return var->vn->obj;
  30. return NULL;
  31. }
  32. /*
  33. * 函数名: getBaseObject
  34. * 目标: getBaseObjectFromCore的对外接口
  35. */
  36. af_Object *getBaseObject(char *name, af_Environment *env) {
  37. return getBaseObjectFromCore(name, env->core);
  38. }
  39. static void checkInherit(af_Inherit **ih, af_Object *obj) {
  40. while (*ih != NULL) {
  41. if ((*ih)->obj->data == obj->data) {
  42. if ((*ih)->next == NULL && (*ih)->obj == obj) // 最后一个就是obj
  43. return; // 不需要任何更改
  44. *ih = freeIherit(*ih); // 释放该ih
  45. } else
  46. ih = &((*ih)->next);
  47. }
  48. *ih = makeIherit(obj);
  49. }
  50. static bool checkInheritAPI(af_ObjectData *od) {
  51. if (od->api != NULL)
  52. return true;
  53. if (!od->inherit_api)
  54. return false;
  55. if (od->iherit->obj->data->api == NULL && !checkInheritAPI(od->iherit->obj->data))
  56. return false;
  57. od->api = od->iherit->obj->data->api;
  58. return true;
  59. }
  60. static bool enableCore(af_Core *core) {
  61. af_Object *object = getBaseObjectFromCore("object", core);
  62. af_Object *global = getBaseObjectFromCore("global", core);
  63. if (global == NULL || global->belong != NULL)
  64. return false; // global未找到 或其有属对象
  65. if (object == NULL || object->data->iherit != NULL || object->data->inherit_api || !object->data->allow_inherit)
  66. return false; // object未找到 或其继承自其他对象 或其使用继承api 或其不可被继承
  67. core->global = global;
  68. core->object = object;
  69. addVarSpaceGCByCore(global->data->var_space, core);
  70. for (af_Object *obj = core->object; obj != NULL; obj = obj->gc.next) {
  71. if (obj == global)
  72. continue;
  73. if (obj->belong == NULL)
  74. obj->belong = global;
  75. }
  76. af_ObjectData *last = NULL;
  77. for (af_ObjectData *od = core->gc_ObjectData; od != NULL; od = od->gc.next) {
  78. last = od;
  79. if (od == object->data)
  80. continue;
  81. checkInherit(&od->iherit, object);
  82. }
  83. // 先创造的obj在后面, 因此倒着遍历, 先遍历到的obj依赖少, 可以减少checkInheritAPI递归的深度
  84. for (af_ObjectData *od = last; od != NULL; od = od->gc.prev) {
  85. if (od == object->data)
  86. continue;
  87. if(!checkInheritAPI(od))
  88. return false;
  89. }
  90. core->in_init = false;
  91. return true;
  92. }
  93. static af_Activity *makeActivity(af_ByteCode *bt, bool new_vs, af_VarSpaceListNode *vsl, af_Object *belong) {
  94. af_Activity *activity = calloc(sizeof(af_Activity), 1);
  95. activity->bt = bt;
  96. activity->bt_start = bt;
  97. if (new_vs) {
  98. activity->var_list = pushNewVarList(vsl);
  99. activity->new_vs_count = 1;
  100. } else {
  101. activity->var_list = vsl;
  102. activity->new_vs_count = 0;
  103. }
  104. activity->belong = belong;
  105. return activity;
  106. }
  107. static af_Activity *freeActivity(af_Activity *activity) {
  108. af_Activity *prev = activity->prev;
  109. af_VarSpaceListNode *vs = activity->var_list;
  110. for (int i = activity->new_vs_count; i > 0; i--) {
  111. if (vs == NULL) // 发生了错误
  112. break;
  113. vs = popLastVarList(vs);
  114. }
  115. free(activity);
  116. return prev;
  117. }
  118. static void freeAllActivity(af_Activity *activity) {
  119. while (activity != NULL)
  120. activity = freeActivity(activity);
  121. }
  122. af_Environment *makeEnvironment(void) {
  123. af_Environment *env = calloc(sizeof(af_Environment), 1);
  124. env->core = makeCore();
  125. return env;
  126. }
  127. bool enableEnvironment(af_ByteCode *bt, af_Environment *env) {
  128. if (!enableCore(env->core))
  129. return false;
  130. env->activity = makeActivity(bt, false, NULL, env->core->global);
  131. env->activity->new_vs_count = 2;
  132. env->activity->var_list = makeVarSpaceList(env->core->global->data->var_space);
  133. env->activity->var_list->next = makeVarSpaceList(env->core->protect);
  134. return true;
  135. }
  136. void freeEnvironment(af_Environment *env) {
  137. freeCore(env->core);
  138. freeAllActivity(env->activity);
  139. free(env);
  140. }
  141. void pushActivity(af_ByteCode *bt, bool new_vs, af_VarSpaceListNode *vsl, af_Object *belong,
  142. af_Environment *env) {
  143. af_Activity *activity = makeActivity(bt, new_vs, vsl, belong);
  144. activity->prev = env->activity;
  145. env->activity = activity;
  146. }
  147. void popActivity(af_Environment *env) {
  148. env->activity = freeActivity(env->activity);
  149. }