On Sun, 2006-10-29 at 12:34 +1100, Jonathan Kelly wrote:

> Also, how do I define an external function that takes a "char *" or 
> "const char *".


BTW: I'm not 100% sure what you mean by 'define an external function'.

If you mean how do you *wrap* (or lift) a C function into 
Felix, there are two distinct ways:

First, to call the function you have the usual wrapper stuff:

        fun sin: double -> double;

This just means

        fun sin: double -> double = "sin($1)";

When you use this like:

        print$ sin 1.2;

Felix just emits

        sin(1.2)

by physically substituting the C representation of the argument
expression into the text of the function: this is why Felix
cannot use continuations for functions: they have to input
and output ordinary nestable C expressions.

When you try to pass 'sin' as a function:

        fun apl (f: double -> double, x: double) => f x;
        print$ apl (the sin, 1.2);

Felix automatically generates a *Felix* function wrapper
to act as a closure (although hopefully this does NOT
happen in the example .. the closure should be optimised
away). The wrapper is basically:

        fun wrapper_sin(x:double)=> sin x;

Lol! But that generates a C++ class of the usual kind
with an apply method that looks like

        double wrapper_sin::apply(x:double) { return sin x; }

This is all very well but you cannot pass such a function
to a C function which requires a function argument, that is,
a C callback!

If you wish to use a C function as a callback there is
another way:

        const sin: double --> double = "sin";

The type denoted by

        d --> c

is a C function pointer. You can apply it in the usual way:

        print$ sin x;

will work. But you cannot pass it where a Felix function is
expected, because it has a different type (it is a 
C function pointer, Felix functions are C++ classes).

Of course the workaround is just

        fun sin_wrapper (x:double)=>sin x;

which is a Felix function.

It is ALSO possible to actually use Felix functions as callbacks,
but only if the C external functions support passing client
data as a void*. In that case, a fixed callback wrapper is used
as a proxy callback, which converts the void *client_data argument
into a Felix function with a cast, then applies it to the
argument(s) using its apply() method.

Flxcc handles this automatically. Unfortunately it is very
messy. I introduced a new feature to Felix:

http://felix.sourceforge.net/doc/tutorial/embedding/en_flx_tut_bind_top.html

to try to fix this. Unfortunately it doesn't work as 
I had expected because the type of a callback generated
with the callback keyword is NOT the same as the type
the C function expects: the Felix version has type

        argtype --> restype

but the C function expects a void*. If I downgrade the type
to a void*, Felix would not only fail to be typesafe,
it also wouldn't actually be able to apply the callback
in ordinary Felix code (because that depends on it having
the right type). This would be 'fixed' if I could insert
a cast at the right point in the generated code, but I can't:
the cast is only needed calling an external C function, not
when calling a Felix lift of that same C function,
and there's no way to tell the difference.  

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to