@@ -454,45 +454,71 @@ void PerlOMP_VERIFY_2D_AoA(SV *AoA) {
454
454
}
455
455
}
456
456
457
- /* Helper function to verify element types */
458
- bool is_float (SV * sv ) { return SvNOK (sv ); }
459
- bool is_int (SV * sv ) { return SvIOK (sv ); }
460
- bool is_string (SV * sv ) { return SvPOK (sv ); }
457
+ /* Checks if an SV is an integer, allowing for stored floats that are whole numbers */
458
+ bool is_int (SV * sv ) {
459
+ return SvIOK (sv ) || (SvNOK (sv ) && SvIV (sv ) == SvNV (sv ));
460
+ }
461
+
462
+ /* Checks if an SV is a float, including cases where Perl stores it as an integer */
463
+ bool is_float (SV * sv ) {
464
+ return SvNOK (sv ) || (SvIOK (sv ) && SvIV (sv ) != SvNV (sv ));
465
+ }
466
+
467
+ /* Checks if an SV is a string, ensuring it's not just a numeric representation */
468
+ bool is_string (SV * sv ) {
469
+ return SvPOK (sv ) || (!SvNOK (sv ) && !SvIOK (sv ));
470
+ }
461
471
462
- /* Generic function to verify a 1D array's element type */
463
472
void verify_1D_array_type (SV * array , bool (* type_check )(SV * ), const char * type_name ) {
464
473
if (!is_array_ref (array )) {
465
474
croak ("Expected a 1D array reference" );
466
475
}
467
476
AV * av = (AV * )SvRV (array );
468
- I32 len = av_len (av ) + 1 ;
477
+ I32 len = av_len (av );
478
+ if (len == -1 ) return ; // Handle empty arrays safely
479
+ len ++ ; // Convert last index to total count
480
+
469
481
for (I32 i = 0 ; i < len ; i ++ ) {
470
482
SV * * element = av_fetch (av , i , 0 );
471
- if (!element || !type_check (* element )) {
472
- croak ("Expected all elements to be %s at index %d" , type_name , i );
483
+ if (!element || !* element || ! type_check (* element )) {
484
+ croak ("Expected all elements to be %s at index %" IVdf , type_name , ( IV ) i );
473
485
}
474
486
}
475
487
}
476
488
477
489
/* Implement type-specific 1D array verifications */
478
490
void PerlOMP_VERIFY_1D_FLOAT_ARRAY (SV * array ) { verify_1D_array_type (array , is_float , "float" ); }
479
491
void PerlOMP_VERIFY_1D_INT_ARRAY (SV * array ) { verify_1D_array_type (array , is_int , "integer" ); }
480
- void PerlOMP_VERIFY_1D_DOUBLE_ARRAY (SV * array ) { verify_1D_array_type (array , is_float , "double" ); }
481
492
void PerlOMP_VERIFY_1D_STRING_ARRAY (SV * array ) { verify_1D_array_type (array , is_string , "string" ); }
482
493
483
494
/* Generic function to verify a 2D array's element type */
484
495
void verify_2D_array_type (SV * AoA , bool (* type_check )(SV * ), const char * type_name ) {
485
- PerlOMP_VERIFY_2D_AoA (AoA );
496
+ PerlOMP_VERIFY_2D_AoA (AoA ); // Assuming this macro validates AoA correctly
497
+
498
+ if (!is_array_ref (AoA )) {
499
+ croak ("Expected a 2D array reference" );
500
+ }
501
+
486
502
AV * outer = (AV * )SvRV (AoA );
487
- I32 rows = av_len (outer ) + 1 ;
503
+ I32 rows = av_len (outer );
504
+ if (rows == -1 ) return ; // Handle empty outer array safely
505
+ rows ++ ; // Convert last index to total count
506
+
488
507
for (I32 i = 0 ; i < rows ; i ++ ) {
489
508
SV * * inner_ref = av_fetch (outer , i , 0 );
509
+ if (!inner_ref || !* inner_ref || !is_array_ref (* inner_ref )) {
510
+ croak ("Expected an array reference at row %" IVdf , (IV )i );
511
+ }
512
+
490
513
AV * inner = (AV * )SvRV (* inner_ref );
491
- I32 cols = av_len (inner ) + 1 ;
514
+ I32 cols = av_len (inner );
515
+ if (cols == -1 ) continue ; // Handle empty inner arrays safely
516
+ cols ++ ; // Convert last index to total count
517
+
492
518
for (I32 j = 0 ; j < cols ; j ++ ) {
493
519
SV * * element = av_fetch (inner , j , 0 );
494
- if (!element || !type_check (* element )) {
495
- croak ("Expected all elements to be %s at [%d ][%d ]" , type_name , i , j );
520
+ if (!element || !* element || ! type_check (* element )) {
521
+ croak ("Expected all elements to be %s at [%" IVdf " ][%" IVdf " ]" , type_name , ( IV ) i , ( IV ) j );
496
522
}
497
523
}
498
524
}
@@ -501,6 +527,5 @@ void verify_2D_array_type(SV *AoA, bool (*type_check)(SV *), const char *type_na
501
527
/* Implement type-specific 2D array verifications */
502
528
void PerlOMP_VERIFY_2D_FLOAT_ARRAY (SV * AoA ) { verify_2D_array_type (AoA , is_float , "float" ); }
503
529
void PerlOMP_VERIFY_2D_INT_ARRAY (SV * AoA ) { verify_2D_array_type (AoA , is_int , "integer" ); }
504
- void PerlOMP_VERIFY_2D_DOUBLE_ARRAY (SV * AoA ) { verify_2D_array_type (AoA , is_float , "double" ); }
505
530
void PerlOMP_VERIFY_2D_STRING_ARRAY (SV * AoA ) { verify_2D_array_type (AoA , is_string , "string" ); }
506
531
0 commit comments