[pypy-svn] r75839 - in pypy/branch/reflex-support/pypy/module/cppyy: . include src test

cfbolz at codespeak.net cfbolz at codespeak.net
Mon Jul 5 18:23:25 CEST 2010


Author: cfbolz
Date: Mon Jul  5 18:23:24 2010
New Revision: 75839

Modified:
   pypy/branch/reflex-support/pypy/module/cppyy/include/cppyy.h
   pypy/branch/reflex-support/pypy/module/cppyy/include/reflexcwrapper.h
   pypy/branch/reflex-support/pypy/module/cppyy/interp_cppyy.py
   pypy/branch/reflex-support/pypy/module/cppyy/src/reflexcwrapper.cxx
   pypy/branch/reflex-support/pypy/module/cppyy/test/test_cppyy.py
Log:
(arigo, wlav, antocuni, cfbolz): hack some more until we are able to construct
an object (with a complete hack) and call a method on it. This still leaks
memory.


Modified: pypy/branch/reflex-support/pypy/module/cppyy/include/cppyy.h
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/include/cppyy.h	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/include/cppyy.h	Mon Jul  5 18:23:24 2010
@@ -3,5 +3,6 @@
 
 #include "Reflex/Type.h"
 #include "Reflex/Member.h"
+#include "Reflex/Object.h"
 
 #endif // CPPYY_CPPYY

Modified: pypy/branch/reflex-support/pypy/module/cppyy/include/reflexcwrapper.h
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/include/reflexcwrapper.h	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/include/reflexcwrapper.h	Mon Jul  5 18:23:24 2010
@@ -4,6 +4,8 @@
 
 extern "C" {
     long callstatic_l(const char* class_name, const char* method_name, int numargs, void* args[]);
+    void* construct(const char* class_name, int numargs, void* args[]);
+    long callmethod_l(const char* class_name, const char* method_name, void* self, int numargs, void* args[]);
 }
 
 #endif // ifndef CPPYY_REFLEXCWRAPPER

Modified: pypy/branch/reflex-support/pypy/module/cppyy/interp_cppyy.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/interp_cppyy.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/interp_cppyy.py	Mon Jul  5 18:23:24 2010
@@ -28,6 +28,15 @@
     "callstatic_l",
     [rffi.CCHARP, rffi.CCHARP, rffi.INT, rffi.VOIDPP], rffi.LONG,
     compilation_info=eci)
+construct = rffi.llexternal(
+    "construct",
+    [rffi.CCHARP, rffi.INT, rffi.VOIDPP], rffi.VOIDP,
+    compilation_info=eci)
+callmethod_l = rffi.llexternal(
+    "callmethod_l",
+    [rffi.CCHARP, rffi.CCHARP, rffi.VOIDP, rffi.INT, rffi.VOIDPP], rffi.LONG,
+    compilation_info=eci)
+
 
 
 def load_lib(space, name):
@@ -35,6 +44,20 @@
     return W_CPPLibrary(space, cdll)
 load_lib.unwrap_spec = [ObjSpace, str]
 
+def prepare_arguments(space, args_w):
+    args = lltype.malloc(rffi.CArray(rffi.VOIDP), len(args_w), flavor='raw')
+    for i in range(len(args_w)):
+        arg = space.int_w(args_w[i])
+        x = lltype.malloc(rffi.LONGP.TO, 1, flavor='raw')
+        x[0] = arg
+        args[i] = rffi.cast(rffi.VOIDP, x)
+    return args
+
+def free_arguments(args, numargs):
+    for i in range(numargs):
+        lltype.free(args[i], flavor='raw')
+    lltype.free(args, flavor='raw')
+
 class W_CPPLibrary(Wrappable):
     def __init__(self, space, cdll):
         self.cdll = cdll
@@ -56,23 +79,40 @@
         self.name = name
 
     def invoke(self, name, args_w):
-        args = lltype.malloc(rffi.CArray(rffi.VOIDP), len(args_w), flavor='raw')
-        for i in range(len(args_w)):
-            arg = self.space.int_w(args_w[i])
-            x = lltype.malloc(rffi.LONGP.TO, 1, flavor='raw')
-            x[0] = arg
-            args[i] = rffi.cast(rffi.VOIDP, x)
+        args = prepare_arguments(self.space, args_w)
         result = callstatic_l(self.name, name, len(args_w), args)
-        for i in range(len(args_w)):
-            lltype.free(args[i], flavor='raw')
-        lltype.free(args, flavor='raw')
+        free_arguments(args, len(args_w))
         return self.space.wrap(result)
 
     def construct(self, args_w):
-        xxx
+        args = prepare_arguments(self.space, args_w)
+        result = construct(self.name, len(args_w), args)
+        free_arguments(args, len(args_w))
+        return W_CPPObject(self, result)
 
 W_CPPType.typedef = TypeDef(
     'CPPType',
     invoke = interp2app(W_CPPType.invoke, unwrap_spec=['self', str, 'args_w']),
     construct = interp2app(W_CPPType.construct, unwrap_spec=['self', 'args_w']),
 )
+
+
+
+class W_CPPObject(Wrappable):
+    def __init__(self, cppclass, rawobject):
+        self.space = cppclass.space
+        self.cppclass = cppclass
+        self.rawobject = rawobject
+
+    def invoke(self, method_name, args_w):
+        args = prepare_arguments(self.space, args_w)
+        result = callmethod_l(self.cppclass.name, method_name,
+                              self.rawobject, len(args_w), args)
+        free_arguments(args, len(args_w))
+        return self.space.wrap(result)
+
+
+W_CPPObject.typedef = TypeDef(
+    'CPPObject',
+    invoke = interp2app(W_CPPObject.invoke, unwrap_spec=['self', str, 'args_w']),
+)

Modified: pypy/branch/reflex-support/pypy/module/cppyy/src/reflexcwrapper.cxx
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/src/reflexcwrapper.cxx	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/src/reflexcwrapper.cxx	Mon Jul  5 18:23:24 2010
@@ -2,11 +2,30 @@
 #include "reflexcwrapper.h"
 #include <vector>
 
-long callstatic_l(const char* classname, const char* methodname, int numargs, void* args[]) {
+long callstatic_l(const char* class_name, const char* method_name, int numargs, void* args[]) {
     long result;
     std::vector<void*> arguments(args, args+numargs);
-    Reflex::Type t = Reflex::Type::ByName(classname);
-    Reflex::Member m = t.FunctionMemberByName(methodname);
+    Reflex::Type t = Reflex::Type::ByName(class_name);
+    Reflex::Member m = t.FunctionMemberByName(method_name);
     m.Invoke(result, arguments);
     return result;
 }
+
+long callmethod_l(const char* class_name, const char* method_name,
+	          void* self, int numargs, void* args[]) {
+    long result;
+    std::vector<void*> arguments(args, args+numargs);
+    Reflex::Type t = Reflex::Type::ByName(class_name);
+    Reflex::Object o(t, self);
+    o.Invoke(method_name, result, arguments);
+    return result;
+}
+
+void* construct(const char* class_name, int numargs, void* args[]) {
+    std::vector<void*> arguments(args, args+numargs);
+    Reflex::Type t = Reflex::Type::ByName(class_name);
+    Reflex::Object r = Reflex::Object(t, t.Allocate());
+    t.FunctionMemberAt(1).Invoke(r, 0, arguments);
+    return r.Address();
+}
+

Modified: pypy/branch/reflex-support/pypy/module/cppyy/test/test_cppyy.py
==============================================================================
--- pypy/branch/reflex-support/pypy/module/cppyy/test/test_cppyy.py	(original)
+++ pypy/branch/reflex-support/pypy/module/cppyy/test/test_cppyy.py	Mon Jul  5 18:23:24 2010
@@ -20,5 +20,5 @@
     def test_example01method(self):
         t = self.example01.type_byname("example01")
         instance = t.construct(7)
-        res = instance.invoke("add2", 4)
+        res = instance.invoke("add", 4)
         assert res == 11



More information about the Pypy-commit mailing list