tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

cgo only supports integer-sized parameters

Open Leopotam opened this issue 1 year ago • 6 comments

// native.h
typedef struct Vector2
{
    float x;
    float y;
} Vector2;

Vector2 Vector2Add(Vector2 v1, Vector2 v2)
{
    Vector2 result = {v1.x + v2.x, v1.y + v2.y};
    return result;
}
// main.go
package main

// #include "native.h"
import "C"
import "fmt"

func main() {
	a := C.Vector2{1, 2}
	b := C.Vector2{1, 2}
	c := C.Vector2Add(a, b)
	fmt.Println(c)
}
> go run main.go
{2 4}
> tinygo run main.go
{3 2}
> tinygo version
tinygo version 0.33.0 darwin/amd64 (using go version go1.23.2 and LLVM version 18.1.2)

Leopotam avatar Oct 02 '24 15:10 Leopotam

Windows:

> tinygo.exe run main.go
{0 0}
> go run main.go
{2 4}

supremestranger avatar Oct 02 '24 20:10 supremestranger

Ugh, this has got to be calling convention differences for non-integer-sized objects.

dgryski avatar Oct 03 '24 18:10 dgryski

Oh, cgo in tinygo supports only integer-sized objects - its not issue, nevermind then.

Leopotam avatar Oct 03 '24 20:10 Leopotam

Reopening because it's still a cgo bug/limitation.

dgryski avatar Oct 03 '24 20:10 dgryski

@dgryski any workaround for this issue? Can I compile c-code as part of go-project to wasm-browser target with tinygo? Standard go-compiler doesnt support it.

Leopotam avatar Oct 19 '24 06:10 Leopotam

Matching the C ABI is a difficult problem, since this is basically an implementation detail between Clang and LLVM. There have been various proposals to expose this as a library, the most recent being https://discourse.llvm.org/t/rfc-an-abi-lowering-library-for-llvm/84495 (and https://github.com/llvm/llvm-project/pull/158329).

I hope one of these proposals will eventually be usable so we can finally fix this.

aykevl avatar Oct 01 '25 10:10 aykevl