Repository: lucy-clownfish
Updated Branches:
  refs/heads/master c911cd66b -> f26da1edf


Modify Go proof-of-concept design.

*   Make structs public: `FooIMP` instead of `implFoo`.
*   Embed parent structs.
*   Make `ref` a `uintptr` instead of a pointer to a C type.
*   Add the INITOBJ method to ObjIMP.
*   Use one finalizer and call SetFinalizer from only one location.
*   Implement TOPTR() only once.


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

Branch: refs/heads/master
Commit: 6bacb27eb9f5f0cd0ac9aaa8c7cedc5336560c17
Parents: aab0146
Author: Marvin Humphrey <[email protected]>
Authored: Mon Apr 27 18:51:10 2015 -0700
Committer: Marvin Humphrey <[email protected]>
Committed: Fri May 1 18:41:29 2015 -0700

----------------------------------------------------------------------
 runtime/go/clownfish/clownfish.go | 79 ++++++++++++++++------------------
 1 file changed, 38 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/6bacb27e/runtime/go/clownfish/clownfish.go
----------------------------------------------------------------------
diff --git a/runtime/go/clownfish/clownfish.go 
b/runtime/go/clownfish/clownfish.go
index 626633f..d4b61e7 100644
--- a/runtime/go/clownfish/clownfish.go
+++ b/runtime/go/clownfish/clownfish.go
@@ -72,8 +72,8 @@ type Obj interface {
        TOPTR() uintptr
 }
 
-type implObj struct {
-       ref *C.cfish_Obj
+type ObjIMP struct {
+       ref uintptr
 }
 
 type Err interface {
@@ -81,40 +81,40 @@ type Err interface {
        Error() string
 }
 
-type implErr struct {
-       ref *C.cfish_Err
+type ErrIMP struct {
+       ObjIMP
 }
 
 type String interface {
        Obj
 }
 
-type implString struct {
-       ref *C.cfish_String
+type StringIMP struct {
+       ObjIMP
 }
 
-type implByteBuf struct {
-       ref *C.cfish_ByteBuf
+type ByteBufIMP struct {
+       ObjIMP
 }
 
-type implHash struct {
-       ref *C.cfish_Hash
+type HashIMP struct {
+       ObjIMP
 }
 
-type implVector struct {
-       ref *C.cfish_Vector
+type VectorIMP struct {
+       ObjIMP
 }
 
-type implClass struct {
-       ref *C.cfish_Class
+type ClassIMP struct {
+       ObjIMP
 }
 
-type implMethod struct {
-       ref *C.cfish_Method
+type MethodIMP struct {
+       ObjIMP
 }
 
-type implLockFreeRegistry struct {
-       ref *C.cfish_LockFreeRegistry
+type LockFreeRegistryIMP struct {
+       ObjIMP
 }
 
 func NewString(goString string) String {
@@ -124,19 +124,24 @@ func NewString(goString string) String {
        return WRAPString(unsafe.Pointer(cfObj))
 }
 
-func WRAPString(ptr unsafe.Pointer) String {
-       obj := &implString{((*C.cfish_String)(ptr))}
-       runtime.SetFinalizer(obj, (*implString).finalize)
-       return obj
+func (o *ObjIMP) INITOBJ(ptr unsafe.Pointer) {
+       o.ref = uintptr(ptr)
+       runtime.SetFinalizer(o, ClearRef)
+}
+
+func ClearRef (o *ObjIMP) {
+       C.cfish_dec_refcount(unsafe.Pointer(o.ref))
+       o.ref = 0
 }
 
-func (obj *implString) finalize() {
-       C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
-       obj.ref = nil
+func (o *ObjIMP) TOPTR() uintptr {
+       return o.ref
 }
 
-func (obj *implString) TOPTR() uintptr {
-       return uintptr(unsafe.Pointer(obj.ref))
+func WRAPString(ptr unsafe.Pointer) String {
+       s := &StringIMP{}
+       s.INITOBJ(ptr)
+       return s
 }
 
 func CFStringToGo(ptr unsafe.Pointer) string {
@@ -162,22 +167,14 @@ func NewErr(mess string) Err {
 }
 
 func WRAPErr(ptr unsafe.Pointer) Err {
-       obj := &implErr{((*C.cfish_Err)(ptr))}
-       runtime.SetFinalizer(obj, (*implErr).finalize)
-       return obj
-}
-
-func (obj *implErr) finalize() {
-       C.cfish_dec_refcount(unsafe.Pointer(obj.ref))
-       obj.ref = nil
-}
-
-func (obj *implErr) TOPTR() uintptr {
-       return uintptr(unsafe.Pointer(obj.ref))
+       e := &ErrIMP{}
+       e.INITOBJ(ptr)
+       return e
 }
 
-func (obj *implErr) Error() string {
-       return CFStringToGo(unsafe.Pointer(C.CFISH_Err_Get_Mess(obj.ref)))
+func (e *ErrIMP) Error() string {
+       mess := C.CFISH_Err_Get_Mess((*C.cfish_Err)(unsafe.Pointer(e.ref)))
+       return CFStringToGo(unsafe.Pointer(mess))
 }
 
 //export GoCfish_PanicErr_internal

Reply via email to