You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2014/11/30 06:09:12 UTC

[15/20] lucy-clownfish git commit: Add Go wrappers for Clownfish basic types.

Add Go wrappers for Clownfish basic types.


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/26a9f65f
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/26a9f65f
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/26a9f65f

Branch: refs/heads/go_bindings_2
Commit: 26a9f65f617ab2711eba6dba186fb32bae824a53
Parents: 047d52d
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Wed Nov 5 10:58:27 2014 -0800
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Sat Nov 29 21:07:53 2014 -0800

----------------------------------------------------------------------
 runtime/go/clownfish/clownfish.go      | 73 +++++++++++++++++++++++++++--
 runtime/go/clownfish/clownfish_test.go |  6 ++-
 2 files changed, 74 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/26a9f65f/runtime/go/clownfish/clownfish.go
----------------------------------------------------------------------
diff --git a/runtime/go/clownfish/clownfish.go b/runtime/go/clownfish/clownfish.go
index 5ed0b8c..7f4db15 100644
--- a/runtime/go/clownfish/clownfish.go
+++ b/runtime/go/clownfish/clownfish.go
@@ -33,13 +33,78 @@ package clownfish
 
 */
 import "C"
+import "runtime"
+import "unsafe"
 
 func init() {
 	C.cfish_bootstrap_parcel()
 }
 
-// Temporary test-only routine.
-func DoStuff() {
-	hash := C.cfish_Hash_new(C.uint32_t(0))
-	C.CFISH_Hash_Dec_RefCount(hash)
+type Obj interface {
+	ToPtr() unsafe.Pointer
+}
+
+type Err struct {
+	ref *C.cfish_Err
+}
+
+type String struct {
+	ref *C.cfish_String
+}
+
+type ByteBuf struct {
+	ref *C.cfish_ByteBuf
+}
+
+type Hash struct {
+	ref *C.cfish_Hash
+}
+
+type VArray struct {
+	ref *C.cfish_VArray
+}
+
+type Class struct {
+	ref *C.cfish_Class
+}
+
+type Method struct {
+	ref *C.cfish_Method
+}
+
+type LockFreeRegistry struct {
+	ref *C.cfish_LockFreeRegistry
+}
+
+func NewString(goString string) String {
+	str := C.CString(goString)
+	len := C.size_t(len(goString))
+	obj := String{
+		C.cfish_Str_new_steal_utf8(str, len),
+	}
+	runtime.SetFinalizer(&obj, (*String).callDecRef)
+	return obj
+}
+
+func (obj *String) callDecRef() {
+	C.CFISH_Str_Dec_RefCount(obj.ref)
+	obj.ref = nil
+}
+
+func (obj *String) ToPtr() unsafe.Pointer {
+	return unsafe.Pointer(obj.ref)
+}
+
+func CFStringToGo(ptr unsafe.Pointer) string {
+	cfString := (*C.cfish_String)(ptr);
+	if cfString == nil {
+		return ""
+	}
+	if !C.CFISH_Str_Is_A(cfString, C.CFISH_STRING) {
+		cfString := C.CFISH_Str_To_String(cfString)
+		defer C.CFISH_Str_Dec_RefCount(cfString)
+	}
+	data := C.CFISH_Str_Get_Ptr8(cfString)
+	size := C.int(C.CFISH_Str_Get_Size(cfString))
+	return C.GoStringN(data, size)
 }

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/26a9f65f/runtime/go/clownfish/clownfish_test.go
----------------------------------------------------------------------
diff --git a/runtime/go/clownfish/clownfish_test.go b/runtime/go/clownfish/clownfish_test.go
index 3872b92..1709dfc 100644
--- a/runtime/go/clownfish/clownfish_test.go
+++ b/runtime/go/clownfish/clownfish_test.go
@@ -20,5 +20,9 @@ import "git-wip-us.apache.org/repos/asf/lucy-clownfish.git/runtime/go/clownfish"
 import "testing"
 
 func TestStuff(t *testing.T) {
-	clownfish.DoStuff()
+	cfString := clownfish.NewString("foo")
+	goString := clownfish.CFStringToGo(cfString.ToPtr())
+	if goString != "foo" {
+		t.Error("Round-tripping strings failed")
+	}
 }