Use another language to call a function: Difference between revisions

Content added Content deleted
(Added HaXe language)
m (Fixed lang tags.)
Line 27: Line 27:
=={{header|Ada}}==
=={{header|Ada}}==
The interface package Exported specification:
The interface package Exported specification:
<lang Ada>with Interfaces.C; use Interfaces.C;
<lang Ada>
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
with Interfaces.C.Strings; use Interfaces.C.Strings;


Line 35: Line 34:
return int;
return int;
pragma Export (C, Query, "Query");
pragma Export (C, Query, "Query");
end Exported;
end Exported;</lang>
</lang>
The package implementation:
The package implementation:
<lang Ada>
<lang Ada>package body Exported is
package body Exported is
function Query (Data : chars_ptr; Size : access size_t)
function Query (Data : chars_ptr; Size : access size_t)
return int is
return int is
Line 52: Line 49:
end if;
end if;
end Query;
end Query;
end Exported;
end Exported;</lang>
</lang>
With [[GNAT]] it can be built as follows:
With [[GNAT]] it can be built as follows:
<lang sh>
<lang ada>gcc -c main.c
gcc -c main.c
gnatmake -c exported.adb
gnatmake -c exported.adb
gnatbind -n exported.ali
gnatbind -n exported.ali
gnatlink exported.ali main.o -o main
gnatlink exported.ali main.o -o main</lang>
</lang>
Sample output:
Sample output:
<pre>
<pre>
Line 68: Line 62:
=={{header|HaXe}}==
=={{header|HaXe}}==
=== PHP ===
=== PHP ===
<pre>untyped __call__("functionName", args);</pre>
<lang haxe>untyped __call__("functionName", args);</lang>


=={{header|Lisaac}}==
=={{header|Lisaac}}==
query.li
query.li
<lang Lisaac>
<lang Lisaac>Section Header
Section Header


+ name := QUERY;
+ name := QUERY;
Line 104: Line 97:
+ size : NATIVE_ARRAY[INTEGER];
+ size : NATIVE_ARRAY[INTEGER];
query(buffer, size); // need this to pull the query() method
query(buffer, size); // need this to pull the query() method
);
);</lang>
</lang>
Makefile
Makefile
<lang make>
<lang lisaac>TARGET=test_query
TARGET=test_query


all: $(TARGET)
all: $(TARGET)
Line 122: Line 113:


clean:
clean:
rm -f $(TARGET) *.o query.c
rm -f $(TARGET) *.o query.c</lang>
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
<lang C>#include <stdio.h>
<lang ocaml>#include <stdio.h>
#include <string.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/mlvalues.h>
Line 172: Line 162:
;;</lang>
;;</lang>


<lang sh>ocamlopt -output-obj caml_part.ml -o caml_part_obj.o
<lang ocaml>ocamlopt -output-obj caml_part.ml -o caml_part_obj.o
gcc -c main.c -I"`ocamlc -where`"
gcc -c main.c -I"`ocamlc -where`"
gcc -o prog.opt main.o caml_part_obj.o \
gcc -o prog.opt main.o caml_part_obj.o \
Line 182: Line 172:
===‘In’ Parameters===
===‘In’ Parameters===
To connect a function to Tcl that passes an arbitrary C string as input, you'd use a short C thunk, like this:
To connect a function to Tcl that passes an arbitrary C string as input, you'd use a short C thunk, like this:
<lang c>int Query (char * Data, size_t * Length) {
<lang tcl>int Query (char * Data, size_t * Length) {
Tcl_Obj *arguments[2];
Tcl_Obj *arguments[2];
int code;
int code;
Line 211: Line 201:
===‘Out’ Parameters===
===‘Out’ Parameters===
However, in the specific case of writing to a user-specified buffer (an “out” parameter) the thunk code would instead manage copying the result from the interpreter back to the buffer:
However, in the specific case of writing to a user-specified buffer (an “out” parameter) the thunk code would instead manage copying the result from the interpreter back to the buffer:
<lang c>int Query (char * Data, size_t * Length) {
<lang tcl>int Query (char * Data, size_t * Length) {
const char *str;
const char *str;
int len;
int len;
Line 232: Line 222:
===Connecting up the pieces===
===Connecting up the pieces===
You would also need a short piece of code in <code>main()</code> to initialize the Tcl library and create an interpreter instance, and you would need to build and link against [[libtcl]].
You would also need a short piece of code in <code>main()</code> to initialize the Tcl library and create an interpreter instance, and you would need to build and link against [[libtcl]].
<lang c>#include <tcl.h>
<lang tcl>#include <tcl.h>
Tcl_Interp *interp;
Tcl_Interp *interp;