Skip to content

Commit 2fb7fa2

Browse files
committed
initial try
0 parents  commit 2fb7fa2

File tree

8 files changed

+248
-0
lines changed

8 files changed

+248
-0
lines changed

.clang-format

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: Mozilla
4+
IndentWidth: 4
5+
SortIncludes: false

.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Build
2+
/MANIFEST.SKIP.bak
3+
/MANIFEST.bak
4+
META.YML
5+
MYMETA*
6+
Makefile
7+
Makefile.old
8+
SSL
9+
blib
10+
blib/
11+
.vscode/
12+
_vscode/
13+
.build/
14+
_build/
15+
cover_db/
16+
/_eumm/
17+
/UV*
18+
UV-*/
19+
UV-*.tar.gz
20+
.perl-version
21+
pm_to_blib
22+
*.sw*
23+
*.tar.gz
24+
xx*
25+
*.bs
26+
*.o
27+
lib/*.c
28+
lib/UV/*.c
29+
.tmp/

.perltidyrc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-pbp # Start with Perl Best Practices
2+
-nst # undo -st from -pbp, to allow for command line use
3+
-w # Show all warnings
4+
-iob # Ignore old breakpoints
5+
-l=80 # 80 characters per line
6+
-mbl=2 # No more than 2 blank lines
7+
-i=4 # Indentation is 4 columns
8+
-ci=4 # Continuation indentation is 4 columns
9+
-vt=0 # Less vertical tightness
10+
-pt=2 # High parenthesis tightness
11+
-bt=2 # High brace tightness
12+
-sbt=2 # High square bracket tightness
13+
-isbc # Don't indent comments without leading space

cpanfile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
on 'runtime' => sub {
2+
requires 'strict';
3+
requires 'warnings';
4+
requires 'feature';
5+
requires 'utf8';
6+
requires 'Alien::Base::Wrapper';
7+
requires 'Alien::libuv' => '1.013';
8+
requires 'Carp';
9+
requires 'Cwd';
10+
requires 'Data::Dumper';
11+
requires 'Exporter' => '5.57';
12+
requires 'FFI::Platypus' => '1.00';
13+
requires 'File::Basename';
14+
requires 'File::ShareDir';
15+
requires 'File::Spec';
16+
requires 'Path::Tiny';
17+
requires 'Ref::Util';
18+
requires 'Sub::Util';
19+
};
20+
21+
on 'test' => sub {
22+
requires 'Test::More' => '0.88';
23+
};

dist.ini

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
name = Sodium-FFI
3+
author = Chase Whitener <[email protected]>
4+
license = Perl_5
5+
copyright_holder = Chase Whitener <[email protected]>
6+
copyright_year = 2020
7+
8+
[ReadmeAnyFromPod / Markdown_Readme]
9+
type = gfm
10+
source_filename = lib/Sodium/FFI.pm
11+
filename = README.md
12+
location = root
13+
14+
[Regenerate::AfterReleasers]
15+
plugin = Markdown_Readme
16+
17+
[@Starter::Git]
18+
revision = 4
19+
managed_versions = 1
20+
installer = MakeMaker::Awesome
21+
; MakeMaker::Awesome.WriteMakefile_arg[0] = DEFINE => "-DMARKED_SECTION"
22+
; MakeMaker::Awesome.WriteMakefile_arg[1] = H => [ qw(hparser.h hctype.h tokenpos.h pfunc.h hparser.c util.c) ]
23+
RewriteVersion.global = 1
24+
NextRelease.format = %-9v %{yyyy-MM-dd}d
25+
regenerate = Makefile.PL
26+
regenerate = META.json
27+
regenerate = README.md
28+
regenerate = LICENSE
29+
regenerate = t/00-report-prereqs.t
30+
Git::Check.allow_dirty = META.json
31+
32+
[Prereqs::FromCPANfile]
33+
34+
[MinimumPerl]
35+
perl = 5.008
36+
37+
[Git::Contributors]
38+
[GithubMeta]
39+
issues = 1
40+
user = genio
41+
42+
[CheckChangeLog]
43+
[CheckChangesHasContent]
44+
[Test::ChangesHasContent]
45+
46+
[Test::Kwalitee]
47+
skiptest = no_symlinks
48+
[Test::Version]
49+
filename_match = qr/FFI\.pm$/
50+
[Test::Pod::Coverage::Configurable]
51+
trustme = Sodium::FFI
52+
53+
[Test::PodSpelling]
54+
wordlist = Pod::Wordlist
55+
spell_cmd = aspell list
56+
stopword = CPAN

ffi/constants.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <ffi_platypus_bundle.h>
2+
#include <string.h>
3+
#include <stdint.h>
4+
#include <sodium.h>
5+
6+
#define _str(name) c->set_str(#name, name)
7+
#define _sint(name) c->set_sint(#name, name)
8+
9+
void
10+
ffi_pl_bundle_constant(const char* package, ffi_platypus_constant_t* c)
11+
{
12+
_str(SODIUM_VERSION_STRING);
13+
14+
_sint(SODIUM_LIBRARY_VERSION_MAJOR);
15+
_sint(SODIUM_LIBRARY_VERSION_MINOR);
16+
_sint(SODIUM_LIBRARY_MINIMAL);
17+
}

lib/Sodium/FFI.pm

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package Sodium::FFI;
2+
use strict;
3+
use warnings;
4+
5+
our $VERSION = '0.001';
6+
7+
use Carp qw(croak);
8+
use Data::Dumper::Concise qw(Dumper);
9+
use Exporter qw(import);
10+
11+
use Alien::Sodium;
12+
use FFI::Platypus;
13+
use Path::Tiny qw(path);
14+
use Sub::Util qw(set_subname);
15+
16+
my $ffi;
17+
BEGIN {
18+
$ffi = FFI::Platypus->new(api => 1);
19+
$ffi->lib(Alien::Sodium->dynamic_libs);
20+
# $ffi->bundle();
21+
}
22+
23+
# Protect subclasses using AUTOLOAD
24+
sub DESTROY { }
25+
our @EXPORT_OK = qw();
26+
27+
# All of these functions were shipped with libuv v1.0 and don't
28+
# need to be gated by version.
29+
$ffi->attach('sodium_version_string' => [] => 'string');
30+
$ffi->attach('sodium_library_version_major' => [] => 'int');
31+
$ffi->attach('sodium_library_version_minor' => [] => 'int');
32+
$ffi->attach('sodium_library_minimal' => [] => 'int');
33+
34+
our %function = (
35+
);
36+
37+
our %maybe_function = (
38+
);
39+
40+
foreach my $func (keys %function) {
41+
$ffi->attach($func, @{$function{$func}});
42+
}
43+
44+
foreach my $func (keys %maybe_function) {
45+
my $href = $maybe_function{$func};
46+
if (_version_or_better(@{$href->{added}})) {
47+
$ffi->attach($func, @{$href->{ffi}});
48+
}
49+
else {
50+
# monkey patch in the subref
51+
no strict 'refs';
52+
no warnings 'redefine';
53+
my $pkg = __PACKAGE__;
54+
*{"${pkg}::$func"} = set_subname("${pkg}::$func", $href->{fallback});
55+
}
56+
}
57+
58+
59+
sub _version_or_better {
60+
my ($maj, $min, $pat) = @_;
61+
$maj //= 0;
62+
$min //= 0;
63+
$pat //= 0;
64+
foreach my $partial ($maj, $min, $pat) {
65+
if ($partial =~ /[^0-9]/) {
66+
croak("_version_or_better requires 1 - 3 integers representing major, minor and patch numbers");
67+
}
68+
}
69+
# if no number was passed in, then the current version is higher
70+
return 1 unless ($maj || $min || $pat);
71+
72+
73+
return 0 if Sodium::FFI->UV_VERSION_MAJOR < $maj; # full version behind of requested
74+
return 1 if Sodium::FFI->UV_VERSION_MAJOR > $maj; # full version ahead of requested
75+
# now we should be matching major versions
76+
return 1 unless $min; # if we were only given major, move on
77+
return 0 if Sodium::FFI->UV_VERSION_MINOR < $min; # same major, lower minor
78+
return 1 if Sodium::FFI->UV_VERSION_MINOR > $min; # same major, higher minor
79+
# now we should be matching major and minor, check patch
80+
return 1 unless $pat; # move on if we were given maj, min only
81+
return 0 if Sodium::FFI->UV_VERSION_PATCH < $pat;
82+
return 1;
83+
}
84+
85+
1;

t/10-versions.t

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use strict;
2+
use warnings;
3+
use Test::More;
4+
use Sodium::FFI ();
5+
6+
my $ver_string = Sodium::FFI::sodium_version_string();
7+
my $ver_major = Sodium::FFI::sodium_library_version_major();
8+
my $ver_minor = Sodium::FFI::sodium_library_version_minor();
9+
ok($ver_string, "version string: $ver_string");
10+
ok($ver_major, "version major: $ver_major");
11+
ok($ver_minor, "version minor: $ver_minor");
12+
# ok(Sodium::FFI::sodium_library_minimal(), "version minimal");
13+
# ok(defined Sodium::FFI::UV_VERSION_MAJOR, "VERSION MAJOR: ". Sodium::FFI::UV_VERSION_MAJOR);
14+
# ok(defined Sodium::FFI::UV_VERSION_MINOR, "VERSION MINOR: ". Sodium::FFI::UV_VERSION_MINOR);
15+
# ok(defined Sodium::FFI::UV_VERSION_PATCH, "VERSION PATCH: ". Sodium::FFI::UV_VERSION_PATCH);
16+
# ok(defined Sodium::FFI::UV_VERSION_HEX, "VERSION HEX: ". Sodium::FFI::UV_VERSION_HEX);
17+
# ok(defined Sodium::FFI::UV_VERSION_SUFFIX, "VERSION SUFFIX: ". Sodium::FFI::UV_VERSION_SUFFIX);
18+
# ok(defined Sodium::FFI::UV_VERSION_IS_RELEASE, "VERSION IS RELEASE: ". Sodium::FFI::UV_VERSION_IS_RELEASE);
19+
20+
done_testing;

0 commit comments

Comments
 (0)