env.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. #include "aFunCore.h"
  2. #include "__env.h"
  3. #include "__global_obj.h"
  4. #include "__run.h"
  5. /* Core 创建和释放 */
  6. static af_Core *makeCore(enum GcRunTime grt, af_Environment *env);
  7. static void freeCore(af_Environment *env);
  8. /* Activity 创建和释放 */
  9. static af_Activity *makeActivity(af_Message *msg_up, af_VarSpaceListNode *vsl, af_Object *belong);
  10. static af_Activity *makeFuncActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  11. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func);
  12. static af_Activity *makeTopActivity(af_Code *bt_top, af_Code *bt_start, af_VarSpace *protect, af_Object *belong);
  13. static af_Activity *makeTopImportActivity(af_Code *bt_top, af_Code *bt_start, af_VarSpace *protect, af_Object *belong, char *mark);
  14. static af_Activity *makeGcActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env);
  15. static af_Activity *freeActivity(af_Activity *activity);
  16. static void freeActivityTop(af_Activity *activity);
  17. static void freeAllActivity(af_Activity *activity);
  18. static void clearActivity(af_Activity *activity);
  19. /* Activity 相关处理函数 */
  20. static void freeMark(af_Activity *activity);
  21. static void newActivity(af_Code *bt, const af_Code *next, bool return_first, af_Environment *env);
  22. /* ActivityTrackBack 创建与释放 */
  23. static af_ActivityTrackBack *makeActivityTrackBack(af_Activity *activity);
  24. static af_ActivityTrackBack *freeActivityTrackBack(af_ActivityTrackBack *atb);
  25. static void freeAllActivityTrackBack(af_ActivityTrackBack *atb);
  26. /* 环境变量 创建与释放 */
  27. static af_EnvVar *makeEnvVar(char *name);
  28. static af_EnvVar *freeEnvVar(af_EnvVar *var);
  29. static void freeAllEnvVar(af_EnvVar *var);
  30. static void freeEnvVarSpace(af_EnvVarSpace *evs);
  31. /* 顶层消息处理器 创建与释放 */
  32. static af_TopMsgProcess *makeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func);
  33. static af_TopMsgProcess *freeTopMsgProcess(af_TopMsgProcess *mp);
  34. static void freeAllTopMsgProcess(af_TopMsgProcess *mp);
  35. /* 顶层消息处理器 处理函数 */
  36. static af_TopMsgProcess *findTopMsgProcessFunc(char *type, af_Environment *env);
  37. /* LiteralData 创建与释放 */
  38. static af_LiteralDataList *makeLiteralDataList(char *data);
  39. static af_LiteralDataList *freeLiteralData_Pri(af_LiteralDataList *ld);
  40. /* LiteralRegex 创建与释放 */
  41. static af_LiteralRegex *makeLiteralRegex(char *pattern, char *func, bool in_protect);
  42. static af_LiteralRegex *freeLiteralRegex(af_LiteralRegex *lr);
  43. static void freeAllLiteralRegex(af_LiteralRegex *lr);
  44. /* af_ErrorBacktracking 创建与释放 */
  45. static af_ErrorBacktracking *makeErrorBacktracking(FileLine line, FilePath file, char *note);
  46. static af_ErrorBacktracking *freeErrorBacktracking(af_ErrorBacktracking *ebt);
  47. static void freeAllErrorBacktracking(af_ErrorBacktracking *ebt);
  48. /* af_ErrorBacktracking 相关函数 */
  49. static char *getActivityInfoToBacktracking(af_Activity *activity);
  50. static char *getActivityTrackBackInfoToBacktracking(af_ActivityTrackBack *atb);
  51. static void fprintfNote(FILE *file, char *note);
  52. static void fprintfNoteStderr(char *note);
  53. static void fprintfNoteStdout(char *note);
  54. /* 内置顶层消息处理器 */
  55. static void mp_NORMAL(af_Message *msg, bool is_gc, af_Environment *env);
  56. static void mp_ERROR(af_Message *msg, bool is_gc, af_Environment *env);
  57. static void mp_IMPORT(af_Message *msg, bool is_gc, af_Environment *env);
  58. /* 变量检查函数 */
  59. static bool isInfixFunc(af_Code *code, af_Environment *env);
  60. static af_Core *makeCore(enum GcRunTime grt, af_Environment *env) {
  61. af_Core *core = calloc(1, sizeof(af_Core));
  62. /* 设置默认prefix */
  63. char prefix[PREFIX_SIZE + 1] = "";
  64. prefix[E_QUOTE] = '\'';
  65. prefix[B_EXEC] = '\'';
  66. prefix[B_EXEC_FIRST] = '$';
  67. core->prefix = setEnvVarData_(ev_sys_prefix, prefix, env);
  68. core->gc_runtime = setEnvVarNumber_(ev_grt, grt, env);
  69. core->gc_max = setEnvVarNumber_(ev_gcmax, DEFAULT_GC_COUNT_MAX, env);
  70. core->gc_count = setEnvVarNumber_(ev_gccount, 0, env);
  71. core->exit_code_ = setEnvVarNumber_(ev_exit_code, 0, env);
  72. core->argc = setEnvVarNumber_(ev_argc, 0, env);
  73. core->status = core_creat;
  74. core->protect = makeVarSpaceByCore(NULL, 3, 3, 3, core);
  75. return core;
  76. }
  77. /*
  78. * 函数名: freeCore
  79. * 目标: 释放Core
  80. * 因为gc_freeAllValue需要env作为参数, 故使用env作为freeCore的参数
  81. */
  82. static void freeCore(af_Environment *env) {
  83. printGCByCore(env->core);
  84. gc_freeAllValue(env);
  85. freeAllLiteralRegex(env->core->lr);
  86. free(env->core);
  87. }
  88. char setPrefix(size_t name, char prefix, af_Environment *env) {
  89. if (name >= PREFIX_SIZE)
  90. return '-'; // 表示未获取到prefix (NUL在Code中表示无prefix)
  91. char *prefix_ = env->core->prefix->data;
  92. if (prefix_ == NULL || strlen(prefix_) < PREFIX_SIZE)
  93. return '-';
  94. switch (name) {
  95. case E_QUOTE:
  96. if (prefix == NUL && strchr(E_PREFIX, prefix) == NULL)
  97. prefix = '-';
  98. break;
  99. case B_EXEC:
  100. case B_EXEC_FIRST:
  101. if (prefix == NUL && strchr(B_PREFIX, prefix) == NULL)
  102. prefix = '-';
  103. break;
  104. default:
  105. break;
  106. }
  107. char old = prefix_[name];
  108. prefix_[name] = prefix;
  109. return old;
  110. }
  111. char getPrefix(size_t name, af_Environment *env) {
  112. if (name >= PREFIX_SIZE)
  113. return '-'; // 表示未获取到prefix (NUL在Code中表示无prefix)
  114. char *prefix = env->core->prefix->data;
  115. if (prefix == NULL || strlen(prefix) < PREFIX_SIZE)
  116. return '-';
  117. return prefix[name];
  118. }
  119. af_VarSpace *getProtectVarSpace(af_Environment *env) {
  120. return env->core->protect;
  121. }
  122. /*
  123. * 函数名: getBaseObjectFromCore
  124. * 目标: 从VarSpace中获取一个量
  125. * 作用: 用于init初始化时在保护空间获得一些初始化对象
  126. */
  127. af_Object *getBaseObjectFromCore(char *name, af_Core *core) {
  128. af_Var *var = findVarFromVarSpace(name, NULL, core->protect);
  129. if (var != NULL)
  130. return var->vn->obj;
  131. return NULL;
  132. }
  133. /*
  134. * 函数名: getBaseObject
  135. * 目标: getBaseObjectFromCore的对外接口
  136. */
  137. af_Object *getBaseObject(char *name, af_Environment *env) {
  138. return getBaseObjectFromCore(name, env->core);
  139. }
  140. void setCoreStop(af_Environment *env) {
  141. if (env->core->status != core_exit)
  142. env->core->status = core_stop;
  143. }
  144. void setCoreExit(int exit_code, af_Environment *env) {
  145. env->core->status = core_exit;
  146. env->core->exit_code_->num = exit_code;
  147. }
  148. void setCoreNormal(af_Environment *env) {
  149. if (env->core->status == core_exit || env->core->status == core_stop) {
  150. env->core->status = core_normal;
  151. env->core->exit_code_->num = 0;
  152. }
  153. }
  154. static af_Activity *makeActivity(af_Message *msg_up, af_VarSpaceListNode *vsl, af_Object *belong) {
  155. af_Activity *activity = calloc(1, sizeof(af_Activity));
  156. activity->msg_up = msg_up;
  157. activity->msg_up_count = 0;
  158. activity->var_list = vsl;
  159. activity->new_vs_count = 0;
  160. activity->belong = belong;
  161. activity->line = 1;
  162. return activity;
  163. }
  164. static af_Activity *makeFuncActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
  165. af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func) {
  166. af_Activity *activity = makeActivity(msg_up, vsl, belong);
  167. activity->type = act_func;
  168. activity->status = act_func_get;
  169. activity->func = func;
  170. setActivityBtTop(bt_top, activity); // 非NORMAL期间, bt_top被设定
  171. setActivityBtStart(bt_start, activity);
  172. activity->return_first = return_first;
  173. return activity;
  174. }
  175. static af_Activity *makeTopActivity(af_Code *bt_top, af_Code *bt_start, af_VarSpace *protect, af_Object *belong) {
  176. af_Activity *activity = makeActivity(NULL, NULL, belong);
  177. activity->type = act_top;
  178. activity->status = act_func_normal;
  179. activity->new_vs_count = 2;
  180. activity->var_list = makeVarSpaceList(belong->data->var_space);
  181. activity->var_list->next = makeVarSpaceList(protect);
  182. setActivityBtTop(NULL, activity); // top-activity直接就在normal, bt_top将不被设定
  183. setActivityBtStart(bt_start, activity);
  184. return activity;
  185. }
  186. static af_Activity *makeTopImportActivity(af_Code *bt_top, af_Code *bt_start, af_VarSpace *protect, af_Object *belong, char *mark) {
  187. af_Activity *activity = makeTopActivity(bt_top, bt_start, protect, belong);
  188. activity->type = act_top_import;
  189. if (mark != NULL)
  190. activity->import_mark = strCopy(mark);
  191. return activity;
  192. }
  193. static af_Activity *makeGcActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env) {
  194. af_Activity *activity = makeActivity(NULL, NULL, env->core->global);
  195. activity->type = act_gc;
  196. activity->var_list = makeVarSpaceList(getProtectVarSpace(env));
  197. activity->new_vs_count = 1;
  198. activity->file = strCopy("gc.aun.sys");
  199. activity->line = 1;
  200. activity->dl = dl;
  201. activity->pdl = pdl;
  202. activity->dl_next = dl;
  203. return activity;
  204. }
  205. static af_Activity *freeActivity(af_Activity *activity) {
  206. af_Activity *prev = activity->prev;
  207. freeAllMessage(activity->msg_down); // msg转移后需要将对应成员设置为NULL
  208. freeMessageCount(activity->msg_up_count, activity->msg_up);
  209. freeVarSpaceListCount(activity->new_vs_count, activity->var_list);
  210. free(activity->file);
  211. if (activity->type == act_gc) {
  212. if (activity->dl != NULL)
  213. freeAllDestructList(activity->dl);
  214. } else {
  215. // vsl 是引用自 var_list和func_var_list的 故不释放
  216. // func_var_list 是引用自函数的 故不释放
  217. freeVarSpaceListCount(activity->macro_vs_count, activity->macro_vsl);
  218. freeAllArgCodeList(activity->acl_start);
  219. if (activity->fi != NULL)
  220. freeFuncInfo(activity->fi);
  221. freeAllLiteralData(activity->ld);
  222. freeAllActivityTrackBack(activity->tb);
  223. free(activity->import_mark);
  224. }
  225. free(activity);
  226. return prev;
  227. }
  228. static void freeActivityTop(af_Activity *activity) {
  229. freeAllMessage(activity->msg_down); // msg转移后需要将对应成员设置为NULL
  230. freeMessageCount(activity->msg_up_count, activity->msg_up);
  231. setActivityBtTop(NULL, activity);
  232. setActivityBtStart(NULL, activity);
  233. free(activity->file);
  234. }
  235. static void freeAllActivity(af_Activity *activity) {
  236. while (activity != NULL)
  237. activity = freeActivity(activity);
  238. }
  239. /*
  240. * 函数名: clearActivity
  241. * 目标: 尾调用优化时用于清理Activity.
  242. */
  243. static void clearActivity(af_Activity *activity) {
  244. freeVarSpaceListCount(activity->macro_vs_count, activity->macro_vsl);
  245. /* acl在runArgList之后就被释放了 */
  246. /* acl在FuncBody暂时不释放 */
  247. activity->func_var_list = NULL;
  248. setActivityBtTop(NULL, activity);
  249. setActivityBtStart(NULL, activity);
  250. /* acl_start 在 setFuncActivityAddVar 时被释放 */
  251. activity->acl_start = NULL;
  252. activity->acl_done = NULL;
  253. /* activity->fi 暂时不清理, 直到setFuncActivityAddVar时才清理 */
  254. activity->body_next = NULL;
  255. /* mark在setFuncActivityToNormal被清理*/
  256. /* 只有FuncBody执行到最后一个(意味着Mark被清理)后才会有尾调用优化 */
  257. activity->mark = NULL;
  258. /* file和line都遗留 */
  259. }
  260. /*
  261. * 函数名: af_ActivityTrackBack
  262. * 目标: 把 activity 上的内容转移到新的 af_ActivityTrackBack 上
  263. */
  264. static af_ActivityTrackBack *makeActivityTrackBack(af_Activity *activity) {
  265. af_ActivityTrackBack *atb = calloc(1, sizeof(af_ActivityTrackBack));
  266. #define EXCHANGE(name) (atb->name = activity->name)
  267. EXCHANGE(file);
  268. atb->file = strCopy(activity->file);
  269. EXCHANGE(status);
  270. EXCHANGE(line);
  271. EXCHANGE(return_first);
  272. EXCHANGE(run_in_func);
  273. EXCHANGE(is_macro_call);
  274. EXCHANGE(is_gc_call);
  275. EXCHANGE(is_literal);
  276. EXCHANGE(is_obj_func);
  277. EXCHANGE(is_execution);
  278. EXCHANGE(optimization);
  279. #undef EXCHANGE
  280. return atb;
  281. }
  282. static af_ActivityTrackBack *freeActivityTrackBack(af_ActivityTrackBack *atb) {
  283. af_ActivityTrackBack *next = atb->next;
  284. free(atb->file);
  285. free(atb);
  286. return next;
  287. }
  288. static void freeAllActivityTrackBack(af_ActivityTrackBack *atb) {
  289. while (atb != NULL)
  290. atb = freeActivityTrackBack(atb);
  291. }
  292. /*
  293. * 函数名: tailCallActivity
  294. * 目标: 记录ActivityTrackBack然后清除Activity
  295. */
  296. static void tailCallActivity(af_Activity *activity) {
  297. af_ActivityTrackBack *atb = makeActivityTrackBack(activity);
  298. atb->next = activity->tb;
  299. activity->tb = atb;
  300. clearActivity(activity);
  301. }
  302. /*
  303. * 函数名: setActivityBtTop
  304. * 目标: 设置activity的bt_top, 并且设置行号
  305. * bt_start和bt_next会被设置为NULL
  306. */
  307. void setActivityBtTop(af_Code *bt_top, af_Activity *activity) {
  308. activity->bt_top = bt_top;
  309. activity->bt_start = NULL;
  310. activity->bt_next = NULL;
  311. if (bt_top != NULL) {
  312. activity->line = bt_top->line;
  313. if (bt_top->path != NULL) {
  314. free(activity->file);
  315. activity->file = strCopy(bt_top->path);
  316. }
  317. } else
  318. activity->line = 1;
  319. }
  320. /*
  321. * 函数名: setActivityBtStart
  322. * 目标: 设置activity的bt_start, 并且设置行号
  323. * bt_next会被设置为NULL
  324. */
  325. void setActivityBtStart(af_Code *bt_start, af_Activity *activity) {
  326. activity->bt_start = bt_start;
  327. activity->bt_next = bt_start;
  328. if (bt_start != NULL) {
  329. activity->line = bt_start->line;
  330. if (bt_start->path != NULL) {
  331. free(activity->file);
  332. activity->file = strCopy(bt_start->path);
  333. }
  334. } else
  335. activity->line = 1;
  336. }
  337. /*
  338. * 函数名: setActivityBtNext
  339. * 目标: 设置activity的bt_next, 并且设置行号
  340. */
  341. void setActivityBtNext(af_Code *bt_next, af_Activity *activity) {
  342. activity->bt_next = bt_next;
  343. if (bt_next != NULL) {
  344. activity->line = bt_next->line;
  345. if (bt_next->path != NULL) {
  346. free(activity->file);
  347. activity->file = strCopy(bt_next->path);
  348. }
  349. } else
  350. activity->line = 1;
  351. }
  352. /*
  353. * 函数名: makeLiteralDataList
  354. * 目标: 生成一个 af_LiteralDataList
  355. * 注意: char *data 要求传入一个已经被复制的data值
  356. * makeLiteralDataList是内部函数, 属于可控函数, 因此data在函数内部不再复制
  357. */
  358. static af_LiteralDataList *makeLiteralDataList(char *data) {
  359. af_LiteralDataList *ld = calloc(1, sizeof(af_LiteralDataList));
  360. ld->literal_data = data;
  361. return ld;
  362. }
  363. static af_LiteralDataList *freeLiteralData_Pri(af_LiteralDataList *ld) {
  364. af_LiteralDataList *next = ld->next;
  365. free(ld->literal_data);
  366. free(ld);
  367. return next;
  368. }
  369. void freeAllLiteralData(af_LiteralDataList *ld) {
  370. while (ld != NULL)
  371. ld = freeLiteralData_Pri(ld);
  372. }
  373. void pushLiteralData(char *data, af_Environment *env) {
  374. af_LiteralDataList *ld = makeLiteralDataList(data);
  375. ld->next = env->activity->ld;
  376. env->activity->ld = ld;
  377. }
  378. af_Message *makeMessage(char *type, size_t size) {
  379. af_Message *msg = calloc(1, sizeof(af_Message));
  380. msg->type = strCopy(type);
  381. if (size != 0)
  382. msg->msg = calloc(1, size);
  383. msg->size = size;
  384. return msg;
  385. }
  386. af_Message *freeMessage(af_Message *msg) {
  387. af_Message *next = msg->next;
  388. free(msg->type);
  389. free(msg->msg);
  390. free(msg);
  391. return next;
  392. }
  393. void freeAllMessage(af_Message *msg) {
  394. while (msg != NULL)
  395. msg = freeMessage(msg);
  396. }
  397. bool freeMessageCount(size_t count, af_Message *msg) {
  398. for (size_t i = count; i > 0; i--) {
  399. if (msg == NULL) // 发生了错误
  400. return false;
  401. msg = freeMessage(msg);
  402. }
  403. return true;
  404. }
  405. void pushMessageUp(af_Message *msg, af_Environment *env) {
  406. msg->next = env->activity->msg_up;
  407. env->activity->msg_up = msg;
  408. env->activity->msg_up_count++;
  409. }
  410. void pushMessageDown(af_Message *msg, af_Environment *env) {
  411. msg->next = env->activity->msg_down;
  412. env->activity->msg_down = msg;
  413. }
  414. void *popMessageUpData(char *type, af_Environment *env) {
  415. for (af_Message **pmsg = &env->activity->msg_up; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  416. if (EQ_STR((*pmsg)->type, type))
  417. return (*pmsg)->msg; // msg_up是只读的
  418. }
  419. return NULL;
  420. }
  421. af_Message *popMessageUp(af_Environment *env) {
  422. if (env->activity->msg_up_count == 0 || env->activity->msg_up == NULL)
  423. return NULL;
  424. af_Message *msg = env->activity->msg_up;
  425. env->activity->msg_up = msg->next;
  426. msg->next = NULL;
  427. env->activity->msg_up_count--;
  428. return msg;
  429. }
  430. /*
  431. * 函数名: getMessageData
  432. * 目标: 获取`msg`的数据, 对外API
  433. */
  434. void *getMessageData(af_Message *msg) {
  435. return msg->msg;
  436. }
  437. af_Message *popMessageDown(char *type, af_Environment *env) {
  438. for (af_Message **pmsg = &env->activity->msg_down; *pmsg != NULL; pmsg = &((*pmsg)->next)) {
  439. if (EQ_STR((*pmsg)->type, type)) {
  440. af_Message *msg = *pmsg;
  441. *pmsg = msg->next;
  442. msg->next = NULL;
  443. return msg;
  444. }
  445. }
  446. return NULL;
  447. }
  448. af_Message *getFirstMessage(af_Environment *env) {
  449. af_Message *msg = env->activity->msg_down;
  450. env->activity->msg_down = msg->next;
  451. msg->next = NULL;
  452. return msg;
  453. }
  454. void connectMessage(af_Message **base, af_Message *msg) {
  455. while (*base != NULL)
  456. base = &((*base)->next);
  457. *base = msg;
  458. }
  459. af_Message *makeNORMALMessage(af_Object *obj) {
  460. af_Message *msg = makeMessage("NORMAL", sizeof(af_Object *));
  461. *(af_Object **)msg->msg = obj;
  462. gc_addReference(obj);
  463. return msg;
  464. }
  465. af_Message *makeERRORMessage(char *type, char *error, af_Environment *env) {
  466. char *info = getActivityInfoToBacktracking(env->activity);
  467. af_ErrorInfo *ei = makeErrorInfo(type, error, info, env->activity->line, env->activity->file);
  468. free(info);
  469. for (af_ActivityTrackBack *atb = env->activity->tb; atb != NULL; atb = atb->next) {
  470. info = getActivityTrackBackInfoToBacktracking(atb);
  471. pushErrorBacktracking(atb->line, atb->file, info, ei);
  472. free(info);
  473. }
  474. for (af_Activity *activity = env->activity->prev; activity != NULL; activity = activity->prev) {
  475. info = getActivityInfoToBacktracking(activity);
  476. pushErrorBacktracking(activity->line, activity->file, info, ei);
  477. free(info);
  478. for (af_ActivityTrackBack *atb = activity->tb; atb != NULL; atb = atb->next) {
  479. info = getActivityTrackBackInfoToBacktracking(atb);
  480. pushErrorBacktracking(atb->line, atb->file, info, ei);
  481. free(info);
  482. }
  483. }
  484. af_Message *msg = makeMessage("ERROR", sizeof(af_ErrorInfo *));
  485. *(af_ErrorInfo **)msg->msg = ei;
  486. return msg;
  487. }
  488. af_Message *makeERRORMessageFormat(char *type, af_Environment *env, const char *format, ...) {
  489. char buf[1024];
  490. va_list ap;
  491. va_start(ap, format);
  492. vsnprintf(buf, 1024, format, ap);
  493. va_end(ap);
  494. return makeERRORMessage(type, buf, env);;
  495. }
  496. af_Message *makeIMPORTMessage(char *mark, af_Object *obj) {
  497. af_Message *msg = makeMessage("IMPORT", sizeof(af_ImportInfo *));
  498. *(af_ImportInfo **)msg->msg = makeImportInfo(mark, obj);
  499. return msg;
  500. }
  501. static af_EnvVar *makeEnvVar(char *name) {
  502. af_EnvVar *var = calloc(1, sizeof(af_EnvVar));
  503. var->name = strCopy(name);
  504. return var;
  505. }
  506. static af_EnvVar *freeEnvVar(af_EnvVar *var) {
  507. af_EnvVar *next = var->next;
  508. free(var->data);
  509. free(var->name);
  510. free(var);
  511. return next;
  512. }
  513. static void freeAllEnvVar(af_EnvVar *var) {
  514. while (var != NULL)
  515. var = freeEnvVar(var);
  516. }
  517. static af_EnvVarSpace *makeEnvVarSpace(void) {
  518. af_EnvVarSpace *esv = calloc(1, sizeof(af_EnvVarSpace));
  519. return esv;
  520. }
  521. static void freeEnvVarSpace(af_EnvVarSpace *evs) {
  522. for (int i = 0; i < ENV_VAR_HASH_SIZE; i++)
  523. freeAllEnvVar(evs->var[i]);
  524. free(evs);
  525. }
  526. af_EnvVar *setEnvVarData_(char *name, char *data, af_Environment *env) {
  527. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  528. af_EnvVar **pvar = &env->esv->var[index];
  529. env->esv->count++;
  530. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  531. if (EQ_STR((*pvar)->name, name)) {
  532. free((*pvar)->data);
  533. (*pvar)->data = strCopy(data);
  534. return *pvar;
  535. }
  536. }
  537. *pvar = makeEnvVar(name);
  538. (*pvar)->data = strCopy(data);
  539. return *pvar;
  540. }
  541. af_EnvVar *setEnvVarNumber_(char *name, int32_t data, af_Environment *env) {
  542. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  543. af_EnvVar **pvar = &env->esv->var[index];
  544. env->esv->count++;
  545. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  546. if (EQ_STR((*pvar)->name, name)) {
  547. free((*pvar)->data);
  548. (*pvar)->num = data;
  549. return *pvar;
  550. }
  551. }
  552. *pvar = makeEnvVar(name);
  553. (*pvar)->num = data;
  554. return *pvar;
  555. }
  556. void setEnvVarData(char *name, char *data, af_Environment *env) {
  557. setEnvVarData_(name, data, env);
  558. }
  559. void setEnvVarNumber(char *name, int32_t data, af_Environment *env) {
  560. setEnvVarNumber_(name, data, env);
  561. }
  562. char *findEnvVarData(char *name, af_Environment *env) {
  563. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  564. af_EnvVar **pvar = &env->esv->var[index];
  565. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  566. if (EQ_STR((*pvar)->name, name))
  567. return (*pvar)->data;
  568. }
  569. return NULL;
  570. }
  571. int32_t *findEnvVarNumber(char *name, af_Environment *env) {
  572. time33_t index = time33(name) % ENV_VAR_HASH_SIZE;
  573. af_EnvVar **pvar = &env->esv->var[index];
  574. for (NULL; *pvar != NULL; pvar = &((*pvar)->next)) {
  575. if (EQ_STR((*pvar)->name, name))
  576. return &(*pvar)->num; // 返回指针, NULL表示没找到
  577. }
  578. return NULL;
  579. }
  580. static void mp_NORMAL(af_Message *msg, bool is_gc, af_Environment *env) {
  581. if (msg->msg == NULL || *(af_Object **)msg->msg == NULL) {
  582. writeErrorLog(aFunCoreLogger, "NORMAL msg: %p error", msg->msg);
  583. return;
  584. }
  585. gc_delReference(*(af_Object **)msg->msg);
  586. if (!is_gc)
  587. writeDebugLog(aFunCoreLogger, "NORMAL Point: %p", *(af_Object **)msg->msg);
  588. }
  589. static void mp_ERROR(af_Message *msg, bool is_gc, af_Environment *env) {
  590. if (msg->msg == NULL || *(af_ErrorInfo **)msg->msg == NULL) {
  591. writeErrorLog(aFunCoreLogger, "ERROR msg: %p error", msg->msg);
  592. return;
  593. }
  594. if (!is_gc)
  595. fprintfErrorInfoStdout(*(af_ErrorInfo **)msg->msg); // TODO-szh 获取EnvVar, 设定输出的位置 (stdout/stderr)
  596. freeErrorInfo(*(af_ErrorInfo **)msg->msg);
  597. }
  598. static void mp_IMPORT(af_Message *msg, bool is_gc, af_Environment *env) {
  599. if (msg->msg == NULL || *(af_ImportInfo **)msg->msg == NULL) {
  600. writeErrorLog(aFunCoreLogger, "IMPORT msg: %p error", msg->msg);
  601. return;
  602. }
  603. af_ImportInfo *ii = *(af_ImportInfo **)msg->msg;
  604. if (ii->obj == NULL) {
  605. writeErrorLog(aFunCoreLogger, "IMPORT msg: %p do not get obj", msg->msg);
  606. return;
  607. }
  608. if (ii->mark != NULL) {
  609. makeVarToProtectVarSpace(ii->mark, 3, 3, 3, ii->obj, env);
  610. writeDebugLog(aFunCoreLogger, "IMPORT point: [%s] %p", ii->mark, ii->obj);
  611. } else
  612. writeDebugLog(aFunCoreLogger, "IMPORT point: <no-name> %p", ii->obj);
  613. freeImportInfo(ii);
  614. }
  615. af_Environment *makeEnvironment(enum GcRunTime grt) {
  616. af_Environment *env = calloc(1, sizeof(af_Environment));
  617. env->esv = makeEnvVarSpace();
  618. env->core = makeCore(grt, env);
  619. /* 生成global对象 */
  620. env->core->global = makeGlobalObject(env);
  621. addVarToProtectVarSpace(makeVar("global", 3, 3, 3, env->core->global, env), env);
  622. /* 设置NORMAL顶级处理器 */
  623. DLC_SYMBOL(TopMsgProcessFunc) func1 = MAKE_SYMBOL(mp_NORMAL, TopMsgProcessFunc);
  624. addTopMsgProcess("NORMAL", func1, env);
  625. FREE_SYMBOL(func1);
  626. DLC_SYMBOL(TopMsgProcessFunc) func2 = MAKE_SYMBOL(mp_ERROR, TopMsgProcessFunc);
  627. addTopMsgProcess("ERROR", func2, env);
  628. FREE_SYMBOL(func2);
  629. DLC_SYMBOL(TopMsgProcessFunc) func3 = MAKE_SYMBOL(mp_IMPORT, TopMsgProcessFunc);
  630. addTopMsgProcess("IMPORT", func3, env);
  631. FREE_SYMBOL(func3);
  632. env->core->status = core_init;
  633. env->activity = makeTopActivity(NULL, NULL, env->core->protect, env->core->global);
  634. return env;
  635. }
  636. void enableEnvironment(af_Environment *env) {
  637. env->core->protect->is_protect = true;
  638. env->core->status = core_normal;
  639. }
  640. void freeEnvironment(af_Environment *env) {
  641. bool res = true;
  642. if (env->core->status != core_creat)
  643. res = iterDestruct(10, env);
  644. freeAllActivity(env->activity);
  645. freeCore(env);
  646. freeEnvVarSpace(env->esv);
  647. freeAllTopMsgProcess(env->process);
  648. if (!res)
  649. writeErrorLog(aFunCoreLogger, "Run iterDestruct error.");
  650. free(env);
  651. }
  652. static af_TopMsgProcess *makeTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func) {
  653. af_TopMsgProcess *mp = calloc(1, sizeof(af_TopMsgProcess));
  654. mp->type = strCopy(type);
  655. mp->func = COPY_SYMBOL(func, TopMsgProcessFunc);
  656. return mp;
  657. }
  658. static af_TopMsgProcess *freeTopMsgProcess(af_TopMsgProcess *mp) {
  659. af_TopMsgProcess *next = mp->next;
  660. free(mp->type);
  661. FREE_SYMBOL(mp->func);
  662. free(mp);
  663. return next;
  664. }
  665. static void freeAllTopMsgProcess(af_TopMsgProcess *mp) {
  666. while (mp != NULL)
  667. mp = freeTopMsgProcess(mp);
  668. }
  669. static af_TopMsgProcess *findTopMsgProcessFunc(char *type, af_Environment *env) {
  670. af_TopMsgProcess *mp = env->process;
  671. for (NULL; mp != NULL; mp = mp->next) {
  672. if (EQ_STR(type, mp->type))
  673. return mp;
  674. }
  675. return NULL;
  676. }
  677. bool addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
  678. af_Environment *env) {
  679. af_TopMsgProcess *mp = findTopMsgProcessFunc(type, env);
  680. if (mp != NULL)
  681. return false;
  682. mp = makeTopMsgProcess(type, func);
  683. mp->next = env->process;
  684. env->process = mp;
  685. return true;
  686. }
  687. static void newActivity(af_Code *bt, const af_Code *next, bool return_first, af_Environment *env){
  688. if (next == NULL && env->activity->body_next == NULL && env->activity->type == act_func) {
  689. writeTrackLog(aFunCoreLogger, "Tail call optimization");
  690. tailCallActivity(env->activity);
  691. setActivityBtTop(bt, env->activity);
  692. env->activity->optimization = true;
  693. if (!env->activity->return_first) // 若原本就有设置 return_first 则没有在设置的必要了, 因为该执行不会被返回
  694. env->activity->return_first = return_first;
  695. } else {
  696. af_Activity *activity = makeFuncActivity(bt, NULL, return_first, env->activity->msg_up,
  697. env->activity->var_list, env->activity->belong,
  698. env->activity->func);
  699. activity->prev = env->activity;
  700. env->activity = activity;
  701. }
  702. }
  703. /*
  704. * 函数名: isInfixFunc
  705. * 目标: 检查是否中缀调用函数
  706. */
  707. static bool isInfixFunc(af_Code *code, af_Environment *env) {
  708. if (code == NULL || code->type != code_element || code->prefix == getPrefix(E_QUOTE, env)) // 检查是否element, 且无引用前缀
  709. return false;
  710. if (checkLiteralCode(code->element.data, NULL, NULL, env)) // 检查是否字面量
  711. return false;
  712. af_Var *var = findVarFromVarList(code->element.data, env->activity->belong, env->activity->var_list);
  713. if (var == NULL)
  714. return false;
  715. obj_isInfixFunc *func = findAPI("obj_isInfixFunc", var->vn->obj->data->api);
  716. if (func == NULL)
  717. return false;
  718. return func(var->vn->obj->data->id, var->vn->obj);
  719. }
  720. bool pushExecutionActivity(af_Code *bt, bool return_first, af_Environment *env) {
  721. af_Code *next;
  722. next = getCodeNext(bt);
  723. writeTrackLog(aFunCoreLogger, "Run execution");
  724. if (bt->type != code_block || bt->block.is_empty) {
  725. pushMessageDown(makeERRORMessage(SYNTAX_ERROR, NOT_CODE_INFO, env), env);
  726. return false;
  727. }
  728. setActivityBtNext(next, env->activity);
  729. newActivity(bt, next, return_first, env);
  730. setActivityBtStart(bt->next, env->activity);
  731. env->activity->status = act_func_normal;
  732. env->activity->is_execution = true;
  733. return true;
  734. }
  735. bool pushFuncActivity(af_Code *bt, af_Environment *env) {
  736. af_Code *next;
  737. af_Code *func;
  738. af_Object *parentheses_call = env->activity->parentheses_call;
  739. env->activity->parentheses_call = NULL;
  740. writeTrackLog(aFunCoreLogger, "Run func");
  741. next = getCodeNext(bt);
  742. switch (bt->block.type) {
  743. case curly:
  744. if (bt->block.is_empty) {
  745. pushMessageDown(makeERRORMessage(CALL_ERROR, CURLY_FUNC_BODY_INFO, env), env);
  746. return false;
  747. }
  748. func = bt->next;
  749. break;
  750. case brackets:
  751. func = NULL;
  752. for (af_Code *code = bt->next; code != NULL; code = getCodeNext(code)) {
  753. if (isInfixFunc(code, env)) {
  754. func = code;
  755. break;
  756. }
  757. }
  758. if (func == NULL) {
  759. pushMessageDown(makeERRORMessage(CALL_ERROR, BRACKETS_FUNC_BODY_INFO, env), env);
  760. return false;
  761. }
  762. break;
  763. case parentheses:
  764. func = NULL; // 小括号则不在需要匹配
  765. break;
  766. default:
  767. break;
  768. }
  769. setActivityBtNext(next, env->activity); // 设置当前Activity的bt_next
  770. newActivity(bt, next, false, env); // 添加新的Activity或尾调用优化
  771. setActivityBtStart(func, env->activity);
  772. env->activity->call_type = env->activity->bt_top->block.type; // newActivity时会设置bt_top
  773. env->activity->status = act_func_get;
  774. if (env->activity->call_type == parentheses) { // 对于类前缀调用, 已经获得func的实际值了
  775. if (parentheses_call == NULL) {
  776. pushMessageDown(makeERRORMessage(CALL_ERROR, PARENTHESES_FUNC_BODY_INFO, env), env);
  777. return false;
  778. }
  779. return setFuncActivityToArg(parentheses_call, env);
  780. }
  781. return true;
  782. }
  783. bool pushLiteralActivity(af_Code *bt, char *data, af_Object *func, af_Environment *env) {
  784. setActivityBtNext(bt->next, env->activity);
  785. writeTrackLog(aFunCoreLogger, "Run literal");
  786. newActivity(bt, bt->next, false, env);
  787. env->activity->is_literal = true;
  788. pushLiteralData(strCopy(data), env); // FuncBody的释放导致code和literal_data释放, 所以要复制
  789. return setFuncActivityToArg(func, env);
  790. }
  791. bool pushVariableActivity(af_Code *bt, af_Object *func, af_Environment *env) {
  792. setActivityBtNext(bt->next, env->activity);
  793. writeTrackLog(aFunCoreLogger, "Run variable");
  794. newActivity(bt, bt->next, false, env);
  795. env->activity->is_obj_func = true;
  796. return setFuncActivityToArg(func, env);
  797. }
  798. bool pushMacroFuncActivity(af_Object *func, af_Environment *env) {
  799. /* Macro是隐式调用, bt不移动 */
  800. /* 沿用activity */
  801. writeTrackLog(aFunCoreLogger, "Run macro");
  802. if (!freeVarSpaceListCount(env->activity->new_vs_count, env->activity->var_list)) { // 释放外部变量空间
  803. env->activity->new_vs_count = 0;
  804. pushMessageDown(makeERRORMessage(RUN_ERROR, FREE_VARSPACE_INFO, env), env);
  805. return false;
  806. }
  807. env->activity->var_list = env->activity->macro_vsl;
  808. env->activity->new_vs_count = env->activity->macro_vs_count;
  809. env->activity->macro_vs_count = 0;
  810. env->activity->is_macro_call = true;
  811. tailCallActivity(env->activity); /* 隐式调用不设置 bt_top */
  812. return setFuncActivityToArg(func, env);
  813. }
  814. void pushGCActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env) {
  815. for (af_Activity *tmp = env->activity; tmp != NULL; tmp = tmp->prev) {
  816. if (tmp->type == act_gc) {
  817. *(tmp->pdl) = dl;
  818. tmp->pdl = pdl;
  819. if (tmp->dl_next == NULL) // 原dl_next已经运行到末端
  820. tmp->dl_next = dl;
  821. return;
  822. }
  823. }
  824. /* gc Activity 可能创建为顶层 activity, 故信息不能继承上一级(可能没有上一级) */
  825. af_Activity *activity = makeGcActivity(dl, pdl, env);
  826. activity->prev = env->activity;
  827. env->activity = activity;
  828. }
  829. bool pushImportActivity(af_Code *bt, af_Object **obj, char *mark, af_Environment *env) {
  830. af_Object *tmp = NULL;
  831. if (obj == NULL)
  832. obj = &tmp;
  833. if (*obj == NULL)
  834. *obj = makeGlobalObject(env);
  835. if (*obj == NULL)
  836. return false;
  837. af_Activity *activity = makeTopImportActivity(bt, bt, env->core->protect, *obj, mark);
  838. activity->prev = env->activity;
  839. env->activity = activity;
  840. return true;
  841. }
  842. bool pushDestructActivity(gc_DestructList *dl, af_Environment *env) {
  843. env->activity->dl_next = dl->next;
  844. /* 隐式调用不设置 bt_top */
  845. af_Activity *activity = makeFuncActivity(NULL, NULL, false, env->activity->msg_up,
  846. env->activity->var_list, env->activity->belong, NULL);
  847. activity->prev = env->activity;
  848. env->activity = activity;
  849. env->activity->is_gc_call = true;
  850. return setFuncActivityToArg(dl->func, env);
  851. }
  852. void setArgCodeListToActivity(af_ArgCodeList *acl, af_Environment *env) {
  853. if (acl != NULL) {
  854. setActivityBtStart(acl->code, env->activity);
  855. env->activity->run_in_func = acl->run_in_func;
  856. } else {
  857. setActivityBtStart(NULL, env->activity);
  858. env->activity->run_in_func = false;
  859. }
  860. }
  861. bool setFuncActivityToArg(af_Object *func, af_Environment *env) {
  862. obj_funcGetArgCodeList *get_acl = findAPI("obj_funcGetArgCodeList", func->data->api);
  863. obj_funcGetVarList *get_var_list = findAPI("obj_funcGetVarList", func->data->api);
  864. if (get_var_list == NULL) {
  865. pushMessageDown(makeERRORMessage(TYPE_ERROR, API_NOT_FOUND_INFO(obj_funcGetVarList), env), env);
  866. return false;
  867. }
  868. env->activity->func = func;
  869. env->activity->belong = getBelongObject(func);
  870. env->activity->status = act_func_arg;
  871. /* 遇到错误时 get_acl 和 get_var_list 要自行设定msg */
  872. if (get_acl != NULL) {
  873. if (!get_acl(func->data->id, func, &env->activity->acl_start, env->activity->bt_top, &env->activity->mark, env)) // 设置acl
  874. return false;
  875. } else
  876. env->activity->acl_start = NULL;
  877. if (!get_var_list(func->data->id, func, &env->activity->func_var_list, env->activity->mark, env)) // 设置 func_var_list
  878. return false;
  879. env->activity->acl_done = env->activity->acl_start;
  880. setArgCodeListToActivity(env->activity->acl_start, env);
  881. return true;
  882. }
  883. bool setFuncActivityAddVar(af_Environment *env){
  884. obj_funcGetInfo *get_info = findAPI("obj_funcGetInfo", env->activity->func->data->api);
  885. obj_funcGetArgList *get_arg_list = findAPI("obj_funcGetArgList", env->activity->func->data->api);
  886. af_FuncInfo *fi = NULL;
  887. if (get_info == NULL) {
  888. pushMessageDown(makeERRORMessage(TYPE_ERROR, API_NOT_FOUND_INFO(obj_funcGetInfo), env), env);
  889. return false;
  890. }
  891. /* env->activity->fi可能还存储着旧的FuncInfo(尾调用优化), 因此不能直接保存到 env->activity->fi 中 */
  892. if (!get_info(env->activity->func->data->id, env->activity->func, &fi, env->activity->bt_top, env->activity->mark, env))
  893. return false;
  894. if (fi == NULL) {
  895. pushMessageDown(makeERRORMessage(API_RUN_ERROR, API_DONOT_GIVE(FuncInfo), env), env);
  896. return false;
  897. }
  898. if (fi->scope == super_pure_scope && env->activity->fi->scope == super_embedded) {
  899. /* 超纯函数和超内嵌函数不得搭配使用 */
  900. pushMessageDown(makeERRORMessage(RUN_ERROR, PURE_EMBEDDED_INFO, env), env);
  901. return false;
  902. }
  903. if (fi->is_macro) { // 是宏函数则保存变量空间
  904. env->activity->macro_vsl = env->activity->var_list;
  905. env->activity->macro_vs_count = env->activity->new_vs_count;
  906. } else if (fi->scope != inline_scope) { // 非内联函数, 释放外部变量空间
  907. if (!freeVarSpaceListCount(env->activity->new_vs_count, env->activity->var_list)) {
  908. pushMessageDown(makeERRORMessage(RUN_ERROR, FREE_VARSPACE_INFO, env), env); // 释放失败
  909. return false;
  910. }
  911. }
  912. if (fi->scope == normal_scope) { // 使用函数变量空间
  913. env->activity->var_list = env->activity->func_var_list;
  914. env->activity->new_vs_count = 0;
  915. } else if (fi->scope == pure_scope) { // 纯函数只有 protect 变量空间
  916. env->activity->var_list = makeVarSpaceList(env->core->protect);
  917. env->activity->new_vs_count = 0;
  918. } else if (fi->scope == super_pure_scope) { // 超纯函数没有变量空间, 因此不得为超内嵌函数(否则var_list就为NULL了)
  919. env->activity->var_list = NULL;
  920. env->activity->new_vs_count = 0;
  921. }
  922. if (fi->embedded != super_embedded) { // 不是超内嵌函数则引入一层新的变量空间
  923. /* 新层的变量空间应该属于belong而不是func */
  924. env->activity->var_list = pushNewVarList(env->activity->belong, env->activity->var_list, env);
  925. env->activity->new_vs_count++;
  926. }
  927. env->activity->func_var_list = NULL;
  928. if (fi->var_this && env->activity->belong != NULL) {
  929. if (!makeVarToVarSpaceList("this", 3, 3, 3, env->activity->belong,
  930. env->activity->var_list, env->activity->belong, env)) {
  931. pushMessageDown(makeERRORMessage(RUN_ERROR, IMPLICIT_SET_INFO(this), env), env);
  932. return false;
  933. }
  934. }
  935. if (fi->var_func && env->activity->func != NULL) {
  936. if (!makeVarToVarSpaceList("func", 3, 3, 3, env->activity->func,
  937. env->activity->var_list, env->activity->belong, env)) {
  938. pushMessageDown(makeERRORMessage(RUN_ERROR, IMPLICIT_SET_INFO(func), env), env);
  939. return false;
  940. }
  941. }
  942. /* 计算参数 */
  943. if (get_arg_list != NULL) {
  944. af_ArgList *al;
  945. if (!get_arg_list(env->activity->func->data->id, env->activity->func, &al, env->activity->acl_start, env->activity->mark, env))
  946. return false;
  947. runArgList(al, env->activity->var_list, env);
  948. freeAllArgList(al);
  949. }
  950. if (fi->embedded == protect_embedded)
  951. env->activity->var_list->vs->is_protect = true;
  952. /* ArgCodeList 在此处被清理 */
  953. freeAllArgCodeList(env->activity->acl_start);
  954. env->activity->acl_start = NULL;
  955. env->activity->acl_done = NULL;
  956. /* 此处检查fi是否为NULL, 不为(通常为尾调用优化)则释放fi */
  957. /* 旧的FuncBody延迟到此处才释放(freeFuncInfo释放FuncBody), 是因为获取函数参数的相关运算中可能会使用旧FuncBody中的代码 */
  958. /* 因为调用函数的代码是在旧FuncBody中的, 因此参数计算的相关代码也可能在旧FuncBody中 */
  959. /* 也就是说ArgCodeList, bt_top, bt_start中的代码可能是来自旧FuncBody的 */
  960. /* 所以他们要延迟到现在才被释放 */
  961. /* 而get_arg_list是最后一次使用旧FuncBody中的代码(bt_top), 因此此处可以释放 */
  962. if (env->activity->fi != NULL)
  963. freeFuncInfo(env->activity->fi); // 延迟到这里再释放, 主要是FuncBody中的bt可能会被使用
  964. env->activity->fi = fi;
  965. env->activity->body_next = fi->body;
  966. /* bt_top等的相关设定会在 setFuncActivityToNormal 中被进行 */
  967. if (setFuncActivityToNormal(env) == 0)
  968. return false; // 运行结束, 且已写入msg
  969. return true;
  970. }
  971. static void initCallFuncInfo(CallFuncInfo *cfi, af_Environment *env) {
  972. cfi->mark = env->activity->mark;
  973. cfi->belong = env->activity->belong;
  974. cfi->func = env->activity->func;
  975. cfi->var_list = env->activity->var_list; // 传var_list而非vsl
  976. cfi->call_type = env->activity->call_type;
  977. cfi->is_gc_call = env->activity->is_gc_call;
  978. cfi->is_literal = env->activity->is_literal;
  979. cfi->is_obj_func = env->activity->is_obj_func;
  980. cfi->is_macro_call = env->activity->is_macro_call;
  981. }
  982. /*
  983. * 函数名: setFuncActivityToNormal
  984. * 目标: 获取下一步需要运行的结果
  985. * 返回 (0) 表示无下一步
  986. * 返回 (-1) 表示运行C函数, 并且设置了 process_msg_first
  987. * 返回 (1) 表示下一步运行Code
  988. * 返回 (2) 表示遇到未被替换的动态代码块
  989. */
  990. int setFuncActivityToNormal(af_Environment *env){ // 获取函数的函数体
  991. int re;
  992. af_FuncBody *body = env->activity->body_next;
  993. env->activity->status = act_func_normal;
  994. setActivityBtTop(NULL, env->activity); // NORMAL期, bt_top将不被设定
  995. setActivityBtNext(NULL, env->activity);
  996. if (body == NULL) // 已经没有下一步了 (原msg不释放)
  997. return 0;
  998. af_Activity *activity = env->activity; // 防止在函数调用期间env->activity被修改
  999. env->activity->body_next = body->next;
  1000. switch (body->type) {
  1001. case func_body_c: {
  1002. CallFuncInfo cfi;
  1003. initCallFuncInfo(&cfi, env);
  1004. af_FuncBody *new = GET_SYMBOL(body->c_func)(&cfi, env);
  1005. activity->process_msg_first++; // 处理C函数通过msg_down返回的结果
  1006. pushDynamicFuncBody(new, body);
  1007. activity->body_next = body->next; // 添加新元素后要重新设定body_next的位置
  1008. re = -1;
  1009. break;
  1010. }
  1011. case func_body_import:
  1012. if (!pushImportActivity(body->code, NULL, NULL, env)) {
  1013. pushMessageDown(makeERRORMessage(IMPORT_ERROR, IMPORT_OBJ_ERROR, env), env);
  1014. activity->process_msg_first++;
  1015. re = 2;
  1016. break;
  1017. }
  1018. re = 1;
  1019. break;
  1020. case func_body_code:
  1021. setActivityBtStart(body->code, env->activity);
  1022. re = 1;
  1023. break;
  1024. default:
  1025. case func_body_dynamic:
  1026. pushMessageDown(makeERRORMessage(RUN_ERROR, FUNCBODY_ERROR_INFO, env), env);
  1027. activity->process_msg_first++;
  1028. re = 2;
  1029. break;
  1030. }
  1031. /* 在最后一个aFunBody完毕后, mark被释放(因为此后不会再有函数需要使用Mark) */
  1032. if (activity->body_next == NULL) // 最后一个aFunBody
  1033. freeMark(activity);
  1034. return re;
  1035. }
  1036. /*
  1037. * 函数名: runTopMessageProcess
  1038. * 目标: 运行顶层信息处理器
  1039. */
  1040. void runTopMessageProcess(bool is_gc, af_Environment *env) {
  1041. af_Message **pmsg = &env->activity->msg_down;
  1042. while (*pmsg != NULL) {
  1043. af_TopMsgProcess *mp = findTopMsgProcessFunc((*pmsg)->type, env);
  1044. if (mp != NULL) {
  1045. GET_SYMBOL(mp->func)(*pmsg, is_gc, env);
  1046. *pmsg = freeMessage(*pmsg);
  1047. } else
  1048. pmsg = &((*pmsg)->next);
  1049. }
  1050. }
  1051. static void freeMark(af_Activity *activity) {
  1052. if (activity->type == act_func && activity->func != NULL && activity->mark != NULL) {
  1053. obj_funcFreeMask *func = findAPI("obj_funcFreeMask", activity->func->data->api);
  1054. if (func != NULL)
  1055. func(activity->func->data->id, activity->func, activity->mark);
  1056. activity->mark = NULL;
  1057. }
  1058. }
  1059. /*
  1060. * 函数名: popActivity
  1061. * 目标: 释放Activity
  1062. * 只有is_normal为false时才会检查释放mark
  1063. */
  1064. void popActivity(bool is_normal, af_Message *msg, af_Environment *env) {
  1065. if (env->activity->type == act_func || env->activity->type == act_top || env->activity->type == act_top_import) {
  1066. if (msg != NULL && env->activity->return_first) { // msg有内容, 并且设定了返回首位, 则清除msg内容, 并压入首位(压入的代码在下面)
  1067. if (EQ_STR(msg->type, "NORMAL")) {
  1068. gc_delReference(*(af_Object **) msg->msg);
  1069. freeMessage(msg);
  1070. msg = NULL;
  1071. }
  1072. } else if (env->activity->return_first) { // msg无内容, 并且设定了返回首位, 则检查msg_down是否有normal, 有则清除
  1073. if (env->activity->msg_down != NULL && EQ_STR(env->activity->msg_down->type, "NORMAL")) {
  1074. af_Message *tmp = getFirstMessage(env);
  1075. gc_delReference(*(af_Object **) (tmp->msg));
  1076. freeMessage(tmp);
  1077. }
  1078. }
  1079. if (msg == NULL && env->activity->return_first) { // 如果首位
  1080. if (env->activity->return_obj == NULL)
  1081. msg = makeERRORMessage(RUN_ERROR, RETURN_OBJ_NOT_FOUND_INFO, env);
  1082. else
  1083. msg = makeNORMALMessage(env->activity->return_obj);
  1084. }
  1085. }
  1086. if (msg != NULL)
  1087. pushMessageDown(msg, env);
  1088. if (env->activity->type == act_top_import && /* import模式, 并且msg_down中有normal, 则把normal替换为belong */
  1089. env->activity->msg_down != NULL && EQ_STR(env->activity->msg_down->type, "NORMAL")) {
  1090. af_Message *tmp = getFirstMessage(env);
  1091. pushMessageDown(makeIMPORTMessage(env->activity->import_mark, env->activity->belong), env); // 压入belong作为msg
  1092. pushMessageDown(tmp, env);
  1093. }
  1094. /* 正常情况下在执行完最后一个FuncBody后释放mark, 非正常情况(即最后一个FuncBody可能还没执行)则需要在此释放mark */
  1095. if (!is_normal)
  1096. freeMark(env->activity); // 遇到非正常退出时, 释放`mark`
  1097. if (env->activity->type == act_top || env->activity->type == act_gc) // 顶层或gc层
  1098. runTopMessageProcess((env->activity->type == act_gc), env);
  1099. else {
  1100. connectMessage(&(env->activity->msg_down), env->activity->prev->msg_down);
  1101. env->activity->prev->msg_down = env->activity->msg_down;
  1102. env->activity->msg_down = NULL;
  1103. /* popActivity必然设定process_msg_first */
  1104. env->activity->prev->process_msg_first++;
  1105. }
  1106. if (env->activity->type != act_top)
  1107. env->activity = freeActivity(env->activity);
  1108. else
  1109. freeActivityTop(env->activity); // activity不被释放
  1110. }
  1111. static af_LiteralRegex *makeLiteralRegex(char *pattern, char *func, bool in_protect) {
  1112. af_Regex *rg = makeRegex(pattern);
  1113. if (rg == NULL)
  1114. return NULL;
  1115. af_LiteralRegex *lr = calloc(1, sizeof(af_LiteralRegex));
  1116. lr->rg = rg;
  1117. lr->func = strCopy(func);
  1118. lr->in_protect = in_protect;
  1119. return lr;
  1120. }
  1121. static af_LiteralRegex *freeLiteralRegex(af_LiteralRegex *lr) {
  1122. af_LiteralRegex *next = lr->next;
  1123. freeRegex(lr->rg);
  1124. free(lr->func);
  1125. free(lr);
  1126. return next;
  1127. }
  1128. static void freeAllLiteralRegex(af_LiteralRegex *lr) {
  1129. while (lr != NULL)
  1130. lr = freeLiteralRegex(lr);
  1131. }
  1132. bool pushLiteralRegex(char *pattern, char *func, bool in_protect, af_Environment *env) {
  1133. af_LiteralRegex *lr = makeLiteralRegex(pattern, func, in_protect);
  1134. if (lr == NULL)
  1135. return false;
  1136. lr->next = env->core->lr;
  1137. env->core->lr = lr;
  1138. return true;
  1139. }
  1140. /*
  1141. * 函数名: checkLiteralCode
  1142. * 目标: 检查对象是否为字面量
  1143. * 注意: func被写入函数名, 但不是复制式写入
  1144. */
  1145. bool checkLiteralCode(char *literal, char **func, bool *in_protect, af_Environment *env) {
  1146. for (af_LiteralRegex *lr = env->core->lr; lr != NULL; lr = lr->next) {
  1147. if (matchRegex(literal, lr->rg) == 1) {
  1148. if (func != NULL)
  1149. *func = lr->func; // 不使用复制
  1150. if (in_protect != NULL)
  1151. *in_protect = lr->in_protect;
  1152. return true;
  1153. }
  1154. }
  1155. return false;
  1156. }
  1157. af_ErrorInfo *makeErrorInfo(char *type, char *error, char *note, FileLine line, FilePath path) {
  1158. af_ErrorInfo *ei = calloc(1, sizeof(af_ErrorInfo));
  1159. ei->error_type = strCopy(type);
  1160. ei->error = strCopy(error);
  1161. pushErrorBacktracking(line, path, note, ei);
  1162. return ei;
  1163. }
  1164. void freeErrorInfo(af_ErrorInfo *ei) {
  1165. free(ei->error_type);
  1166. free(ei->error);
  1167. if (ei->obj != NULL)
  1168. gc_delReference(ei->obj);
  1169. freeAllErrorBacktracking(ei->track);
  1170. free(ei);
  1171. }
  1172. static void fprintfNote(FILE *file, char *note) {
  1173. char *ent = NULL;
  1174. while(true) {
  1175. ent = strchr(note, '\n');
  1176. if (ent != NULL)
  1177. *ent = NUL;
  1178. fprintf(file, " #note %s\n", note);
  1179. if (ent == NULL) // 意味着是最后一部分`note`
  1180. break;
  1181. *ent = '\n';
  1182. note = ent + 1;
  1183. }
  1184. }
  1185. void fprintfErrorInfo(FILE *file, af_ErrorInfo *ei) {
  1186. fprintf(file, "Error Traceback (most recent call last):\n");
  1187. for (af_ErrorBacktracking *ebt = ei->track; ebt != NULL; ebt = ebt->next) {
  1188. fprintf(file, " File \"%s\", line %d\n", ebt->file, ebt->line);
  1189. if (ebt->note != NULL)
  1190. fprintfNote(file, ebt->note);
  1191. }
  1192. fprintf(file, "%s: \"%s\"\n", ei->error_type, ei->error);
  1193. fflush(file);
  1194. }
  1195. static void fprintfNoteStderr(char *note) {
  1196. char *ent = NULL;
  1197. while(true) {
  1198. ent = strchr(note, '\n');
  1199. if (ent != NULL)
  1200. *ent = NUL;
  1201. printf_stderr(0, " #note %s\n", note);
  1202. if (ent == NULL) // 意味着是最后一部分`note`
  1203. break;
  1204. *ent = '\n';
  1205. note = ent + 1;
  1206. }
  1207. }
  1208. void fprintfErrorInfoStderr(af_ErrorInfo *ei) {
  1209. printf_stderr(0, "Error Traceback (most recent call last):\n");
  1210. for (af_ErrorBacktracking *ebt = ei->track; ebt != NULL; ebt = ebt->next) {
  1211. printf_stderr(0, " File \"%s\", line %d\n", ebt->file, ebt->line);
  1212. if (ebt->note != NULL)
  1213. fprintfNoteStderr(ebt->note);
  1214. }
  1215. printf_stderr(0, "%s: \"%s\"\n", ei->error_type, ei->error);
  1216. fflush(stderr);
  1217. }
  1218. static void fprintfNoteStdout(char *note) {
  1219. char *ent = NULL;
  1220. while(true) {
  1221. ent = strchr(note, '\n');
  1222. if (ent != NULL)
  1223. *ent = NUL;
  1224. printf_stdout(0, " #note %s\n", note);
  1225. if (ent == NULL) // 意味着是最后一部分`note`
  1226. break;
  1227. *ent = '\n';
  1228. note = ent + 1;
  1229. }
  1230. }
  1231. void fprintfErrorInfoStdout(af_ErrorInfo *ei) {
  1232. printf_stdout(0, "Error Traceback (most recent call last):\n");
  1233. for (af_ErrorBacktracking *ebt = ei->track; ebt != NULL; ebt = ebt->next) {
  1234. printf_stdout(0, " File \"%s\", line %d\n", ebt->file, ebt->line);
  1235. if (ebt->note != NULL)
  1236. fprintfNoteStdout(ebt->note);
  1237. }
  1238. printf_stdout(0, "%s: \"%s\"\n", ei->error_type, ei->error);
  1239. fflush(stdout);
  1240. }
  1241. static af_ErrorBacktracking *makeErrorBacktracking(FileLine line, FilePath file, char *note) {
  1242. af_ErrorBacktracking *ebt = calloc(1, sizeof(af_ErrorBacktracking));
  1243. ebt->line = line;
  1244. if (file == NULL)
  1245. ebt->file = strCopy("unknown.aun.sys");
  1246. else
  1247. ebt->file = strCopy(file);
  1248. if (note != NULL)
  1249. ebt->note = strCopy(note);
  1250. return ebt;
  1251. }
  1252. static af_ErrorBacktracking *freeErrorBacktracking(af_ErrorBacktracking *ebt) {
  1253. af_ErrorBacktracking *next = ebt->next;
  1254. free(ebt->note);
  1255. free(ebt->file);
  1256. free(ebt);
  1257. return next;
  1258. }
  1259. static void freeAllErrorBacktracking(af_ErrorBacktracking *ebt) {
  1260. while(ebt != NULL) {
  1261. ebt = freeErrorBacktracking(ebt);
  1262. }
  1263. }
  1264. void pushErrorBacktracking(FileLine line, FilePath file, char *note, af_ErrorInfo *ei) {
  1265. af_ErrorBacktracking *ebt = makeErrorBacktracking(line, file, note);
  1266. ebt->next = ei->track;
  1267. ei->track = ebt;
  1268. }
  1269. static char *getActivityInfoToBacktracking(af_Activity *activity){
  1270. char info[512] = "";
  1271. /* strcat拼接的字符是可控的, 因此不需要使用安全函数 */
  1272. if (activity->type == act_gc) {
  1273. strcat(info, "gc-activity;");
  1274. return strCopy(info);
  1275. } else if (activity->type == act_top)
  1276. strcat(info, "top-activity;");
  1277. else if (activity->type == act_top_import)
  1278. strcat(info, "top-import-activity;");
  1279. else if (activity->is_execution)
  1280. strcat(info, "execution-activity;");
  1281. else if (activity->is_gc_call)
  1282. strcat(info, "gc-destruct-function-call-activity;");
  1283. else
  1284. strcat(info, "function-call-activity;");
  1285. switch (activity->status) {
  1286. case act_func_get:
  1287. strcat(info, "\nfunc-get;");
  1288. break;
  1289. case act_func_arg:
  1290. strcat(info, "\nfunc-arg;");
  1291. if (activity->run_in_func)
  1292. strcat(info, " run-in-function-var-space;");
  1293. break;
  1294. case act_func_normal:
  1295. strcat(info, "\nrun-code;");
  1296. if (activity->return_first)
  1297. strcat(info, " return-first-result;");
  1298. break;
  1299. default:
  1300. break;
  1301. }
  1302. if (activity->is_macro_call)
  1303. strcat(info, "\nmacro-call;");
  1304. if (activity->is_literal)
  1305. strcat(info, "\nliteral-call;");
  1306. if (activity->is_obj_func)
  1307. strcat(info, "\nobject-function-call;");
  1308. if (activity->optimization)
  1309. strcat(info, "\ntail-call-optimization;");
  1310. return strCopy(info);
  1311. }
  1312. static char *getActivityTrackBackInfoToBacktracking(af_ActivityTrackBack *atb) {
  1313. char info[512] = "backtracking;";
  1314. /* strcat拼接的字符是可控的, 因此不需要使用安全函数 */
  1315. if (atb->is_execution)
  1316. strcat(info, "\nexecution-activity;");
  1317. else if (atb->is_gc_call)
  1318. strcat(info, "\ngc-destruct-function-call-activity;");
  1319. else
  1320. strcat(info, "\nfunction-call-activity;");
  1321. switch (atb->status) {
  1322. case act_func_get:
  1323. strcat(info, "\nfunc-get;");
  1324. break;
  1325. case act_func_arg:
  1326. strcat(info, "\nfunc-arg;");
  1327. if (atb->run_in_func)
  1328. strcat(info, " run-in-function-var-space;");
  1329. break;
  1330. case act_func_normal:
  1331. strcat(info, "\nrun-code;");
  1332. if (atb->return_first)
  1333. strcat(info, " return-first-result;");
  1334. break;
  1335. default:
  1336. break;
  1337. }
  1338. if (atb->is_macro_call)
  1339. strcat(info, "\nmacro-call;");
  1340. if (atb->is_literal)
  1341. strcat(info, "\nliteral-call;");
  1342. if (atb->is_obj_func)
  1343. strcat(info, "\nobject-function-call;");
  1344. if (atb->optimization)
  1345. strcat(info, "\ntail-call-optimization;");
  1346. return strCopy(info);
  1347. }
  1348. af_ImportInfo *makeImportInfo(char *mark, af_Object *obj) {
  1349. af_ImportInfo *ii = calloc(1, sizeof(af_ImportInfo));
  1350. if (mark != NULL)
  1351. ii->mark = strCopy(mark);
  1352. ii->obj = obj;
  1353. gc_addReference(obj);
  1354. return ii;
  1355. }
  1356. void freeImportInfo(af_ImportInfo *ii) {
  1357. free(ii->mark);
  1358. if (ii->obj != NULL)
  1359. gc_delReference(ii->obj);
  1360. free(ii);
  1361. }
  1362. void setGcMax(int32_t max, af_Environment *env) {
  1363. env->core->gc_max->num = max;
  1364. }
  1365. void setGcRun(enum GcRunTime grt, af_Environment *env) {
  1366. env->core->gc_runtime->num = grt;
  1367. }
  1368. int32_t getGcCount(af_Environment *env) {
  1369. return env->core->gc_count->num;
  1370. }
  1371. int32_t getGcMax(af_Environment *env) {
  1372. return env->core->gc_max->num;
  1373. }
  1374. enum GcRunTime getGcRun(af_Environment *env) {
  1375. return env->core->gc_runtime->num;
  1376. }
  1377. af_Object *getCoreGlobal(af_Environment *env) {
  1378. return env->core->global;
  1379. }
  1380. af_Object *getGlobal(af_Environment *env) {
  1381. af_Activity *activity = env->activity;
  1382. for (NULL; activity != NULL; activity = activity->prev) {
  1383. if (activity->type == act_top || activity->type == act_top_import)
  1384. return activity->belong;
  1385. }
  1386. return env->core->global;
  1387. }
  1388. af_Object *getBelong(af_Environment *env) {
  1389. if (env == NULL || env->activity == NULL)
  1390. return NULL;
  1391. return env->activity->belong;
  1392. }
  1393. FilePath getActivityFile(af_Environment *env){
  1394. if (env == NULL || env->activity == NULL)
  1395. return NULL;
  1396. return env->activity->file;
  1397. }
  1398. FileLine getActivityLine(af_Environment *env){
  1399. if (env == NULL || env->activity == NULL)
  1400. return 0;
  1401. return env->activity->line;
  1402. }
  1403. af_Object *getMsgNormalData(af_Message *msg) {
  1404. if (!EQ_STR("NORMAL", msg->type))
  1405. return NULL;
  1406. af_Object *obj = *(af_Object **)msg->msg;
  1407. gc_delReference(obj);
  1408. *(af_Object **)msg->msg = NULL;
  1409. return obj;
  1410. }
  1411. af_ErrorInfo *getMsgErrorInfo(af_Message *msg) {
  1412. if (!EQ_STR("ERROR", msg->type))
  1413. return NULL;
  1414. af_ErrorInfo *ei = *(af_ErrorInfo **)msg->msg;
  1415. return ei;
  1416. }
  1417. af_ImportInfo *getMsgImportInfo(af_Message *msg) {
  1418. if (!EQ_STR("IMPORT", msg->type))
  1419. return NULL;
  1420. af_ImportInfo *ii = *(af_ImportInfo **)msg->msg;
  1421. return ii;
  1422. }
  1423. char *getErrorType(af_ErrorInfo *ei) {
  1424. return ei->error_type;
  1425. }
  1426. char *getError(af_ErrorInfo *ei) {
  1427. return ei->error;
  1428. }
  1429. char *getImportMark(af_ImportInfo *ii) {
  1430. return ii->mark;
  1431. }
  1432. af_Object *getImportObject(af_ImportInfo *ii) {
  1433. af_Object *obj = ii->obj;
  1434. if (obj == NULL)
  1435. return NULL;
  1436. ii->obj = NULL;
  1437. gc_delReference(obj);
  1438. return obj;
  1439. }
  1440. af_VarSpaceListNode *getRunVarSpaceList(af_Environment *env) {
  1441. if (env->activity->type == act_gc)
  1442. return env->activity->var_list;
  1443. else
  1444. return env->activity->vsl;
  1445. }
  1446. int isCoreExit(af_Environment *env) {
  1447. if (env->core->status == core_exit)
  1448. return 1;
  1449. else if (env->core->status == core_stop)
  1450. return -1;
  1451. return 0;
  1452. }