env.c 16 KB

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