[MLton] Representation of strings in the FFI

Stephen Weeks MLton@mlton.org
Sun, 1 May 2005 00:27:33 -0700


> Since globals are treated as roots by the garbage collector,
> I can use global reference cells to prevent the garbage
> collector to free needed values.

Possibly.  You need to worry about MLton's optimizer being clever
enough to notice that a ref cell's contents are unused and then
removing the contents, or even the ref cell.

> The overall goal is to make it easy to use an MLton library for a
> Scheme programmer knowing (almost) nothing about ML.
...
> the Scheme-ML FFI should automatically convert Scheme values to ML
> values, when a call to an ML functions is made, and the ML return
> value should be converted to a Scheme value.

OK, although copying unbounded-size data structures seems like an
costly approach.  Plus, there are issues with sharing and mutability.

> The conversion of return values from ML to Scheme turned out to be
> easy.
...
> The other way, converting Scheme values to ML values, is tricky due
> to the garbage collector.

I don't think this entirely solves your problem, but perhaps it would
help to expose Scheme's universal datatype to SML via some _import's
and then convert a Scheme value to an ML value by making a single call
to an _export'ed function, thus sidestepping the GC issue.  Of course,
the ML code has to know all the possible types it might deal with.