[svn:mod_parrot] r586 - in mod_parrot/trunk: include src

[email protected] Wed, 7 Jan 2009 08:54:15 -0800 (PST)
Newsgroups perl.cvs.mod_parrot
Message-ID <[email protected]>
Author: jhorwitz
Date: Wed Jan  7 08:54:14 2009
New Revision: 586

Modified:
   mod_parrot/trunk/include/mod_parrot.h
   mod_parrot/trunk/src/module.c
   mod_parrot/trunk/src/parrot_util.c

Log:
refactor apache type wrappers to a generic modparrot_wrap_apache_type function


Modified: mod_parrot/trunk/include/mod_parrot.h
==============================================================================
--- mod_parrot/trunk/include/mod_parrot.h	(original)
+++ mod_parrot/trunk/include/mod_parrot.h	Wed Jan  7 08:54:14 2009
@@ -101,5 +101,14 @@
 modparrot_context *modparrot_get_current_ctx(apr_pool_t *);
 void modparrot_set_current_ctx(apr_pool_t *, modparrot_context *);
 apr_size_t modparrot_request_read(request_rec *, char *, apr_size_t);
+Parrot_PMC modparrot_wrap_apache_type(Parrot_Interp, char *, char *, void *);
+
+/* macros for wrapping apache types */
+#define modparrot_wrap_apr_pool(i, x) \
+    (modparrot_wrap_apache_type(i, "ModParrot;APR;Pool", "apr_pool", x))
+
+#define modparrot_wrap_server_rec(i, x) \
+    (modparrot_wrap_apache_type(i, "ModParrot;Apache;ServerRec", \
+    "server_rec", x))
 
 #endif /* _MODPARROT_H */

Modified: mod_parrot/trunk/src/module.c
==============================================================================
--- mod_parrot/trunk/src/module.c	(original)
+++ mod_parrot/trunk/src/module.c	Wed Jan  7 08:54:14 2009
@@ -40,12 +40,6 @@
 /* used when we have no other way of getting the server config */
 static server_rec *our_server;
 
-/* XXX mod_parrot can crash on x86_64 w/o this.
- * there's no prototype in the standard parrot includes, so compiler assumes
- * return type of 'int', and sizeof(int) != sizeof(pointer) on x86_64.
- */
-Parrot_PMC Parrot_Class_instantiate(PARROT_INTERP, PMC *, PMC *init);
-
 static apr_status_t modparrot_remove_module(void *data)
 {
     module *modp = (module *)data;
@@ -196,78 +190,6 @@
     return(args);
 }
 
-static Parrot_PMC modparrot_wrap_apr_pool(Parrot_Interp interp, apr_pool_t *p)
-{
-    Parrot_PMC pool_class;
-    Parrot_PMC pool_pmc;
-    Parrot_PMC pointer_pmc;
-    Parrot_PMC namespace;
-    Parrot_PMC init;
-    int typenum;
-
-    typenum = Parrot_PMC_typenum(interp, "ResizableStringArray");
-    namespace = (Parrot_PMC)Parrot_PMC_new(interp, typenum);
-    Parrot_register_pmc(interp, namespace);
-    Parrot_PMC_set_intval(interp, namespace, 3);
-    Parrot_PMC_set_cstring_intkey(interp, namespace, 0, "ModParrot");
-    Parrot_PMC_set_cstring_intkey(interp, namespace, 1, "APR");
-    Parrot_PMC_set_cstring_intkey(interp, namespace, 2, "Pool");
-    pool_class = Parrot_oo_get_class(interp, namespace);
-    Parrot_unregister_pmc(interp, namespace);
-
-    typenum = Parrot_PMC_typenum(interp, "UnManagedStruct");
-    pointer_pmc = (Parrot_PMC)Parrot_PMC_new(interp, typenum);
-    Parrot_PMC_set_pointer(interp, pointer_pmc, p);
-
-    typenum = Parrot_PMC_typenum(interp, "Hash");
-    init = (Parrot_PMC)Parrot_PMC_new(interp, typenum);
-    Parrot_register_pmc(interp, init);
-    Parrot_PMC_set_pmc_keyed_str(interp, init,
-        string_from_literal(interp, "apr_pool"), pointer_pmc);
-    pool_pmc = Parrot_Class_instantiate(interp, pool_class, init);
-    Parrot_register_pmc(interp, pool_pmc);
-    Parrot_unregister_pmc(interp, init);
-    Parrot_unregister_pmc(interp, pool_class);
-
-    return(pool_pmc);
-}
-
-static Parrot_PMC modparrot_wrap_server_rec(Parrot_Interp interp, server_rec *s)
-{
-    Parrot_PMC sr_class;
-    Parrot_PMC sr_pmc;
-    Parrot_PMC pointer_pmc;
-    Parrot_PMC namespace;
-    Parrot_PMC init;
-    int typenum;
-
-    typenum = Parrot_PMC_typenum(interp, "ResizableStringArray");
-    namespace = (Parrot_PMC)Parrot_PMC_new(interp, typenum);
-    Parrot_register_pmc(interp, namespace);
-    Parrot_PMC_set_intval(interp, namespace, 3);
-    Parrot_PMC_set_cstring_intkey(interp, namespace, 0, "ModParrot");
-    Parrot_PMC_set_cstring_intkey(interp, namespace, 1, "Apache");
-    Parrot_PMC_set_cstring_intkey(interp, namespace, 2, "ServerRec");
-    sr_class = Parrot_oo_get_class(interp, namespace);
-    Parrot_unregister_pmc(interp, namespace);
-
-    typenum = Parrot_PMC_typenum(interp, "UnManagedStruct");
-    pointer_pmc = (Parrot_PMC)Parrot_PMC_new(interp, typenum);
-    Parrot_PMC_set_pointer(interp, pointer_pmc, s);
-
-    typenum = Parrot_PMC_typenum(interp, "Hash");
-    init = (Parrot_PMC)Parrot_PMC_new(interp, typenum);
-    Parrot_register_pmc(interp, init);
-    Parrot_PMC_set_pmc_keyed_str(interp, init,
-        string_from_literal(interp, "server_rec"), pointer_pmc);
-    sr_pmc = Parrot_Class_instantiate(interp, sr_class, init);
-    Parrot_register_pmc(interp, sr_pmc);
-    Parrot_unregister_pmc(interp, init);
-    Parrot_unregister_pmc(interp, sr_class);
-
-    return(sr_pmc);
-}
-
 static const char *modparrot_module_cmd_take123(cmd_parms *cmd, void *mconfig,
                                        const char *arg1,
                                        const char *arg2,

Modified: mod_parrot/trunk/src/parrot_util.c
==============================================================================
--- mod_parrot/trunk/src/parrot_util.c	(original)
+++ mod_parrot/trunk/src/parrot_util.c	Wed Jan  7 08:54:14 2009
@@ -22,6 +22,12 @@
 #include "parrot/extend.h"
 #include "mod_parrot.h"
 
+/* XXX mod_parrot can crash on x86_64 w/o this.
+ * there's no prototype in the standard parrot includes, so compiler assumes
+ * return type of 'int', and sizeof(int) != sizeof(pointer) on x86_64.
+ */
+Parrot_PMC Parrot_Class_instantiate(PARROT_INTERP, PMC *, PMC *init);
+
 static Parrot_PMC get_sub_pmc_s(Parrot_Interp interp, char *namespace, char *name)
 {
     Parrot_PMC sub;
@@ -241,3 +247,42 @@
     trace_string = string_to_cstring(interp, buf);
     return(trace_string);
 }
+
+Parrot_PMC modparrot_wrap_apache_type(Parrot_Interp interp, char *classname,
+    char *init_key, void *init_val)
+{
+    Parrot_PMC _class;
+    Parrot_PMC obj;
+    Parrot_PMC pointer_pmc;
+    Parrot_PMC namespace;
+    Parrot_PMC init;
+    int typenum;
+
+    namespace = string_split(interp, string_from_literal(interp, ";"),
+        string_from_cstring(interp, classname, strlen(classname)));
+    Parrot_register_pmc(interp, namespace);
+    _class = Parrot_oo_get_class(interp, namespace);
+    Parrot_unregister_pmc(interp, namespace);
+
+    typenum = Parrot_PMC_typenum(interp, "UnManagedStruct");
+    pointer_pmc = (Parrot_PMC)Parrot_PMC_new(interp, typenum);
+    Parrot_PMC_set_pointer(interp, pointer_pmc, init_val);
+
+    if (init_key && init_val) {
+        typenum = Parrot_PMC_typenum(interp, "Hash");
+        init = (Parrot_PMC)Parrot_PMC_new(interp, typenum);
+        Parrot_register_pmc(interp, init);
+        Parrot_PMC_set_pmc_keyed_str(interp, init,
+            string_from_cstring(interp, init_key, strlen(init_key)),
+                pointer_pmc);
+        obj = Parrot_Class_instantiate(interp, _class, init);
+    }
+    else {
+        obj = Parrot_Class_instantiate(interp, _class, PMCNULL);
+    }
+    Parrot_register_pmc(interp, obj);
+    Parrot_unregister_pmc(interp, init);
+    Parrot_unregister_pmc(interp, _class);
+
+    return(obj);
+}