cgo: refactor Go types a little bit

Code like this is not allowed by the upstream Go CGo implementation, but
was allowed by TinyGo:

    var _ int8 = C.int8_t(5)

The reason it shouldn't be allowed is a little bit complicated. While
it is true that C.int8_t is always the same underlying data type as Go
int8 (signed 8-bit integer), the C type is actually a typedef of one of
the base C types (usually unsigned char or signed char) which in turn do
_not_ map cleanly to Go types: the 'char' type is ambiguous (it may be
either signed or unsigned depending on the ABI) and types like 'int'
vary in size by ABI as well.

To make code more portable, I think it's better to match the upstream
implementation.
This commit is contained in:
Ayke van Laethem
2023-09-22 15:51:44 +02:00
parent ed2a98c7d0
commit 3ecefd6a0a
4 changed files with 20 additions and 23 deletions
-4
View File
@@ -52,10 +52,6 @@ func main() {
println("static headerfunc:", C.headerfunc_static(5))
headerfunc_2()
// equivalent types
var goInt8 int8 = 5
var _ C.int8_t = goInt8
// more globals
println("bool:", C.globalBool, C.globalBool2 == true)
println("float:", C.globalFloat)