inter.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "inter.h"
  2. #include "core-activation.h"
  3. #include "core-init.h"
  4. #include "msg.h"
  5. #include "core-exception.h"
  6. #include "func-import.h"
  7. #include "func-exit.h"
  8. namespace aFuncore {
  9. Inter::Inter(Environment &env_)
  10. : out{}, in{}, env{env_}{
  11. status = inter_creat;
  12. activation = nullptr;
  13. status = inter_init;
  14. env++;
  15. }
  16. Inter::Inter(const Inter &base_inter)
  17. : out{}, in{}, env{base_inter.env}{
  18. status = inter_creat;
  19. activation = nullptr;
  20. for (auto &i: base_inter.literal)
  21. literal.push_back(i);
  22. status = inter_normal;
  23. env++;
  24. }
  25. Inter::~Inter(){
  26. env--;
  27. }
  28. /**
  29. * 使能 (激活解释器)
  30. */
  31. void Inter::enable(){
  32. if (status == inter_init) {
  33. env.protect->setProtect(true);
  34. status = inter_normal;
  35. }
  36. }
  37. /**
  38. * 运行代码(直接运行activation)
  39. * @return
  40. */
  41. bool Inter::runCode(){
  42. if (status == inter_stop)
  43. status = inter_normal;
  44. while (activation != nullptr) {
  45. if (isInterStop()) {
  46. while (activation != nullptr)
  47. delete popActivation();
  48. return false;
  49. }
  50. const Code::ByteCode *code = nullptr;
  51. Activation::ActivationStatus as = activation->getCode(code);
  52. switch (as) {
  53. case Activation::as_end: {
  54. delete popActivation();
  55. break;
  56. }
  57. case Activation::as_run:
  58. activation->runCode(code);
  59. break;
  60. case Activation::as_end_run:
  61. activation->endRun();
  62. break;
  63. default:
  64. errorLog(aFunCoreLogger, "Error activation status.");
  65. activation->getDownStream().pushMessage("ERROR",
  66. new ErrorMessage("RuntimeError", "Error activation status.", activation));
  67. break;
  68. }
  69. }
  70. return true;
  71. }
  72. /**
  73. * 运行代码
  74. * @param code 代码
  75. * @return
  76. */
  77. bool Inter::runCode(const Code &code){
  78. if (activation != nullptr) {
  79. errorLog(aFunCoreLogger, "Run code with activation");
  80. return false;
  81. }
  82. new TopActivation(code, *this);
  83. return runCode();
  84. }
  85. /**
  86. * 函数调用
  87. * @param code 代码
  88. * @return
  89. */
  90. bool Inter::runCode(Object *func){
  91. if (activation != nullptr) {
  92. errorLog(aFunCoreLogger, "Run function with activation");
  93. return false;
  94. }
  95. auto func_obj = dynamic_cast<Function *>(func);
  96. if (func_obj == nullptr) {
  97. errorLog(aFunCoreLogger, "Run without function");
  98. return false;
  99. }
  100. new FuncActivation(func_obj, *this);
  101. return runCode();
  102. }
  103. /**
  104. * 检查字面量是否匹配
  105. * @param element 字面量
  106. * @return
  107. */
  108. bool Inter::checkLiteral(const std::string &element) const{
  109. if (literal.empty())
  110. return false;
  111. for (auto &i: literal) {
  112. try {
  113. if (i.rg.match(element) != 1)
  114. continue;
  115. return true;
  116. } catch (aFuntool::RegexException &e) {
  117. continue;
  118. }
  119. }
  120. return false;
  121. }
  122. /**
  123. * 检查字面量正则匹配
  124. * @param element 字面量
  125. * @param literaler 函数
  126. * @param in_protect 是否保护空间
  127. * @return
  128. */
  129. bool Inter::checkLiteral(const std::string &element, std::string &literaler, bool &in_protect) const{
  130. if (literal.empty())
  131. return false;
  132. for (auto &i: literal) {
  133. try {
  134. if (i.rg.match(element) != 1)
  135. continue;
  136. literaler = i.literaler;
  137. in_protect = i.in_protect;
  138. return true;
  139. } catch (aFuntool::RegexException &e) {
  140. continue;
  141. }
  142. }
  143. return false;
  144. }
  145. bool Inter::pushLiteral(const std::string &pattern, const std::string &literaler, bool in_protect){
  146. try {
  147. literal.push_front({aFuntool::Regex(pattern), pattern, literaler, in_protect});
  148. } catch (aFuntool::RegexException &e) {
  149. return false;
  150. }
  151. return true;
  152. }
  153. void Environment::gcThread() {
  154. while(true) {
  155. std::queue<Object *> del;
  156. std::queue<Object *> des;
  157. {
  158. std::unique_lock<std::mutex> mutex{lock};
  159. if (destruct)
  160. break;
  161. Object::checkReachable(gc);
  162. Object::setReachable(gc, des, del);
  163. }
  164. Object::deleteUnreachable(del);
  165. Object::destructUnreachable(des, gc_inter);
  166. aFuntool::safeSleep(1);
  167. }
  168. Object::destructAll(gc, gc_inter); /* 不需要mutex锁 */
  169. }
  170. Environment::Environment(int argc, char **argv)
  171. : reference{0}, gc_inter{*(new Inter(*this))},
  172. protect{new ProtectVarSpace(*this)}, global{new VarSpace(*this)},
  173. global_varlist{new VarList(protect)}, destruct{false} {
  174. global_varlist->push(global);
  175. /* 生成 gc_inter 后, reference == 1 */
  176. envvar.setNumber("sys:gc-runtime", 2);
  177. envvar.setString("sys:prefix", "''"); // 引用,顺序执行
  178. envvar.setNumber("sys:exit-code", 0);
  179. envvar.setNumber("sys:argc", argc);
  180. envvar.setNumber("sys:error_std", 0);
  181. for (int i = 0; i < argc; i++) {
  182. char buf[20];
  183. snprintf(buf, 10, "sys:arg%d", i);
  184. envvar.setString(buf, argv[i]);
  185. }
  186. gc_thread = std::thread([this](){this->gcThread();});
  187. { // 退出函数
  188. auto exit = new ExitFunction(*this);
  189. protect->defineVar("exit", exit);
  190. exit->delReference();
  191. }
  192. { // 导入函数
  193. auto import = new ImportFunction(*this);
  194. protect->defineVar("import", import);
  195. import->delReference();
  196. }
  197. }
  198. Environment::~Environment() noexcept(false) {
  199. { /* 使用互斥锁, 防止与gc线程出现不同步的情况 */
  200. std::unique_lock<std::mutex> mutex{lock};
  201. if (reference != 1) // gc_inter 有一个引用
  202. throw EnvironmentDestructException();
  203. if (destruct)
  204. return;
  205. destruct = true;
  206. }
  207. gc_thread.join();
  208. delete &gc_inter;
  209. delete global_varlist;
  210. protect->delReference();
  211. global->delReference();
  212. Object::deleteAll(gc); /* 不需要mutex锁 */
  213. if (reference != 0)
  214. throw EnvironmentDestructException();
  215. }
  216. }