sys.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include "__ofunc.h"
  2. static ResultType vm_super(O_FUNC){
  3. Value *arg_father = NULL;
  4. Value *arg_child = NULL;
  5. LinkValue *next_father = NULL;
  6. ArgumentParser ap[] = {{.type=name_value, .name=L"class_", .must=1, .long_arg=false},
  7. {.type=name_value, .name=L"obj_", .must=1, .long_arg=false},
  8. {.must=-1}};
  9. setResultCore(result);
  10. parserArgumentUnion(ap, arg, CNEXT_NT);
  11. if (!CHECK_RESULT(result))
  12. return result->type;
  13. freeResult(result);
  14. arg_father = ap[0].value->value;
  15. arg_child = ap[1].value->value;
  16. if (arg_child == arg_father) {
  17. if (arg_child->object.inherit != NULL) {
  18. result->value = COPY_LINKVALUE(arg_child->object.inherit->value, inter);
  19. result->type = R_opt;
  20. gc_addTmpLink(&result->value->gc_status);
  21. } else
  22. setResultError(E_SuperException, L"object has no next father", LINEFILE, true, CNEXT_NT);
  23. return result->type;
  24. }
  25. for (Inherit *self_father = arg_child->object.inherit; self_father != NULL; self_father = self_father->next) {
  26. if (self_father->value->value == arg_father) {
  27. if (self_father->next != NULL)
  28. next_father = COPY_LINKVALUE(self_father->next->value, inter);
  29. break;
  30. }
  31. }
  32. if (next_father != NULL) {
  33. result->value = next_father;
  34. result->type = R_opt;
  35. gc_addTmpLink(&result->value->gc_status);
  36. } else
  37. setResultError(E_SuperException, L"object has no next father", LINEFILE, true, CNEXT_NT);
  38. return result->type;
  39. }
  40. static ResultType vm_setNowRunCore(O_FUNC, bool type){
  41. LinkValue *function_value = NULL;
  42. ArgumentParser ap[] = {{.type=name_value, .name=L"func", .must=1, .long_arg=false},
  43. {.must=-1}};
  44. setResultCore(result);
  45. {
  46. parserArgumentUnion(ap, arg, CNEXT_NT);
  47. if (!CHECK_RESULT(result))
  48. return result->type;
  49. freeResult(result);
  50. }
  51. function_value = ap[0].value;
  52. function_value->value->data.function.function_data.run = type;
  53. result->value = function_value;
  54. gc_addTmpLink(&result->value->gc_status);
  55. result->type = R_opt;
  56. return R_opt;
  57. }
  58. static ResultType vm_setMethodCore(O_FUNC, enum FunctionPtType type){
  59. LinkValue *function_value = NULL;
  60. ArgumentParser ap[] = {{.type=name_value, .name=L"func", .must=1, .long_arg=false},
  61. {.must=-1}};
  62. setResultCore(result);
  63. {
  64. parserArgumentUnion(ap, arg, CNEXT_NT);
  65. if (!CHECK_RESULT(result))
  66. return result->type;
  67. freeResult(result);
  68. }
  69. function_value = ap[0].value;
  70. function_value->value->data.function.function_data.pt_type = type;
  71. result->value = function_value;
  72. gc_addTmpLink(&result->value->gc_status);
  73. result->type = R_opt;
  74. return R_opt;
  75. }
  76. static ResultType vm_cls(O_FUNC){
  77. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_cls);
  78. }
  79. static ResultType vm_func_cls(O_FUNC){
  80. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_func_cls);
  81. }
  82. static ResultType vm_no_(O_FUNC){
  83. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_no_);
  84. }
  85. static ResultType vm_func_(O_FUNC){
  86. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_func_);
  87. }
  88. static ResultType vm_func_class(O_FUNC){
  89. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_func_class);
  90. }
  91. static ResultType vm_func_obj(O_FUNC){
  92. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_func_obj);
  93. }
  94. static ResultType vm_class(O_FUNC){
  95. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_class);
  96. }
  97. static ResultType vm_obj(O_FUNC){
  98. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_obj);
  99. }
  100. static ResultType vm_all(O_FUNC){
  101. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_all);
  102. }
  103. static ResultType vm_func_all(O_FUNC){
  104. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_func_all);
  105. }
  106. static ResultType vm_cls_obj(O_FUNC){
  107. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_cls_obj);
  108. }
  109. static ResultType vm_cls_class(O_FUNC){
  110. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_cls_class);
  111. }
  112. static ResultType vm_cls_all(O_FUNC){
  113. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_cls_all);
  114. }
  115. static ResultType vm_func_cls_obj(O_FUNC){
  116. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_func_cls_obj);
  117. }
  118. static ResultType vm_func_cls_class(O_FUNC){
  119. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_func_cls_class);
  120. }
  121. static ResultType vm_func_cls_all(O_FUNC){
  122. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), fp_func_cls_all);
  123. }
  124. static ResultType vm_isnowrun(O_FUNC){
  125. return vm_setNowRunCore(CO_FUNC(arg, var_list, result, belong), true);
  126. }
  127. static ResultType vm_disnowrun(O_FUNC){
  128. return vm_setNowRunCore(CO_FUNC(arg, var_list, result, belong), false);
  129. }
  130. static ResultType vm_open(O_FUNC){
  131. return callBackCore(inter->data.base_obj[B_FILE], arg, LINEFILE, 0, CNEXT_NT);
  132. }
  133. static ResultType vm_getValuePointer(O_FUNC){
  134. ArgumentParser ap[] = {{.type=name_value, .name=L"value", .must=1, .long_arg=false},
  135. {.must=-1}};
  136. setResultCore(result);
  137. {
  138. parserArgumentUnion(ap, arg, CNEXT_NT);
  139. if (!CHECK_RESULT(result))
  140. return result->type;
  141. freeResult(result);
  142. }
  143. makePointerValue((void *)ap[0].value->value, LINEFILE, CNEXT_NT);
  144. return result->type;
  145. }
  146. static ResultType vm_getLinkValuePointer(O_FUNC){
  147. ArgumentParser ap[] = {{.type=name_value, .name=L"link", .must=1, .long_arg=false},
  148. {.must=-1}};
  149. setResultCore(result);
  150. {
  151. parserArgumentUnion(ap, arg, CNEXT_NT);
  152. if (!CHECK_RESULT(result))
  153. return result->type;
  154. freeResult(result);
  155. }
  156. makePointerValue((void *)ap[0].value, LINEFILE, CNEXT_NT);
  157. return result->type;
  158. }
  159. static ResultType vm_quit(O_FUNC){
  160. if (arg != NULL)
  161. setResultError(E_ArgumentException, MANY_ARG, LINEFILE, true, CNEXT_NT);
  162. else
  163. setResultError(E_QuitException, L"vmcore quit()", LINEFILE, true, CNEXT_NT);
  164. return R_error;
  165. }
  166. static ResultType vm_raise(O_FUNC){
  167. ArgumentParser ap[] = {{.type=name_value, .name=L"signal", .must=1, .long_arg=false},
  168. {.must=-1}};
  169. setResultCore(result);
  170. {
  171. parserArgumentUnion(ap, arg, CNEXT_NT);
  172. if (!CHECK_RESULT(result))
  173. return result->type;
  174. freeResult(result);
  175. }
  176. if (ap[0].value->value->type != V_int) {
  177. setResultError(E_TypeException, ONLY_ACC(signal, int), LINEFILE, true, CNEXT_NT);
  178. return R_error;
  179. }
  180. raise(ap[0].value->value->data.int_.num);
  181. setResult(result, inter);
  182. return result->type;
  183. }
  184. static ResultType vm_signal(O_FUNC){
  185. ArgumentParser ap[] = {{.type=name_value, .name=L"signal", .must=1, .long_arg=false},
  186. {.type=name_value, .name=L"func", .must=1, .long_arg=false},
  187. {.must=-1}};
  188. SignalList *sl = inter->sig_list;
  189. SignalList *tmp;
  190. vsignal sig;
  191. setResultCore(result);
  192. {
  193. parserArgumentUnion(ap, arg, CNEXT_NT);
  194. if (!CHECK_RESULT(result))
  195. return result->type;
  196. freeResult(result);
  197. }
  198. if (ap[0].value->value->type != V_int) {
  199. setResultError(E_TypeException, ONLY_ACC(signal, int), LINEFILE, true, CNEXT_NT);
  200. return R_error;
  201. }
  202. sig = ap[0].value->value->data.int_.num;
  203. if ((tmp = checkSignalList(sig, inter->sig_list)) == NULL) {
  204. inter->sig_list = makeSignalList(ap[0].value->value->data.int_.num, ap[1].value); // 添加新的节点
  205. inter->sig_list->next = sl; // 重新拼接
  206. setResult(result, inter); // 返回值为null
  207. } else {
  208. LinkValue *old = exchangeSignalFunc(tmp, ap[1].value); // 此处会为 old 添加 tmp_link
  209. setResultOperation(result, old); // 更换函数, 旧函数作为返回值 (此处也会为 old 添加 tmp_link)
  210. gc_freeTmpLink(&old->gc_status); // 释放掉 exchangeSignalFunc 为 old 添加到 tmp_link
  211. }
  212. signal(sig, vmSignalHandler); // 绑定函数
  213. return result->type;
  214. }
  215. static ResultType vm_signal_core(O_FUNC, int status){
  216. ArgumentParser ap[] = {{.type=name_value, .name=L"signal", .must=1, .long_arg=false},
  217. {.must=-1}};
  218. vsignal sig;
  219. LinkValue *old;
  220. setResultCore(result);
  221. {
  222. parserArgumentUnion(ap, arg, CNEXT_NT);
  223. if (!CHECK_RESULT(result))
  224. return result->type;
  225. freeResult(result);
  226. }
  227. if (ap[0].value->value->type != V_int) {
  228. setResultError(E_TypeException, ONLY_ACC(signal, int), LINEFILE, true, CNEXT_NT);
  229. return R_error;
  230. }
  231. sig = ap[0].value->value->data.int_.num;
  232. if ((old = delSignalList(sig, &(inter->sig_list))) != NULL) {
  233. setResultOperation(result, old);
  234. gc_freeTmpLink(&old->gc_status); // 释放 delSignalList 中设置 old 的 tmp_link
  235. } else
  236. setResult(result, inter); // 默认返回none
  237. switch (status) {
  238. default:
  239. case 1:
  240. signal(sig, SIG_IGN); // vm 不捕捉该信号
  241. break;
  242. case 2:
  243. signal(sig, vmSignalHandler); // 用 vm 的默认方式处理信号
  244. break;
  245. case 3:
  246. signal(sig, SIG_ERR); // 按错误处理
  247. break;
  248. }
  249. return result->type;
  250. }
  251. static ResultType vm_signal_ignore(O_FUNC) {
  252. return vm_signal_core(CO_FUNC(arg, var_list, result, belong), 1);
  253. }
  254. static ResultType vm_signal_default(O_FUNC) {
  255. return vm_signal_core(CO_FUNC(arg, var_list, result, belong), 2);
  256. }
  257. static ResultType vm_signal_err(O_FUNC) {
  258. return vm_signal_core(CO_FUNC(arg, var_list, result, belong), 3);
  259. }
  260. void registeredSysFunction(R_FUNC){
  261. NameFunc tmp[] = {{L"super", vm_super, fp_no_, .var=nfv_notpush},
  262. {L"static_method", vm_no_, fp_no_, .var=nfv_notpush},
  263. {L"func_method", vm_func_, fp_no_, .var=nfv_notpush},
  264. {L"func_class_method", vm_func_class, fp_no_, .var=nfv_notpush},
  265. {L"func_obj_method", vm_func_obj, fp_no_, .var=nfv_notpush},
  266. {L"class_method", vm_class, fp_no_, .var=nfv_notpush},
  267. {L"obj_method", vm_obj, fp_no_, .var=nfv_notpush},
  268. {L"simple_method", vm_all, fp_no_, .var=nfv_notpush},
  269. {L"func_simple_method", vm_func_all, fp_no_, .var=nfv_notpush},
  270. {L"cls_method", vm_cls, fp_no_, .var=nfv_notpush},
  271. {L"func_cls_method", vm_func_cls, fp_no_, .var=nfv_notpush},
  272. {L"cls_obj_method", vm_cls_obj, fp_no_, .var=nfv_notpush},
  273. {L"cls_class_method", vm_cls_class, fp_no_, .var=nfv_notpush},
  274. {L"cls_simple_method", vm_cls_all, fp_no_, .var=nfv_notpush},
  275. {L"func_cls_obj_method", vm_func_cls_obj, fp_no_, .var=nfv_notpush},
  276. {L"func_cls_obj_method", vm_func_cls_class, fp_no_, .var=nfv_notpush},
  277. {L"func_cls_simple_method", vm_func_cls_all, fp_no_, .var=nfv_notpush},
  278. {L"is_now_run", vm_isnowrun, fp_no_, .var=nfv_notpush},
  279. {L"dis_now_run", vm_disnowrun, fp_no_, .var=nfv_notpush},
  280. {L"open", vm_open, fp_no_, .var=nfv_notpush},
  281. {L"getValuePointer", vm_getValuePointer,fp_no_, .var=nfv_notpush},
  282. {L"getLinkValuePointer", vm_getLinkValuePointer,fp_no_, .var=nfv_notpush},
  283. {L"quit", vm_quit, fp_no_, .var=nfv_notpush},
  284. {L"raise", vm_raise, fp_no_, .var=nfv_notpush},
  285. {L"signal", vm_signal, fp_no_, .var=nfv_notpush},
  286. {L"signal_ignore", vm_signal_ignore, fp_no_, .var=nfv_notpush},
  287. {L"signal_default", vm_signal_default, fp_no_, .var=nfv_notpush},
  288. {L"signal_err", vm_signal_err, fp_no_, .var=nfv_notpush},
  289. {NULL, NULL}};
  290. iterBaseNameFunc(tmp, belong, CFUNC_CORE(var_list));
  291. }