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 2016/02/25 00:07:40 UTC

[13/20] lucy-clownfish git commit: Stub out callback-generating code for Python.

Stub out callback-generating code for Python.


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

Branch: refs/heads/master
Commit: 8996f0f62d0e07ed3b0d14934041aeb9d71fa698
Parents: 514827b
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Wed Jan 27 16:05:37 2016 -0800
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Tue Feb 23 18:22:04 2016 -0800

----------------------------------------------------------------------
 compiler/src/CFCPyMethod.c | 38 ++++++++++++++++++++++++++++++++
 compiler/src/CFCPyMethod.h | 39 ++++++++++++++++++++++++++++++++
 compiler/src/CFCPython.c   | 49 ++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 125 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/8996f0f6/compiler/src/CFCPyMethod.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPyMethod.c b/compiler/src/CFCPyMethod.c
new file mode 100644
index 0000000..2892b86
--- /dev/null
+++ b/compiler/src/CFCPyMethod.c
@@ -0,0 +1,38 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+#include "CFCPyMethod.h"
+#include "CFCPyTypeMap.h"
+#include "CFCUtil.h"
+#include "CFCClass.h"
+#include "CFCFunction.h"
+#include "CFCMethod.h"
+#include "CFCSymbol.h"
+#include "CFCType.h"
+#include "CFCParcel.h"
+#include "CFCParamList.h"
+#include "CFCVariable.h"
+
+#ifndef true
+#define true  1
+#define false 0
+#endif
+
+char*
+CFCPyMethod_callback_def(CFCMethod *method, CFCClass *invoker) {
+    return CFCUtil_strdup("");
+}

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/8996f0f6/compiler/src/CFCPyMethod.h
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPyMethod.h b/compiler/src/CFCPyMethod.h
new file mode 100644
index 0000000..15fa1e6
--- /dev/null
+++ b/compiler/src/CFCPyMethod.h
@@ -0,0 +1,39 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef H_CFCPYMETHOD
+#define H_CFCPYMETHOD
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct CFCMethod;
+struct CFCClass;
+
+/** Return C code which knows how to call back into Python for this method.  This
+ * code is run when a Python subclass has overridden a method in a Clownfish base
+ * class.
+ */
+char*
+CFCPyMethod_callback_def(struct CFCMethod *method, struct CFCClass *invoker);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_CFCPYMETHOD */
+

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/8996f0f6/compiler/src/CFCPython.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPython.c b/compiler/src/CFCPython.c
index 3efaa3a..10c9872 100644
--- a/compiler/src/CFCPython.c
+++ b/compiler/src/CFCPython.c
@@ -24,6 +24,7 @@
 #define CFC_NEED_BASE_STRUCT_DEF
 #include "CFCBase.h"
 #include "CFCPython.h"
+#include "CFCPyMethod.h"
 #include "CFCParcel.h"
 #include "CFCClass.h"
 #include "CFCMethod.h"
@@ -108,6 +109,48 @@ S_write_hostdefs(CFCPython *self) {
     FREEMEM(content);
 }
 
+static char*
+S_gen_callbacks(CFCPython *self, CFCParcel *parcel, CFCClass **ordered) {
+    char *callbacks  = CFCUtil_strdup("");
+
+    // Generate implementation files containing callback definitions.
+    for (size_t i = 0; ordered[i] != NULL; i++) {
+        CFCClass *klass = ordered[i];
+        if (CFCClass_included(klass)
+            || CFCClass_inert(klass)
+            //|| CFCClass_get_parcel(klass) != parcel
+           ) {
+            continue;
+        }
+
+        CFCMethod **fresh_methods = CFCClass_fresh_methods(klass);
+        for (int meth_num = 0; fresh_methods[meth_num] != NULL; meth_num++) {
+            CFCMethod *method = fresh_methods[meth_num];
+
+            // Define callback.
+            if (CFCMethod_novel(method) && !CFCMethod_final(method)) {
+                char *cb_def = CFCPyMethod_callback_def(method, klass);
+                callbacks = CFCUtil_cat(callbacks, cb_def, "\n", NULL);
+                FREEMEM(cb_def);
+            }
+        }
+    }
+
+    static const char helpers[] =
+        ""
+        ;
+
+    static const char pattern[] =
+        "%s\n"
+        "\n"
+        "%s"
+        ;
+    char *content = CFCUtil_sprintf(pattern, helpers, callbacks);
+
+    FREEMEM(callbacks);
+    return content;
+}
+
 static void
 S_write_module_file(CFCPython *self, CFCParcel *parcel, const char *dest) {
     const char *parcel_name = CFCParcel_get_name(parcel);
@@ -127,6 +170,7 @@ S_write_module_file(CFCPython *self, CFCParcel *parcel, const char *dest) {
 
     CFCClass  **ordered = CFCHierarchy_ordered_classes(self->hierarchy);
     CFCParcel **parcels = CFCParcel_all_parcels();
+    char *callbacks          = S_gen_callbacks(self, parcel, ordered);
     char *pound_includes     = CFCUtil_strdup("");
 
     for (size_t i = 0; ordered[i] != NULL; i++) {
@@ -146,6 +190,8 @@ S_write_module_file(CFCPython *self, CFCParcel *parcel, const char *dest) {
         "#include \"CFBind.h\"\n"
         "%s\n"
         "\n"
+        "%s\n" // callbacks
+        "\n"
         "static PyModuleDef module_def = {\n"
         "    PyModuleDef_HEAD_INIT,\n"
         "    \"%s\",\n" // module name
@@ -164,7 +210,7 @@ S_write_module_file(CFCPython *self, CFCParcel *parcel, const char *dest) {
         "\n";
 
     char *content
-        = CFCUtil_sprintf(pattern, self->header, pound_includes,
+        = CFCUtil_sprintf(pattern, self->header, pound_includes, callbacks,
                           helper_mod_name, last_component, self->footer);
 
     char *filepath = CFCUtil_sprintf("%s" CHY_DIR_SEP "_%s.c", dest,
@@ -176,6 +222,7 @@ S_write_module_file(CFCPython *self, CFCParcel *parcel, const char *dest) {
     FREEMEM(helper_mod_name);
     FREEMEM(pymod_name);
     FREEMEM(pound_includes);
+    FREEMEM(callbacks);
     FREEMEM(ordered);
 }