[Ur] Calling C functions from Ur
Adam Chlipala
adamc at impredicative.com
Wed Jul 8 08:45:48 EDT 2009
Manu wrote:
> Say, I would like to use a hash function to encrypt users passwords,
> since the Ur Basis library do not (yet) provide such a function, I am
> thinking of using a C library.
> How would one call C functions from Ur, can someone show me an example ?
>
You would use the FFI, which is documented to some extent in the
manual. Some day it would be good to have an Ur wiki that has targeted
pages like one to answer your question.
For now, the relevant documentation is in section 3.1, "Project Files."
The directives related to the C FFI (there is also JavaScript FFI
support) are "ffi," "header," and "link." Checking the manual now, I've
realized that I inadvertently omitted an entry for "header"; that should
be fixed in the next release.
Basically, you build your C library and use "header" and "link" to
specify what header file to include and which object files to link, when
building Ur programs that use your library. You write an Ur .urs file
giving a signature for your library, treated as a module. Then you can
refer to this module just like any other in your Ur sources.
See include/types.h for the types you should use in your C functions,
and see include/urweb.h for the full set of public functions from the
runtime system. There are many potential issues specific to particular
applications, but it sounds like yours is pretty mundane. Especially if
you want to write an FFI function that has side effects, or if you have
a question about how to get the runtime system to do something in
particular, please ask again on this list.
More information about the Ur
mailing list