blib/lib/SDL2/assert.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 125 CC 3 SCAR 12.0
CC
3
Cov
0%
next
6.0
condition
6.0
filename
2.0
CRAP 3.3
1 package SDL2::assert {
2 use strictures 2;
3 use experimental 'signatures';
4 use SDL2::Utils;
5 use Try::Tiny;
6 #
7 enum SDL_AssertState => [
8 qw[ SDL_ASSERTION_RETRY
9 SDL_ASSERTION_BREAK
10 SDL_ASSERTION_ABORT
11 SDL_ASSERTION_IGNORE
12 SDL_ASSERTION_ALWAYS_IGNORE
13 ]
14 ];
15  
16 package SDL2::AssertData {
17 use SDL2::Utils;
18 has
19 always_ignore => 'int',
20 trigger_count => 'uint',
21 _condition => 'opaque', # string
22 _filename => 'opaque', # string
23 linenum => 'int',
24 _function => 'opaque', # string
25 _next => 'opaque'; # const struct SDL_AssertData *next
26 ffi->attach_cast( '_cast' => 'opaque' => 'SDL_AssertData' );
27  
28 sub next { # TODO: Broken.
29 my ($self) = @_;
30 defined $self->_next ? _cast( $self->_next ) : undef;
31 }
32  
33 sub condition {
34 defined $_[1] ? $_[0]->_condition( ffi->cast( 'string', 'opaque', $_[1] ) ) :
35 ffi->cast( 'opaque', 'string', $_[0]->_condition );
36 }
37  
38 sub filename {
39 ffi->cast( 'opaque', 'string', $_[0]->_filename );
40 }
41  
42 sub function {
43 ffi->cast( 'opaque', 'string', $_[0]->_function );
44 }
45 };
46 ffi->type( '(int,opaque)->int' => 'SDL_AssertionHandler' );
47 attach assert => {
48 SDL_ReportAssertion =>
49 [ [ 'SDL_AssertData', 'string', 'string', 'int' ], 'SDL_AssertState' ],
50 SDL_SetAssertionHandler => [
51 [ 'SDL_AssertionHandler', 'opaque' ] => sub {
52 my ( $inner, $handler, $userdata ) = @_;
53 my $ref = ffi->closure( sub { $handler->( shift, $userdata ) } );
54 $inner->( $ref, $userdata );
55 }
56 ],
57 SDL_GetDefaultAssertionHandler => [ [], 'SDL_AssertionHandler' ],
58 SDL_GetAssertionHandler => [ ['opaque*'], 'SDL_AssertionHandler' ],
59 SDL_GetAssertionReport => [ [], 'SDL_AssertData' ] # SDL_AssertData[]
60 };
61 define assert => [
62 [ SDL_disabled_assert => sub ( $code, $condition = () ) {1} ],
63  
64 # the do {} while(0) avoids dangling else problems:
65 # if (x) SDL_assert(y); else blah();
66 # ... without the do/while, the "else" could attach to this macro's "if".
67 # We try to handle just the minimum we need here in a macro...the loop,
68 # the static vars, and break points. The heavy lifting is handled in
69 # SDL_ReportAssertion(), in SDL_assert.c.
70 #
71 [ SDL_enabled_assert => sub ( $code, $condition = () ) {
72 my $index = 0;
73 ++$index while scalar caller($index) eq __PACKAGE__;
74 my ( $package, $filename, $line, $subroutine ) = caller($index);
75 $subroutine = '' if $subroutine =~ m[^SDL2::FFI::__ANON__];
76 do {
77 if ( ref $code eq 'CODE' ) {
78 while ( !eval { &{$code}; } ) {
79 if ( !defined $condition ) {
80 try {
81 require B::Deparse;
82 $condition = B::Deparse->new(qw[-d -si8T])->coderef2text($code);
83 }
84 catch {
85 $condition
86 = 'code display non-functional on this version of Perl, sorry'
87 }
88 }
89 CORE::state $sdl_assert_data //= SDL2::AssertData->new(
90 { condition =>
91  
92 #SDL2::Utils::ffi->cast( 'string', 'opaque', $condition ),
93 $condition
94 }
95 );
96 my $sdl_assert_state
97 = SDL_ReportAssertion( $sdl_assert_data, $subroutine, $filename,
98 $line );
99 if ( $sdl_assert_state == SDL_ASSERTION_RETRY() ) {
100 next; # go again.
101 }
102 elsif ( $sdl_assert_state == SDL_ASSERTION_BREAK() ) {
103  
104 #SDL_TriggerBreakpoint();
105 # https://perldoc.perl.org/perldebug#Debugging-Compile-Time-Statements
106 # https://perlmaven.com/add-debugger-breakpoint-to-your-code
107 $DB::trace = 1;
108 return $DB::single = 1;
109 }
110 last; # not retrying.
111 }
112 }
113 else {
114 while ( !$code ) {
115 CORE::state $sdl_assert_data //= SDL2::AssertData->new(
116 { condition =>
117  
118 #SDL2::Utils::ffi->cast( 'string', 'opaque', $condition ),
119 $condition
120 }
121 );
122 my $sdl_assert_state
123 = SDL_ReportAssertion( $sdl_assert_data, $subroutine, $filename,
124 $line );
125 if ( $sdl_assert_state == SDL_ASSERTION_RETRY() ) {
126 next; # go again.
127 }
128 elsif ( $sdl_assert_state == SDL_ASSERTION_BREAK() ) {
129  
130 #SDL_TriggerBreakpoint();
131 # https://perldoc.perl.org/perldebug#Debugging-Compile-Time-Statements
132 # https://perlmaven.com/add-debugger-breakpoint-to-your-code
133 $DB::trace = 1;
134 return $DB::single = 1;
135 }
136 last; # not retrying.
137 }
138 }
139 } while (0);
140 }
141 ],
142  
143 # TODO: enable these with a parameter passed to FFI import?
144 # Enable various levels of assertions.
145 [ SDL_assert_always => sub ($condition) { SDL_enabled_assert($condition) } ], (
146 SDL2::FFI::SDL_ASSERT_LEVEL() == 0 ? ( # assertions disabled
147 [ SDL_assert => *SDL_disabled_assert ],
148 [ SDL_assert_release => *SDL_disabled_assert ],
149 [ SDL_assert_paranoid => *SDL_disabled_assert ]
150 ) :
151 SDL2::FFI::SDL_ASSERT_LEVEL() == 1 ? ( # release
152 [ SDL_assert => *SDL_disabled_assert ],
153 [ SDL_assert_release => *SDL_enabled_assert ],
154 [ SDL_assert_paranoid => *SDL_disabled_assert ]
155 ) :
156 SDL2::FFI::SDL_ASSERT_LEVEL() == 2 ? ( # normal
157 [ SDL_assert => *SDL_enabled_assert ],
158 [ SDL_assert_release => *SDL_enabled_assert ],
159 [ SDL_assert_paranoid => *SDL_disabled_assert ]
160 ) :
161 SDL2::FFI::SDL_ASSERT_LEVEL() == 3 ? ( # paranoid
162 [ SDL_assert => *SDL_enabled_assert ],
163 [ SDL_assert_release => *SDL_enabled_assert ],
164 [ SDL_assert_paranoid => *SDL_enabled_assert ]
165 ) :
166 ()
167 )
168 ];
169  
170 =encoding utf-8
171  
172 =head1 NAME
173  
174 SDL2::assert - SDL Assertion Functions
175  
176 =head1 SYNOPSIS
177  
178 use SDL2::FFI qw[:assert];
179 SDL_assert( 1 == 1 );
180 my $test = 'nope';
181 SDL_assert(
182 sub {
183 warn 'testing';
184 my $retval = $test eq "blah";
185 $test = "blah";
186 $retval;
187 }
188 );
189  
190 =head1 DESCRIPTION
191  
192 SDL2::assert implements an assertion system. Failures even cause the perl
193 debugger to halt if enabled.
194  
195 =head1 Functions
196  
197 These may be imported by name or with the C<:assert> tag.
198  
199 =head2 C<SDL_ReportAssertion( ... )>
200  
201 Never call this directly. Use L<< C<SDL_assert( ... )>|/C<SDL_assert( ... )>
202 >>, L<< C<SDL_assert_release( ... )>|/C<SDL_assert_release( ... )> >>, L<<
203 C<SDL_assert_paranoid( ... )>|/C<SDL_assert_paranoid( ... )> >>, and L<<
204 C<SDL_assert_always( ... )>|/C<SDL_assert_always( ... )> >>.
205  
206 =head2 C<SDL_SetAssertionHandler( ... )>
207  
208 Set an application-defined assertion handler.
209  
210 SDL_SetAssertionHandler( sub { ... } );
211  
212 This function allows an application to show its own assertion UI and/or force
213 the response to an assertion failure. If the application doesn't provide this,
214 SDL will try to do the right thing, popping up a system-specific GUI dialog,
215 and probably minimizing any fullscreen windows.
216  
217 This callback may fire from any thread, but it runs wrapped in a mutex, so it
218 will only fire from one thread at a time.
219  
220 This callback is NOT reset to SDL's internal handler upon C<SDL_Quit( )>!
221  
222 Expected parameters include:
223  
224 =over
225  
226 =item C<handler> - the L<< C<SDL_AssertionHandler>|/SDL_AssertionHandler >> function to call when an assertion fails or undef for the default handler
227  
228 =back
229  
230 =head2 C<SDL_GetDefaultAssertionHandler( )>
231  
232 Get the default assertion handler.
233  
234 This returns the function pointer that is called by default when an assertion
235 is triggered. This is an internal function provided by SDL, that is used for
236 assertions when L<< C<SDL_SetAssertionHandler( ...
237 )>|/C<SDL_SetAssertionHandler( ... )> >> hasn't been used to provide a
238 different function.
239  
240 Returns the default C<SDL_AssertionHandler> that is called when an assert
241 triggers.
242  
243 =head2 C<SDL_GetAssertionHandler( ... )>
244  
245 Get the current assertion handler.
246  
247 This returns the function pointer that is called when an assertion is
248 triggered. This is either the value last passed to L<<
249 C<SDL_SetAssertionHandler( ... )>|/C<SDL_SetAssertionHandler( ... )> >>, or if
250 no application-specified function is set, is equivalent to calling L<<
251 C<SDL_GetDefaultAssertionHandler( )>|/C<SDL_GetDefaultAssertionHandler( )> >>.
252  
253 The parameter C<puserdata> is a pointer to a C<void*>, which will store the
254 "userdata" pointer that was passed to L<< C<SDL_SetAssertionHandler( ...
255 )>|/C<SDL_SetAssertionHandler( ... )> >>. This value will always be NULL for
256 the default handler. If you don't care about this data, it is safe to pass a
257 NULL pointer to this function to ignore it.
258  
259 Expected parameters include:
260  
261 =over
262  
263 =item C<puserdata> pointer which is filled with the "userdata" pointer that was passed to L<< C<SDL_SetAssertionHandler( ... )>|/C<SDL_SetAssertionHandler( ... )> >>
264  
265 =back
266  
267 Returns the C<SDL_AssertionHandler> that is called when an assert triggers.
268  
269 =head2 C<SDL_GetAssertionReport( )>
270  
271 Get a list of all assertion failures.
272  
273 This function gets all assertions triggered since the last call to L<<
274 C<SDL_ResetAssertionReport( )>|/C<SDL_ResetAssertionReport( )> >>, or the start
275 of the program.
276  
277 The proper way to examine this data looks something like this:
278  
279 my $item = SDL_GetAssertionReport();
280 while ($item) {
281 printf( " ==> '%s', %s (%s:%d), triggered %u times, always ignore: %s.\n\n",
282 $item->condition, $item->function, $item->filename, $item->linenum,
283 $item->trigger_count, $item->always_ignore ? 'yes' : 'no' );
284 $item = $item->next;
285 }
286  
287 Returns a list of all failed assertions or NULL if the list is empty. This
288 memory should not be modified or freed by the application.
289  
290 =head2 C<SDL_ResetAssertionReport( )>
291  
292 Clear the list of all assertion failures.
293  
294 SDL_ResetAssertionReport( );
295  
296 This function will clear the list of all assertions triggered up to that point.
297 Immediately following this call, L<< C<SDL_GetAssertionReport(
298 )>|/C<SDL_GetAssertionReport( )> >> will return no items. In addition, any
299 previously-triggered assertions will be reset to a C<trigger_count> of zero,
300 and their C<always_ignore> state will be false.
301  
302  
303 =head2 C<SDL_assert( ... )>
304  
305 Use this macro to create an assertion for debugging.
306  
307 SDL_assert(1 == 0); # triggers an assertion.
308 SDL_assert(1 == 1); # does NOT trigger an assertion
309  
310 This function is enabled only when the C<SDL_ASSERT_LEVEL> is set to C<2> or
311 C<3>, otherwise it is disabled.
312  
313 Expected parameters include:
314  
315 =over
316  
317 =item C<condition> - the expression to check
318  
319 =back
320  
321 =head2 C<SDL_assert_release( ... )>
322  
323 Use this function to create an assertion for release builds.
324  
325 SDL_assert_release( time > -f __FILE__ );
326  
327 Expected parameters include:
328  
329 =over
330  
331 =item C<condition> - the expression to check
332  
333 =back
334  
335 This function is enabled by default. It can be disabled by setting the
336 C<SDL_ASSERT_LEVEL> to C<0>.
337  
338 =head2 C<SDL_assert_paranoid( ... )>
339  
340 Use this function to create an assertion for detailed checking.
341  
342 SDL_assert_paranoid( 5 == 10 );
343  
344 This function is disabled by default. It is available for use only when the
345 C<SDL_ASSERT_LEVEL> is set to C<3>.
346  
347 Expected parameters include:
348  
349 =over
350  
351 =item C<condition> - the expression to check
352  
353 =back
354  
355 =head2 C<SDL_assert_always( ... )>
356  
357 Use this function to create an assertion regardless of the current
358 C<SDL_ASSERT_LEVEL>.
359  
360 SDL_assert_always( 5 == 10 );
361  
362 Expected parameters include:
363  
364 =over
365  
366 =item C<condition> - the expression to check
367  
368 =back
369  
370 =head1 Types
371  
372 =head2 C<SDL_AssertionHandler>
373  
374 A callback that fires when an SDL assertion fails.
375  
376 This function should expect the following parameters:
377  
378 =over
379  
380 =item C<data> - a pointer to the L<SDL2::AssertData> structure corresponding to the current assertion
381  
382 =item C<userdata> - what was passed as C<userdata> to C<SDL_SetAssertionHandler( )>
383  
384 =back
385  
386 You should return an L<< assert state|/C<:assertState> >> value indicating how
387 to handle the failure.
388  
389 =head1 Defines and Enum
390  
391 Defines and Enumerations listed here may be imported from SDL2::FFI with the
392 following tags:
393  
394 =head2 C<:assertState>
395  
396 =over
397  
398 =item C<SDL_ASSERTION_RETRY> - Retry the assert immediately
399  
400 =item C<SDL_ASSERTION_BREAK> - Make the debugger trigger a breakpoint
401  
402 =item C<SDL_ASSERTION_ABORT> - Terminate the program
403  
404 =item C<SDL_ASSERTION_IGNORE> - Ignore the assert
405  
406 =item C<SDL_ASSERTION_ALWAYS_IGNORE> - Ignore the assert from now on
407  
408 =back
409  
410 =head1 LICENSE
411  
412 Copyright (C) Sanko Robinson.
413  
414 This library is free software; you can redistribute it and/or modify it under
415 the terms found in the Artistic License 2. Other copyrights, terms, and
416 conditions may apply to data transmitted through this module.
417  
418 =head1 AUTHOR
419  
420 Sanko Robinson E<lt>sanko@cpan.orgE<gt>
421  
422 =begin stopwords
423  
424 mutex userdata
425  
426 =end stopwords
427  
428 =cut
429  
430 };
431 1;