-
Notifications
You must be signed in to change notification settings - Fork 75
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
proposal: Remove 'C' types from the exposed APIs #113
Comments
In general, removing C types from the
A counter-proposal to consider is to replace such C types with I think we should evaluate both and make an informed decision of which one to pick. Whether a package should expose C
/cc @slimsag @dominikh for their Cgo/ |
Thank you! From those comments, my understanding is that Then +1 to |
I'm not sure if that's quite right. My understanding is that if a package returns package main
import "your/pkg"
// note that "unsafe" isn't imported here
func main() {
foo := pkg.DoSomething()
// foo is unsafe.Pointer... yet this package didn't import "unsafe"
// so it doesn't appear to be using unsafe
} So, it's the fact that importing What I don't know is how bad that is compared to the other alternatives we might have at our disposal. At the end of the day, Cgo code isn't going to be as great as one might want, so we have to find and use the least bad solution. |
The reason Go packages in general should not return an
Now, the above two points are true for Go packages in general, but with OpenGL wrappers the fact of the matter is that they are often not safe APIs in general. It is very easy to pass incorrect arguments to OpenGL APIs and get e.g. an invalid pointer, stack overflow, etc., etc. Even browsers have a very hard time sanitizing OpenGL code for the purpose of WebGL, and have to e.g. create lists of driver implementations that are bad. So the benefits of the two above points are lesser for OpenGL Go wrappers. I think One last note: |
Thanks!
What do you think about the current a lot of
+1. |
One issue I'm currently facing with pointers to C types returned as unsafe.Pointer(window.GetCocoaWindow()) // need to convert to unsafe.Pointer to pass back to Cgo in my code With:
@slimsag Any suggestions on how to work around that other than to use |
Apologies for the trouble / misleading statements here. The reason https://godoc.org/github.com/golang/go/src/cmd/vet#hdr-Misuse_of_unsafe_Pointers If I understand correctly, it is because that uintptr memory could be allocated by Go, and Fundamentally, I think the problem here is: Canonical Go packages should not expose unsafe.Pointer, they should expose a safe Go API. But what we're doing here is actually rather low level (these GL packages are intended to be used by a package that does expose a safe Go API), and hence we're not subject to that same general contract. And I believe we've came to the same conclusion in past discussions here as well. I take back what I said about uintptr being the right thing here. I think |
Looks like GitHub is having issues and didn't submit my last comment.... so I unfortunately lost it. TL;DR was that we should actually use unsafe.Pointer not uintptr here. go-gl is low-level and not really subject to the same restrictions of more canonical Go packages where e.g. only exposing a safe Go API is the target goal |
Sounds good; I'm in agreement. Following the same logic, I think we should change (or consider changing; perhaps for 3.3) |
(Looks like GitHub shows today's comments now :-) TBH I didn't have a strong opinion on |
This PR replaces exported C types with unsafe.Pointer, so that gl will not expose C types. This PR also adds comments describing the original C types. Example output: // Parameter context has type *C.struct__cl_context. // Parameter event has type *C.struct__cl_event. func CreateSyncFromCLeventARB(context unsafe.Pointer, event unsafe.Pointer, flags uint32) uintptr { ... } Updates go-gl/gl#113.
This change applies the glow change go-gl/glow#101. Done with: go generate -tags=gen github.com/go-gl/gl Fixes #113.
Now gl packages has APIs that expose C types:
The problem is that this violates the guideline of Cgo: https://golang.org/cmd/cgo/ says that "a Go package should not expose C types in its exported API". Furthermore, #109 (remove Cgo dependencies on Windows) is now impossible due to these APIs.
I propose to replace such C types with
unsafe.Pointer
:C.GLeglClientBufferEXT
is equivalent tovoid*
: https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_external_buffer.txt*C.struct__cl_context
is a pointer*C.struct__cl_event
is a pointerC.GLDEBUGPROCAMD
is a function pointer: https://www.khronos.org/registry/OpenGL/extensions/AMD/AMD_debug_output.txtC.GLeglImageOES
is equiavalent tovoid*
: https://www.khronos.org/registry/OpenGL/extensions/OES/OES_EGL_image_external.txtC.GLVULKANPROCNV
is a function pointer: (could not find a source but some implementation says it is a pointer).This proposal breaks the backward compability since this changes the exposed APIs, but I think the risk is low. GitHub code search says there is no actual usage of these functions:
It's possible to do further investigation with sourcegraph.com, but this is harder since this needs to specified GL versions.
What do you think? Thanks!
CC @dmitshur
The text was updated successfully, but these errors were encountered: