[m-users.] Seeking some feedback on FFI style...

Sean Charles (emacstheviking) objitsu at gmail.com
Sun Aug 7 16:53:09 AEST 2022


Thanks Julien, some obvious errors and some finer points.
The first proc I hand-rolled, in my shame, I made an Espanso macro and sadly boiler plated the rest with out due care and attention! Tut tut.

That's excellent feedback, thanks again.
Sean
:)


> On 7 Aug 2022, at 06:34, Julien Fischer <jfischer at opturion.com> wrote:
> 
> 
> Hi,
> 
> On Sat, 6 Aug 2022, Sean Charles (emacstheviking) wrote:
> 
>> In an ongoing effort to not forget things as I learn, I just added to
>> my collection of mercury samples with this:
>> https://github.com/emacstheviking/mercury-library-samples/blob/main/FFI/sample.m
>> I just wondered if, by the final version, if the code could be shorter
>> / cleaner / mopre elegant etc and also, more importantly, is there
>> anything I've done wrong, not done as well as it could have been in
>> terms of efficiency ?
> 
> 1. The will_not_throw_exception attribute is meaningless on
> foreign_procs that also set the will_not_call_mercury attribute; you can
> omit it.
> 
> 2. Speaking of which, you have set the attribute will_not_call_mercury
> for the precdicate filestat4/4. However, the foreign_proc for that
> predicate *does* call Mercury, e.g. via the exported predicate
> statResponse().
> 
> 3. Ditto for filestat3/4, which calls Mercury via makeError() but has
> the will_not_call_mercury attribute set.
> 
> 4. I would handle the I/O state in foreign_procs via don't-care
> variables instead of having an assignment in the body of the
> foreign_proc.  E.g. do this.
> 
>    :- pragma foreign_proc("C",
>        foo(_IO0::di, _IO::uo),
>        [promise_pure, will_not_call_mercury, etc etc],
>    "
>      ...
>    ").
> 
> rather than this:
> 
>    :- pragma foreign_proc("C",
>        foo(IO0::di, IO::uo),
>        [promise_pure, will_not_call_mercury, etc etc],
>    "
>      ...
>      IO = IO0;
>    ").
> 
> The former approach saves a line. In the past the latter approach
> has also triggered warnings from some C compilers.
> 
> 5. You may want to look into the settings the tabled_for_io attribute
> on foreign_procs that do I/O if you want to be able to use the debugger
> with such code.
> 
> Julien.



More information about the users mailing list