I made a non-richly-typed FFI module a while back (https://benjamin.barenblat.name/git/urweb-regex.git, mirrored at https://github.com/bbarenblat/urweb-regex). You may be interested in using it as a base for your work.