Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Perl examples #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ RUN \
python3.8 \
ruby2.7 \
ruby2.7-dev \
perl \
cpanminus \
\
&& rm -rf /var/lib/apt/lists/*

Expand Down
4 changes: 4 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ test-output/

# C#
*/csharp-example

# Perl
.perl-dependencies
/local
6 changes: 4 additions & 2 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ TEST_HASKELL := tests_haskell.mk
TEST_NODEJS := tests_nodejs.mk
TEST_CSHARP := tests_csharp.mk
TEST_JULIA := tests_julia.mk
TEST_PERL := tests_perl.mk
TEST_ALL_LANGUAGES := tests_all.mk

dir := integers
Expand All @@ -66,7 +67,7 @@ include ${dir}/rules.mk
dir := vector_return
include ${dir}/rules.mk

all: c ruby python haskell nodejs csharp julia
all: c ruby python haskell nodejs csharp julia perl
.PHONY: all

# Test only a single language
Expand All @@ -81,5 +82,6 @@ haskell:
nodejs:
csharp:
julia:
perl:

.PHONY: c ruby python haskell nodejs csharp julia
.PHONY: c ruby python haskell nodejs csharp julia perl
3 changes: 3 additions & 0 deletions examples/cpanfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
requires 'FFI::Platypus', '1.00';
requires 'FFI::Platypus::Lang::Rust';
requires 'FFI::Platypus::Type::PtrObject';
13 changes: 13 additions & 0 deletions examples/integers/src/main.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use strict;
use warnings;
use 5.010;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the examples require Perl 5.10 feature of say which makes the code more like the other languages and easier to read without having to us print and explicitly put an "\n"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

use FFI::Platypus 1.00;

my $ffi = FFI::Platypus->new(
api => 1,
lang => 'Rust',
);
$ffi->find_lib( lib => 'integers');
$ffi->attach( addition => ['u32','u32'] => 'u32' );

say addition(1,2);
40 changes: 40 additions & 0 deletions examples/objects/src/main.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use strict;
use warnings;
use 5.014;
use FFI::Platypus 1.00;

my $ffi = FFI::Platypus->new(
api => 1,
lang => 'Rust',
);
$ffi->mangler(sub { "zip_code_database_$_[0]" });
$ffi->find_lib( lib => 'objects' );
$ffi->load_custom_type('::PtrObject' => 'ZipCodeDatabase' => 'ZipCodeDatabase' );

package ZipCodeDatabase {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples with a package inside of them use the package block syntax for readability. This requires Perl 5.14, which was first released in 2011. It might make sense for all of the examples to require the same version, but I've only specified versions where I need say and the package block syntax, and just the minimum perl required for each example.


$ffi->attach( new => [] => 'opaque' => sub {
my($xsub, $class) = @_;
my $ptr = $xsub->();
bless { ptr => $ptr }, $class;
});

$ffi->attach( populate => ['ZipCodeDatabase']);
$ffi->attach( population_of => ['ZipCodeDatabase','string'] => 'u32');

$ffi->attach( free => ['opaque'] => sub {
my($xsub, $self) = @_;
if(my $ptr = delete $self->{ptr}) {
$xsub->($ptr);
}
});

}

my $database = ZipCodeDatabase->new;

$database->populate;
my $pop1 = $database->population_of("90210");
my $pop2 = $database->population_of("20500");

say $pop1 - $pop2;
17 changes: 17 additions & 0 deletions examples/slice_arguments/src/main.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use strict;
use warnings;
use 5.010;
use FFI::Platypus 1.00;

my $ffi = FFI::Platypus->new(
api => 1,
lang => 'Rust',
);
$ffi->find_lib( lib => 'slice_arguments' );
$ffi->attach( sum_of_even => [ 'u32[]', 'isize' ] => 'u32' => sub {
my($xsub, $n) = @_;
my $len = @$n;
return $xsub->($n, $len);
});

say sum_of_even([1,2,3,4,5,6]);
13 changes: 13 additions & 0 deletions examples/string_arguments/src/main.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use strict;
use warnings;
use 5.010;
use FFI::Platypus 1.00;

my $ffi = FFI::Platypus->new(
api => 1,
lang => 'Rust',
);
$ffi->find_lib( lib => 'string_arguments' );
$ffi->attach( how_many_characters => [ 'string' ] => 'u32' );

say how_many_characters("göes to élevên");
23 changes: 23 additions & 0 deletions examples/string_return/src/main.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use strict;
use warnings;
use 5.010;
use FFI::Platypus 1.00;

my $ffi = FFI::Platypus->new(
api => 1,
lang => 'Rust',
);
$ffi->find_lib( lib => 'string_return' );

$ffi->attach_cast( opaque_to_string => 'opaque' => 'string');
$ffi->attach( theme_song_free => ['opaque'] );
$ffi->attach( theme_song_generate => ['u8'] => 'opaque' => sub {
my($xsub, $length) = @_;
my $ptr = $xsub->($length);
my $str = opaque_to_string($ptr);
utf8::decode($str);
theme_song_free($ptr);
return $str;
});

say theme_song_generate(5);
1 change: 1 addition & 0 deletions examples/tests_all.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ include ${TEST_HASKELL}
include ${TEST_NODEJS}
include ${TEST_CSHARP}
include ${TEST_JULIA}
include ${TEST_PERL}
26 changes: 26 additions & 0 deletions examples/tests_perl.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
include ${COMMON_TEST_RULES}

# System-wide packages need to be installed. We run the appropriate
# package manager and leave a small cookie file to indicate
# success. The execution depends on the cookie file.

ifndef PERL_DEPENDENCIES

PERL_DEPENDENCIES := .perl-dependencies

${PERL_DEPENDENCIES}: cpanfile
cpanm --installdeps -L local .
touch $@

endif

${TEST_DIR_${d}}/perl-test: LIB_DIR := ${LIB_DIR_${d}}
${TEST_DIR_${d}}/perl-test: ${d}/src/main.pl ${TEST_DIR_${d}} ${LIB_${d}} ${PERL_DEPENDENCIES}
LD_LIBRARY_PATH=${LIB_DIR} perl -Ilocal/lib $< > $@

.PHONY: perl-test_${d}
perl-test_${d}: EXPECTED := ${d}/expected-output
perl-test_${d}: ${TEST_DIR_${d}}/perl-test
diff -q ${EXPECTED} $<

perl: perl-test_${d}
1 change: 1 addition & 0 deletions examples/tuples/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include ${TEST_RUBY}
include ${TEST_PYTHON}
include ${TEST_NODEJS}
include ${TEST_CSHARP}
include ${TEST_PERL}

d := ${dirstack_${sp}}
sp := ${basename ${sp}}
35 changes: 35 additions & 0 deletions examples/tuples/src/main.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use strict;
use warnings;
use 5.014;
use FFI::Platypus 1.00;

my $ffi = FFI::Platypus->new(
api => 1,
lang => 'Rust',
);
$ffi->find_lib( lib => 'tuples' );

package Tuple {
use FFI::Platypus::Record;

use overload
'""' => sub { shift->to_string },
bool => sub { 1 }, fallback => 1;

record_layout_1(
$ffi,
u32 => 'x',
u32 => 'y',
);

sub to_string {
my($self) = @_;
sprintf "(%d,%d)", $self->x, $self->y;
}
}

$ffi->attach( flip_things_around => ['record(Tuple)'] => 'record(Tuple)' );

my $tup = Tuple->new( x => 10, y => 20 );

say flip_things_around($tup);
6 changes: 6 additions & 0 deletions site/integers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,9 @@ Windows by copying `target\debug\integers.dll` to the current
directory and running `julia src\main.jl`.

[ccall]: https://docs.julialang.org/en/v1/base/c/#ccall

## Perl

{% example src/main.pl %}

TODO
6 changes: 6 additions & 0 deletions site/objects/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,9 @@ creating and freeing the object. With the `do` syntax, the user code
becomes similar to one using Python's `with` syntax. Alternatively,
the programmer can use the other constructor and call the method
`close` when it is no longer needed.

## Perl

{% example src/main.pl %}

TODO
6 changes: 6 additions & 0 deletions site/slice_arguments/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,9 @@ to the rule, and should be passed with a plain `Ptr` and length.
[julia-Ptr]: https://docs.julialang.org/en/v1/base/c/#Core.Ptr
[julia-Ref]: https://docs.julialang.org/en/v1/base/c/#Core.Ref
[julia-refptr]: https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#When-to-use-T,-Ptr{T}-and-Ref{T}-1

## Perl

{% example src/main.pl %}

TODO
6 changes: 6 additions & 0 deletions site/string_arguments/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ Julia strings (of base type `AbstractString`) are automatically
converted to C strings. The `Cstring` type from Julia is compatible
with the Rust type `CStr`, as it also assumes a `NUL` terminator byte
and does not allow `NUL` bytes embedded in the string.

## Perl

{% example src/perl.pl %}

TODO
6 changes: 6 additions & 0 deletions site/string_return/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,9 @@ by Julia, and transfers the Rust string back afterwards. The
resource is kept alive in Julia.

[julia-objects]: ../objects#julia

## Perl

{% example src/main.pl %}

TODO
6 changes: 6 additions & 0 deletions site/tuples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ will store each member inline and will be passed to the native
function by value.

[julia-isbits]: https://docs.julialang.org/en/v1/base/base/#Base.isbits

## Perl

{% example src/main.pl %}

TODO