Commits

ariovistus  committed 869a3d9

looks like pypy 2.0 will have some hooks we can use.

also, dmd 2.063 will be able to build shared libs on linux
that have working unittests and shared ctors!

  • Participants
  • Parent commits e101797

Comments (0)

Files changed (9)

File examples/misc/cffi/boilerplate.d

+extern(C) shared bool _D2rt6dmain212_d_isHaltingOb;
+alias _D2rt6dmain212_d_isHaltingOb _d_isHalting;
+extern(C) {
+
+    void rt_init();
+    void rt_term();
+
+    void my_init() {
+        rt_init();
+    }
+
+    void my_fini() {
+        if(!_d_isHalting){
+            rt_term();
+        }
+    }
+    void _Dmain(){}
+
+}
+

File examples/misc/cffi/common.py

+from ctypes import *
+dll = None
+LIST = []
+def reg_fun(nom, fun):
+    LIST.append(fun)
+    dll.pyd_reg_fun(nom, fun)
+UTF8CONV = CFUNCTYPE(py_object, POINTER(c_char), c_size_t)
+def utf8_to_str(_buffer, _len):
+    bz = bytearray(_len)
+    for i in xrange(_len): bz[i] = _buffer[i];
+    return unicode(str(bz), 'utf8' )
+LCONV = CFUNCTYPE(py_object, c_longlong)
+LUCONV = CFUNCTYPE(py_object, c_ulonglong)
+def my_id(l): return l
+FUNC2 = CFUNCTYPE(py_object, py_object, py_object)
+def get_item(thing, index): return thing[index]

File examples/misc/cffi/makefile

+BITSI = 
+ifeq ($(BITSI), 32)
+DMD_M=-m32
+GCC_M=-m32
+else	
+ifeq ($(BITSI), 64)
+DMD_M=-m64
+GCC_M=-m64
+else
+DMD_M=
+GCC_M=
+endif
+endif
+DMD=dmd $(DMD_M)
+GCC=gcc $(GCC_M)
+PYTHON = pypy
+
+test1: libtest1.so test1.py
+	$(PYTHON) test1.py
+so_ctor.o: so_ctor.c
+	$(GCC) -c -fPIC so_ctor.c -o so_ctor.o
+
+libtest1.so: test1.d boilerplate.d so_ctor.o 
+	$(DMD) -unittest -fPIC -defaultlib=phobos2so -shared $^ -of$@

File examples/misc/cffi/so_ctor.c

+void my_init();
+void my_fini();
+
+__attribute__((__constructor__)) void actual_init() {
+    my_init();
+}
+

File examples/misc/cffi/test1.d

+import std.exception;
+import std.typetuple;
+import std.conv;
+import std.traits;
+import std.string;
+
+extern(C) int int_test(int i, int j) {
+    return 2*i + 3*j - 5;
+}
+
+extern(C) double float_test(double d, double e) {
+    return 2.5 * d + 1.25 * e;
+}
+
+extern(C) string str_test(string s, string t) {
+    return format("I believe in %s, and banging two %s together!", s, t);
+}

File examples/misc/cffi/test1.py

+from cffi import FFI
+import ctypes.util;
+import os, os.path
+
+ffi = FFI()
+
+ffi.cdef("""
+        int int_test(int,int);
+        """);
+ffi.cdef("""
+        double float_test(double,double);
+        """);
+ffi.cdef("""
+        typedef struct {
+            size_t length;
+            char *ptr;
+        } DString;
+        DString str_test(DString,DString);
+        """)
+doh = ffi.dlopen(os.path.abspath("libtest1.so"))
+
+
+print (doh.int_test(1,2));
+print (doh.float_test(100,1));
+def DString(str_):
+    return {'length': len(str_), 'ptr':ffi.new("char[]",str_)}
+s1 = ffi.new("DString *")
+s1.ptr = ffi.new("char[]", "world peace")
+s1.length = len("world peace")
+s2 = ffi.new("DString *")
+s2.ptr = ffi.new("char[]", "bricks")
+s2.length = len("bricks")
+
+mystr = (doh.str_test(DString("world peace"),DString("world peace")))
+print mystr.length
+print mystr.ptr
+
+

File examples/misc/dmd_sharedlibs/boilerplate.d

             rt_term();
         }
     }
+    void _Dmain(){}
 
 }
 
-// (*^*&%^(* druntime
-
-extern(C) void _Dmain(){}

File examples/misc/dmd_sharedlibs/makefile

 	rm -f *.x
 
 lib%.so: %.d boilerplate.d so_ctor.o 
-	$(DMD) -unittest -fPIC -shared $^ -of$@
+	$(DMD) -unittest -fPIC -defaultlib=phobos2so -shared $^ -of$@
 so_ctor.o: so_ctor.c
-	$(GCC) -c so_ctor.c -o so_ctor.o
+	$(GCC) -c -fPIC so_ctor.c -o so_ctor.o
 
 test1.x: test1.c libtest1.so
 	$(GCC) $< `pwd`/libtest1.so -o $@
-RC1
+0.1