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

Replace exported C types with unsafe.Pointer #101

Merged
merged 9 commits into from Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions package.go
Expand Up @@ -31,6 +31,28 @@ type PackageFunction struct {
Doc string
}

// Comment returns the comment explaining the function.
func (f *PackageFunction) Comment() string {
var lines []string
if f.Doc != "" {
lines = append(lines, "// " + f.Doc)
}

// Adds explanations about C types that are unsafe.Pointer in Go world.
// See also https://github.com/go-gl/gl/issues/113.
for _, p := range f.Function.Parameters {
if t := p.Type; t.GoType() == "unsafe.Pointer" && t.Name != "void" && t.Name != "GLvoid" {
lines = append(lines, fmt.Sprintf("// Parameter %s has type %s.", p.Name, t.GoCType()))
}
}

if r := f.Function.Return; r.GoType() == "unsafe.Pointer" && r.Name != "void" && r.Name != "GLvoid" {
lines = append(lines, fmt.Sprintf("// Return value has type %s.", r.GoCType()))
}

return strings.Join(lines, "\n")
}

// UniqueName returns a globally unique Go-compatible name for this package.
func (pkg *Package) UniqueName() string {
version := strings.Replace(pkg.Version.String(), ".", "", -1)
Expand Down
2 changes: 1 addition & 1 deletion tmpl/package.tmpl
Expand Up @@ -94,7 +94,7 @@ func boolToInt(b bool) int {

{{define "bridgeCall"}}C.glow{{.GoName}}(gp{{.GoName}}{{if ge (len .Parameters) 1}}, {{end}}{{template "paramsGoCall" .Parameters}}){{end}}
{{range .Functions}}
{{with .Doc}}// {{.}}{{end}}
{{.Comment}}
func {{.GoName}}({{template "paramsGoDecl" .Parameters}}){{if not .Return.IsVoid}} {{.Return.GoType}}{{end}} {
{{range .Parameters}}
{{if .Type.IsDebugProc}}userDebugCallback = {{.GoName}}{{end}}
Expand Down
4 changes: 2 additions & 2 deletions type.go
Expand Up @@ -113,7 +113,7 @@ func (t Type) GoType() string {
// Special case mapping to the type defined in debug.tmpl
return "DebugProc"
}
return t.GoCType()
return "unsafe.Pointer"
}

// ConvertGoToC returns an expression that converts a variable from the Go type to the C type.
Expand All @@ -128,7 +128,7 @@ func (t Type) ConvertGoToC(name string) string {
case "GLDEBUGPROC", "GLDEBUGPROCARB", "GLDEBUGPROCKHR":
return fmt.Sprintf("(C.%s)(unsafe.Pointer(&%s))", t.Name, name)
}
if t.PointerLevel >= 1 {
if t.PointerLevel >= 1 && t.GoType() != "unsafe.Pointer" {
return fmt.Sprintf("(%s)(unsafe.Pointer(%s))", t.GoCType(), name)
}
return fmt.Sprintf("(%s)(%s)", t.GoCType(), name)
Expand Down