I notice that the buildClosure<N>withAbi functions take the SML function to call back to in the same tuple as the type of the callback arguments and return value. Consequently the interface for constructing closures using doesn't allow the same CIF to be used for multiple callbacks to different SML functions (of the same type). Is that deliberate or could there be some advantage in allowing that?
For example, we could have:
val buildClosure2withAbi: LibFFI.abi * ('a conversion * 'b conversion) * 'c conversion -> ('a * 'b -> 'c) -> ('a * 'b -> 'c) closure
fun buildClosure2withAbi ( abi: abi, (arg1Conv: 'a conversion, arg2Conv: 'b conversion), resConv: 'c conversion ) : ('a * 'b -> 'c) -> ('a * 'b -> 'c) closure = let fun callback f (args, res) = ...
val argTypes = [#ctype arg1Conv, #ctype arg2Conv] and resType = #ctype resConv
val makeCallback = cFunctionWithAbi abi argTypes resType in fn f => Memory.memoise (fn () => makeCallback(callback f)) () end
Then, at the top-level, an application could define e.g.
val binOpClosure = buildClosure2 ((cDouble, cDouble), cDouble) val addClosure = binOpClosure Real.+ val subtractClosure = binOpClosure Real.- ...
I don't know enough about libffi to know whether that is useful or would be problematic.
Phil
On 16/12/2015 12:35, David Matthews wrote:
There have been a few changes to the Foreign structure. The callN functions have been renamed as buildCallN and the way functions are passed as arguments has been changed.
The reason for the changes is to make clear that the expensive operations are creating the C functions and closures and that calling a C function or passing a constructed closure are comparatively cheap.
It is important to call the buildXXX functions at the top-level e.g. in a structure, so that the C function is created once. The old callN functions were curried and it wasn't apparent that the partial application to the conversions was quite different to the application of this to the arguments. For that reason the buildXXX take a tuple.
David
polyml mailing list polyml at inf.ed.ac.uk http://lists.inf.ed.ac.uk/mailman/listinfo/polyml