mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-17 19:23:25 +00:00
cgo: added CBytes implementation
This commit is contained in:
+8
-1
@@ -162,6 +162,13 @@ func __GoBytes(unsafe.Pointer, uintptr) []byte
|
|||||||
func GoBytes(ptr unsafe.Pointer, length C.int) []byte {
|
func GoBytes(ptr unsafe.Pointer, length C.int) []byte {
|
||||||
return C.__GoBytes(ptr, uintptr(length))
|
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
|
// 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) {
|
switch decl := decl.(type) {
|
||||||
case *ast.FuncDecl:
|
case *ast.FuncDecl:
|
||||||
switch decl.Name.Name {
|
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
|
// Adjust the name to have a "C." prefix so it is correctly
|
||||||
// resolved.
|
// resolved.
|
||||||
decl.Name.Name = "C." + decl.Name.Name
|
decl.Name.Name = "C." + decl.Name.Name
|
||||||
|
|||||||
Vendored
+7
@@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
|
|||||||
return C.__GoBytes(ptr, uintptr(length))
|
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 (
|
type (
|
||||||
C.char uint8
|
C.char uint8
|
||||||
C.schar int8
|
C.schar int8
|
||||||
|
|||||||
Vendored
+7
@@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
|
|||||||
return C.__GoBytes(ptr, uintptr(length))
|
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 (
|
type (
|
||||||
C.char uint8
|
C.char uint8
|
||||||
C.schar int8
|
C.schar int8
|
||||||
|
|||||||
Vendored
+7
@@ -44,6 +44,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
|
|||||||
return C.__GoBytes(ptr, uintptr(length))
|
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 (
|
type (
|
||||||
C.char uint8
|
C.char uint8
|
||||||
C.schar int8
|
C.schar int8
|
||||||
|
|||||||
Vendored
+7
@@ -29,6 +29,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
|
|||||||
return C.__GoBytes(ptr, uintptr(length))
|
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 (
|
type (
|
||||||
C.char uint8
|
C.char uint8
|
||||||
C.schar int8
|
C.schar int8
|
||||||
|
|||||||
Vendored
+7
@@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
|
|||||||
return C.__GoBytes(ptr, uintptr(length))
|
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 (
|
type (
|
||||||
C.char uint8
|
C.char uint8
|
||||||
C.schar int8
|
C.schar int8
|
||||||
|
|||||||
Vendored
+7
@@ -24,6 +24,13 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
|
|||||||
return C.__GoBytes(ptr, uintptr(length))
|
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 (
|
type (
|
||||||
C.char uint8
|
C.char uint8
|
||||||
C.schar int8
|
C.schar int8
|
||||||
|
|||||||
@@ -283,3 +283,10 @@ func cgo_GoBytes(ptr unsafe.Pointer, length uintptr) []byte {
|
|||||||
}
|
}
|
||||||
return buf
|
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
|
||||||
|
}
|
||||||
|
|||||||
Vendored
+2
@@ -165,6 +165,8 @@ func main() {
|
|||||||
println("C.GoString(nil):", C.GoString(nil))
|
println("C.GoString(nil):", C.GoString(nil))
|
||||||
println("len(C.GoStringN(nil, 0)):", len(C.GoStringN(nil, 0)))
|
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(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.
|
// libc: test whether C functions work at all.
|
||||||
buf1 := []byte("foobar\x00")
|
buf1 := []byte("foobar\x00")
|
||||||
|
|||||||
Vendored
+2
@@ -73,6 +73,8 @@ C.CStringN: 4 2 0 4 8
|
|||||||
C.GoString(nil):
|
C.GoString(nil):
|
||||||
len(C.GoStringN(nil, 0)): 0
|
len(C.GoStringN(nil, 0)): 0
|
||||||
len(C.GoBytes(nil, 0)): 0
|
len(C.GoBytes(nil, 0)): 0
|
||||||
|
len(C.GoBytes(C.CBytes(nil),0)): 0
|
||||||
|
rountrip CBytes: hello
|
||||||
copied string: foobar
|
copied string: foobar
|
||||||
CGo sqrt(3): +1.732051e+000
|
CGo sqrt(3): +1.732051e+000
|
||||||
C sqrt(3): +1.732051e+000
|
C sqrt(3): +1.732051e+000
|
||||||
|
|||||||
Reference in New Issue
Block a user