Commits

Yosuke Onoue committed b11a548

add caml_copy_string_array, caml_alloc_small, caml_alloc_shr

  • Participants
  • Parent commits f46e4c1

Comments (0)

Files changed (2)

File template/otypes.py

 #define Custom_tag 255
 CUSTOM_TAG = 255
 
+#define Max_young_wosize 256
+MAX_YOUNG_WOSIZE = 256
+
 class ml_intnat(ctypes.c_long):
     '''
     typedef long intnat;
     '''
     return context.caml_alloc(size, tag)
 
-def caml_alloc_small(size, tag):
-    '''
-    CAMLextern value caml_alloc_small (mlsize_t, tag_t);
-    '''
-    return context.caml_alloc(size, tag)
-
 def caml_alloc_tuple(size):
     '''
     CAMLextern value caml_alloc_tuple (mlsize_t);
     '''
     return context.caml_copy_nativeint(ml_intnat(x))
 
+def caml_alloc_array(f, a):
+    '''
+    caml_alloc_array(f, a) allocates an array of values, calling function f over each element of input sequence a to transform it into a value.
+
+    CAMLextern value caml_alloc_array (value (*funct) (char const *), char const ** array);
+    '''
+    raise NotImplementedError
+
+def caml_copy_string_array(strings):
+    '''
+    caml_copy_string_array(p) allocates an array of strings, copied from Python strings.
+
+    CAMLextern value caml_copy_string_array (char const **);
+    '''
+    array = (ctypes.c_char_p * (len(strings) + 1))()
+    for i, string in enumerate(strings):
+        array[i] = string
+    array[-1] = None
+    return context.caml_copy_string_array(array)
+
+def caml_alloc_small(size, tag):
+    '''
+    caml_alloc_small(n, t) returns a fresh small block of size n <= Max_young_wosize words, with tag t.
+
+    CAMLextern value caml_alloc_small (mlsize_t, tag_t);
+    '''
+    return context.caml_alloc_small(size, tag)
+
+def caml_alloc_shr(size, tag):
+    '''
+    caml_alloc_shr(n, t) returns a fresh block of size n, with tag t.
+
+    CAMLextern value caml_alloc_shr (mlsize_t, tag_t);
+    '''
+    return context.caml_alloc_shr(size, tag)
+
 def caml_modify(dest, src):
     return context.caml_modify(dest, src)
 

File test/test.py

         pass
 
     def test_caml_alloc(self):
-        block = lib.caml_alloc(3, lib.DOUBLE_TAG)
+        block = lib.caml_alloc(3, lib.DOUBLE_ARRAY_TAG)
         self.assertEqual(lib.wosize_val(block), 3)
-        self.assertEqual(lib.tag_val(block), lib.DOUBLE_TAG)
+        self.assertEqual(lib.tag_val(block), lib.DOUBLE_ARRAY_TAG)
 
     def test_caml_alloc_tuple(self):
         block = lib.caml_alloc_tuple(2)
         pass
 
     def test_caml_copy_string_array(self):
-        pass
+        v = lib.caml_copy_string_array(['spam', 'ham', 'egg'])
+        self.assertEqual(lib.wosize_val(v), 3)
+        self.assertEqual(lib.string_val(lib.field(v, 0)), 'spam')
+        self.assertEqual(lib.string_val(lib.field(v, 1)), 'ham')
+        self.assertEqual(lib.string_val(lib.field(v, 2)), 'egg')
 
     def test_caml_alloc_small(self):
-        pass
+        block = lib.caml_alloc_small(100, lib.DOUBLE_ARRAY_TAG)
+        self.assertEqual(lib.wosize_val(block), 100)
+        self.assertEqual(lib.tag_val(block), lib.DOUBLE_ARRAY_TAG)
 
     def test_caml_alloc_shr(self):
-        pass
+        block = lib.caml_alloc_small(300, lib.DOUBLE_ARRAY_TAG)
+        self.assertEqual(lib.wosize_val(block), 300)
+        self.assertEqual(lib.tag_val(block), lib.DOUBLE_ARRAY_TAG)
 
     def test_caml_callback(self):
         a = lib.val_int(1)