Call a foreign-language function: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 17:
*   [[Use another language to call a function]]
<br><br>
 
=={{header|8th}}==
<lang forth>
\ tell 8th what the function expects:
"ZZ" "strdup" func: strdup
"VZ" "free" func: free
\ call the external funcs
"abc" dup \ now we have two strings "abc" on the stack
strdup .s cr \ after strdup, you'll have the new (but duplicate) string on the stack
\ the ".s" will show both strings and you can see they are different items on the stack
free \ let the c library free the string
</lang>
 
=={{header|Ada}}==
Ada provides standard interfaces to [[C]], [[C++]], [[Fortran]] and [[Cobol]]. Other language interfaces can be provided as well, but are not mandatory. Usually it is possible to communicate to any language that supports calling conventions standard to the [[OS]] ('''cdecl''', '''stdcall''' etc).
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
 
procedure Test_C_Interface is
function strdup (s1 : Char_Array) return Chars_Ptr;
pragma Import (C, strdup, "_strdup");
 
S1 : constant String := "Hello World!";
S2 : Chars_Ptr;
begin
S2 := strdup (To_C (S1));
Put_Line (Value (S2));
Free (S2);
end Test_C_Interface;</lang>
 
=={{header|Aikido}}==
There are two ways to call a <em>native</em> function in Aikido. The first is to write a wrapper function in C++ that is invoked from the Aikido interpreter. In a C++ file:
<lang aikido>#include <aikido.h>
extern "C" { // need C linkage
 
// define the function using a macro defined in aikido.h
AIKIDO_NATIVE(strdup) {
aikido::string *s = paras[0].str;
char *p = strdup (s->c_str());
aikido::string *result = new aikido::string(p);
free (p);
return result;
}
 
}</lang>
 
Then in the Aikido program:
<lang aikido>native function strdup(s)
println (strdup ("Hello World!"))</lang>
 
The second way is to use a <em>raw native</em> function. These functions must adhere to a defined set of rules and can be called directly from the Aikido interpreter. In the case of <code>strdup</code> we need to play a nasty trick because it returns a pointer that we need to print as a string.
 
<lang aikido>native function strdup (s) // declare native
native function free(p) // also need to free the result
 
var s = strdup ("hello world\n")
var p = s // this is an integer type
for (;;) {
var ch = peek (p, 1) // read a single character
if (ch == 0) {
break
}
print (cast<char>(ch)) // print as a character
p++
}
free (s) // done with the memory now</lang>
 
=={{header|ALGOL 68}}==
Line 98 ⟶ 165:
</pre>
 
=={{header|8th}}==
<lang forth>
\ tell 8th what the function expects:
"ZZ" "strdup" func: strdup
"VZ" "free" func: free
\ call the external funcs
"abc" dup \ now we have two strings "abc" on the stack
strdup .s cr \ after strdup, you'll have the new (but duplicate) string on the stack
\ the ".s" will show both strings and you can see they are different items on the stack
free \ let the c library free the string
</lang>
=={{header|Ada}}==
Ada provides standard interfaces to [[C]], [[C++]], [[Fortran]] and [[Cobol]]. Other language interfaces can be provided as well, but are not mandatory. Usually it is possible to communicate to any language that supports calling conventions standard to the [[OS]] ('''cdecl''', '''stdcall''' etc).
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings; use Interfaces.C.Strings;
 
procedure Test_C_Interface is
function strdup (s1 : Char_Array) return Chars_Ptr;
pragma Import (C, strdup, "_strdup");
 
S1 : constant String := "Hello World!";
S2 : Chars_Ptr;
begin
S2 := strdup (To_C (S1));
Put_Line (Value (S2));
Free (S2);
end Test_C_Interface;</lang>
 
=={{header|Aikido}}==
There are two ways to call a <em>native</em> function in Aikido. The first is to write a wrapper function in C++ that is invoked from the Aikido interpreter. In a C++ file:
<lang aikido>#include <aikido.h>
extern "C" { // need C linkage
 
// define the function using a macro defined in aikido.h
AIKIDO_NATIVE(strdup) {
aikido::string *s = paras[0].str;
char *p = strdup (s->c_str());
aikido::string *result = new aikido::string(p);
free (p);
return result;
}
 
}</lang>
 
Then in the Aikido program:
<lang aikido>native function strdup(s)
println (strdup ("Hello World!"))</lang>
 
The second way is to use a <em>raw native</em> function. These functions must adhere to a defined set of rules and can be called directly from the Aikido interpreter. In the case of <code>strdup</code> we need to play a nasty trick because it returns a pointer that we need to print as a string.
 
<lang aikido>native function strdup (s) // declare native
native function free(p) // also need to free the result
 
var s = strdup ("hello world\n")
var p = s // this is an integer type
for (;;) {
var ch = peek (p, 1) // read a single character
if (ch == 0) {
break
}
print (cast<char>(ch)) // print as a character
p++
}
free (s) // done with the memory now</lang>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
Line 220 ⟶ 222:
bx lr @ return
</lang>
 
=={{header|AutoHotkey}}==
from the documentation for dllcall: <lang AutoHotkey>; Example: Calls the Windows API function "MessageBox" and report which button the user presses.
Line 225 ⟶ 228:
WhichButton := DllCall("MessageBox", "int", "0", "str", "Press Yes or No", "str", "Title of box", "int", 4)
MsgBox You pressed button #%WhichButton%.</lang>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Line 1,434 ⟶ 1,438:
f(5, 6);
11</lang>
 
=={{header|Mercury}}==
 
Line 1,876 ⟶ 1,881:
};
c_hello 'world';</lang>
=={{header|Perl 6}}==
{{Works with|rakudo|2016.07}}
<lang perl6>use NativeCall;
 
sub strdup(Str $s --> OpaquePointer) is native {*}
sub puts(OpaquePointer $p --> int32) is native {*}
sub free(OpaquePointer $p --> int32) is native {*}
 
my $p = strdup("Success!");
say 'puts returns ', puts($p);
say 'free returns ', free($p);</lang>
{{out}}
<pre>Success!
puts returns 9
free returns 0</pre>
 
=={{header|Phix}}==
Line 2,095 ⟶ 2,085:
libc.strcmp("abc", "def") # -1
libc.strcmp("hello", "hello") # 0</lang>
 
 
 
=={{header|Racket}}==
Line 2,143 ⟶ 2,131:
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.07}}
<lang perl6>use NativeCall;
 
sub strdup(Str $s --> OpaquePointer) is native {*}
sub puts(OpaquePointer $p --> int32) is native {*}
sub free(OpaquePointer $p --> int32) is native {*}
 
my $p = strdup("Success!");
say 'puts returns ', puts($p);
say 'free returns ', free($p);</lang>
{{out}}
<pre>Success!
puts returns 9
free returns 0</pre>
 
=={{header|REALbasic}}==
Line 2,187 ⟶ 2,190:
Code page: 437
</pre>
 
 
 
=={{header|Ruby}}==
Line 2,370 ⟶ 2,371:
println(callStrdup("Hello World!"))
}</lang>
 
=={{header|Stata}}==
Here are examples showing how to build and call from Stata a plugin written in C or Java. See also the entries 29 to 32 in the ''[https://blog.stata.com/2016/01/15/programming-an-estimation-command-in-stata-a-map-to-posted-entries/ Programming an estimation command in Stata]'' series by David M. Drukker, on [https://blog.stata.com/ Stata Blog].
10,327

edits