aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorleongross <[email protected]>2024-09-17 07:12:57 -0700
committerGitHub <[email protected]>2024-09-17 16:12:57 +0200
commita9bf981d9219ad894c39d5dddce517708f7d0be5 (patch)
tree04982af928a5dd4d080416cd5581601291ee23c6
parentd4729f92bde874dac83c07d96538dba90fd716d3 (diff)
downloadtinygo-a9bf981d9219ad894c39d5dddce517708f7d0be5.tar.gz
tinygo-a9bf981d9219ad894c39d5dddce517708f7d0be5.zip
Cgo add cbytes implementation (rebased version of #3318) (#4470)
cgo: added CBytes implementation
-rw-r--r--cgo/cgo.go9
-rw-r--r--cgo/testdata/basic.out.go7
-rw-r--r--cgo/testdata/const.out.go7
-rw-r--r--cgo/testdata/errors.out.go7
-rw-r--r--cgo/testdata/flags.out.go7
-rw-r--r--cgo/testdata/symbols.out.go7
-rw-r--r--cgo/testdata/types.out.go7
-rw-r--r--src/runtime/string.go7
-rw-r--r--testdata/cgo/main.go2
-rw-r--r--testdata/cgo/out.txt2
10 files changed, 61 insertions, 1 deletions
diff --git a/cgo/cgo.go b/cgo/cgo.go
index d5f122120..4a5d7efed 100644
--- a/cgo/cgo.go
+++ b/cgo/cgo.go
@@ -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
@@ -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
diff --git a/cgo/testdata/basic.out.go b/cgo/testdata/basic.out.go
index f2daa78da..6c2623980 100644
--- a/cgo/testdata/basic.out.go
+++ b/cgo/testdata/basic.out.go
@@ -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
diff --git a/cgo/testdata/const.out.go b/cgo/testdata/const.out.go
index 4a5f5fd5b..2b48163b4 100644
--- a/cgo/testdata/const.out.go
+++ b/cgo/testdata/const.out.go
@@ -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
diff --git a/cgo/testdata/errors.out.go b/cgo/testdata/errors.out.go
index d0e04320a..43a6a65c9 100644
--- a/cgo/testdata/errors.out.go
+++ b/cgo/testdata/errors.out.go
@@ -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
diff --git a/cgo/testdata/flags.out.go b/cgo/testdata/flags.out.go
index 72520ab6d..83ca60420 100644
--- a/cgo/testdata/flags.out.go
+++ b/cgo/testdata/flags.out.go
@@ -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
diff --git a/cgo/testdata/symbols.out.go b/cgo/testdata/symbols.out.go
index f2826ae2e..569cb65f6 100644
--- a/cgo/testdata/symbols.out.go
+++ b/cgo/testdata/symbols.out.go
@@ -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
diff --git a/cgo/testdata/types.out.go b/cgo/testdata/types.out.go
index a35d3733c..e5382ec80 100644
--- a/cgo/testdata/types.out.go
+++ b/cgo/testdata/types.out.go
@@ -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
diff --git a/src/runtime/string.go b/src/runtime/string.go
index 13bfcd0ed..aeefe1d4f 100644
--- a/src/runtime/string.go
+++ b/src/runtime/string.go
@@ -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
+}
diff --git a/testdata/cgo/main.go b/testdata/cgo/main.go
index ddd1992e2..00e0ba01d 100644
--- a/testdata/cgo/main.go
+++ b/testdata/cgo/main.go
@@ -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")
diff --git a/testdata/cgo/out.txt b/testdata/cgo/out.txt
index 4ea45d864..3088d4cb4 100644
--- a/testdata/cgo/out.txt
+++ b/testdata/cgo/out.txt
@@ -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