Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #17 Support callback #18

Closed
28 changes: 25 additions & 3 deletions src/cmd/cgogen.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,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
Expand Down Expand Up @@ -828,8 +828,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" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cgogen code has to be generic , it must not depend upon specific names of a particular library used as input

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in my opinion , maybe this check should be about whether type was defined as ast.FuncType .

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem of callbacks that are very specific. And the functions are different, it can't be treated that way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it definitely can't be treated as hard-coded type names belonging in a separate library package .

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly then what do you suggest

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)
}
}
Expand Down Expand Up @@ -954,6 +958,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,
Expand Down Expand Up @@ -1368,6 +1373,23 @@ var basicTypesMap = map[string]string{

var packageSeparator = "__"

func getCallbackCode(varName string) *jen.Statement {

varFunction := jen.Func().Params(
jen.Id("pTx").Id("*coin.Transaction"),
).Parens(jen.Uint64().Op(",").Error()).Block(
jen.Var().Id("fee").Id("C.GoUint64_"),
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.Return(jen.Lit(0), jen.Qual("errors", "New").Call(jen.Lit("Error calculating fee"))),
)

return varFunction
}

func getPathPackage(path string) (packagePath_ string, mainPackagePath_ string) {

index := strings.LastIndex(path, "/")
Expand Down
13 changes: 0 additions & 13 deletions vendor/vendor.json

This file was deleted.