Call a foreign-language function: Difference between revisions

Add Standard ML version using PolyML
(Add Standard ML version using PolyML)
 
(12 intermediate revisions by 6 users not shown)
Line 679:
 
Afterwards usage of the function is just as with any other function.
 
=={{header|Ecstasy}}==
Ecstasy was designed around software containers and a strong security model. As such, Ecstasy does not have an FFI, and Ecstasy code cannot direcly access operating system or other foreign functions. More specifically, code running within an Ecstasy container cannot call foreign functions; any such required capabilities must be implemented outside of Ecstasy and then <i>injected</i> into an Ecstasy container.
 
=={{header|Factor}}==
If you declare a parameter as <code>c-string</code>, Factor automatically converts NULL-terminated C strings to Factor strings and back. In this case we additionally have to free the returned string, so we have to do the conversion explicitly; else the reference to the pointer would be dropped behind the scenes.
Line 870 ⟶ 874:
 
=={{header|FutureBasic}}==
The C Standary Library doesn't include the strdup() function. In the code below, strdup has been declared, written and executed in C. FB allows users to pass-through and compile C code, andwhile thatit treating its execution as if it's native FB. That's what been chosen for this example.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
Line 883 ⟶ 887:
if (dst == NULL) return NULL; // No memory
strcpy(dst, src); // Copy the characters
return dst; // Return the new string
}
EndC
Line 2,057 ⟶ 2,061:
(g_application_run app 0 #false)
</syntaxhighlight>
 
=={{header|Odin}}==
 
<syntaxhighlight lang="odin">package main
 
import "core:fmt"
 
foreign import libc "system:c"
 
@(default_calling_convention="c")
foreign libc {
@(link_name="strdup") cstrdup :: proc(_: cstring) -> cstring ---
@(link_name="free") cfree :: proc(_: rawptr) ---
}
 
main :: proc() {
s1 : cstring = "hello"
s2 := cstrdup(s1)
fmt.printf("{}\n", s2)
cfree(rawptr(s2))</syntaxhighlight>
 
=={{header|Oz}}==
First we need to create a so-called "native functor" that converts the arguments and describes the C functions:
Line 2,144 ⟶ 2,169:
"Hello World!"
</pre>
=={{header|PHP}}==
{{works with|PHP|7.4}}
 
PHP 7.4+ has as support to call external C-like functions using extension . See this Windows example:
<syntaxhighlight lang="php">$ffi = FFI::cdef("char *_strdup(const char *strSource);", "msvcrt.dll");
 
$cstr = $ffi->_strdup("success");
$str = FFI::string($cstr);
echo $str;
FFI::free($cstr);
</syntaxhighlight>
=={{header|PicoLisp}}==
The easiest is to inline the C code. Another possibility would be to write it
Line 2,632 ⟶ 2,668:
 
Transcript showCR:( CallDemo strdup:'Hello' )</syntaxhighlight>
 
=={{header|Standard ML}}==
{{works with|Poly/ML}}
<syntaxhighlight lang="sml">local
val libc = Foreign.loadLibrary "libc.so.6"
val sym = Foreign.getSymbol libc "strdup"
in
val strdup = Foreign.buildCall1(sym, (Foreign.cString), Foreign.cString)
end</syntaxhighlight>
{{out}}
<pre>
> strdup "test string";
val it = "test string": string
>
</pre>
 
=={{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].
Line 2,764 ⟶ 2,816:
 
There is no way to use the <code>str</code> family of types, yet do manual memory management; FFI manages automatically. Code that wants to manually manage a foreign resource referenced by pointer should use <code>cptr</code> or <code>carray</code>, depending on required semantics.
 
=={{header|V (Vlang)}}==
Note: Vlang also has a C2V transpiler.
<syntaxhighlight lang="vlang">
#include "stdlib.h"
#include "string.h"
 
// Declare C functions that will be used.
fn C.strdup(txt &char) &char
fn C.strcat(dest &char, src &char) &char
 
fn main() {
txt_1 := "Hello World!"
txt_2 := " Let's Wish for Peace!"
// Memory-unsafe operations must be marked as such (unsafe {...}), or won't compile.
unsafe {
dup := C.strdup(txt_1.str)
println('${cstring_to_vstring(dup)}')
addto := C.strcat(dup, txt_2.str)
println('${cstring_to_vstring(addto)}')
 
// Must manually free memory or program can hang because unsafe.
free(dup)
free(addto)
}
exit(0)
}
</syntaxhighlight>
 
{{out}}
<pre>
Hello World!
Hello World! Let's Wish for Peace!
</pre>
 
=={{header|Wren}}==
Although RC task solutions are usually written for execution by Wren CLI, the language's main purpose is for embedding and the embedding API is written in C. It is therefore a relative easy matter to call a C function from Wren after first embedding the latter in a suitable C program.
 
<syntaxhighlight lang="ecmascriptwren">/* call_foreign_language_functionCall_a_foreign-language_function.wren */
 
class C {
Line 2,845 ⟶ 2,933:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "call_foreign_language_functionCall_a_foreign-language_function.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 2,867 ⟶ 2,955:
Hello World!
</pre>
 
=={{header|X86-64 Assembly}}==
===UASM 2.52===
23

edits