Commits

Yosuke Onoue  committed 9a8f6e5

dynamic creation of module

  • Participants
  • Parent commits 7c58046

Comments (0)

Files changed (2)

File otypes.py

-#!/usr/bin/env python
-
-import ctypes
-import sys
-
-DOUBLE_TAG = 253
-
-class MLDLL(object):
-    def __init__(self, name):
-        self.lib = ctypes.CDLL(name)
-        ArgArray = ctypes.c_char_p * (len(sys.argv) + 1)
-        args = ArgArray()
-        for i,arg in enumerate(sys.argv):
-            args[i] = ctypes.c_char_p(arg)
-        args[len(sys.argv)] = None
-        self.lib.caml_startup(args)
-        self.lib.caml_named_value.restype = ctypes.POINTER(ctypes.c_long)
-        self.lib.caml_callback.restype = ctypes.c_long
-        self.lib.caml_callback2.restype = ctypes.c_long
-        self.lib.caml_callback3.restype = ctypes.c_long
-        self.lib.caml_alloc.restype = ctypes.c_long
-        #self.lib.caml_Double_val.restype = ctypes.c_double
-
-    def caml_named_value(self, name):
-        return self.lib.caml_named_value(ctypes.c_char_p(name)).contents
-
-    def caml_callback(self, *args):
-        l = len(args)
-        if l == 2:
-            return ctypes.c_long(self.lib.caml_callback(*args))
-        elif l == 3:
-            return ctypes.c_long(self.lib.caml_callback2(*args))
-        elif l == 4:
-            return ctypes.c_long(self.lib.caml_callback3(*args))
-        else:
-            # TODO
-            raise NotImplementedError()
-
-    def caml_alloc(self, size, tag):
-        return ctypes.c_long(self.lib.caml_alloc(size, tag))
-
-    def caml_modify(self, dest, src):
-        return self.lib.caml_modify(dest, src)
-
-    def field(self, x, i):
-        return ctypes.c_long(ctypes.pointer(ctypes.c_long.from_address(x.value))[i])
-
-    def store_field(self, block, offset, val):
-        block = ctypes.c_long(block.value + ctypes.alignment(ctypes.c_long) * offset)
-        return self.caml_modify(block, val)
-
-    def is_long(self, x):
-        return (x.value & 1) != 0
-
-    def is_block(self, x):
-        return (x.value & 1) == 0
-
-    def val_long(self, x):
-        return (x << 1) + 1
-
-    def long_val(self, x):
-        return x.value >> 1
-
-    def val_int(self, x):
-        return self.val_long(x)
-
-    def int_val(self, x):
-        return ctypes.c_int(self.long_val(x))
-
-    def store_double_val(self, v, d):
-        ctypes.c_double.from_address(v.value).value = d
-
-    def double_val(self, v):
-        return ctypes.c_double.from_address(v.value).value
-
-    def val_emptylist(self):
-        return self.val_int(0)

File test/test.py

 #!/usr/bin/env python
 
 import otypes
-from otypes import MLDLL
-
-print 'importing mllibrary'
-lib = MLDLL('sample.so')
+otypes.make('sample.so', 'sample')
+import sample
+import sample as lib
 
 print 'test1'
 x = lib.caml_named_value('x')
-print lib.int_val(x)
+print lib.int_val(x).value
 print
 
 print 'test2'
 f = lib.caml_named_value('f')
 res = lib.caml_callback(f, x)
-print lib.int_val(res)
+print lib.int_val(res).value
 print
 
 print 'test3'
 print
 
 print 'test4'
-d = lib.caml_alloc(1, otypes.DOUBLE_TAG)
+d = lib.caml_alloc(1, sample.DOUBLE_TAG)
 lib.store_double_val(d, 0.3)
 print lib.double_val(d)
 print
 
 print 'test5'
 ab = lib.caml_named_value('tuple')
-print chr(lib.int_val(lib.field(ab, 0)))
-print chr(lib.int_val(lib.field(ab, 1)))
+print chr(lib.int_val(lib.field(ab, 0)).value)
+print chr(lib.int_val(lib.field(ab, 1)).value)
 print
 
 print 'test6 passing tuple'
 lib.store_field(arg, 1, lib.val_int(8))
 lib.store_field(arg, 2, lib.val_int(5))
 ret = lib.caml_callback(max3, arg)
-print lib.int_val(ret)
+print lib.int_val(ret).value
 print
 
 print 'test7 sorting array'
     lib.store_field(ml_array, i, lib.val_int(item))
 lib.caml_callback(sort_array, ml_array)
 for i in range(len(l)):
-    print lib.int_val(lib.field(ml_array, i))
+    print lib.int_val(lib.field(ml_array, i)).value
 print
 
 print 'test8 sorting list'
 tl = cons(lib.val_int(46), tl)
 l = cons(lib.val_int(58), tl)
 l = lib.caml_callback(sort_list, l)
-print lib.int_val(lib.field(l, 0))
+print lib.int_val(lib.field(l, 0)).value
 l = lib.field(l, 1)
-print lib.int_val(lib.field(l, 0))
+print lib.int_val(lib.field(l, 0)).value
 l = lib.field(l, 1)
-print lib.int_val(lib.field(l, 0))
+print lib.int_val(lib.field(l, 0)).value