Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #56 from vmarkovtsev/master
Browse files Browse the repository at this point in the history
Wrap more objects with CGo
  • Loading branch information
mcuadros authored Jun 27, 2016
2 parents e305c47 + da07dca commit 808076a
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 9 deletions.
8 changes: 4 additions & 4 deletions cshared/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ and it is the responsibility of the other side to `free()` it or it will leak
otherwise.

Another tricky part is in `c_std_map_get_str_str` and similar places
where you need to return `*C.char` from an unaddressed array accessed under
where you need to return `*C.char` from an unaddressable array accessed under
a pseudonym type through reflection. The only way I've found working
is using `reflect.Copy` to byte slice (copy) and then conversion to
`string` (copy), then `C.CString` (copy) and finally another (copy) on the
receiving side because the latter must be `free()`-d. Extremely efficient.
is using `reflect.Copy` to byte slice (copy), then `CBytes` (copy) and
finally another (copy) on the receiving side because the latter must be
`free()`-d.
51 changes: 51 additions & 0 deletions cshared/blame_cshared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// +build ignore
package main

import (
"C"

"gopkg.in/src-d/go-git.v3"
)

//export c_Blame_get_Path
func c_Blame_get_Path(b uint64) *C.char {
obj, ok := GetObject(Handle(b))
if !ok {
return nil
}
blame := obj.(*git.Blame)
return C.CString(blame.Path)
}

//export c_Blame_get_Rev
func c_Blame_get_Rev(b uint64) *C.char {
obj, ok := GetObject(Handle(b))
if !ok {
return nil
}
blame := obj.(*git.Blame)
return CBytes(blame.Rev[:])
}


//export c_Blame_get_Lines_len
func c_Blame_get_Lines_len(b uint64) int {
obj, ok := GetObject(Handle(b))
if !ok {
return 0
}
blame := obj.(*git.Blame)
return len(blame.Lines)
}

//export c_Blame_get_Lines_item
func c_Blame_get_Lines_item(b uint64, i int) {
obj, ok := GetObject(Handle(b))
if !ok {
return
}
blame := obj.(*git.Blame)
line := blame.Lines[i]
_ = line
}

41 changes: 40 additions & 1 deletion cshared/commit_cshared.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package main
import (
"C"
"io"
"reflect"
"unsafe"

"gopkg.in/src-d/go-git.v3"
"gopkg.in/src-d/go-git.v3/core"
Expand All @@ -16,7 +18,7 @@ func c_Commit_get_Hash(c uint64) *C.char {
return nil
}
commit := obj.(*git.Commit)
return C.CString(string(commit.Hash[:]))
return CBytes(commit.Hash[:])
}

//export c_Commit_get_Author
Expand Down Expand Up @@ -142,6 +144,43 @@ func c_Commit_String(c uint64) *C.char {
return C.CString(commit.String())
}

//export c_Commit_References
func c_Commit_References(c uint64, path string) (*C.char, int, int, *C.char) {
obj, ok := GetObject(Handle(c))
if !ok {
return nil, 0, ErrorCodeNotFound, C.CString(MessageNotFound)
}
commit := obj.(*git.Commit)
refs, err := commit.References(CopyString(path))
if err != nil {
return nil, 0, ErrorCodeInternal, C.CString(err.Error())
}
handles := make([]uint64, len(refs))
for i, c := range(refs) {
handles[i] = uint64(RegisterObject(c))
}
size := 8 * len(handles)
dest := C.malloc(C.size_t(size))
header := (*reflect.SliceHeader)(unsafe.Pointer(&handles))
header.Len *= 8
copy((*[1<<30]byte)(dest)[:], *(*[]byte)(unsafe.Pointer(header)))
return (*C.char)(dest), size / 8, ErrorCodeSuccess, nil
}

//export c_Commit_Blame
func c_Commit_Blame(c uint64, path string) (uint64, int, *C.char) {
obj, ok := GetObject(Handle(c))
if !ok {
return IH, ErrorCodeNotFound, C.CString(MessageNotFound)
}
commit := obj.(*git.Commit)
blame, err := commit.Blame(CopyString(path))
if err != nil {
return IH, ErrorCodeInternal, C.CString(err.Error())
}
return uint64(RegisterObject(blame)), ErrorCodeSuccess, nil
}

//export c_NewCommitIter
func c_NewCommitIter(r uint64, iter uint64) uint64 {
obj, ok := GetObject(Handle(r))
Expand Down
67 changes: 67 additions & 0 deletions cshared/file_cshared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"C"

"gopkg.in/src-d/go-git.v3"
)

//export c_File_get_Name
func c_File_get_Name(f uint64) *C.char {
obj, ok := GetObject(Handle(f))
if !ok {
return nil
}
file := obj.(*git.File)
return C.CString(file.Name)
}

//export c_File_get_Mode
func c_File_get_Mode(f uint64) uint32 {
obj, ok := GetObject(Handle(f))
if !ok {
return 0
}
file := obj.(*git.File)
return uint32(file.Mode)
}

//export c_NewFileIter
func c_NewFileIter(r uint64, t uint64) uint64 {
obj, ok := GetObject(Handle(r))
if !ok {
return IH
}
repo := obj.(*git.Repository)
obj, ok = GetObject(Handle(t))
if !ok {
return IH
}
tree := obj.(*git.Tree)
iter := git.NewFileIter(repo, tree)
return uint64(RegisterObject(iter))
}

//export c_FileIter_Next
func c_FileIter_Next(i uint64) (uint64, int, *C.char) {
obj, ok := GetObject(Handle(i))
if !ok {
return IH, ErrorCodeNotFound, C.CString(MessageNotFound)
}
iter := obj.(*git.FileIter)
file, err := iter.Next()
if err != nil {
return IH, ErrorCodeInternal, C.CString(err.Error())
}
return uint64(RegisterObject(file)), ErrorCodeSuccess, nil
}

//export c_FileIter_Close
func c_FileIter_Close(i uint64) {
obj, ok := GetObject(Handle(i))
if !ok {
return
}
iter := obj.(*git.FileIter)
iter.Close()
}
7 changes: 7 additions & 0 deletions cshared/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ func CopyString(str string) string {
return string(buf)
}

// https://github.com/golang/go/issues/14838
func CBytes(bytes []byte) *C.char {
ptr := C.malloc(C.size_t(len(bytes)))
copy((*[1<<30]byte)(ptr)[:], bytes)
return (*C.char)(ptr)
}

func SafeIsNil(v reflect.Value) bool {
defer func() { recover() }()
return v.IsNil()
Expand Down
2 changes: 1 addition & 1 deletion cshared/objects_cshared.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func c_Blob_get_Hash(b uint64) *C.char {
return nil
}
blob := obj.(*git.Blob)
return C.CString(string(blob.Hash[:]))
return CBytes(blob.Hash[:])
}

//export c_Blob_Size
Expand Down
4 changes: 2 additions & 2 deletions cshared/remote_cshared.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func c_Remote_Head(r uint64) (*C.char, int, *C.char) {
if err != nil {
return nil, ErrorCodeInternal, C.CString(err.Error())
}
return C.CString(string(hash[:])), ErrorCodeSuccess, nil
return CBytes(hash[:]), ErrorCodeSuccess, nil
}

//export c_Remote_Fetch
Expand Down Expand Up @@ -177,7 +177,7 @@ func c_Remote_Ref(r uint64, refName string) (*C.char, int, *C.char) {
if err != nil {
return nil, ErrorCodeInternal, C.CString(err.Error())
}
return C.CString(string(hash[:])), ErrorCodeSuccess, nil
return CBytes(hash[:]), ErrorCodeSuccess, nil
}

//export c_Remote_Refs
Expand Down
2 changes: 1 addition & 1 deletion cshared/std_cshared.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func c_std_map_get_str_str(m uint64, key string) *C.char {
val.Type().Elem().Kind() == reflect.Uint8 {
arr := make([]byte, val.Len(), val.Len())
reflect.Copy(reflect.ValueOf(arr), val)
return C.CString(string(arr))
return CBytes(arr)
}
return C.CString(val.String())
}
Expand Down
Loading

0 comments on commit 808076a

Please sign in to comment.