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 2015/05/07 00:18:34 UTC

[08/23] lucy-clownfish git commit: Autogenerate per-Class Go helper func.

Autogenerate per-Class Go helper func.

Just one for now: `WRAPFoo`.


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

Branch: refs/heads/master
Commit: 0d5e92edd3cf64ac04a98def5cec84c108473921
Parents: dac0535
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Tue Mar 31 17:14:33 2015 -0700
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Wed May 6 14:28:12 2015 -0700

----------------------------------------------------------------------
 compiler/src/CFCGo.c              | 16 ++++++++++++++--
 compiler/src/CFCGoClass.c         | 25 +++++++++++++++++++++++++
 compiler/src/CFCGoClass.h         |  5 +++++
 runtime/go/clownfish/clownfish.go | 12 ------------
 4 files changed, 44 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/0d5e92ed/compiler/src/CFCGo.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCGo.c b/compiler/src/CFCGo.c
index 13ceb63..36d1263 100644
--- a/compiler/src/CFCGo.c
+++ b/compiler/src/CFCGo.c
@@ -189,7 +189,8 @@ S_gen_init_code(CFCParcel *parcel) {
 static char*
 S_gen_autogen_go(CFCGo *self, CFCParcel *parcel) {
     CFCGoClass **registry = CFCGoClass_registry();
-    char *type_decs = CFCUtil_strdup("");
+    char *type_decs   = CFCUtil_strdup("");
+    char *boilerplate = CFCUtil_strdup("");
 
     for (int i = 0; registry[i] != NULL; i++) {
         CFCGoClass *class_binding = registry[i];
@@ -197,15 +198,25 @@ S_gen_autogen_go(CFCGo *self, CFCParcel *parcel) {
         char *type_dec = CFCGoClass_go_typing(class_binding);
         type_decs = CFCUtil_cat(type_decs, type_dec, "\n", NULL);
         FREEMEM(type_dec);
+
+        char *boiler_code = CFCGoClass_boilerplate_funcs(class_binding);
+        boilerplate = CFCUtil_cat(boilerplate, boiler_code, "\n", NULL);
+        FREEMEM(boiler_code);
     }
 
     char pattern[] =
         "// Type declarations.\n"
+        "\n"
+        "%s\n"
+        "\n"
+        "// Autogenerated utility functions.\n"
+        "\n"
         "%s\n"
         "\n"
         ;
-    char *content = CFCUtil_sprintf(pattern, type_decs);
+    char *content = CFCUtil_sprintf(pattern, type_decs, boilerplate);
 
+    FREEMEM(boilerplate);
     FREEMEM(type_decs);
     return content;
 }
@@ -227,6 +238,7 @@ S_write_cfbind_go(CFCGo *self, CFCParcel *parcel, const char *dest,
         "%s\n"
         "*/\n"
         "import \"C\"\n"
+        "import \"unsafe\"\n"
         "\n"
         "%s\n"
         "\n"

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/0d5e92ed/compiler/src/CFCGoClass.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCGoClass.c b/compiler/src/CFCGoClass.c
index e504c87..d35d3f3 100644
--- a/compiler/src/CFCGoClass.c
+++ b/compiler/src/CFCGoClass.c
@@ -215,6 +215,31 @@ CFCGoClass_go_typing(CFCGoClass *self) {
     return content;
 }
 
+char*
+CFCGoClass_boilerplate_funcs(CFCGoClass *self) {
+    char *content = NULL;
+    if (!self->client) {
+        CFCUtil_die("Can't find class for %s", self->class_name);
+    }
+    else if (CFCClass_inert(self->client)) {
+        content = CFCUtil_strdup("");
+    } else {
+        const char *short_struct = CFCClass_get_struct_sym(self->client);
+        const char *full_struct  = CFCClass_full_struct_sym(self->client);
+        char pattern[] =
+            "func WRAP%s(ptr unsafe.Pointer) %s {\n"
+            "\tobj := &%sIMP{}\n"
+            "\tobj.INITOBJ(ptr)\n"
+            "\treturn obj\n"
+            "}\n"
+            ;
+
+        content = CFCUtil_sprintf(pattern, short_struct, short_struct,
+                                  short_struct, full_struct, short_struct);
+    }
+    return content;
+}
+
 static void
 S_lazy_init_method_bindings(CFCGoClass *self) {
     if (self->method_bindings) {

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/0d5e92ed/compiler/src/CFCGoClass.h
----------------------------------------------------------------------
diff --git a/compiler/src/CFCGoClass.h b/compiler/src/CFCGoClass.h
index dec2e44..a1f603c 100644
--- a/compiler/src/CFCGoClass.h
+++ b/compiler/src/CFCGoClass.h
@@ -65,6 +65,11 @@ CFCGoClass_get_client(CFCGoClass *self);
 char*
 CFCGoClass_go_typing(CFCGoClass *self);
 
+/** Return boilerplate Go code needed for each Clownfish class.
+ */
+char*
+CFCGoClass_boilerplate_funcs(CFCGoClass *self);
+
 #ifdef __cplusplus
 }
 #endif

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/0d5e92ed/runtime/go/clownfish/clownfish.go
----------------------------------------------------------------------
diff --git a/runtime/go/clownfish/clownfish.go b/runtime/go/clownfish/clownfish.go
index 12e625a..d7d3926 100644
--- a/runtime/go/clownfish/clownfish.go
+++ b/runtime/go/clownfish/clownfish.go
@@ -93,12 +93,6 @@ func (o *ObjIMP) TOPTR() uintptr {
 	return o.ref
 }
 
-func WRAPString(ptr unsafe.Pointer) String {
-	s := &StringIMP{}
-	s.INITOBJ(ptr)
-	return s
-}
-
 func CFStringToGo(ptr unsafe.Pointer) string {
 	cfString := (*C.cfish_String)(ptr)
 	if cfString == nil {
@@ -121,12 +115,6 @@ func NewErr(mess string) Err {
 	return WRAPErr(unsafe.Pointer(cfObj))
 }
 
-func WRAPErr(ptr unsafe.Pointer) Err {
-	e := &ErrIMP{}
-	e.INITOBJ(ptr)
-	return e
-}
-
 func (e *ErrIMP) Error() string {
 	mess := C.CFISH_Err_Get_Mess((*C.cfish_Err)(unsafe.Pointer(e.ref)))
 	return CFStringToGo(unsafe.Pointer(mess))