Skip to content

Commit

Permalink
Cgo add cbytes implementation (rebased version of #3318) (#4470)
Browse files Browse the repository at this point in the history
cgo: added CBytes implementation
  • Loading branch information
leongross authored Sep 17, 2024
1 parent d4729f9 commit a9bf981
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cgo/cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ func __GoBytes(unsafe.Pointer, uintptr) []byte
func GoBytes(ptr unsafe.Pointer, length C.int) []byte {
return C.__GoBytes(ptr, uintptr(length))
}
//go:linkname C.__CBytes runtime.cgo_CBytes
func __CBytes([]byte) unsafe.Pointer
func CBytes(b []byte) unsafe.Pointer {
return C.__CBytes(b)
}
`

// Process extracts `import "C"` statements from the AST, parses the comment
Expand Down Expand Up @@ -218,7 +225,7 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
switch decl := decl.(type) {
case *ast.FuncDecl:
switch decl.Name.Name {
case "CString", "GoString", "GoStringN", "__GoStringN", "GoBytes", "__GoBytes":
case "CString", "GoString", "GoStringN", "__GoStringN", "GoBytes", "__GoBytes", "CBytes", "__CBytes":
// Adjust the name to have a "C." prefix so it is correctly
// resolved.
decl.Name.Name = "C." + decl.Name.Name
Expand Down
7 changes: 7 additions & 0 deletions cgo/testdata/basic.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
return C.__GoBytes(ptr, uintptr(length))
}

//go:linkname C.__CBytes runtime.cgo_CBytes
func C.__CBytes([]byte) unsafe.Pointer

func C.CBytes(b []byte) unsafe.Pointer {
return C.__CBytes(b)
}

type (
C.char uint8
C.schar int8
Expand Down
7 changes: 7 additions & 0 deletions cgo/testdata/const.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
return C.__GoBytes(ptr, uintptr(length))
}

//go:linkname C.__CBytes runtime.cgo_CBytes
func C.__CBytes([]byte) unsafe.Pointer

func C.CBytes(b []byte) unsafe.Pointer {
return C.__CBytes(b)
}

type (
C.char uint8
C.schar int8
Expand Down
7 changes: 7 additions & 0 deletions cgo/testdata/errors.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
return C.__GoBytes(ptr, uintptr(length))
}

//go:linkname C.__CBytes runtime.cgo_CBytes
func C.__CBytes([]byte) unsafe.Pointer

func C.CBytes(b []byte) unsafe.Pointer {
return C.__CBytes(b)
}

type (
C.char uint8
C.schar int8
Expand Down
7 changes: 7 additions & 0 deletions cgo/testdata/flags.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
return C.__GoBytes(ptr, uintptr(length))
}

//go:linkname C.__CBytes runtime.cgo_CBytes
func C.__CBytes([]byte) unsafe.Pointer

func C.CBytes(b []byte) unsafe.Pointer {
return C.__CBytes(b)
}

type (
C.char uint8
C.schar int8
Expand Down
7 changes: 7 additions & 0 deletions cgo/testdata/symbols.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
return C.__GoBytes(ptr, uintptr(length))
}

//go:linkname C.__CBytes runtime.cgo_CBytes
func C.__CBytes([]byte) unsafe.Pointer

func C.CBytes(b []byte) unsafe.Pointer {
return C.__CBytes(b)
}

type (
C.char uint8
C.schar int8
Expand Down
7 changes: 7 additions & 0 deletions cgo/testdata/types.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
return C.__GoBytes(ptr, uintptr(length))
}

//go:linkname C.__CBytes runtime.cgo_CBytes
func C.__CBytes([]byte) unsafe.Pointer

func C.CBytes(b []byte) unsafe.Pointer {
return C.__CBytes(b)
}

type (
C.char uint8
C.schar int8
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,10 @@ func cgo_GoBytes(ptr unsafe.Pointer, length uintptr) []byte {
}
return buf
}

func cgo_CBytes(b []byte) unsafe.Pointer {
p := malloc(uintptr(len(b)))
s := unsafe.Slice((*byte)(p), len(b))
copy(s, b)
return p
}
2 changes: 2 additions & 0 deletions testdata/cgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func main() {
println("C.GoString(nil):", C.GoString(nil))
println("len(C.GoStringN(nil, 0)):", len(C.GoStringN(nil, 0)))
println("len(C.GoBytes(nil, 0)):", len(C.GoBytes(nil, 0)))
println("len(C.GoBytes(C.CBytes(nil),0)):", len(C.GoBytes(C.CBytes(nil), 0)))
println(`rountrip CBytes:`, C.GoString((*C.char)(C.CBytes([]byte("hello\000")))))

// libc: test whether C functions work at all.
buf1 := []byte("foobar\x00")
Expand Down
2 changes: 2 additions & 0 deletions testdata/cgo/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ C.CStringN: 4 2 0 4 8
C.GoString(nil):
len(C.GoStringN(nil, 0)): 0
len(C.GoBytes(nil, 0)): 0
len(C.GoBytes(C.CBytes(nil),0)): 0
rountrip CBytes: hello
copied string: foobar
CGo sqrt(3): +1.732051e+000
C sqrt(3): +1.732051e+000
Expand Down

0 comments on commit a9bf981

Please sign in to comment.