[m-users.] C foreign language interface question
Julien Fischer
jfischer at opturion.com
Thu Apr 22 17:23:12 AEST 2021
Hi Zoltan,
On Thu, 22 Apr 2021, Zoltan Somogyi wrote:
> On Wed, 21 Apr 2021 15:17:52 +0200, fabrice nicol <fabrnicol at gmail.com> wrote:
>
>> Perhaps a few words on such issues would be useful in the FLI sections
>> of the Reference manual. Incidentally, perhaps this manual could
>> document MR_ArrayPtr in 15.3.1 (used in array.m). It may be of interest
>> when Mercury arrays are passed along to the C FFI (as in ML_resize_array
>> for example).
>
> The Mercury reference manual documents how to pass atomic data
> between Mercury and target languages such as C. It deliberately does not
> document how to pass compound data structures, because we did not want
> to be locked into supporting design decisions that we may need to change.
> (We do document how to pass lists between languages, precisely because
> we are pretty sure that representation is set in stone.)
>
> The idea is that if you have e.g. a Mercury array, and you want to
> give access to it to C code, then you can export to C some functions
> that implement basic operations (such as lookup and update) on the array.
> In these operations, the arguments holding the arrays will be MR_Words,
> effectively black boxes that the C code is not supposed to understand
> or change.
>
> If you want to teach C code about how Mercury arrays are implemented,
> then (a) congratulations, you are now a Mercury implementor, and (b)
> commiserations, you will need to update your code whenever the internals
> of Mercury arrays are changed by anybody. They have been stable
> for a long time, but they have changed in the past, and we can't promise
> they won't change again.
MR_ArrayType has had its present structure since at least 1997*. The
only changes made to since then were the addition of the MR_ prefix on
the field types and moving it of runtime/mercury_type_info.h** into its
present location.
* it possibly had the same structure before that, but the runtime was
restructured in 1997 and tracking where things came from is too much
bother.
** which seems a very odd place for it to reside.
Interfacing with Mercury arrays in C code is useful enough that I
think we should document it. Yes, the elements are MR_Words but
that is also the case for list elements.
Julien.
More information about the users
mailing list