Up Top       Prev Base Library:      Next CECIL


SmallEiffel Help: External calls

How to call C or Java routines from Eiffel

Read this file if you want to call C or Java code from Eiffel code (to call an Eiffel feature from C or Java see cecil).

The most common way to call external code from Eiffel is to use the external feature definition as described in ETL, page 401ff. See also the SmallEiffel directory lib_show/external to see some examples.

Here is the current list of SmallEiffel `external' specifications. In these specifications, Current is the current instance of the class.

external "C_WithoutCurrent"

A simple call to a C function (or procedure). The external called routine does not get Current. A C prototype is automatically added in the generated header C file.

Note that, for compatibility with other Eiffel compilers, `external "C"' is treated as equivalent to `external "C_WithoutCurrent"'.

external "C_WithCurrent"

A C call that passes Current. The external called routine gets Current as its first argument. A C prototype is automatically added in the generated C header file.

external "C_InlineWithoutCurrent"

An inlined C call. As with "C_WithoutCurrent", the external called routine does not get Current. No prototype is generated in the header file.

external "C_InlineWithCurrent"

An inlined C call with Current. As with "C_WithCurrent", the called routine gets Current as the first argument. No prototype is generated in the header file.

external "SmallEiffel"

This external specification is reserved for SmallEiffel itself. Features which are so defined are directly supported both by compile_to_c and compile_to_jvm. For example, infix "+" of INTEGER is directly mapped as C '+' by compile_to_c. For the same infix "+", compile_to_jvm generates the appropriate Java virtual machine byte code.

external "JVM_invokestatic"

Calling a Java static method. As it is a static Java call, the Eiffel receiver is not used. The "alias" field of the feature is used to indicate the Java class, the Java function name and the corresponding Java bytecode descriptor.

The syntax to use in the alias string is:

alias "<JavaClassPath>.<MethodName> <Descriptor>"
A dot is the separator between <JavaClassPath> and <MethodName>.
A blank character is the separator between <MethodName> and <Descriptor>.

The <JavaClassPath> uses the JVM internal notation as described in JVMS1. For example, when one wants to use java.lang.Math, the <JavaClassPath> is "java/lang/Math". The <MethodName> gives the Java method name. The <Descriptor> gives the internal Java bytecode descriptor as described in JVMS1.

external "JVM_invokevirtual"

Calling a Java virtual method. As for "JVM_invokestatic", the alias string gives all the information necessary to call the virtual function. The Current Eiffel target is not used (only arguments and the result are considered).

Inlining C code

Another way to call C code from Eiffel is to use "c_inline_c" or "c_inline_h" as defined in GENERAL. The argument of "c_inline_c" (or "c_inline_h") must always be a manifest string which contains the C code you want to insert.

Using c_inline_c or c_inline_h also presupposes that you know exactly what C code SmallEiffel will generate. For portability, it is better to use the previously mentioned solutions.



1 - JVMS -
"The Java Virtual Machine Specification",
Tim Lindholm and Frank Yellin, The Java Series,
Addison-Wesley, ISBN 0-201-63452-X.




Up Top       Prev Base Library:      Next CECIL



This page was last changed by Oliver Elphick on 3rd August 1997.