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/08/03 23:47:43 UTC
[4/7] lucy-clownfish git commit: Map Vector to Go `[]interface{}`.
Map Vector to Go `[]interface{}`.
Convert Vector to slice of empty interface for both input (func
parameters) and output (return 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/04a57319
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/04a57319
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/04a57319
Branch: refs/heads/master
Commit: 04a573198d2467a87c293833669a00e6f5e2e16b
Parents: 55d40ac
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Thu Jul 30 16:23:20 2015 -0700
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Fri Jul 31 11:06:27 2015 -0700
----------------------------------------------------------------------
compiler/src/CFCGoFunc.c | 65 +++++++++++++++++-----------------
compiler/src/CFCGoTypeMap.c | 3 ++
runtime/go/build.go | 4 +++
runtime/go/clownfish/clownfish.go | 8 +++++
4 files changed, 48 insertions(+), 32 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/04a57319/compiler/src/CFCGoFunc.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCGoFunc.c b/compiler/src/CFCGoFunc.c
index 060e62b..64f2255 100644
--- a/compiler/src/CFCGoFunc.c
+++ b/compiler/src/CFCGoFunc.c
@@ -55,6 +55,8 @@ CFCGoFunc_go_meth_name(const char *orig) {
static char*
S_prep_start(CFCParcel *parcel, const char *name, CFCClass *invoker,
CFCParamList *param_list, CFCType *return_type, int targ) {
+ const char *clownfish_dot = CFCParcel_is_cfish(parcel)
+ ? "" : "clownfish.";
CFCVariable **param_vars = CFCParamList_get_variables(param_list);
char *invocant;
char go_name[GO_NAME_BUF_SIZE];
@@ -85,22 +87,21 @@ S_prep_start(CFCParcel *parcel, const char *name, CFCClass *invoker,
// Convert certain types and defer their destruction until after the
// Clownfish call returns.
- if (CFCType_is_string_type(type)) {
- const char *cf_prefix = CFCParcel_is_cfish(parcel)
- ? "" : "clownfish.";
- char pattern[] =
- "%s\t%sCF := C.cfish_Str_new_steal_utf8("
- "C.CString(%s), C.size_t(len(%s)))\n"
- ;
- char *temp = CFCUtil_sprintf(pattern, converted, go_name, go_name,
- go_name, go_name);
- FREEMEM(converted);
- converted = temp;
- if (!CFCType_decremented(type)) {
- converted = CFCUtil_cat(converted,
- "\tdefer C.cfish_dec_refcount(unsafe.Pointer(", go_name,
- "CF))\n", NULL);
- }
+ char *convertible = NULL;
+ if (CFCType_cfish_string(type)) { convertible = "String"; }
+ else if (CFCType_cfish_vector(type)) { convertible = "Vector"; }
+ else { continue; }
+ char pattern[] =
+ "\t%sCF := (*C.cfish_%s)(%sGoTo%s(%s))\n";
+ char *conversion = CFCUtil_sprintf(pattern, go_name, convertible,
+ clownfish_dot, convertible,
+ go_name);
+ converted = CFCUtil_cat(converted, conversion, NULL);
+ FREEMEM(conversion);
+ if (CFCType_decremented(type)) {
+ converted = CFCUtil_cat(converted,
+ "\tdefer C.cfish_decref(unsafe.Pointer(",
+ go_name, "CF))\n", NULL);
}
}
@@ -170,8 +171,9 @@ S_prep_cfargs(CFCParcel *parcel, CFCClass *invoker,
cfargs = CFCUtil_cat(cfargs, "C.", CFCType_get_specifier(type),
"(", go_name, ")", NULL);
}
- else if (CFCType_is_string_type(type)
- // Don't convert a clownfish.String invocant.
+ else if ((CFCType_is_string_type(type)
+ || CFCType_cfish_vector(type))
+ // Don't convert an invocant.
&& (targ != IS_METHOD || i != 0)
) {
cfargs = CFCUtil_cat(cfargs, go_name, "CF", NULL);
@@ -208,7 +210,12 @@ CFCGoFunc_ctor_cfargs(CFCParcel *parcel, CFCParamList *param_list) {
char*
CFCGoFunc_return_statement(CFCParcel *parcel, CFCType *return_type,
const char *cf_retval) {
+ const char *clownfish_dot = CFCParcel_is_cfish(parcel)
+ ? "" : "clownfish.";
+ const char *maybe_decref = CFCType_incremented(return_type)
+ ? "\tdefer C.cfish_decref(unsafe.Pointer(retvalCF))\n" : "";
char *statement = NULL;
+
if (CFCType_is_void(return_type)) {
return CFCUtil_strdup("");
}
@@ -223,20 +230,14 @@ CFCGoFunc_return_statement(CFCParcel *parcel, CFCType *return_type,
statement = CFCUtil_sprintf("\treturn %s(retvalCF)\n", ret_type_str);
}
else if (CFCType_is_string_type(return_type)) {
- const char *clownfish_dot = CFCParcel_is_cfish(parcel)
- ? "" : "clownfish.";
- if (CFCType_incremented(return_type)) {
- char pattern[] =
- "\tdefer C.cfish_dec_refcount(unsafe.Pointer(retvalCF))\n"
- "\treturn %sCFStringToGo(unsafe.Pointer(retvalCF))\n"
- ;
- statement = CFCUtil_sprintf(pattern, clownfish_dot);
- }
- else {
- char pattern[] =
- "\treturn %sCFStringToGo(unsafe.Pointer(retvalCF))\n";
- statement = CFCUtil_sprintf(pattern, clownfish_dot);
- }
+ char pattern[] =
+ "%s\treturn %sCFStringToGo(unsafe.Pointer(retvalCF))\n";
+ statement = CFCUtil_sprintf(pattern, maybe_decref, clownfish_dot);
+ }
+ else if (CFCType_cfish_vector(return_type)) {
+ char pattern[] =
+ "%s\treturn %sVectorToGo(unsafe.Pointer(retvalCF))\n";
+ statement = CFCUtil_sprintf(pattern, maybe_decref, clownfish_dot);
}
else if (CFCType_is_object(return_type)) {
char *go_type_name = CFCGoTypeMap_go_type_name(return_type, parcel);
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/04a57319/compiler/src/CFCGoTypeMap.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCGoTypeMap.c b/compiler/src/CFCGoTypeMap.c
index 7ddbf08..15e42a7 100644
--- a/compiler/src/CFCGoTypeMap.c
+++ b/compiler/src/CFCGoTypeMap.c
@@ -96,6 +96,9 @@ CFCGoTypeMap_go_type_name(CFCType *type, CFCParcel *current_parcel) {
if (CFCType_is_string_type(type)) {
return CFCUtil_strdup("string");
}
+ else if (CFCType_cfish_vector(type)) {
+ return CFCUtil_strdup("[]interface{}");
+ }
else if (CFCType_is_object(type)) {
// Divide the specifier into prefix and struct name.
const char *specifier = CFCType_get_specifier(type);
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/04a57319/runtime/go/build.go
----------------------------------------------------------------------
diff --git a/runtime/go/build.go b/runtime/go/build.go
index 186eebd..fd96fd1 100644
--- a/runtime/go/build.go
+++ b/runtime/go/build.go
@@ -148,6 +148,10 @@ func specMethods(parcel *cfc.Parcel) {
stringBinding.SpecMethod("Code_Point_From", "CodePointFrom(uintptr) rune")
stringBinding.SpecMethod("Swap_Chars", "SwapChars(rune, rune) string")
stringBinding.Register()
+
+ vecBinding := cfc.NewGoClass(parcel, "Clownfish::Vector")
+ vecBinding.SetSuppressCtor(true)
+ vecBinding.Register()
}
func prep() {
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/04a57319/runtime/go/clownfish/clownfish.go
----------------------------------------------------------------------
diff --git a/runtime/go/clownfish/clownfish.go b/runtime/go/clownfish/clownfish.go
index 21eb567..eefd831 100644
--- a/runtime/go/clownfish/clownfish.go
+++ b/runtime/go/clownfish/clownfish.go
@@ -130,6 +130,14 @@ func NewString(goString string) String {
return WRAPString(unsafe.Pointer(cfObj))
}
+func NewVector(size int) Vector {
+ if (size < 0 || uint64(size) > ^uint64(0)) {
+ panic(NewErr(fmt.Sprintf("Param 'size' out of range: %d", size)))
+ }
+ cfObj := C.cfish_Vec_new(C.size_t(size))
+ return WRAPVector(unsafe.Pointer(cfObj))
+}
+
func (o *ObjIMP) INITOBJ(ptr unsafe.Pointer) {
o.ref = uintptr(ptr)
runtime.SetFinalizer(o, ClearRef)