Use another language to call a function: Difference between revisions

m
Fixed lang tags.
(Added HaXe language)
m (Fixed lang tags.)
Line 27:
=={{header|Ada}}==
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;
 
Line 35 ⟶ 34:
return int;
pragma Export (C, Query, "Query");
end Exported;</lang>
</lang>
The package implementation:
<lang Ada>package body Exported is
package body Exported is
function Query (Data : chars_ptr; Size : access size_t)
return int is
Line 52 ⟶ 49:
end if;
end Query;
end Exported;</lang>
</lang>
With [[GNAT]] it can be built as follows:
<lang shada>gcc -c main.c
gcc -c main.c
gnatmake -c exported.adb
gnatbind -n exported.ali
gnatlink exported.ali main.o -o main</lang>
</lang>
Sample output:
<pre>
Line 68 ⟶ 62:
=={{header|HaXe}}==
=== PHP ===
<prelang haxe>untyped __call__("functionName", args);</prelang>
 
=={{header|Lisaac}}==
query.li
<lang Lisaac>Section Header
Section Header
 
+ name := QUERY;
Line 104 ⟶ 97:
+ size : NATIVE_ARRAY[INTEGER];
query(buffer, size); // need this to pull the query() method
);</lang>
</lang>
Makefile
<lang makelisaac>TARGET=test_query
TARGET=test_query
 
all: $(TARGET)
Line 122 ⟶ 113:
 
clean:
rm -f $(TARGET) *.o query.c</lang>
</lang>
 
=={{header|OCaml}}==
<lang Cocaml>#include <stdio.h>
#include <string.h>
#include <caml/mlvalues.h>
Line 172 ⟶ 162:
;;</lang>
 
<lang shocaml>ocamlopt -output-obj caml_part.ml -o caml_part_obj.o
gcc -c main.c -I"`ocamlc -where`"
gcc -o prog.opt main.o caml_part_obj.o \
Line 182 ⟶ 172:
===‘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:
<lang ctcl>int Query (char * Data, size_t * Length) {
Tcl_Obj *arguments[2];
int code;
Line 211 ⟶ 201:
===‘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:
<lang ctcl>int Query (char * Data, size_t * Length) {
const char *str;
int len;
Line 232 ⟶ 222:
===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]].
<lang ctcl>#include <tcl.h>
Tcl_Interp *interp;
 
Anonymous user