function.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. value->data.function.function_data.push = true;
  7. }
  8. ResultType function_new(O_FUNC){
  9. LinkValue *value = NULL;
  10. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  11. {.must=-1}};
  12. int status = 1;
  13. setResultCore(result);
  14. arg = parserValueArgument(ap, arg, &status, NULL);
  15. if (status != 1){
  16. setResultError(E_ArgumentException, FEW_ARG, LINEFILE, true, CNEXT_NT);
  17. return R_error;
  18. }
  19. { // 不使用make_new, 需要设定makeObject的set_out_var
  20. Inherit *object_father = getInheritFromValueCore(inter->data.base_obj[B_FUNCTION]);
  21. Value *new_object = makeObject(inter, NULL, NULL, false, object_father); // 不加入out_var
  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_, .var=nfv_notpush},
  88. {inter->data.mag_func[M_INIT], function_init, object_free_, .var=nfv_notpush},
  89. {NULL, NULL}};
  90. gc_addTmpLink(&object->gc_status);
  91. addBaseClassVar(L"func", object, belong, inter);
  92. iterBaseClassFunc(tmp, object, CFUNC_CORE(inter->var_list));
  93. gc_freeTmpLink(&object->gc_status);
  94. }
  95. void makeBaseFunction(Inter *inter){
  96. LinkValue *function = makeBaseChildClass(inter->data.base_obj[B_VOBJECT], inter);
  97. gc_addStatementLink(&function->gc_status);
  98. inter->data.base_obj[B_FUNCTION] = function;
  99. }
  100. void functionPresetting(LinkValue *func, Inter *inter) { // 提前注册func_new
  101. Result result;
  102. VarList *object_var = func->value->object.var;
  103. LinkValue *func_new;
  104. setResultCore(&result);
  105. func_new = makeCFunctionFromOf(function_new, func, function_new, func, NULL, inter); // var_list为NULL, 即声明为内联函数 (若不声明为内联函数则改为inter.var_list即可)
  106. func_new->value->data.function.function_data.pt_type = class_free_;
  107. addStrVar(inter->data.mag_func[M_NEW], false, true, func_new, LINEFILE, false, CFUNC_NT(object_var, &result, func));
  108. freeResult(&result);
  109. freeResult(&result);
  110. }