Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -0,0 +1,7 @@
julia> p = ccall(:strdup, Ptr{Uint8}, (Ptr{Uint8},), "Hello world")
Ptr{Uint8} @0x000000011731bde0
julia> bytestring(p) # convert pointer back to a native Julia string
"Hello world"
julia> ccall(:free, Void, (Ptr{Uint8},), p)

View file

@ -0,0 +1,23 @@
:- module test_ffi.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
% The actual FFI code begins here.
:- pragma foreign_decl("C", "char *strdup(const char *s);").
:- func strdup(string::in) = (string::out) is det.
:- pragma foreign_proc("C", strdup(S::in) = (SD::out),
[will_not_call_mercury, not_thread_safe, promise_pure],
"SD = strdup(S);").
% The actual FFI code ends here.
main(!IO) :-
io.write_string(strdup("Hello, worlds!\n"), !IO).
:- end_module test_ffi.

View file

@ -0,0 +1 @@
:- func strdup(string) = string.

View file

@ -0,0 +1,2 @@
:- module(plffi, [strdup/2]).
:- use_foreign_library(plffi).

View file

@ -0,0 +1,20 @@
#include <string.h>
#include <stdio.h>
#include <SWI-Prolog.h>
static foreign_t pl_strdup(term_t string0, term_t string1)
{
char *input_string, *output_string;
if (PL_get_atom_chars(string0, &input_string))
{
output_string = strdup(input_string);
return PL_unify_atom_chars(string1, output_string);
}
PL_fail;
}
install_t install_plffi()
{
PL_register_foreign("strdup", 2, pl_strdup, 0);
}

View file

@ -0,0 +1 @@
$ swipl-ld -o plffi -shared plffi.c

View file

@ -0,0 +1,15 @@
?- [plffi].
% plffi compiled into plffi 0.04 sec, 1,477 clauses
true.
?- strdup('Booger!', X).
X = 'Booger!'.
?- strdup(booger, X).
X = booger.
?- strdup(booger, booger).
true.
?- X = booger, strdup(booger, X).
X = booger.