function.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "__ofunc.h"
  2. static void setFunctionData(Value *value, LinkValue *cls, Inter *inter) {
  3. value->data.function.function_data.pt_type = inter->data.default_pt_type;
  4. value->data.function.function_data.cls = cls;
  5. value->data.function.function_data.run = false;
  6. }
  7. ResultType function_new(O_FUNC){
  8. LinkValue *value = NULL;
  9. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  10. {.must=-1}};
  11. int status = 1;
  12. setResultCore(result);
  13. arg = parserValueArgument(ap, arg, &status, NULL);
  14. if (status != 1){
  15. setResultError(E_ArgumentException, FEW_ARG, LINEFILE, true, CNEXT_NT);
  16. return R_error;
  17. }
  18. {
  19. Inherit *object_father = getInheritFromValueCore(inter->data.base_obj[B_FUNCTION]);
  20. VarList *new_var = copyVarList(var_list, false, inter);
  21. Value *new_object = makeObject(inter, NULL, new_var, object_father);
  22. value = makeLinkValue(new_object, belong, auto_aut, inter);
  23. gc_freeTmpLink(&new_object->gc_status);
  24. }
  25. value->value->type = V_func;
  26. value->value->data.function.type = vm_func;
  27. value->value->data.function.function = NULL;
  28. value->value->data.function.pt = NULL;
  29. value->value->data.function.of = NULL;
  30. setFunctionData(value->value, ap->value, inter);
  31. run_init(value, arg, LINEFILE, CNEXT_NT);
  32. return result->type;
  33. }
  34. ResultType function_init(O_FUNC){
  35. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  36. {.type=only_value, .must=0, .long_arg=false},
  37. {.must=-1}};
  38. LinkValue *func;
  39. setResultCore(result);
  40. parserArgumentUnion(ap, arg, CNEXT_NT);
  41. if (!CHECK_RESULT(result))
  42. return result->type;
  43. freeResult(result);
  44. if ((func = ap[0].value)->value->type != V_func) {
  45. setResultError(E_TypeException, INSTANCE_ERROR(func), LINEFILE, true, CNEXT_NT);
  46. return R_error;
  47. }
  48. if (ap[1].value != NULL) {
  49. Statement *return_ = makeBaseLinkValueStatement(ap[1].value, LINEFILE);
  50. func->value->data.function.function = makeReturnStatement(return_, LINEFILE);
  51. func->value->data.function.function_data.pt_type = free_;
  52. func->value->data.function.type = vm_func;
  53. }
  54. setResult(result, inter);
  55. return result->type;
  56. }
  57. ResultType function_set(O_FUNC){ // 针对FFI设置vaargs
  58. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  59. {.type=only_value, .must=0, .long_arg=true},
  60. {.must=-1}};
  61. LinkValue *func;
  62. setResultCore(result);
  63. parserArgumentUnion(ap, arg, CNEXT_NT);
  64. if (!CHECK_RESULT(result))
  65. return result->type;
  66. freeResult(result);
  67. if ((func = ap[0].value)->value->type != V_func || (func = ap[0].value)->value->data.function.type != f_func) {
  68. setResultError(E_TypeException, INSTANCE_ERROR(func), LINEFILE, true, CNEXT_NT);
  69. return R_error;
  70. }
  71. if (ap[1].arg != NULL) {
  72. LinkValue *list;
  73. makeListValue(ap[1].arg, LINEFILE, L_tuple, CNEXT_NT);
  74. if (!CHECK_RESULT(result))
  75. return result->type;
  76. GET_RESULT(list, result);
  77. addAttributes(L"vaargs", false, list, LINEFILE, true, CFUNC_NT(var_list, result, func));
  78. gc_freeTmpLink(&list->gc_status);
  79. } else
  80. findFromVarList(L"vaargs", 0, del_var, CFUNC_CORE(var_list));
  81. if (CHECK_RESULT(result))
  82. setResultOperation(result, func);
  83. return result->type;
  84. }
  85. void registeredFunction(R_FUNC){
  86. LinkValue *object = inter->data.base_obj[B_FUNCTION];
  87. NameFunc tmp[] = {{L"set", function_set, object_free_},
  88. {NULL, NULL}};
  89. gc_addTmpLink(&object->gc_status);
  90. addBaseClassVar(L"func", object, belong, inter);
  91. iterBaseClassFunc(tmp, object, CFUNC_CORE(inter->var_list));
  92. gc_freeTmpLink(&object->gc_status);
  93. }
  94. void makeBaseFunction(Inter *inter){
  95. LinkValue *function = makeBaseChildClass(inter->data.base_obj[B_VOBJECT], inter);
  96. gc_addStatementLink(&function->gc_status);
  97. inter->data.base_obj[B_FUNCTION] = function;
  98. }
  99. void functionPresetting(LinkValue *func, LinkValue **func_new, LinkValue **func_init, Inter *inter) {
  100. *func_new = makeCFunctionFromOf(function_new, func, function_new, function_init, func, inter->var_list, inter);
  101. *func_init = makeCFunctionFromOf(function_init, func, function_new, function_init, func, inter->var_list, inter);
  102. (*func_new)->value->data.function.function_data.pt_type = class_free_;
  103. (*func_init)->value->data.function.function_data.pt_type = object_free_;
  104. }
  105. void functionPresettingLast(LinkValue *func, LinkValue *func_new, LinkValue *func_init, Inter *inter) {
  106. Result result;
  107. VarList *object_var = func->value->object.var;
  108. setResultCore(&result);
  109. addStrVar(inter->data.mag_func[M_NEW], false, true, func_new, LINEFILE, false, CFUNC_NT(object_var, &result, func));
  110. freeResult(&result);
  111. addStrVar(inter->data.mag_func[M_INIT], false, true, func_init, LINEFILE, false, CFUNC_NT(object_var, &result, func));
  112. freeResult(&result);
  113. }