env.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. #include "__env.h"
  2. /* Core 创建和释放 */
  3. static af_Core *makeCore(enum GcRunTime grt);
  4. static void freeCore(af_Environment *env);
  5. /* Core 初始化 */
  6. static bool enableCore(af_Core *core);
  7. static void checkInherit(af_Inherit **ih, af_Object *obj);
  8. /* Activity 创建和释放 */
  9. static af_Activity *makeActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  10. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func);
  11. static af_Activity *freeActivity(af_Activity *activity);
  12. static void freeAllActivity(af_Activity *activity);
  13. static void clearActivity(af_Activity *activity);
  14. /* Activity 相关处理函数 */
  15. static void freeMark(af_Environment *env);
  16. static void newActivity(af_Code *bt, const af_Code *next, bool return_first, af_Environment *env);
  17. static void freeMarkByActivity(af_Activity *activity);
  18. /* 环境变量 创建与释放 */
  19. static af_EnvVar *makeEnvVar(char *name, char *data);
  20. static af_EnvVar *freeEnvVar(af_EnvVar *var);
  21. static void freeAllEnvVar(af_EnvVar *var);
  22. static void freeEnvVarSpace(af_EnvVarSpace *evs);
  23. /* 顶层消息处理器 创建与释放 */
  24. static af_TopMsgProcess *makeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func);
  25. static af_TopMsgProcess *freeTopMsgProcess(af_TopMsgProcess *mp);
  26. static void freeAllTopMsgProcess(af_TopMsgProcess *mp);
  27. /* 顶层消息处理器 处理函数 */
  28. static void *findTopMsgProcessFunc(char *type, af_Environment *env);
  29. static void runTopMessageProcess(af_Environment *env);
  30. /* LiteralData 创建与释放 */
  31. static af_LiteralDataList *makeLiteralDataList(char *data);
  32. static af_LiteralDataList *freeLiteralData_Pri(af_LiteralDataList *ld);
  33. static af_Core *makeCore(enum GcRunTime grt) {
  34. af_Core *core = calloc(sizeof(af_Core), 1);
  35. core->in_init = true;
  36. core->protect = makeVarSpaceByCore(core);
  37. core->prefix[V_QUOTE] = '\'';
  38. core->prefix[B_EXEC] = '\'';
  39. core->prefix[B_EXEC_FIRST] = ',';
  40. core->gc_run = grt;
  41. core->gc_count_max = DEFAULT_GC_COUNT_MAX;
  42. return core;
  43. }
  44. /*
  45. * 函数名: freeCore
  46. * 目标: 释放Core
  47. * 因为gc_freeAllValue需要env作为参数, 故使用env作为freeCore的参数
  48. */
  49. static void freeCore(af_Environment *env) {
  50. printGCByCode(env->core);
  51. gc_freeAllValue(env);
  52. free(env->core);
  53. }
  54. char setPrefix(size_t name, char prefix, af_Environment *env) {
  55. char old = env->core->prefix[name];
  56. if (name >= PREFIX_SIZE || prefix == NUL)
  57. return NUL;
  58. env->core->prefix[name] = prefix;
  59. return old;
  60. }
  61. char getPrefix(size_t name, af_Environment *env) {
  62. return env->core->prefix[name];
  63. }
  64. af_VarSpace *getProtectVarSpace(af_Environment *env) {
  65. return env->core->protect;
  66. }
  67. /*
  68. * 函数名: getBaseObjectFromCore
  69. * 目标: 从VarSpace中获取一个量
  70. * 作用: 用于init初始化时在保护空间获得一些初始化对象
  71. */
  72. af_Object *getBaseObjectFromCore(char *name, af_Core *core) {
  73. af_Var *var = findVarFromVarSpace(name, core->protect);
  74. if (var != NULL)
  75. return var->vn->obj;
  76. return NULL;
  77. }
  78. /*
  79. * 函数名: getBaseObject
  80. * 目标: getBaseObjectFromCore的对外接口
  81. */
  82. af_Object *getBaseObject(char *name, af_Environment *env) {
  83. return getBaseObjectFromCore(name, env->core);
  84. }
  85. static void checkInherit(af_Inherit **ih, af_Object *obj) {
  86. while (*ih != NULL) {
  87. if ((*ih)->obj->data == obj->data) {
  88. if ((*ih)->next == NULL && (*ih)->obj == obj) // 最后一个就是obj
  89. return; // 不需要任何更改
  90. *ih = freeInherit(*ih); // 释放该ih
  91. } else
  92. ih = &((*ih)->next);
  93. }
  94. *ih = makeInherit(obj);
  95. }
  96. static bool enableCore(af_Core *core) {
  97. af_Object *object = getBaseObjectFromCore("object", core);
  98. af_Object *global = getBaseObjectFromCore("global", core);
  99. if (global == NULL || global->belong != NULL)
  100. return false; // global未找到 或其有属对象
  101. if (object == NULL || object->data->inherit != NULL || !object->data->allow_inherit)
  102. return false; // object未找到 或其继承自其他对象 或其不可被继承
  103. for (af_Object *obj = core->gc_Object; obj != NULL; obj = obj->gc.next) {
  104. if (obj == global)
  105. continue;
  106. if (obj->belong == NULL)
  107. obj->belong = global;
  108. }
  109. for (af_ObjectData *od = core->gc_ObjectData; od != NULL; od = od->gc.next) {
  110. if (od == object->data)
  111. continue;
  112. checkInherit(&od->inherit, object);
  113. }
  114. core->global = global;
  115. core->object = object;
  116. core->protect->is_protect = true;
  117. core->in_init = false;
  118. return true;
  119. }
  120. static af_Activity *makeActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  121. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func) {
  122. af_Activity *activity = calloc(sizeof(af_Activity), 1);
  123. activity->status = act_func;
  124. activity->msg_up = msg_up;
  125. activity->msg_up_count = 0;
  126. activity->var_list = vsl;
  127. activity->new_vs_count = 0;
  128. activity->belong = belong;
  129. activity->func = func;
  130. activity->bt_top = bt_top;
  131. activity->bt_start = bt_start;
  132. activity->bt_next = bt_start;
  133. activity->return_first = return_first;
  134. return activity;
  135. }
  136. static af_Activity *freeActivity(af_Activity *activity) {
  137. af_Activity *prev = activity->prev;
  138. freeAllMessage(activity->msg_down); // msg转移后需要将对应成员设置为NULL
  139. freeMessageCount(activity->msg_up_count, activity->msg_up);
  140. // vsl 是引用自 var_list和func_var_list的 故不释放
  141. // func_var_list 是引用自函数的 故不释放
  142. freeVarSpaceListCount(activity->new_vs_count, activity->var_list);
  143. freeVarSpaceListCount(activity->macro_vs_count, activity->macro_vsl);
  144. freeAllArgCodeList(activity->acl_start);
  145. if (activity->fi != NULL)
  146. freeFuncInfo(activity->fi);
  147. freeAllLiteralData(activity->ld);
  148. free(activity);
  149. return prev;
  150. }
  151. static void freeAllActivity(af_Activity *activity) {
  152. while (activity != NULL)
  153. activity = freeActivity(activity);
  154. }
  155. static void clearActivity(af_Activity *activity) {
  156. freeMarkByActivity(activity);
  157. freeVarSpaceListCount(activity->macro_vs_count, activity->macro_vsl);
  158. freeAllArgCodeList(activity->acl_start);
  159. if (activity->fi != NULL)
  160. freeFuncInfo(activity->fi);
  161. activity->func_var_list = NULL;
  162. activity->bt_top = NULL;
  163. activity->bt_start = NULL;
  164. activity->bt_next = NULL;
  165. activity->acl_start = NULL;
  166. activity->acl_done = NULL;
  167. activity->fi = NULL;
  168. activity->body_next = NULL;
  169. }
  170. /*
  171. * 函数名: makeLiteralDataList
  172. * 目标: 生成一个 af_LiteralDataList
  173. * 注意: char *data 要求传入一个已经被复制的data值
  174. * makeLiteralDataList是内部函数, 属于可控函数, 因此data在函数内部不再复制
  175. */
  176. static af_LiteralDataList *makeLiteralDataList(char *data) {
  177. af_LiteralDataList *ld = calloc(sizeof(af_LiteralDataList), 1);
  178. ld->literal_data = data;
  179. return ld;
  180. }
  181. static af_LiteralDataList *freeLiteralData_Pri(af_LiteralDataList *ld) {
  182. af_LiteralDataList *next = ld->next;
  183. free(ld->literal_data);
  184. free(ld);
  185. return next;
  186. }
  187. void freeAllLiteralData(af_LiteralDataList *ld) {
  188. while (ld != NULL)
  189. ld = freeLiteralData_Pri(ld);
  190. }
  191. void pushLiteralData(char *data, af_Environment *env) {
  192. af_LiteralDataList *ld = makeLiteralDataList(data);
  193. ld->next = env->activity->ld;
  194. env->activity->ld = ld;
  195. }
  196. af_Message *makeMessage(char *type, size_t size) {
  197. af_Message *msg = calloc(sizeof(af_Message), 1);
  198. msg->type = strCopy(type);
  199. if (size != 0)
  200. msg->msg = calloc(size, 1);
  201. msg->size = size;
  202. return msg;
  203. }
  204. af_Message *freeMessage(af_Message *msg) {
  205. af_Message *next = msg->next;
  206. free(msg->type);
  207. free(msg->msg);
  208. free(msg);
  209. return next;
  210. }
  211. void freeAllMessage(af_Message *msg) {
  212. while (msg != NULL)
  213. msg = freeMessage(msg);
  214. }
  215. bool freeMessageCount(size_t count, af_Message *msg) {
  216. for (size_t i = count; i > 0; i--) {
  217. if (msg == NULL) // 发生了错误
  218. return false;
  219. msg = freeMessage(msg);
  220. }
  221. return true;
  222. }
  223. void pushMessageUp(af_Message *msg, af_Environment *env) {
  224. msg->next = env->activity->msg_up;
  225. env->activity->msg_up = msg;
  226. env->activity->msg_up_count++;
  227. }
  228. void pushMessageDown(af_Message *msg, af_Environment *env) {
  229. msg->next = env->activity->msg_down;
  230. env->activity->msg_down = msg;
  231. }
  232. void *popMessageUpData(char *type, af_Environment *env) {
  233. for (af_Message **pmsg = &env->activity->msg_up; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  234. if (EQ_STR((*pmsg)->type, type))
  235. return (*pmsg)->msg; // msg_up是只读的
  236. }
  237. return NULL;
  238. }
  239. af_Message *popMessageUp(af_Environment *env) {
  240. if (env->activity->msg_up_count == 0 || env->activity->msg_up == NULL)
  241. return NULL;
  242. af_Message *msg = env->activity->msg_up;
  243. env->activity->msg_up = msg->next;
  244. msg->next = NULL;
  245. env->activity->msg_up_count--;
  246. return msg;
  247. }
  248. /*
  249. * 函数名: getMessageData
  250. * 目标: 获取`msg`的数据, 对外API
  251. */
  252. void *getMessageData(af_Message *msg) {
  253. return msg->msg;
  254. }
  255. af_Message *popMessageDown(char *type, af_Environment *env) {
  256. for (af_Message **pmsg = &env->activity->msg_down; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  257. if (EQ_STR((*pmsg)->type, type)) {
  258. af_Message *msg = *pmsg;
  259. *pmsg = msg->next;
  260. msg->next = NULL;
  261. return msg;
  262. }
  263. }
  264. return NULL;
  265. }
  266. af_Message *getFirstMessage(af_Environment *env) {
  267. af_Message *msg = env->activity->msg_down;
  268. env->activity->msg_down = msg->next;
  269. msg->next = NULL;
  270. return msg;
  271. }
  272. void connectMessage(af_Message **base, af_Message *msg) {
  273. while (*base != NULL)
  274. base = &((*base)->next);
  275. *base = msg;
  276. }
  277. void mp_NORMAL(af_Message *msg, af_Environment *env) {
  278. if (msg->msg == NULL || *(af_Object **)msg->msg == NULL) {
  279. printf("msg: %p error\n", msg->msg);
  280. return;
  281. }
  282. gc_delReference(*(af_Object **)msg->msg);
  283. printf("NORMAL Point: %p\n", *(af_Object **)msg->msg);
  284. }
  285. af_Message *makeNORMALMessage(af_Object *obj) {
  286. af_Message *msg = makeMessage("NORMAL", sizeof(af_Object *));
  287. *(af_Object **)msg->msg = obj; // env->activity->return_obj本来就有一个gc_Reference
  288. gc_addReference(obj);
  289. return msg;
  290. }
  291. static af_EnvVar *makeEnvVar(char *name, char *data) {
  292. af_EnvVar *var = calloc(sizeof(af_EnvVar), 1);
  293. var->name = strCopy(name);
  294. var->data = strCopy(data);
  295. return var;
  296. }
  297. static af_EnvVar *freeEnvVar(af_EnvVar *var) {
  298. af_EnvVar *next = var->next;
  299. free(var->data);
  300. free(var->name);
  301. free(var);
  302. return next;
  303. }
  304. static void freeAllEnvVar(af_EnvVar *var) {
  305. while (var != NULL)
  306. var = freeEnvVar(var);
  307. }
  308. static af_EnvVarSpace *makeEnvVarSpace(void) {
  309. af_EnvVarSpace *esv = calloc(sizeof(af_EnvVarSpace), 1);
  310. return esv;
  311. }
  312. static void freeEnvVarSpace(af_EnvVarSpace *evs) {
  313. for (int i = 0; i < ENV_VAR_HASH_SIZE; i++)
  314. freeAllEnvVar(evs->var[i]);
  315. free(evs);
  316. }
  317. void setEnvVar(char *name, char *data, af_Environment *env) {
  318. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  319. af_EnvVar **pvar = &env->esv->var[index];
  320. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  321. if (EQ_STR((*pvar)->name, name)) {
  322. free((*pvar)->data);
  323. (*pvar)->data = strCopy(data);
  324. return;
  325. }
  326. }
  327. *pvar = makeEnvVar(name, data);
  328. }
  329. char *findEnvVar(char *name, af_Environment *env) {
  330. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  331. af_EnvVar **pvar = &env->esv->var[index];
  332. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  333. if (EQ_STR((*pvar)->name, name))
  334. return (*pvar)->data;
  335. }
  336. return NULL;
  337. }
  338. af_Environment *makeEnvironment(enum GcRunTime grt) {
  339. af_Environment *env = calloc(sizeof(af_Environment), 1);
  340. DLC_SYMBOL(TopMsgProcessFunc) func = MAKE_SYMBOL(mp_NORMAL, TopMsgProcessFunc);
  341. env->core = makeCore(grt);
  342. env->esv = makeEnvVarSpace();
  343. addTopMsgProcess("NORMAL", func, env);
  344. FREE_SYMBOL(func);
  345. return env;
  346. }
  347. bool addTopActivity(af_Code *code, af_Environment *env) {
  348. if (env->activity != NULL)
  349. return false;
  350. env->activity = makeActivity(NULL, code, false, NULL, NULL, env->core->global, NULL);
  351. env->activity->new_vs_count = 2;
  352. env->activity->var_list = makeVarSpaceList(env->core->global->data->var_space);
  353. env->activity->var_list->next = makeVarSpaceList(env->core->protect);
  354. env->activity->status = act_normal;
  355. return true;
  356. }
  357. bool enableEnvironment(af_Environment *env) {
  358. return enableCore(env->core);
  359. }
  360. void freeEnvironment(af_Environment *env) {
  361. freeCore(env);
  362. freeAllActivity(env->activity);
  363. freeEnvVarSpace(env->esv);
  364. freeAllTopMsgProcess(env->process);
  365. free(env);
  366. }
  367. void checkRunGC(af_Environment *env) {
  368. if (env->core->gc_run == grt_always ||
  369. env->core->gc_run == grt_count && env->core->gc_count >= env->core->gc_count_max) {
  370. gc_RunGC(env);
  371. }
  372. }
  373. bool addVarToProtectVarSpace(af_Var *var, af_Environment *env) {
  374. return addVarToVarSpace(var, env->core->protect);
  375. }
  376. static af_TopMsgProcess *makeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func) {
  377. af_TopMsgProcess *mp = calloc(sizeof(af_TopMsgProcess), 1);
  378. mp->type = strCopy(type);
  379. mp->func = COPY_SYMBOL(func, TopMsgProcessFunc);
  380. return mp;
  381. }
  382. static af_TopMsgProcess *freeTopMsgProcess(af_TopMsgProcess *mp) {
  383. af_TopMsgProcess *next = mp->next;
  384. free(mp->type);
  385. FREE_SYMBOL(mp->func);
  386. free(mp);
  387. return next;
  388. }
  389. static void freeAllTopMsgProcess(af_TopMsgProcess *mp) {
  390. while (mp != NULL)
  391. mp = freeTopMsgProcess(mp);
  392. }
  393. static void *findTopMsgProcessFunc(char *type, af_Environment *env) {
  394. af_TopMsgProcess *mp = env->process;
  395. for (NULL; mp != NULL; mp = mp->next) {
  396. if (EQ_STR(type, mp->type))
  397. return mp;
  398. }
  399. return NULL;
  400. }
  401. void addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
  402. af_Environment *env) {
  403. af_TopMsgProcess *mp = makeTopMsgProcess(type, func);
  404. mp->next = env->process;
  405. env->process = mp;
  406. }
  407. bool changeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
  408. af_Environment *env) {
  409. af_TopMsgProcess *mp = findTopMsgProcessFunc(type, env);
  410. if (mp == NULL)
  411. return false;
  412. FREE_SYMBOL(mp->func);
  413. mp->func = COPY_SYMBOL(func, TopMsgProcessFunc);
  414. return true;
  415. }
  416. static void newActivity(af_Code *bt, const af_Code *next, bool return_first, af_Environment *env){
  417. if (next == NULL && env->activity->body_next == NULL) {
  418. printf("Tail tone recursive optimization\n");
  419. clearActivity(env->activity);
  420. env->activity->bt_top = bt;
  421. if (!env->activity->return_first) // 若原本就有设置 return_first 则没有在设置的必要了, 因为该执行不会被返回
  422. env->activity->return_first = return_first;
  423. } else {
  424. af_Activity *activity = makeActivity(bt, NULL, return_first, env->activity->msg_up,
  425. env->activity->var_list, env->activity->belong,
  426. env->activity->func);
  427. activity->prev = env->activity;
  428. env->activity = activity;
  429. }
  430. }
  431. bool pushExecutionActivity(af_Code *bt, bool return_first, af_Environment *env) {
  432. af_Code *next;
  433. if (!getCodeBlockNext(bt, &next)) {
  434. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  435. return false;
  436. }
  437. env->activity->bt_next = next;
  438. newActivity(bt, next, return_first, env);
  439. env->activity->bt_start = bt->next;
  440. env->activity->bt_next = bt->next;
  441. env->activity->status = act_normal;
  442. return true;
  443. }
  444. bool pushFuncActivity(af_Code *bt, af_Environment *env) {
  445. af_Code *next;
  446. af_Code *func;
  447. af_Object *parentheses_call = env->activity->parentheses_call;
  448. env->activity->parentheses_call = NULL;
  449. if (!getCodeBlockNext(bt, &next)) {
  450. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  451. return false;
  452. }
  453. if (bt->block.type == curly) { // 大括号
  454. if (bt->block.elements == 0) {
  455. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  456. return false;
  457. } else
  458. func = bt->next;
  459. } else if (bt->block.type == brackets) { // 暂时不考虑中括号
  460. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  461. return false;
  462. } else
  463. func = NULL; // 小括号则不在需要匹配
  464. env->activity->bt_next = next;
  465. newActivity(bt, next, false, env);
  466. env->activity->bt_start = func;
  467. env->activity->bt_next = func;
  468. env->activity->call_type = env->activity->bt_top->block.type;
  469. env->activity->status = act_func;
  470. if (env->activity->call_type == parentheses) // 对于类前缀调用, 已经获得func的实际值了
  471. return setFuncActivityToArg(parentheses_call, env);
  472. return true;
  473. }
  474. bool pushLiteralActivity(af_Code *bt, af_Object *func, af_Environment *env) {
  475. char *literal_data = strCopy(bt->literal.literal_data); // newActivity可能会导致code和literal_data释放
  476. env->activity->bt_next = bt->next;
  477. /* 隐式调用不设置 bt_top */
  478. newActivity(NULL, bt->next, false, env); // 如果原activity也是字面量, 则不进行尾调递归优化
  479. env->activity->is_literal = true;
  480. pushLiteralData(literal_data, env);
  481. return setFuncActivityToArg(func, env);
  482. }
  483. bool pushVariableActivity(af_Code *bt, af_Object *func, af_Environment *env) {
  484. env->activity->bt_next = bt->next;
  485. /* 隐式调用不设置 bt_top */
  486. newActivity(bt, bt->next, false, env);
  487. return setFuncActivityToArg(func, env);
  488. }
  489. bool pushMacroFuncActivity(af_Object *func, af_Environment *env) {
  490. /* Macro是隐式调用, bt不移动 */
  491. /* 沿用activity */
  492. printf("Run macro\n");
  493. if (!freeVarSpaceListCount(env->activity->new_vs_count, env->activity->var_list)) { // 释放外部变量空间
  494. env->activity->new_vs_count = 0;
  495. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  496. return false;
  497. }
  498. env->activity->var_list = env->activity->macro_vsl;
  499. env->activity->new_vs_count = env->activity->macro_vs_count;
  500. env->activity->macro_vs_count = 0;
  501. clearActivity(env->activity); /* 隐式调用不设置 bt_top */
  502. return setFuncActivityToArg(func, env);
  503. }
  504. bool setFuncActivityToArg(af_Object *func, af_Environment *env) {
  505. obj_funcGetArgCodeList *get_acl = findAPI("obj_funcGetArgCodeList", func->data->api);
  506. obj_funcGetVarList *get_var_list = findAPI("obj_funcGetVarList", func->data->api);
  507. if (get_var_list == NULL || get_acl == NULL) {
  508. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  509. return false;
  510. }
  511. env->activity->func = func;
  512. env->activity->belong = getBelongObject(func, env);
  513. env->activity->status = act_arg;
  514. /* 遇到错误时 get_acl 和 get_var_list 要自行设定msg */
  515. if (!get_acl(&env->activity->acl_start, func, env->activity->bt_top, &env->activity->mark, env)) // 设置acl
  516. return false;
  517. if (!get_var_list(&env->activity->func_var_list, func, env->activity->mark, env)) // 设置 func_var_list
  518. return false;
  519. env->activity->acl_done = env->activity->acl_start;
  520. if (env->activity->acl_done != NULL)
  521. env->activity->bt_next = env->activity->acl_done->code;
  522. else
  523. env->activity->bt_next = NULL;
  524. return true;
  525. }
  526. bool setFuncActivityAddVar(af_Environment *env){
  527. obj_funcGetInfo *get_info = findAPI("obj_funcGetInfo", env->activity->func->data->api);
  528. obj_funcGetArgList *get_arg_list = findAPI("obj_funcGetArgList", env->activity->func->data->api);
  529. af_ArgList *al;
  530. if (get_info == NULL || get_arg_list == NULL) {
  531. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  532. return false;
  533. }
  534. if (!get_info(&env->activity->fi, env->activity->func, env->activity->bt_top, env->activity->mark, env))
  535. return false;
  536. if (env->activity->fi == NULL) {
  537. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  538. return false;
  539. }
  540. if (env->activity->fi->scope == super_pure_scope && env->activity->fi->scope == super_embedded) {
  541. /* 超纯函数和超内嵌函数不得搭配使用 */
  542. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  543. return false;
  544. }
  545. env->activity->body_next = env->activity->fi->body;
  546. if (env->activity->fi->is_macro) { // 是宏函数则保存变量空间
  547. env->activity->macro_vsl = env->activity->var_list;
  548. env->activity->macro_vs_count = env->activity->new_vs_count;
  549. } else if (env->activity->fi->scope != inline_scope) { // 非内联函数, 释放外部变量空间
  550. if (!freeVarSpaceListCount(env->activity->new_vs_count, env->activity->var_list)) {
  551. pushMessageDown(makeMessage("ERROR-STR", 0), env); // 释放失败
  552. return false;
  553. }
  554. }
  555. if (env->activity->fi->scope == normal_scope) { // 使用函数变量空间
  556. env->activity->var_list = env->activity->func_var_list;
  557. env->activity->new_vs_count = 0;
  558. } else if (env->activity->fi->scope == pure_scope) { // 纯函数只有 protect 变量空间
  559. env->activity->var_list = makeVarSpaceList(env->core->protect);
  560. env->activity->new_vs_count = 0;
  561. } else if (env->activity->fi->scope == super_pure_scope) { // 超纯函数没有变量空间, 因此不得为超内嵌函数(否则var_list就为NULL了)
  562. env->activity->var_list = NULL;
  563. env->activity->new_vs_count = 0;
  564. }
  565. if (env->activity->fi->embedded != super_embedded) { // 不是超内嵌函数则引入一层新的变量空间
  566. env->activity->var_list = pushNewVarList(env->activity->var_list, env);
  567. env->activity->new_vs_count++;
  568. }
  569. env->activity->func_var_list = NULL;
  570. if (env->activity->fi->var_this && env->activity->belong != NULL) {
  571. if (!makeVarToVarSpaceList("this", 3, 3, env->activity->belong, env->activity->var_list, env)) {
  572. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  573. return false;
  574. }
  575. }
  576. if (env->activity->fi->var_func && env->activity->func != NULL) {
  577. if (!makeVarToVarSpaceList("func", 3, 3, env->activity->func, env->activity->var_list, env)) {
  578. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  579. return false;
  580. }
  581. }
  582. if (!get_arg_list(&al, env->activity->func, env->activity->acl_start, env->activity->mark, env)) {
  583. pushMessageDown(makeMessage("ERROR-STR", 0), env);
  584. return false;
  585. }
  586. runArgList(al, env->activity->var_list, env);
  587. freeAllArgList(al);
  588. if (env->activity->fi->embedded == protect_embedded)
  589. env->activity->var_list->vs->is_protect = true;
  590. int status = setFuncActivityToNormal(env);
  591. if (status == -1) {
  592. popActivity(makeMessage("ERROR-STR", 0), env);
  593. return false;
  594. } else if (status == 0)
  595. env->process_msg_first = true; // 先不弹出activity, 通过act_normal处理msg
  596. return true;
  597. }
  598. int setFuncActivityToNormal(af_Environment *env){ // 获取函数的函数体
  599. env->activity->status = act_normal;
  600. env->activity->bt_next = NULL;
  601. if (env->activity->body_next == NULL) // 已经没有下一步了 (原msg不释放)
  602. return -1;
  603. while (env->activity->body_next != NULL) {
  604. if (env->activity->body_next->type == func_body_c) {
  605. GET_SYMBOL(env->activity->body_next->c_func)(env->activity->mark, env);
  606. env->activity->body_next = env->activity->body_next->next;
  607. } else {
  608. env->activity->bt_start = env->activity->body_next->code;
  609. env->activity->bt_next = env->activity->body_next->code;
  610. env->activity->body_next = env->activity->body_next->next;
  611. return 1; // 仍有下一步
  612. }
  613. }
  614. return 0; // 没有下一步, 但运行了C函数 (原msg释放)
  615. }
  616. /*
  617. * 函数名: runTopMessageProcess
  618. * 目标: 运行顶层信息处理器
  619. */
  620. static void runTopMessageProcess(af_Environment *env) {
  621. af_Message **pmsg = &env->activity->msg_down;
  622. while (*pmsg != NULL) {
  623. af_TopMsgProcess *mp = findTopMsgProcessFunc((*pmsg)->type, env);
  624. if (mp != NULL) {
  625. GET_SYMBOL(mp->func)(*pmsg, env);
  626. *pmsg = freeMessage(*pmsg);
  627. } else
  628. pmsg = &((*pmsg)->next);
  629. }
  630. }
  631. static void freeMarkByActivity(af_Activity *activity) {
  632. if (activity->func != NULL) {
  633. obj_funcFreeMask *func = findAPI("obj_funcFreeMask", activity->func->data->api);
  634. if (func != NULL)
  635. func(activity->mark);
  636. activity->mark = NULL;
  637. }
  638. }
  639. static void freeMark(af_Environment *env) {
  640. if (env->activity->func != NULL) {
  641. obj_funcFreeMask *func = findAPI("obj_funcFreeMask", env->activity->func->data->api);
  642. if (func != NULL)
  643. func(env->activity->mark);
  644. env->activity->mark = NULL;
  645. }
  646. }
  647. void popActivity(af_Message *msg, af_Environment *env) {
  648. env->process_msg_first = true;
  649. if (env->activity->return_first) {
  650. if (msg != NULL) {
  651. gc_delReference(*(af_Object **)msg->msg);
  652. freeMessage(msg);
  653. }
  654. if (env->activity->return_obj == NULL)
  655. msg = makeMessage("ERROR-STR", 0);
  656. else {
  657. msg = makeNORMALMessage(env->activity->return_obj);
  658. env->activity->return_obj = NULL;
  659. }
  660. }
  661. freeMark(env);
  662. if (env->activity->prev != NULL) {
  663. af_Message *new_msg;
  664. if (msg != NULL) {
  665. new_msg = msg;
  666. msg->next = env->activity->msg_down;
  667. } else
  668. new_msg = env->activity->msg_down;
  669. env->activity->msg_down = NULL;
  670. connectMessage(&new_msg, env->activity->prev->msg_down);
  671. env->activity->prev->msg_down = new_msg;
  672. } else { // 到顶
  673. if (msg != NULL) {
  674. msg->next = env->activity->msg_down;
  675. env->activity->msg_down = msg;
  676. }
  677. runTopMessageProcess(env);
  678. }
  679. env->activity = freeActivity(env->activity);
  680. }