From 1d6c7dccf8d7c78904f946a590a5f2461e72a692 Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Sun, 19 Jan 2020 11:10:16 -0500 Subject: [PATCH 1/7] [cgogen] refs #17 Correcting name to var in case CamelCase --- src/cmd/cgogen.go | 182 +++++++++++++++++++++------------------------- 1 file changed, 83 insertions(+), 99 deletions(-) diff --git a/src/cmd/cgogen.go b/src/cmd/cgogen.go index ad7c260..484a82b 100644 --- a/src/cmd/cgogen.go +++ b/src/cmd/cgogen.go @@ -88,7 +88,6 @@ var handleTypes map[string]string var returnVarName = "____error_code" var returnErrName = "____return_err" var dealOutStringAsGostring = true -var get_package_path_from_file_name = true func main() { handleTypes = make(map[string]string) @@ -134,11 +133,8 @@ func doGoFile() { fast, err := parser.ParseFile(fset, "", fo, parser.AllErrors|parser.ParseComments) check(err) - packagePath := "" - if get_package_path_from_file_name { - packagePath = getPackagePathFromFileName(cfg.Path) + "/" + fast.Name.Name - applog("Package Path: %s ", packagePath) - } + packagePath := getPackagePathFromFileName(cfg.Path) + "/" + fast.Name.Name + applog("Package Path: %s ", packagePath) if packagePath == "" { packagePath = fast.Name.Name } @@ -327,7 +323,7 @@ func isExternalName(importName string) bool { } } -func typeSpecStr(_typeExpr *ast.Expr, package_name string, isOutput bool) (string, bool) { +func typeSpecStr(_typeExpr *ast.Expr, packageName string, isOutput bool) (string, bool) { addPointer := false spec := "" for _typeExpr != nil { @@ -346,7 +342,7 @@ func typeSpecStr(_typeExpr *ast.Expr, package_name string, isOutput bool) (strin continue } if ellipsisExpr, isEllipsis := (*_typeExpr).(*ast.Ellipsis); isEllipsis { - tspec, ok := typeSpecStr(&ellipsisExpr.Elt, package_name, isOutput) + tspec, ok := typeSpecStr(&ellipsisExpr.Elt, packageName, isOutput) if ok { spec += "..." + tspec _typeExpr = nil @@ -373,8 +369,8 @@ func typeSpecStr(_typeExpr *ast.Expr, package_name string, isOutput bool) (strin continue } if mapExpr, isMap := (*_typeExpr).(*ast.MapType); isMap { - tspeckey, okkey := typeSpecStr(&mapExpr.Key, package_name, false) - tspecvalue, okvalue := typeSpecStr(&mapExpr.Key, package_name, false) + tspeckey, okkey := typeSpecStr(&mapExpr.Key, packageName, false) + tspecvalue, okvalue := typeSpecStr(&mapExpr.Key, packageName, false) if okkey && okvalue { return spec + "map[" + tspeckey + "]" + tspecvalue, true } else { @@ -385,7 +381,7 @@ func typeSpecStr(_typeExpr *ast.Expr, package_name string, isOutput bool) (strin selExpr, isSelector := (*_typeExpr).(*ast.SelectorExpr) if isIdent || isSelector { isDealt := false - extern_package := package_name + externPackage := packageName typeName := "" if isIdent { typeName = identExpr.Name @@ -400,28 +396,28 @@ func typeSpecStr(_typeExpr *ast.Expr, package_name string, isOutput bool) (strin typeName = selExpr.Sel.Name identSelExpr, isSelIdent := (selExpr.X).(*ast.Ident) if isSelIdent { - extern_package = identSelExpr.Name - isDealt = isInHandleTypesList(extern_package + "." + typeName) + externPackage = identSelExpr.Name + isDealt = isInHandleTypesList(externPackage + "." + typeName) if isDealt { - spec = getHandleName(extern_package + "." + typeName) - } else if isInCustomTypesList(extern_package + "." + typeName) { - spec = getCustomTypeName(extern_package + "." + typeName) + spec = getHandleName(externPackage + "." + typeName) + } else if isInCustomTypesList(externPackage + "." + typeName) { + spec = getCustomTypeName(externPackage + "." + typeName) isDealt = true - } else if !isSkycoinName(extern_package) { - return extern_package, false + } else if !isSkycoinName(externPackage) { + return externPackage, false } } } if !isDealt { - if isInHandleTypesList(extern_package + packageSeparator + typeName) { - spec = getHandleName(extern_package + packageSeparator + typeName) + if isInHandleTypesList(externPackage + packageSeparator + typeName) { + spec = getHandleName(externPackage + packageSeparator + typeName) } else { isExported := isAsciiUpper(rune(typeName[0])) if spec == "" && !addPointer && isExported { addPointer = true } if isExported { - spec += "C." + extern_package + packageSeparator + spec += "C." + externPackage + packageSeparator } else { if !IsBasicGoType(typeName) { return "", false //Don't deal with unexported types @@ -497,10 +493,7 @@ func getPackagePathFromFileName(filePath string) string { //Create code for wrapper function func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependant_types *[]string) (isDependant bool) { isDependant = false - packagePath := "" - if get_package_path_from_file_name { - packagePath = getPackagePathFromFileName(cfg.Path) - } + packagePath := getPackagePathFromFileName(cfg.Path) if packagePath == "" { packagePath = fast.Name.Name } @@ -548,33 +541,33 @@ func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependa } allparams := fdecl.Type.Params.List[:] - return_fields_index := len(allparams) + returnFieldsIndex := len(allparams) var retField *ast.Field = nil if fdecl.Type.Results != nil && fdecl.Type.Results.List != nil { //Find the return argument of type error. //It should always be the last argument but search just in case - error_index := -1 + errorIndex := -1 for index, field := range fdecl.Type.Results.List { identExpr, isIdent := (field.Type).(*ast.Ident) if isIdent && identExpr.Name == "error" { - error_index = index + errorIndex = index break } } - if error_index >= 0 { - retField = fdecl.Type.Results.List[error_index] - return_params := append(fdecl.Type.Results.List[0:error_index], fdecl.Type.Results.List[error_index+1:]...) - allparams = append(allparams, return_params...) + if errorIndex >= 0 { + retField = fdecl.Type.Results.List[errorIndex] + returnParams := append(fdecl.Type.Results.List[0:errorIndex], fdecl.Type.Results.List[errorIndex+1:]...) + allparams = append(allparams, returnParams...) } else { allparams = append(allparams, fdecl.Type.Results.List[:]...) } } - var output_vars_convert_code []jen.Code + var outputVarsConvertCode []jen.Code for fieldIdx, field := range allparams { - if fieldIdx >= return_fields_index { + if fieldIdx >= returnFieldsIndex { // Field in return types list typeName, ok := typeSpecStr(&field.Type, fast.Name.Name, true) if !ok || isTypeSpecInDependantList(typeName, dependant_types) { @@ -586,7 +579,7 @@ func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependa } if len(typeName) > 0 && rune(typeName[0]) == '[' { typeName = "*C.GoSlice_" - } else if dealOutStringAsGostring && typeName == "string" { + } else if typeName == "string" { typeName = "*C.GoString_" } else if IsBasicGoType(typeName) { typeName = "*" + typeName @@ -597,7 +590,7 @@ func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependa params = append(params, jen.Id(paramName).Id(typeName)) convertCode := getCodeToConvertOutParameter(&field.Type, fast.Name.Name, paramName, false) if convertCode != nil { - output_vars_convert_code = append(output_vars_convert_code, convertCode) + outputVarsConvertCode = append(outputVarsConvertCode, convertCode) } } else { @@ -637,50 +630,41 @@ func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependa } } var retvars []jen.Code - if return_fields_index < len(allparams) { - for i := return_fields_index; i < len(allparams); i++ { + if returnFieldsIndex < len(allparams) { + for i := returnFieldsIndex; i < len(allparams); i++ { retvars = append(retvars, jen.Id(resultName("arg"+fmt.Sprintf("%d", i)))) } } if retField != nil { retvars = append(retvars, jen.Id(returnErrName)) } - var call_func_code jen.Code + var callFuncCode jen.Code if len(retvars) > 0 { if fdecl.Recv != nil { - call_func_code = + callFuncCode = jen.List(retvars...).Op(":=").Id(fdecl.Recv.List[0].Names[0].Name).Dot(fdecl.Name.Name).Call(callparams...) } else { - if mainPackagePath != "" { - call_func_code = - jen.List(retvars...).Op(":=").Qual(mainPackagePath+packagePath, - fdecl.Name.Name).Call(callparams...) - } else { - call_func_code = - jen.List(retvars...).Op(":=").Id(fdecl.Name.Name).Call(callparams...) - } + callFuncCode = + jen.List(retvars...).Op(":=").Qual(mainPackagePath+packagePath, + fdecl.Name.Name).Call(callparams...) } } else { if fdecl.Recv != nil { - call_func_code = jen.Id(fdecl.Recv.List[0].Names[0].Name).Dot(fdecl.Name.Name).Call(callparams...) + callFuncCode = jen.Id(fdecl.Recv.List[0].Names[0].Name).Dot(fdecl.Name.Name).Call(callparams...) } else { - if mainPackagePath != "" { - call_func_code = jen.Qual(mainPackagePath+packagePath, - fdecl.Name.Name).Call(callparams...) - } else { - call_func_code = jen.Id(fdecl.Name.Name).Call(callparams...) - } + callFuncCode = jen.Qual(mainPackagePath+packagePath, + fdecl.Name.Name).Call(callparams...) } } - blockParams = append(blockParams, call_func_code) + blockParams = append(blockParams, callFuncCode) stmt = stmt.Parens(jen.Id(returnVarName).Id("uint32")) if retField != nil { blockParams = append(blockParams, jen.Id(returnVarName).Op("=").Id("libErrorCode").Call(jen.Id(returnErrName))) - convertOutputCode := jen.If(jen.Id(returnErrName).Op("==").Nil()).Block(output_vars_convert_code...) + convertOutputCode := jen.If(jen.Id(returnErrName).Op("==").Nil()).Block(outputVarsConvertCode...) blockParams = append(blockParams, convertOutputCode) } else { - blockParams = append(blockParams, output_vars_convert_code...) + blockParams = append(blockParams, outputVarsConvertCode...) } blockParams = append(blockParams, jen.Return()) @@ -690,15 +674,15 @@ func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependa } //Check if type is in dependant list -func isTypeSpecInDependantList(typeSpec string, dependant_list *[]string) bool { - if dependant_list == nil { +func isTypeSpecInDependantList(typeSpec string, dependantList *[]string) bool { + if dependantList == nil { return false } //Do not allow extern types in function parameters if strings.Contains(typeSpec, "C._") { return true } - for _, t := range *dependant_list { + for _, t := range *dependantList { if strings.HasSuffix(typeSpec, "C."+t) { return true } @@ -719,11 +703,11 @@ func getTypeCastCode(leftPart *jen.Statement, typeExpr *ast.Expr, } } else if selectorExpr, isSelector := (*typeExpr).(*ast.SelectorExpr); isSelector { if identExpr, isIdent := (selectorExpr.X).(*ast.Ident); isIdent { - extern_package, found := findImportPath(identExpr.Name) + externPackage, found := findImportPath(identExpr.Name) typeName := selectorExpr.Sel.Name if found { - outFile.ImportAlias(extern_package, identExpr.Name) - return leftPart.Qual(extern_package, typeName) + outFile.ImportAlias(externPackage, identExpr.Name) + return leftPart.Qual(externPackage, typeName) } else { return leftPart.Id(identExpr.Name).Dot(typeName) } @@ -846,7 +830,7 @@ func getCodeToConvertOutParameter(_typeExpr *ast.Expr, package_name string, name return getCodeToConvertOutParameter(_type, package_name, name, true) } else if identExpr, isIdent := (*_typeExpr).(*ast.Ident); isIdent { typeName := identExpr.Name - if dealOutStringAsGostring && typeName == "string" { + if typeName == "string" { return jen.Id("copyString").Call(jen.Id(argName(name)), jen.Id(name)) } else if IsBasicGoType(typeName) { return jen.Op("*").Id(name).Op("=").Id(argName(name)) @@ -940,7 +924,7 @@ func processTypeExpression(fast *ast.File, type_expr ast.Expr, dependant := false if typeStruct, isTypeStruct := (type_expr).(*ast.StructType); isTypeStruct { cCode += "struct{\n" - error := false + e := false for _, field := range typeStruct.Fields.List { var names []string for _, fieldName := range field.Names { @@ -961,7 +945,7 @@ func processTypeExpression(fast *ast.File, type_expr ast.Expr, } cCode += typeCode } else { - error = true + e = true } cCode += ";\n" } @@ -979,7 +963,7 @@ func processTypeExpression(fast *ast.File, type_expr ast.Expr, if dependant && depth == 1 { addDependant(dependantTypes, typeName) } - result = !error + result = !e } else if arrayExpr, isArray := (type_expr).(*ast.ArrayType); isArray { var arrayCode string var arrayElCode string @@ -1088,8 +1072,8 @@ func processTypeExpression(fast *ast.File, type_expr ast.Expr, } } typeFound := false - for _, defined_type := range *definedTypes { - if defined_type == typeCode { + for _, definedType := range *definedTypes { + if definedType == typeCode { typeFound = true } } @@ -1132,8 +1116,8 @@ func processTypeExpression(fast *ast.File, type_expr ast.Expr, return cCode, result, dependant } -func isDependantType(dependant_types *[]string, typeName string) bool { - for _, t := range *dependant_types { +func isDependantType(dependantTypes *[]string, typeName string) bool { + for _, t := range *dependantTypes { if t == typeName { return true } @@ -1141,66 +1125,66 @@ func isDependantType(dependant_types *[]string, typeName string) bool { return false } -func addDependant(dependant_types *[]string, typeName string) { - for _, t := range *dependant_types { +func addDependant(dependantTypes *[]string, typeName string) { + for _, t := range *dependantTypes { if t == typeName { return } } - *dependant_types = append(*dependant_types, typeName) + *dependantTypes = append(*dependantTypes, typeName) } /* Process a type definition in GO and returns the c code for the definition */ func processTypeDef(fast *ast.File, tdecl *ast.GenDecl, - defined_types *[]string, forwards_declarations *[]string, - dependant_types *[]string) (string, bool, bool) { - result_code := "" + definedTypes *[]string, forwardsDeclarations *[]string, + dependantTypes *[]string) (string, bool, bool) { + resultCode := "" result := true isDependant := false for _, s := range tdecl.Specs { if typeSpec, isTypeSpec := (s).(*ast.TypeSpec); isTypeSpec { - type_c_code, ok, isDependantExpr := processTypeExpression(fast, typeSpec.Type, - fast.Name.Name, typeSpec.Name.Name, defined_types, forwards_declarations, 1, - dependant_types) + typeCCode, ok, isDependantExpr := processTypeExpression(fast, typeSpec.Type, + fast.Name.Name, typeSpec.Name.Name, definedTypes, forwardsDeclarations, 1, + dependantTypes) if ok { if isDependantExpr { isDependant = true } - result_code += "typedef " - result_code += type_c_code - result_code += ";\n" - *defined_types = append(*defined_types, fast.Name.Name+packageSeparator+typeSpec.Name.Name) + resultCode += "typedef " + resultCode += typeCCode + resultCode += ";\n" + *definedTypes = append(*definedTypes, fast.Name.Name+packageSeparator+typeSpec.Name.Name) } else { result = false } } } - return result_code, result, isDependant + return resultCode, result, isDependant } /* Process all type definitions. Returns c code for all the defintions */ func processTypeDefs(fast *ast.File, typeDecls []*ast.GenDecl, dependant_types *[]string) string { - result_code := "" - var defined_types []string + resultCode := "" + var definedTypes []string for key := range GetBasicTypes() { ctype, ok := GetCTypeFromGoType(key) if ok { - defined_types = append(defined_types, ctype) + definedTypes = append(definedTypes, ctype) } } unprocessed := len(typeDecls) - went_blank := false - for unprocessed > 0 && !went_blank { - went_blank = true + wentBlank := false + for unprocessed > 0 && !wentBlank { + wentBlank = true for index, typeDecl := range typeDecls { if typeDecl != nil { - typeCode, ok, isDependant := processTypeDef(fast, typeDecl, &defined_types, nil, dependant_types) + typeCode, ok, isDependant := processTypeDef(fast, typeDecl, &definedTypes, nil, dependant_types) if ok { - went_blank = false + wentBlank = false typeDecls[index] = nil if !(cfg.IgnoreDependants && isDependant) { - result_code += typeCode + resultCode += typeCode } unprocessed -= 1 } @@ -1209,20 +1193,20 @@ func processTypeDefs(fast *ast.File, typeDecls []*ast.GenDecl, dependant_types * } //TODO: if unprocessed > 0 then there are cyclic type references. Use forward declarations. - var forwards_declarations []string + var forwardsDeclarations []string if unprocessed > 0 { for _, typeDecl := range typeDecls { if typeDecl != nil { - typeCode, ok, isDependant := processTypeDef(fast, typeDecl, &defined_types, &forwards_declarations, dependant_types) + typeCode, ok, isDependant := processTypeDef(fast, typeDecl, &definedTypes, &forwardsDeclarations, dependant_types) if ok { if !(cfg.IgnoreDependants && isDependant) { - result_code += typeCode + resultCode += typeCode } } } } } - return result_code + return resultCode } //Remove extra space in export indication From 5bce1675121bfcc6ce50566269384efcf9589572 Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Sun, 19 Jan 2020 19:08:51 -0500 Subject: [PATCH 2/7] [cmd] refs #17 Remove old varname `dealOutStringAsGostring` --- src/cmd/cgogen.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cmd/cgogen.go b/src/cmd/cgogen.go index 484a82b..73aaa94 100644 --- a/src/cmd/cgogen.go +++ b/src/cmd/cgogen.go @@ -81,13 +81,12 @@ var arrayTypes = map[string]string{ } //Imports used in this code file -var importDefs [](*ast.GenDecl) +var importDefs []*ast.GenDecl //types that will be replaced by handles var handleTypes map[string]string var returnVarName = "____error_code" var returnErrName = "____return_err" -var dealOutStringAsGostring = true func main() { handleTypes = make(map[string]string) From bc8ea9217e978fa7719cd2ed680a77bfde974394 Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Tue, 21 Jan 2020 07:15:21 -0500 Subject: [PATCH 3/7] [cgogen] refs #17 Define map to save all callback --- src/cmd/cgogen.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/cmd/cgogen.go b/src/cmd/cgogen.go index 73aaa94..e1b633f 100644 --- a/src/cmd/cgogen.go +++ b/src/cmd/cgogen.go @@ -62,6 +62,11 @@ var ( //Map of types that will replaced by custom types var customTypesMap = make(map[string]string) +//Map of callback +var callbackMap = map[string]string{ + "coin": "FeeCalculator", +} + //Types that will use functions of type inplace to convert var inplaceConvertTypesPackages = map[string]string{ "PubKeySlice": "cipher", @@ -90,6 +95,7 @@ var returnErrName = "____return_err" func main() { handleTypes = make(map[string]string) + callbackMap = make(map[string]string) cfg.register() flag.Parse() @@ -290,9 +296,9 @@ func findImportPath(importName string) (string, bool) { if importSpec.Name != nil { name = importSpec.Name.Name } else { - path_parts := strings.Split(path, "/") - if len(path_parts) > 0 { - name = path_parts[len(path_parts)-1] + pathParts := strings.Split(path, "/") + if len(pathParts) > 0 { + name = pathParts[len(pathParts)-1] } } if name == importName { @@ -490,7 +496,7 @@ func getPackagePathFromFileName(filePath string) string { } //Create code for wrapper function -func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependant_types *[]string) (isDependant bool) { +func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependantTypes *[]string) (isDependant bool) { isDependant = false packagePath := getPackagePathFromFileName(cfg.Path) if packagePath == "" { @@ -523,7 +529,7 @@ func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependa recvParamName := receiver.List[0].Names[0].Name recvParam := jen.Id(argName(recvParamName)) typeSpec, ok := typeSpecStr(_type, fast.Name.Name, false) - if !ok || isTypeSpecInDependantList(typeSpec, dependant_types) { + if !ok || isTypeSpecInDependantList(typeSpec, dependantTypes) { isDependant = true if cfg.IgnoreDependants { //TODO: stdevEclipse Check if type can be replaced by another type or handle @@ -569,7 +575,7 @@ func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependa if fieldIdx >= returnFieldsIndex { // Field in return types list typeName, ok := typeSpecStr(&field.Type, fast.Name.Name, true) - if !ok || isTypeSpecInDependantList(typeName, dependant_types) { + if !ok || isTypeSpecInDependantList(typeName, dependantTypes) { isDependant = true if cfg.IgnoreDependants { //TODO: stdevEclipse Check if type can be replaced by another type or handle @@ -599,7 +605,7 @@ func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependa params = append(params, jen.Id(argName(ident.Name))) } else { typeName, ok := typeSpecStr(&field.Type, fast.Name.Name, false) - if !ok || isTypeSpecInDependantList(typeName, dependant_types) { + if !ok || isTypeSpecInDependantList(typeName, dependantTypes) { isDependant = true if cfg.IgnoreDependants { //TODO: stdevEclipse Check if type can be replaced by another type or handle @@ -1302,3 +1308,16 @@ var basicTypesMap = map[string]string{ } var packageSeparator = "__" + +func getCallbackCode(name string, typeName string) []jen.Code { + + c := jen.Func().Params( + jen.Id("a").Id("A"), + ).Id("foo").Params( + jen.Id("b"), + jen.Id("c").String(), + ).String().Block( + jen.Return(jen.Id("b").Op("+").Id("c")), + ) + return jenCodeToArray(c) +} From 2a171babf970e0330e8216abd51be91a008c89e5 Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Thu, 23 Jan 2020 14:48:02 -0500 Subject: [PATCH 4/7] [cgogen] refs #17 Complete functions callback --- Gopkg.lock | 4 +--- src/cmd/cgogen.go | 40 ++++++++++++++++++++++++++++++---------- vendor/vendor.json | 13 ------------- 3 files changed, 31 insertions(+), 26 deletions(-) mode change 100644 => 100755 src/cmd/cgogen.go delete mode 100644 vendor/vendor.json diff --git a/Gopkg.lock b/Gopkg.lock index f5db56c..2bd4fa4 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -2,16 +2,14 @@ [[projects]] - digest = "1:76e62cadfb857e4f8e4442799ef04dcc6111d187adac9a4a552a34cdf1862550" name = "github.com/dave/jennifer" packages = ["jen"] - pruneopts = "UT" revision = "724ff9887965e893831e9493011297e6bc01ecb4" version = "v1.4.0" [solve-meta] analyzer-name = "dep" analyzer-version = 1 - input-imports = ["github.com/dave/jennifer/jen"] + inputs-digest = "32c43d922fa782ed1e847c7403dd671b96fc14b9d811152e65deef6fa658c1b3" solver-name = "gps-cdcl" solver-version = 1 diff --git a/src/cmd/cgogen.go b/src/cmd/cgogen.go old mode 100644 new mode 100755 index e1b633f..cedf0a0 --- a/src/cmd/cgogen.go +++ b/src/cmd/cgogen.go @@ -1,5 +1,6 @@ package main +import "C" import ( "bytes" "flag" @@ -1309,15 +1310,34 @@ var basicTypesMap = map[string]string{ var packageSeparator = "__" -func getCallbackCode(name string, typeName string) []jen.Code { - - c := jen.Func().Params( - jen.Id("a").Id("A"), - ).Id("foo").Params( - jen.Id("b"), - jen.Id("c").String(), - ).String().Block( - jen.Return(jen.Id("b").Op("+").Id("c")), +func getCallbackCode() []jen.Code { + + varfunction := jen.Func().Params( + jen.Id("pTx").Id("*coin.Transaction"), + ).Uint64().Error().Block( + jen.Var().Id("fee").Id("C.GoUint64_"), + jen.Id("handle").Op(":=").Id("registerTransactionHandle").Call().Params(jen.Id("pTx")), + jen.Id("result").Op(":=").Id("C.callFeeCalculator").Call(jen.Id("pFeeCalc"), jen.Id("handle"), jen.Id("&fee")), + jen.Id("closeHandle").Call(jen.Id("Handle").Call(jen.Id("handle"))), + jen.If(jen.Id("result").Op("==").Id("SKY_OK")).Block( + jen.Return(jen.Id("uint64").Call(jen.Id("fee"))), jen.Nil()), + jen.Else().Block( + jen.Return(jen.Lit(0), jen.Id("errors.New").Call(jen.Id("Error calculating fee"))), + ), ) - return jenCodeToArray(c) + + varname := jen.Id("feeCalc").Op(":=").Add(varfunction) + + //feeCalc := func(pTx *coin.Transaction) (uint64, error) { + // var fee C.GoUint64_ + // handle := registerTransactionHandle(pTx) + // result := C.callFeeCalculator(pFeeCalc, handle, &fee) + // closeHandle(Handle(handle)) + // if result == SKY_OK { + // return uint64(fee), nil + // } else { + // return 0, errors.New("Error calculating fee") + // } + //} + return jenCodeToArray(varname) } diff --git a/vendor/vendor.json b/vendor/vendor.json deleted file mode 100644 index aef8800..0000000 --- a/vendor/vendor.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "comment": "", - "ignore": "test", - "package": [ - { - "checksumSHA1": "GtqEEHjKsMGUSCqjPyKLi+DfcEs=", - "path": "github.com/dave/jennifer/jen", - "revision": "724ff9887965e893831e9493011297e6bc01ecb4", - "revisionTime": "2019-11-26T19:34:09Z" - } - ], - "rootPath": "github.com/fibercrypto/cgogen" -} From 74ea4734e5df618143172d708c11940eb0a5ba0a Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Fri, 24 Jan 2020 22:16:49 -0500 Subject: [PATCH 5/7] [dep] refs #17 Correcting declarations the Gopkg --- Gopkg.lock | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Gopkg.lock b/Gopkg.lock index 2bd4fa4..f5db56c 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -2,14 +2,16 @@ [[projects]] + digest = "1:76e62cadfb857e4f8e4442799ef04dcc6111d187adac9a4a552a34cdf1862550" name = "github.com/dave/jennifer" packages = ["jen"] + pruneopts = "UT" revision = "724ff9887965e893831e9493011297e6bc01ecb4" version = "v1.4.0" [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "32c43d922fa782ed1e847c7403dd671b96fc14b9d811152e65deef6fa658c1b3" + input-imports = ["github.com/dave/jennifer/jen"] solver-name = "gps-cdcl" solver-version = 1 From 15959c284a41a904a499e636ec51866407e0a193 Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Fri, 24 Jan 2020 22:25:26 -0500 Subject: [PATCH 6/7] [cgogen] refs #17 Rename var in getCallbackCode() --- src/cmd/cgogen.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cmd/cgogen.go b/src/cmd/cgogen.go index cedf0a0..814d0eb 100755 --- a/src/cmd/cgogen.go +++ b/src/cmd/cgogen.go @@ -1312,7 +1312,7 @@ var packageSeparator = "__" func getCallbackCode() []jen.Code { - varfunction := jen.Func().Params( + varFunction := jen.Func().Params( jen.Id("pTx").Id("*coin.Transaction"), ).Uint64().Error().Block( jen.Var().Id("fee").Id("C.GoUint64_"), @@ -1326,7 +1326,7 @@ func getCallbackCode() []jen.Code { ), ) - varname := jen.Id("feeCalc").Op(":=").Add(varfunction) + varName := jen.Id("feeCalc").Op(":=").Add(varFunction) //feeCalc := func(pTx *coin.Transaction) (uint64, error) { // var fee C.GoUint64_ @@ -1339,5 +1339,5 @@ func getCallbackCode() []jen.Code { // return 0, errors.New("Error calculating fee") // } //} - return jenCodeToArray(varname) + return jenCodeToArray(varName) } From 12cff6be34e538331149f8900c67045fecb860fb Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Fri, 24 Jan 2020 23:23:06 -0500 Subject: [PATCH 7/7] [cgogen] refs #17 Call functions in locale that case --- src/cmd/cgogen.go | 54 +++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/src/cmd/cgogen.go b/src/cmd/cgogen.go index 814d0eb..0b52bae 100755 --- a/src/cmd/cgogen.go +++ b/src/cmd/cgogen.go @@ -1,6 +1,5 @@ package main -import "C" import ( "bytes" "flag" @@ -63,11 +62,6 @@ var ( //Map of types that will replaced by custom types var customTypesMap = make(map[string]string) -//Map of callback -var callbackMap = map[string]string{ - "coin": "FeeCalculator", -} - //Types that will use functions of type inplace to convert var inplaceConvertTypesPackages = map[string]string{ "PubKeySlice": "cipher", @@ -96,7 +90,7 @@ var returnErrName = "____return_err" func main() { handleTypes = make(map[string]string) - callbackMap = make(map[string]string) + cfg.register() flag.Parse() @@ -158,7 +152,7 @@ func doGoFile() { #include "skyfee.h"`) } - typeDefs := make([](*ast.GenDecl), 0) + typeDefs := make([]*ast.GenDecl, 0) for _, _decl := range fast.Decls { @@ -795,8 +789,12 @@ func getCodeToConvertInParameter(_typeExpr *ast.Expr, packName string, name stri if !isPointer { leftPart = leftPart.Op("*") } - leftPart = leftPart.Parens(jen.Op("*").Id(packName).Id(".").Id(typeName)). - Parens(jen.Qual("unsafe", "Pointer").Parens(jen.Id(argName(name)))) + if typeName == "FeeCalculator" { + leftPart = jen.Id(name).Op(":=").Add(getCallbackCode(name)) + } else { + leftPart = leftPart.Parens(jen.Op("*").Id(packName).Id(".").Id(typeName)). + Parens(jen.Qual("unsafe", "Pointer").Parens(jen.Id(argName(name)))) + } return jenCodeToArray(leftPart) } } @@ -920,6 +918,7 @@ func isInplaceConvertType(typeName string) bool { } /* Process a type expression. Returns the code in C for the type and ok if successful */ +// typedata func processTypeExpression(fast *ast.File, type_expr ast.Expr, packageName string, name string, definedTypes *[]string, @@ -1169,7 +1168,7 @@ func processTypeDef(fast *ast.File, tdecl *ast.GenDecl, } /* Process all type definitions. Returns c code for all the defintions */ -func processTypeDefs(fast *ast.File, typeDecls []*ast.GenDecl, dependant_types *[]string) string { +func processTypeDefs(fast *ast.File, typeDecls []*ast.GenDecl, dependantTypes *[]string) string { resultCode := "" var definedTypes []string for key := range GetBasicTypes() { @@ -1185,7 +1184,7 @@ func processTypeDefs(fast *ast.File, typeDecls []*ast.GenDecl, dependant_types * wentBlank = true for index, typeDecl := range typeDecls { if typeDecl != nil { - typeCode, ok, isDependant := processTypeDef(fast, typeDecl, &definedTypes, nil, dependant_types) + typeCode, ok, isDependant := processTypeDef(fast, typeDecl, &definedTypes, nil, dependantTypes) if ok { wentBlank = false typeDecls[index] = nil @@ -1203,7 +1202,7 @@ func processTypeDefs(fast *ast.File, typeDecls []*ast.GenDecl, dependant_types * if unprocessed > 0 { for _, typeDecl := range typeDecls { if typeDecl != nil { - typeCode, ok, isDependant := processTypeDef(fast, typeDecl, &definedTypes, &forwardsDeclarations, dependant_types) + typeCode, ok, isDependant := processTypeDef(fast, typeDecl, &definedTypes, &forwardsDeclarations, dependantTypes) if ok { if !(cfg.IgnoreDependants && isDependant) { resultCode += typeCode @@ -1310,34 +1309,19 @@ var basicTypesMap = map[string]string{ var packageSeparator = "__" -func getCallbackCode() []jen.Code { +func getCallbackCode(varName string) *jen.Statement { varFunction := jen.Func().Params( jen.Id("pTx").Id("*coin.Transaction"), - ).Uint64().Error().Block( + ).Parens(jen.Uint64().Op(",").Error()).Block( jen.Var().Id("fee").Id("C.GoUint64_"), - jen.Id("handle").Op(":=").Id("registerTransactionHandle").Call().Params(jen.Id("pTx")), - jen.Id("result").Op(":=").Id("C.callFeeCalculator").Call(jen.Id("pFeeCalc"), jen.Id("handle"), jen.Id("&fee")), + jen.Id("handle").Op(":=").Id("registerTransactionHandle").Call(jen.Id("pTx")), + jen.Id("result").Op(":=").Id("C.callFeeCalculator").Call(jen.Id("_"+varName), jen.Id("handle"), jen.Id("&fee")), jen.Id("closeHandle").Call(jen.Id("Handle").Call(jen.Id("handle"))), jen.If(jen.Id("result").Op("==").Id("SKY_OK")).Block( - jen.Return(jen.Id("uint64").Call(jen.Id("fee"))), jen.Nil()), - jen.Else().Block( - jen.Return(jen.Lit(0), jen.Id("errors.New").Call(jen.Id("Error calculating fee"))), - ), + jen.Return(jen.Id("uint64").Call(jen.Id("fee")), jen.Nil())), + jen.Return(jen.Lit(0), jen.Qual("errors", "New").Call(jen.Lit("Error calculating fee"))), ) - varName := jen.Id("feeCalc").Op(":=").Add(varFunction) - - //feeCalc := func(pTx *coin.Transaction) (uint64, error) { - // var fee C.GoUint64_ - // handle := registerTransactionHandle(pTx) - // result := C.callFeeCalculator(pFeeCalc, handle, &fee) - // closeHandle(Handle(handle)) - // if result == SKY_OK { - // return uint64(fee), nil - // } else { - // return 0, errors.New("Error calculating fee") - // } - //} - return jenCodeToArray(varName) + return varFunction }