[erlang-questions] Wrapping C libraries in pure Erlang

Denis Bilenko denis.bilenko@REDACTED
Wed Apr 4 12:22:17 CEST 2007


Hello,

Python has a very nice package in its stdlib -- ctypes. It allows
wrapping C libraries in pure Python. ctypes' implementation is based
on libffi, C library for handling dynamic libraries.

I wonder if Erlang could have such library, implemented as a driver on
top of libffi, or any other way that serves the goal. Excluding
intermediate IDL and associated compilation phase
simplifies interop a lot. Surely, one could crash an interpreter more
easily, but that can be mitigated by separating unsafe code in another
node.

A couple examples from ctypes documentation translated into (wishful) Erlang:

%% Functions return int by default
1> cee:call(Libc, time, [null]).
1150640792

%% Parameters' types deduced when there exists a well-defined mapping
2> cee:call(Libc, printf, ["%d bottles of beer\n", 42]).

%% Ambiguity must be resolved by user
3> cee:call(Libc, printf, ["%d bottles of beer\n", 42.5]). % float or double?
** exited: {{nocatch,{argument_error, 2}},
            [{erl_eval,do_apply,5},{shell,exprs,6},{shell,eval_loop,3}]} **

4> cee:call(Libc, printf, ["int %d, double %f\n", 1234, cee:double(3.14)]).
int 1234, double 3.1400001049
31

%% where cee:double is something like
double(N) when is_float(N) -> {c_double, N}.

%% This example is somewhat different from Python's,
%% since Erlang disallows mutable data. Although, that
%% doesn't seem a big problem, as we can make more copies.
5> cee:call(Libc, sscanf, ["1 3.14 Hello", "%d %f %s",
                           output(c_int), output(c_float),
                           output(char_array(100))]).
{3, [1, 3.1400001049, "Hello"]}.

(ctypes has much more than that, including passing python
functions as callbacks)

One useful application would be accessing system calls not
covered by existing BIFs/drivers (native GUI goes in this
category)

I would like to hear any comments, especially from people who
know something about Erlang internals (I don't):
Would it be hard to implement?
Has anyone already thought of something like that?

Denis.



More information about the erlang-questions mailing list