blib/lib/SDL2/Utils.pm untested

filter: Statementstmt 0.0% 0 / 206 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 28 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 406 CC 62 SCAR 63.9
CC
62
Cov
0%
_conversion
1482.0
ffi
132.0
enumX
30.0
CRAP 593.8
1 package SDL2::Utils {
2  
3 # FFI utilities
4 use strictures 2;
5 use experimental 'signatures';
6 use base 'Exporter::Tiny';
7 our @EXPORT = qw[attach define deprecate has enum ffi is];
8 use Alien::libsdl2;
9 use FFI::CheckLib;
10 use FFI::Platypus 1.46;
11 use FFI::Platypus::Memory qw[malloc strcpy free];
12 use FFI::C;
13 use FFI::C::Def;
14 use FFI::C::ArrayDef;
15 use FFI::C::StructDef;
16 use FFI::C::UnionDef;
17 use FFI::Platypus::Closure;
18 use File::Spec::Functions qw[catdir canonpath rel2abs];
19 use Path::Tiny qw[path];
20 use File::Share qw[dist_dir];
21 use Config;
22 use SDL2::Utils::Type::Enum;
23 use Ref::Util qw( is_ref is_plain_arrayref is_plain_hashref );
24  
25 sub deprecate ($str) {
26 warnings::warn( 'deprecated', $str ) if warnings::enabled('deprecated');
27 }
28  
29 #sub ver() { # TODO: Remove in favor of SDL2::version
30 # CORE::state $ver;
31 # $ver // ffi()->function( SDL_GetVersion => ['SDL_Version'] )
32 # ->call( $ver = SDL2::Version->new() );
33 # $ver;
34 #}
35 #ddx( Alien::libsdl2->dynamic_libs );
36 sub ffi () {
37 CORE::state $ffi;
38 if ( !defined $ffi ) {
39 use FFI::Build;
40 use FFI::Build::File::C;
41 if ( defined(&Test2::V0::diag) ) {
42 my $Win32 = $^O eq 'MSWin32';
43 #
44 #skip_all 'requires a shared object or DLL'
45 # unless Alien::libsdl2->dynamic_libs;
46 #
47 # nasty hack
48 #$ENV{LD_LIBRARY_PATH} = Alien::libsdl2->dist_dir . '/lib';
49 #$ENV{DYLD_LIBRARY_PATH} = Alien::libsdl2->dist_dir . '/lib';
50 #
51 eval { Test2::V0::diag( 'dist_dir: ' . Alien::libsdl2->dist_dir . '/lib' ) };
52 warn $@ if $@;
53 eval { Test2::V0::diag( 'libs: ' . Alien::libsdl2->libs ) };
54 warn $@ if $@;
55 eval { Test2::V0::diag( 'cflags: ' . Alien::libsdl2->cflags ) };
56 warn $@ if $@;
57 eval { Test2::V0::diag( 'cflags static: ' . Alien::libsdl2->cflags_static ) };
58 warn $@ if $@;
59 eval {
60 Test2::V0::diag( 'Dynamic libs: ' . join ':', Alien::libsdl2->dynamic_libs );
61 };
62 warn $@ if $@;
63 eval { Test2::V0::diag( 'bin dir: ' . join( ' ', Alien::libsdl2->bin_dir ) ) };
64 warn $@ if $@;
65 }
66 my $lib = undef;
67 if (1) {
68 my $root = path(__FILE__)->absolute->parent(3)->realpath;
69 my $dir = eval { dist_dir('SDL2-FFI') };
70 $dir //= $root->child('share')->realpath;
71 my $build = FFI::Build->new(
72 'bundle',
73 dir => $dir,
74 alien => ['Alien::libsdl2'],
75 source => [ $root->child('ffi/bundle.c') ],
76 libs => [ Alien::libsdl2->libs_static() // Alien::libsdl2->dynamic_libs() ],
77 verbose => 2
78 );
79 $lib = (
80 ( !-f $build->file->path ) || (
81 (
82 [ stat $build->file->path ]->[9]
83 < [ stat $root->child('ffi/bundle.c') ]->[9]
84 )
85 )
86 ) ? $build->build : $build->file->path;
87  
88 #$lib
89 # = -f $build->file->path && -f $root->child('ffi/sdl2.c') &&
90 # [ stat $build->file->path ]->[9]
91 # >= [ stat( $root->child('ffi/sdl2.c') ) ]->[9] ? $build->file : $build->build;
92 # warn $lib;
93 }
94 $ffi = FFI::Platypus->new(
95 api => 2,
96 experimental => 2,
97 lib => [ Alien::libsdl2->dynamic_libs, $lib ]
98 );
99 FFI::C->ffi($ffi);
100 $lib // $ffi->bundle;
101 }
102 $ffi;
103 }
104 ffi(); # auto-init
105  
106 sub enumX {
107 (undef) = shift;
108 my $name = defined $_[0] && !is_ref $_[0] ? shift : undef;
109 my @values = defined $_[0] && is_plain_arrayref $_[0] ? @{ shift() } : ();
110 my %config = defined $_[0] && is_plain_hashref $_[0] ? %{ shift() } : ();
111 my ( $class, $filename ) = caller;
112 unless ( defined $name ) {
113 $name = lcfirst [ split /::/, $class ]->[-1];
114 $name =~ s/([A-Z]+)/'_' . lc($1)/ge;
115 $name .= "_t";
116 }
117 my $ffi = FFI::C::_ffi_get($filename), $config{package} ||= $class;
118 my @maps;
119 $config{maps} = \@maps;
120 my $rev = $config{rev} ||= 'str';
121  
122 #use Data::Dump;
123 #ddx [$name, $rev, \%config, \@values];
124 ffi->load_custom_type( 'SDL2::Utils::Type::Enum', $name, \%config, @values );
125 my ( $str_lookup, $int_lookup, $type ) = @maps;
126  
127 #ddx [$name, $rev, $str_lookup, $int_lookup, $type];
128 require FFI::C::Def;
129 ffi->def(
130 'FFI::C::EnumDef',
131 $name,
132 FFI::C::EnumDef->new(
133 str_lookup => $str_lookup,
134 int_lookup => $int_lookup,
135 type => $type,
136 rev => $rev,
137 )
138 );
139 }
140  
141 sub enum (%args) {
142 my ($package) = caller();
143 $package = 'SDL2::FFI';
144 for my $tag ( keys %args ) {
145  
146 #use Data::Dump;
147 #ddx $args{$tag} if $tag eq 'WindowShapeMode';
148 #use Data::Dump;
149 #ddx $args{$tag};
150 #ddx @{$args{$tag}};
151 #my $enum =
152 #FFI::C->enum( $tag => $args{$tag}, { package => $package } );
153 #ffi->load_custom_type('SDL2::Utils::Type::Enum', $tag => { rev=>'int', package => $package, maps => \@maps } => $args{$tag} );
154 enumX(
155 ffi,
156 $tag => $args{$tag},
157 { rev => 'dualvar', package => $package, type => 'int', casing => 'keep' }
158 );
159  
160 #ffi->load_custom_type(
161 # '::Enum',
162 # $tag,
163 # {ref => 'int', package => $package },
164 # @{$args{$tag}}
165 # #{ rev => 'int', package => 'Foo', prefix => 'FOO_' },
166 #);
167 my $_tag = $tag; # Simple rules:
168 $_tag =~ s[^SDL_][]; # No SDL_XXXXX
169 $_tag = lcfirst $_tag unless $_tag =~ m[^.[A-Z]]; # Save GLattr
170  
171 #ddx $enum if $tag eq 'WindowShapeMode';
172 # warn $_tag if $tag eq 'WindowShapeMode';
173 push @{ $SDL2::FFI::EXPORT_TAGS{$_tag} },
174 sort map { ref $_ ? ref $_ eq 'CODE' ? $_->() : $_->[0] : $_ } @{ $args{$tag} };
175 }
176 }
177  
178 sub enumD (%args) {
179 my ($package) = caller();
180 $package = 'SDL2::FFI';
181 for my $tag ( keys %args ) {
182 use Data::Dump;
183  
184 #ddx $args{$tag}; # if $tag eq 'WindowShapeMode';
185 #use Data::Dump;
186 #warn $tag;
187 #ddx $args{$tag};
188 #ddx @{$args{$tag}};
189 #my $enum =
190 #FFI::C->enum( $tag => $args{$tag}, { package => $package } );
191 #ffi->load_custom_type('::Enum', $tag => { package => $package } => [$args{$tag}] );
192 #ffi->load_custom_type('SDL2::Utils::Type::Enum' => $tag => { ref => 'int', package => $package, casing => 'keep' }
193 # , $args{$tag}
194 #);
195 warn $tag;
196 ffi->load_custom_type(
197 'SDL2::Utils::Type::Enum', $tag, { rev => 'dualvar', package => $package },
198 @{ $args{$tag} }
199  
200 #{ rev => 'int', package => 'Foo', prefix => 'FOO_' },
201 );
202 my $_tag = $tag; # Simple rules:
203 $_tag =~ s[^SDL_][]; # No SDL_XXXXX
204 $_tag = lcfirst $_tag unless $_tag =~ m[^.[A-Z]]; # Save GLattr
205  
206 #ddx $enum if $tag eq 'WindowShapeMode';
207 # warn $_tag if $tag eq 'WindowShapeMode';
208 push @{ $SDL2::FFI::EXPORT_TAGS{$_tag} },
209 sort map { ref $_ ? ref $_ eq 'CODE' ? $_->() : $_->[0] : $_ } @{ $args{$tag} };
210 }
211 }
212  
213 sub attach (%args) {
214 my ($package) = caller();
215 $package = 'SDL2::FFI';
216 for my $tag ( sort keys %args ) {
217 for my $func ( sort keys %{ $args{$tag} } ) {
218  
219 #warn sprintf 'ffi->attach( %s => %s);', $func,
220 # Data::Dump::dump( @{ $args{$tag}{$func} } )
221 # if ref $args{$tag}{$func}[1] && ref $args{$tag}{$func}[1] eq 'ARRAY';
222 my $perl = $func;
223 $perl =~ s[^Bundle_][];
224 ffi->attach( [ $func => $package . '::' . $perl ] => @{ $args{$tag}{$func} } );
225 push @{ $SDL2::FFI::EXPORT_TAGS{$tag} }, $perl;
226 }
227 }
228 }
229 my %is;
230  
231 sub is ($is) {
232 my ($package) = caller;
233 $is{$package} = $is;
234 }
235 sub get_is ($package) { $is{$package} // '' }
236  
237 sub has (%args) { # Should be hash-like
238 my ($package) = caller;
239 my $type = $package;
240 $type =~ s[^SDL2::][SDL_];
241 $type =~ s[::][_]g;
242  
243 #$class =~ s[^SDL_(.+)$]['SDL2::' . ucfirst $1]e;
244 #warn sprintf '%-20s => %-20s%s', $name, $class, (
245 # -f sub ($package) { $package =~ m[::(.+)]; './lib/SDL2/' . $1 . '.pod' }
246 # ->($class) ? '' : ' (undocumented)'
247 #);
248 my @args = (
249 ffi,
250 name => $type, # C type
251 class => $package, # package
252 nullable => 1,
253 trim_string => 1,
254 members => \@_ # Keep order rather than use %args
255 );
256 get_is($package) eq 'Union' ? FFI::C::UnionDef->new(@args) : FFI::C::StructDef->new(@args);
257 }
258  
259 sub define (%args) {
260 my ($package) = caller();
261 $package = 'SDL2::FFI';
262 for my $tag ( keys %args ) {
263  
264 #print $_->[0] . ' ' for sort { $a->[0] cmp $b->[0] } @{ $Defines{$tag} };
265 #no strict 'refs';
266 ref $_->[1] eq 'CODE' ?
267  
268 #constant->import( $package . '::' .$_->[0] => $_->[1]->() ) : #
269 sub { no strict 'refs'; *{ $package . '::' . $_->[0] } = $_->[1] }
270 ->() :
271 constant->import( $package . '::' . $_->[0] => $_->[1] )
272 for @{ $args{$tag} };
273 push @{ $SDL2::FFI::EXPORT_TAGS{$tag} },
274 sort map { ref $_ ? $_->[0] : $_ } @{ $args{$tag} };
275 }
276 }
277 #
278 sub _tokenize_in ( $str, $pointers = 1 ) { # TODO: reconciliation between this and _out
279 my @ret;
280 $str =~ s{
281 \%
282 \d*?
283 (?<type> (?:[\+-]?[di]|0|x|X|u|f|F|e|E|a|A|g|G|n|p|s|c|lc|\d\[.+?\]))
284 }{
285 push @ret, _conversion($+{type}, $pointers )}gexsm;
286 @ret;
287 }
288  
289 sub _tokenize_out ( $str, $pointers = 1 ) {
290 my @ret;
291 $str =~ s[
292 %[\^\-\+\d\.#]*?(?<len>(?:\*.)?\*)?(?<type>[csdioxXufFeEaAgGnp])
293 ][
294 push @ret, _conversion($+{type}, $pointers, $+{len} // ())]gexsm;
295 @ret;
296 }
297  
298 sub _conversion ( $conv, $pointers = 1, $len = '' ) {
299 my $count = () = $len =~ m[(\*)]msg;
300 my $retval
301 = $conv eq 'c' ? 'char' : $conv eq 's' ? 'string' : $conv eq '[' ? 'string' : # set
302 $conv eq 'd' ? ( $pointers ? 'int*' : 'int' ) :
303 $conv eq 'i' ? ( $pointers ? 'int*' : 'int' ) :
304 $conv eq 'u' ? ( $pointers ? 'uint*' : 'uint' ) :
305 $conv eq 'o' ? ( $pointers ? 'int*' : 'int' ) : # octal
306 $conv eq 'x' ? ( $pointers ? 'string' : 'int' ) : # hex
307 $conv eq 'X' ? ( $pointers ? 'string' : 'int' ) : # hex
308 $conv eq 'n' ? ( $pointers ? 'int*' : 'int' ) : # num of chars read so far
309 $conv eq 'a' ? ( $pointers ? 'float*' : 'float' ) :
310 $conv eq 'A' ? ( $pointers ? 'float*' : 'float' ) :
311 $conv eq 'e' ? ( $pointers ? 'float*' : 'float' ) :
312 $conv eq 'E' ? ( $pointers ? 'float*' : 'float' ) :
313 $conv eq 'f' ? ( $pointers ? 'float*' : 'float' ) :
314 $conv eq 'F' ? ( $pointers ? 'float*' : 'float' ) :
315 $conv eq 'g' ? ( $pointers ? 'float*' : 'float' ) :
316 $conv eq 'G' ? ( $pointers ? 'float*' : 'float' ) :
317 $conv eq 'p' ? 'opaque' :
318 $conv eq 'lc' ? 'wide_string' :
319 $conv =~ /^\d/ ? 'string' :
320 'broken: ' . $conv;
321 $len ? ( ( map {'int'} 1 .. $count ), $retval ) : $retval;
322 }
323 };
324 1;