| Author |
Message |
|
| ng at snipsnap.edfac.unim |
Posted: Thu Aug 19, 1999 12:40 pm |
|
|
|
Guest
|
G'day,
I am trying to export some C functions to Erlang and at the same time getting
those C functions to invoke Erlang functions in the same program. Like that
of the calback functions. How can this be done in IC ?? if possible at all...
thanks,
mark
Post generated using Mail2Forum (http://m2f.sourceforge.net) |
|
|
| Back to top |
|
| tobbe at serc.rmit.edu.au |
Posted: Thu Aug 19, 1999 2:12 pm |
|
|
|
Guest
|
> I am trying to export some C functions to Erlang and at the same time getting
> those C functions to invoke Erlang functions in the same program. Like that
> of the calback functions. How can this be done in IC ?? if possible at all...
Sorry, I know I shouldn't promote IG (since it is obsolete) but
I can't help myself. Actually I would also be interested in how
you would do it with IC. Anyway, here is how you do it with IG:
example.h
--------------------------------------------------------------
#ifndef _EXAMPLE_H
#define _EXAMPLE_H
#include <stdio.h>
#ifdef HIDE
/*
* C functions we want to call from Erlang
*/
IG_fun FILE *fopen(IG_string path, IG_string mode);
IG_fun int fclose( FILE *);
#endif
#endif
--------------------------------------------------------------
Run this through IG:
erl-shell> ig:gen(example).
Compile the result:
gcc -ansi -pedantic -Wall
-I /usr/local/lib/erlang/usr/include
-L /usr/local/lib/erlang/usr/lib
-o example example_stub.c
/usr/local/lib/erlang/usr/lib/igio.o
/usr/local/lib/erlang/usr/lib/igmain.o -lerl_interface
erlc example.erl
Now you can call those functions from Erlang:
--------------------------------------------------------------
23:57 ~/junk> ls
example* example.beam example.erl example.h example_stub.c
23:57 ~/junk> erl
Erlang (BEAM) emulator version 47.4.1
Eshell V47.4.1 (abort with ^G)
1> S=example:start().
<0.30.0>
2> {ok,Fd} = example:fopen(S,"TOUCH_ME","wb").
{ok,134618608}
3> example:fclose(S,Fd).
{ok,0}
4>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
(v)ersion (k)ill (D)b-tables (d)istribution
a
23:58 ~/junk> ls
TOUCH_ME example* example.beam example.erl example.h example_stub.c
23:58 ~/junk>
--------------------------------------------------------------
To call an Erlang function from C is a bit more complicated,
but basically: add the following to example.h:
--------------------------------------------------------------
/*
* Erlang function we want to call from C
*/
typedef struct { /* First a data type to store */
int hour; /* the result from erlang:time/0 */
int minute;
int second;
} etime;
IG_call etime get_etime(void);
--------------------------------------------------------------
Run IG again. This time you will also get an 'example.hrl' out
from IG. As per default, IG will assume you are implementing
your callback Erlang functions in example_cb.erl, so create
this file:
--------------------------------------------------------------
-module(example_cb).
-export([get_etime/1]).
-include("example.hrl").
get_etime(_Pid) ->
{H,M,S} = time(),
#etime{hour = H, minute = M, second = S}.
--------------------------------------------------------------
The hardest part is to write a C program that can trigger
this code. I leave this as an exercise for you... ;-)
Cheers /Tobbe
Post generated using Mail2Forum (http://m2f.sourceforge.net) |
|
|
| Back to top |
|
| ng at snipsnap.edfac.unim |
Posted: Fri Aug 20, 1999 12:23 pm |
|
|
|
Guest
|
On Fri, Aug 20, 1999 at 12:12:29AM +1000, Torbjorn Tornkvist wrote:
>
> Sorry, I know I shouldn't promote IG (since it is obsolete) but
I have played with it a bit, and like it more than IC for some
tasks. :)
> I can't help myself. Actually I would also be interested in how
> you would do it with IC. Anyway, here is how you do it with IG:
>
<example snipped>
Thanks for the example, but I am really after is something like
where I can pass a lambda to a C function and get that C function
to invoke this piece of Erlang code.
The IC manual has examples on how to call "static" Erlang functions
from C, but not dynamic ones.
mark
Post generated using Mail2Forum (http://m2f.sourceforge.net) |
|
|
| Back to top |
|
| dgud at erix.ericsson.se |
Posted: Fri Aug 20, 1999 1:49 pm |
|
|
|
Guest
|
Something like this should be doable I guess.
Define in IDL:
#include <erlang.idl>
module dynamic
{
interface dyn_erl
{
Status erlApply(in string module,
in string function,
in erlang::term listOfArgs,
in out erlang::term res);
}
}
And in the erlang callback module:
erl_apply(State, Mod, Fun, Args) ->
case catch apply(list_to_atom(Mod),
list_to_atom(Fun), Args) of
{'EXIT', R} ->
{reply, {error, []}, State}};
Result ->
{reply, {ok, Result}, State}}
end.
In 'C' the call would look something like
ETERM * result, empty;
empty = erl_mk_empty_list();
status = dynamic_erlApply(NULL, "erlang", "date", empty, &result, env);
See the erl_interface doc about handling erlang data types in C.
/Dan
Mark NG writes:
> On Fri, Aug 20, 1999 at 12:12:29AM +1000, Torbjorn Tornkvist wrote:
> >
> > Sorry, I know I shouldn't promote IG (since it is obsolete) but
>
> I have played with it a bit, and like it more than IC for some
> tasks.
>
>
> > I can't help myself. Actually I would also be interested in how
> > you would do it with IC. Anyway, here is how you do it with IG:
> >
>
> <example snipped>
>
> Thanks for the example, but I am really after is something like
> where I can pass a lambda to a C function and get that C function
> to invoke this piece of Erlang code.
>
> The IC manual has examples on how to call "static" Erlang functions
> from C, but not dynamic ones.
>
>
> mark
>
--
Dan Gudmundsson Project: Mnesia, Erlang/OTP
Ericsson Utvecklings AB Phone: +46 8 727 5762
UAB/F/P Mobile: +46 70 519 9469
S-125 25 Stockholm Visit addr: Armborstv 1
Post generated using Mail2Forum (http://m2f.sourceforge.net) |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|