执行函数调用 1. 先给参数赋值 2. 调用函数,内部就是递归调用 block
(functionObject *stack.FuncObject, paramValues []interface{})
| 1017 | // 1. 先给参数赋值 |
| 1018 | // 2. 调用函数,内部就是递归调用 block |
| 1019 | func (v *Visitor) executeFunctionCall(functionObject *stack.FuncObject, paramValues []interface{}) interface{} { |
| 1020 | var ret interface{} |
| 1021 | |
| 1022 | // 添加函数栈帧 |
| 1023 | frame := stack.NewObjectStackFrame(functionObject) |
| 1024 | v.pushStack(frame) |
| 1025 | |
| 1026 | // 拿到函数声明时所对应的 ctx, type_scope_resolver.go EnterFunctionDeclaration, g4:functionDeclaration |
| 1027 | declarationContext := functionObject.GetFunction().GetCtx().(*parser.FunctionDeclarationContext) |
| 1028 | formalParametersContext := declarationContext.FormalParameters().(*parser.FormalParametersContext) |
| 1029 | if formalParametersContext.FormalParameterList() != nil { |
| 1030 | // 为函数参数赋值 |
| 1031 | for i, context := range formalParametersContext.FormalParameterList().(*parser.FormalParameterListContext).AllFormalParameter() { |
| 1032 | parameterContext := context.(*parser.FormalParameterContext) |
| 1033 | value := v.VisitVariableDeclaratorId(parameterContext.VariableDeclaratorId().(*parser.VariableDeclaratorIdContext)) |
| 1034 | switch value.(type) { |
| 1035 | case *LeftValue: |
| 1036 | leftValue := value.(*LeftValue) |
| 1037 | leftValue.SetValue(paramValues[i]) |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // 支持可变参数 |
| 1042 | lastParam, ok := formalParametersContext.FormalParameterList().(*parser.FormalParameterListContext) |
| 1043 | if ok { |
| 1044 | context, ok := lastParam.LastFormalParameter().(*parser.LastFormalParameterContext) |
| 1045 | if ok { |
| 1046 | normalParamSize := len(lastParam.AllFormalParameter()) |
| 1047 | value := v.VisitVariableDeclaratorId(context.VariableDeclaratorId().(*parser.VariableDeclaratorIdContext)) |
| 1048 | switch value.(type) { |
| 1049 | case *LeftValue: |
| 1050 | leftValue := value.(*LeftValue) |
| 1051 | // 去掉普通参数 |
| 1052 | variableParams := paramValues[normalParamSize:] |
| 1053 | leftValue.SetValue(variableParams) |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | } |
| 1059 | |
| 1060 | // 执行方法体 |
| 1061 | ret = v.VisitFunctionDeclaration(declarationContext) |
| 1062 | |
| 1063 | //如果是 returnObject,需要把真正的数据取出 |
| 1064 | switch ret.(type) { |
| 1065 | case *ReturnObject: |
| 1066 | ret = ret.(*ReturnObject).GetReturnObject() |
| 1067 | } |
| 1068 | |
| 1069 | v.popStack() |
| 1070 | return ret |
| 1071 | } |
| 1072 | |
| 1073 | // 类函数调用 |
| 1074 | func (v *Visitor) receiveFunctionCall(ctx *parser.FunctionCallContext, classObject *stack.ClassObject, isSuper bool) interface{} { |
no test coverage detected