env.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  8. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func);
  9. static af_Activity *freeActivity(af_Activity *activity);
  10. static void freeAllActivity(af_Activity *activity);
  11. static af_EnvVar *makeEnvVar(char *name, char *data);
  12. static af_EnvVar *freeEnvVar(af_EnvVar *var);
  13. static void freeAllEnvVar(af_EnvVar *var);
  14. static void freeEnvVarSpace(af_EnvVarSpace *evs);
  15. static af_Core *makeCore(void) {
  16. af_Core *core = calloc(sizeof(af_Core), 1);
  17. core->in_init = true;
  18. core->protect = makeVarSpace();
  19. addVarSpaceGCByCore(core->protect, core);
  20. gc_addReference(core->protect); // protect被外部引用, 由gc管理, 此处标记一个Reference
  21. return core;
  22. }
  23. static void freeCore(af_Core *core) {
  24. if (core->object != NULL)
  25. gc_delReference(core->object);
  26. if (core->global != NULL)
  27. gc_delReference(core->global);
  28. gc_delReference(core->protect);
  29. gc_freeAllValue(core);
  30. free(core);
  31. }
  32. /*
  33. * 函数名: getBaseObjectFromCore
  34. * 目标: 从VarSpace中获取一个量
  35. * 作用: 用于init初始化时在保护空间获得一些初始化对象
  36. */
  37. af_Object *getBaseObjectFromCore(char *name, af_Core *core) {
  38. af_Var *var = findVarFromVarSpace(name, core->protect);
  39. if (var != NULL)
  40. return var->vn->obj;
  41. return NULL;
  42. }
  43. /*
  44. * 函数名: getBaseObject
  45. * 目标: getBaseObjectFromCore的对外接口
  46. */
  47. af_Object *getBaseObject(char *name, af_Environment *env) {
  48. return getBaseObjectFromCore(name, env->core);
  49. }
  50. static void checkInherit(af_Inherit **ih, af_Object *obj) {
  51. while (*ih != NULL) {
  52. if ((*ih)->obj->data == obj->data) {
  53. if ((*ih)->next == NULL && (*ih)->obj == obj) // 最后一个就是obj
  54. return; // 不需要任何更改
  55. *ih = freeIherit(*ih); // 释放该ih
  56. } else
  57. ih = &((*ih)->next);
  58. }
  59. *ih = makeIherit(obj);
  60. }
  61. static bool checkInheritAPI(af_ObjectData *od) {
  62. if (od->api != NULL)
  63. return true;
  64. if (!od->inherit_api)
  65. return false;
  66. if (od->iherit->obj->data->api == NULL && !checkInheritAPI(od->iherit->obj->data))
  67. return false;
  68. od->api = od->iherit->obj->data->api;
  69. return true;
  70. }
  71. static bool enableCore(af_Core *core) {
  72. af_Object *object = getBaseObjectFromCore("object", core);
  73. af_Object *global = getBaseObjectFromCore("global", core);
  74. if (global == NULL || global->belong != NULL)
  75. return false; // global未找到 或其有属对象
  76. if (object == NULL || object->data->iherit != NULL || object->data->inherit_api || !object->data->allow_inherit)
  77. return false; // object未找到 或其继承自其他对象 或其使用继承api 或其不可被继承
  78. core->global = global;
  79. core->object = object;
  80. addVarSpaceGCByCore(global->data->var_space, core);
  81. for (af_Object *obj = core->object; obj != NULL; obj = obj->gc.next) {
  82. if (obj == global)
  83. continue;
  84. if (obj->belong == NULL)
  85. obj->belong = global;
  86. }
  87. af_ObjectData *last = NULL;
  88. for (af_ObjectData *od = core->gc_ObjectData; od != NULL; od = od->gc.next) {
  89. last = od;
  90. if (od == object->data)
  91. continue;
  92. checkInherit(&od->iherit, object);
  93. }
  94. // 先创造的obj在后面, 因此倒着遍历, 先遍历到的obj依赖少, 可以减少checkInheritAPI递归的深度
  95. for (af_ObjectData *od = last; od != NULL; od = od->gc.prev) {
  96. if (od == object->data)
  97. continue;
  98. if(!checkInheritAPI(od))
  99. return false;
  100. }
  101. gc_addReference(object);
  102. gc_addReference(global);
  103. addVarSpaceGCByCore(global->data->var_space, core); // global的vs是全局作用空间, 被外部引用, 所以由gc管理 (不需要要标记Reference, global已经标记了)
  104. core->in_init = false;
  105. return true;
  106. }
  107. static af_Activity *makeActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  108. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func) {
  109. af_Activity *activity = calloc(sizeof(af_Activity), 1);
  110. activity->status = act_func;
  111. activity->msg_up = msg_up;
  112. activity->msg_up_count = 0;
  113. activity->var_list = vsl;
  114. activity->new_vs_count = 0;
  115. activity->belong = belong;
  116. activity->func = func;
  117. gc_addReference(belong);
  118. if (func != NULL)
  119. gc_addReference(func);
  120. activity->bt_top = bt_top;
  121. activity->bt_start = bt_start;
  122. activity->bt_next = bt_start;
  123. activity->return_first = return_first;
  124. return activity;
  125. }
  126. static af_Activity *freeActivity(af_Activity *activity) {
  127. af_Activity *prev = activity->prev;
  128. af_VarSpaceListNode *vs = activity->var_list;
  129. af_Message *msg_up = activity->msg_up;
  130. gc_delReference(activity->belong);
  131. if (activity->func != NULL)
  132. gc_delReference(activity->func);
  133. if (activity->return_msg != NULL)
  134. freeMessage(activity->return_msg);
  135. freeAllMessage(activity->msg_down); // msg转移后需要将对应成员设置为NULL
  136. for (int i = activity->msg_up_count; i > 0; i--) {
  137. if (msg_up == NULL) // 发生了错误
  138. break;
  139. msg_up = freeMessage(msg_up);
  140. }
  141. for (int i = activity->new_vs_count; i > 0; i--) {
  142. if (vs == NULL) // 发生了错误
  143. break;
  144. vs = popLastVarList(vs);
  145. }
  146. free(activity);
  147. return prev;
  148. }
  149. static void freeAllActivity(af_Activity *activity) {
  150. while (activity != NULL)
  151. activity = freeActivity(activity);
  152. }
  153. af_Message *makeMessage(char *type, size_t size) {
  154. af_Message *msg = calloc(sizeof(af_Message), 1);
  155. msg->type = strCopy(type);
  156. if (size != 0)
  157. msg->msg = calloc(size, 1);
  158. msg->size = size;
  159. return msg;
  160. }
  161. af_Message *freeMessage(af_Message *msg) {
  162. af_Message *next = msg->next;
  163. free(msg->type);
  164. free(msg->msg);
  165. free(msg);
  166. return next;
  167. }
  168. void freeAllMessage(af_Message *msg) {
  169. while (msg != NULL)
  170. msg = freeMessage(msg);
  171. }
  172. void pushMessageUp(af_Message *msg, af_Environment *env) {
  173. msg->next = env->activity->msg_up;
  174. env->activity->msg_up = msg;
  175. env->activity->msg_up_count++;
  176. }
  177. void pushMessageDown(af_Message *msg, af_Environment *env) {
  178. msg->next = env->activity->msg_down;
  179. env->activity->msg_down = msg;
  180. }
  181. void *popMessageUp(char *type, af_Environment *env) {
  182. for (af_Message **pmsg = &env->activity->msg_up; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  183. if (EQ_STR((*pmsg)->type, type))
  184. return (*pmsg)->msg; // msg_up是只读的
  185. }
  186. return NULL;
  187. }
  188. /*
  189. * 函数名: getMessageData
  190. * 目标: 获取`msg`的数据, 对外API
  191. */
  192. void *getMessageData(af_Message *msg) {
  193. return msg->msg;
  194. }
  195. af_Message *popMessageDown(char *type, af_Environment *env) {
  196. for (af_Message **pmsg = &env->activity->msg_down; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  197. if (EQ_STR((*pmsg)->type, type)) {
  198. af_Message *msg = *pmsg;
  199. *pmsg = msg->next;
  200. msg->next = NULL;
  201. return msg;
  202. }
  203. }
  204. return NULL;
  205. }
  206. af_Message *getFirstMessage(af_Environment *env) {
  207. af_Message *msg = env->activity->msg_down;
  208. env->activity->msg_down = msg->next;
  209. msg->next = NULL;
  210. return msg;
  211. }
  212. void connectMessage(af_Message **base, af_Message *msg) {
  213. while (*base != NULL)
  214. base = &((*base)->next);
  215. *base = msg;
  216. }
  217. static af_EnvVar *makeEnvVar(char *name, char *data) {
  218. af_EnvVar *var = calloc(sizeof(af_EnvVar), 1);
  219. var->name = strCopy(name);
  220. var->data = strCopy(data);
  221. return var;
  222. }
  223. static af_EnvVar *freeEnvVar(af_EnvVar *var) {
  224. af_EnvVar *next = var->next;
  225. free(var->data);
  226. free(var->name);
  227. free(var);
  228. return next;
  229. }
  230. static void freeAllEnvVar(af_EnvVar *var) {
  231. while (var != NULL)
  232. var = freeEnvVar(var);
  233. }
  234. static af_EnvVarSpace *makeEnvVarSpace(void) {
  235. af_EnvVarSpace *esv = calloc(sizeof(af_EnvVarSpace), 1);
  236. return esv;
  237. }
  238. static void freeEnvVarSpace(af_EnvVarSpace *evs) {
  239. for (int i = 0; i < ENV_VAR_HASH_SIZE; i++)
  240. freeAllEnvVar(evs->var[i]);
  241. free(evs);
  242. }
  243. void setEnvVar(char *name, char *data, af_Environment *env) {
  244. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  245. af_EnvVar **pvar = &env->esv->var[index];
  246. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  247. if (EQ_STR((*pvar)->name, name)) {
  248. free((*pvar)->data);
  249. (*pvar)->data = strCopy(data);
  250. return;
  251. }
  252. }
  253. *pvar = makeEnvVar(name, data);
  254. }
  255. char *findEnvVar(char *name, af_Environment *env) {
  256. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  257. af_EnvVar **pvar = &env->esv->var[index];
  258. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  259. if (EQ_STR((*pvar)->name, name))
  260. return (*pvar)->data;
  261. }
  262. return NULL;
  263. }
  264. af_Environment *makeEnvironment(void) {
  265. af_Environment *env = calloc(sizeof(af_Environment), 1);
  266. env->core = makeCore();
  267. env->esv = makeEnvVarSpace();
  268. return env;
  269. }
  270. bool addTopActivity(af_Code *code, af_Environment *env) {
  271. if (env->activity != NULL)
  272. return false;
  273. env->activity = makeActivity(NULL, code, false, NULL, NULL, env->core->global, NULL);
  274. env->activity->new_vs_count = 2;
  275. env->activity->var_list = makeVarSpaceList(env->core->global->data->var_space);
  276. env->activity->var_list->next = makeVarSpaceList(env->core->protect);
  277. env->activity->status = act_normal;
  278. return true;
  279. }
  280. bool enableEnvironment(af_Environment *env) {
  281. return enableCore(env->core);
  282. }
  283. void freeEnvironment(af_Environment *env) {
  284. freeCore(env->core);
  285. freeAllActivity(env->activity);
  286. freeEnvVarSpace(env->esv);
  287. free(env);
  288. }
  289. bool addVarToProtectVarSpace(af_Var *var, af_Environment *env) {
  290. return addVarToVarSpace(var, env->core->protect);
  291. }
  292. bool pushExecutionActivity(af_Code *bt, bool return_first, af_Environment *env) {
  293. af_Code *next;
  294. if (!getCodeBlockNext(bt, &next))
  295. return false;
  296. af_Activity *activity = makeActivity(bt, bt->next, return_first, env->activity->msg_up,
  297. env->activity->var_list, env->activity->belong,
  298. env->activity->func);
  299. env->activity->bt_next = next;
  300. activity->prev = env->activity;
  301. env->activity = activity;
  302. env->activity->status = act_normal;
  303. return true;
  304. }
  305. bool pushFuncActivity(af_Code *bt, af_Environment *env) {
  306. af_Code *next;
  307. if (!getCodeBlockNext(bt, &next))
  308. return false;
  309. af_Activity *activity = makeActivity(bt, bt->next, false, env->activity->msg_up,
  310. env->activity->var_list, env->activity->belong,
  311. env->activity->func);
  312. env->activity->bt_next = next;
  313. activity->prev = env->activity;
  314. env->activity = activity;
  315. env->activity->status = act_func;
  316. return true;
  317. }
  318. bool setFuncActivityToArg(af_Object *func, af_Environment *env) {
  319. gc_delReference(env->activity->belong);
  320. if (env->activity->func != NULL)
  321. gc_delReference(env->activity->func);
  322. gc_addReference(func);
  323. gc_addReference(func->belong); // TODO-szh 处理belong为NULL的情况 (global的belong)
  324. env->activity->func = func;
  325. env->activity->belong = func->belong;
  326. env->activity->status = act_arg;
  327. // TODO-szh 参数处理(计算)
  328. return true;
  329. }
  330. bool setFuncActivityAddVar(af_VarSpaceListNode *vsl, bool new_vsl, bool is_protect, char **msg_type, af_Environment *env) {
  331. if (env->activity->new_vs_count != 0 || !new_vsl && is_protect)
  332. return false;
  333. if (vsl != NULL)
  334. env->activity->var_list = vsl;
  335. if (new_vsl) {
  336. env->activity->var_list = pushNewVarList(env->activity->var_list);
  337. env->activity->new_vs_count = 1;
  338. }
  339. env->activity->msg_type = msg_type;
  340. // TODO-szh 参数处理(赋值)
  341. env->activity->var_list->vs->is_protect = is_protect;
  342. return true;
  343. }
  344. bool setFuncActivityToNormal(af_Code *bt, af_Environment *env) {
  345. env->activity->bt_start = bt;
  346. env->activity->bt_next = bt;
  347. env->activity->status = act_normal;
  348. return true;
  349. }
  350. void popActivity(af_Message *msg, af_Environment *env) {
  351. if (env->activity->prev != NULL) {
  352. af_Message *new_msg;
  353. if (msg != NULL) {
  354. new_msg = msg;
  355. msg->next = env->activity->msg_down;
  356. } else
  357. msg = env->activity->msg_down;
  358. env->activity->msg_down = NULL;
  359. connectMessage(&new_msg, env->activity->prev->msg_down);
  360. env->activity->prev->msg_down = new_msg;
  361. } else if (msg != NULL) { // 到顶 且 msg != NULL
  362. gc_delReference(*(af_Object **)msg->msg);
  363. freeMessage(msg);
  364. }
  365. env->activity = freeActivity(env->activity);
  366. }