[MLton] Representation of strings in the FFI

Jens Axel Søgaard jensaxel@soegaard.net
Wed, 27 Apr 2005 13:14:18 +0200


Jens Axel Søgaard wrote:
> Stephen Weeks wrote:
> 
>> It is quite possible.  The exported string is both a char* and a
>> pointer to a MLton array, from which one can extract the length using
>> GC_arrayNumElements.
> 
> Ok - the layout of arrays were described in runtime/gc.h. The length
> and other GC info is to the left of contents of the array (or string).
> I take it, I should use GC_arrayNumElementsp at the C level.

Here is a small example:

   val e2 = _export "bar" : int array -> int array;
   val _  = e2 (fn (x) => Array.fromList [1, 2, 3]);

the generated C code is:

   Pointer MLton_FFI_Pointer_array[1];
   Pointer *MLton_FFI_Pointer = &MLton_FFI_Pointer_array;
   Int MLton_FFI_op;

   Pointer bar (Pointer x0) {
           MLton_FFI_op = 1;
           MLton_FFI_Pointer_array[0] = x0;
           MLton_callFromC ();
           return MLton_FFI_Pointer_array[0];
   }

Suppose I at the C level want to produce an MLton array and
then call bar with it. Is GC_arrayAllocate the best way of allocating
the array?

-- 
Jens Axel Søgaard