inter.cpp 6.5 KB

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