Skip to content

Conversation

@Skgland
Copy link
Contributor

@Skgland Skgland commented Nov 5, 2025

primarily adds functions Machine::register_struct and Machine::register_function

Machine::register_struct

This is the rust counterpart to to ffi:foreign_struct/2 in prolog.
It allows registering a struct that implements the unsafe CustomFfiStruct trait.

Machine::register_function

There is no prolog equivalent.
Related to use_foreign_module/2, tough without loading a dynamic/shared library and one function per call.

https://github.com/Skgland/scryer-prolog/blob/c1a79f3598c8bd56f7d5a8b3769548ee1bd17cea/tests/scryer/ffi.rs#L373-L388

Todo

  • C compatible API
  • WASM compatible API

Help wanted

Future Work

  • a derive macro to automatically implement CustomFfiStruct
  • a predicate in the prolog ffi moduld for ad-hoc calling of functions via function pointers
    something like ffi:call(FnPtr, ArgTypes, RetType, Args, Ret) (suggestion for a better name appreciated) after this PR most things needed should be in place already
  • it might be nice to split use_foreign_module to have seperate predicates for
    • loading a library (get a library handle)
    • looking up symbols in the library (using the library handle)
      • get a function pointer (without defining it as a prolog predicate)
        • can be used with ffi:call/5 above
      • get a pointer to e.g. a static
    • defining prolog predicates for ffi funtion pointer
      • allow defining the predicate in a different module besides ffi
      • allows the predicate to have a name different from the function symbol
        e.g. allow loading plugins that all define the same entry-point/callback functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant