Affix
view release on metacpan or search on metacpan
t/999_marshaller_2.t view on Meta::CPAN
subtest struct => sub {
typedef Pos => Struct [ x => Int, y => Double ];
affix $lib_path, 'check_pos_val', [ Pos() ], Bool;
affix $lib_path, 'check_pos_ptr', [ Pointer [ Pos() ] ], Bool;
# Allocate raw memory
ok my $mem = alloc_owned( sizeof( Pos() ) ), 'alloc_owned memory for Pos';
# Cast the raw memory to our Pos struct
# This creates a magical 2.0 pin variable
ok my $p = cast( $mem, Pos() ), 'cast memory to Pos struct';
# Manipulate fields natively via magic
$p->{x} = 10;
$p->{y} = 20.5;
# Verify native fields
is $p->{x}, 10, 'field x is 10';
is $p->{y}, 20.5, 'field y is 20.5';
# Pass the magical pin to FFI (By Value)
# Affix will use memcpy because it's a pin
ok check_pos_val($p), 'FFI check_pos_val($p) - passed by value';
# Pass the magical pin to FFI (By Pointer)
# Affix will use get_address_v2 to resolve the pointer
ok check_pos_ptr($p), 'FFI check_pos_ptr($p) - passed by pointer';
diag sprintf( "Address: 0x%X", address($p) );
};
subtest company => sub {
typedef Task => Struct [ id => Int, name => Array [ Char, 16 ] ];
typedef Employee => Struct [ name => Array [ Char, 32 ], tasks => Array [ Task(), 2 ] ];
typedef Company => Struct [ manager => Pointer [ Employee() ], budget => Int ];
#
affix $lib_path, 'verify_hierarchy', [ Pointer [ Company() ] ], Bool;
# Setup nested data in C memory
ok my $mem_comp = alloc_owned( sizeof( Company() ) ), 'alloc Company';
ok my $mem_mgr = alloc_owned( sizeof( Employee() ) ), 'alloc Manager';
ok my $comp = cast( $mem_comp, Company() ), 'cast Company';
ok my $mgr = cast( $mem_mgr, Employee() ), 'cast Manager';
# Link them via pointer
$comp->{manager} = address($mgr);
$comp->{budget} = 50000;
is $comp->{budget}, 50000, 'Direct hash access to C memory works';
# Set deep values
$mgr->{name} = "Alice";
$mgr->{tasks}[0]{id} = 101;
$mgr->{tasks}[0]{name} = "FFI Core";
$mgr->{tasks}[1]{id} = 102;
$mgr->{tasks}[1]{name} = "Marshal v2";
# TEST DEEP ACCESS: $comp -> manager (ptr) -> tasks (array) -> name (string)
is $comp->{manager}{name}, "Alice", 'Deep Read: manager->name';
is $comp->{manager}{tasks}[0]{id}, 101, 'Deep Read: manager->tasks[0]->id';
# TEST DEEP WRITE via the pointer member
$comp->{manager}{tasks}[0]{id} = 999;
is $mgr->{tasks}[0]{id}, 999, 'Deep Write confirmed in original memory';
# Reset for verification function
$comp->{manager}{tasks}[0]{id} = 101;
# Pass to C
ok verify_hierarchy($comp), 'FFI: verify_hierarchy($comp) - deep validation passed';
};
subtest 'smart & safety' => sub {
# Types were defined in previous subtests
affix $lib_path, 'get_null_company', [], Pointer [ Company() ];
ok my $mem_comp = alloc_owned( sizeof( Company() ) ), 'alloc Company';
ok my $mem_mgr = alloc_owned( sizeof( Employee() ) ), 'alloc Manager';
ok my $comp = cast( $mem_comp, Company() ), 'cast Company';
ok my $mgr = cast( $mem_mgr, Employee() ), 'cast Manager';
$mgr->{name} = "Bob";
$comp->{budget} = 50000;
# We assign the $mgr pin DIRECTLY to the manager pointer field.
$comp->{manager} = $mgr;
is address( $comp->{manager} ), address($mgr), 'Smart Assignment: Pin converted to address automatically';
is $comp->{manager}{name}, "Bob", 'Data accessible through smart-assigned pointer';
# Traversing a NULL pointer in a struct
$comp->{manager} = undef; # Set C pointer to NULL
like dies { $comp->{manager}{name} }, qr[undefined value], 'Accessing NULL pointer member is a fatal exception';
# returning NULL
ok my $null_comp = get_null_company(), 'C returns pointer to struct with NULL member';
is $null_comp->{manager}, undef, 'C NULL pointer correctly becomes Perl undef';
# Deep Null
like dies { $null_comp->{manager}{tasks}[0]{id} }, qr[undefined value], 'Deep access on C NULL throws Perl exception';
};
subtest 'Giant Array & Anon Types' => sub {
my $type = Struct [ a => Int, b => Int ];
my $mem = alloc_owned( sizeof($type) );
# TEST ANONYMOUS TYPE EVAPORATION
{
my $p = cast( $mem, $type );
is $p->{a}, 0, 'Anonymous struct works';
}
# At this point, free_v2_pin was called, and the local arena for that
# struct definition is gone. No leak in the global registry!
# TEST GIANT ARRAY SWITCH
# Imagine a C array of 10,000 ints
typedef BigArray => Array [ Int, 10000 ];
$mem = alloc_owned( sizeof( BigArray() ) );
my $arr_pin = cast( $mem, BigArray() );
# $arr_pin is currently a scalar (Lazy Placeholder)
#~ ok !SvROK($arr_pin), 'Giant array is still a lazy scalar';
# Accessing it vivifies the AV
is $arr_pin->[500], 0, 'Accessing giant array element vivifies it on demand';
#~ ok SvROK($arr_pin), 'Now it is a real array reference';
};
subtest calculator => sub {
( run in 0.891 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )