Skip to content

Commit

Permalink
Lazy enum values by installing the CODE ref as required (PerlFFI#11)
Browse files Browse the repository at this point in the history
 Issues:
 - Cannot cast value back to name (for return values from C, etc.)
  • Loading branch information
sanko committed Aug 24, 2021
1 parent 9531dcd commit f2dd466
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/FFI/Platypus/Type/Enum.pm
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ sub ffi_custom_type_api_1
foreach my $name ($name,@aliases)
{
my $full = join '::', $package, $prefix . ( $config{casing} eq 'upper' ? uc($name) : $name );
constant->import($full, $index);
no strict 'refs';
ref $index eq 'CODE' ? *{$full} = $index : constant->import($full, $index);
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions t/ffi_platypus_type_enum.t
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,21 @@ subtest 'dualvar' => sub {

};

subtest 'code' => sub {

my $ffi = FFI::Platypus->new( api => 1 );

$ffi->load_custom_type('::Enum', 'enum1', { package => 'Foo4', casing => 'keep' },
'zero',
'one',
'two',
'three',
['five' => sub () { Foo4::two() + Foo4::three() } ]
);

is($ffi->cast('enum', 'enum1', 2), 'two');
todo '::Enum cannot back cast lazy values' => sub {is($ffi->cast('enum', 'enum1', 5), 'five')};
is(Foo4::five(), 5);
};

done_testing;

0 comments on commit f2dd466

Please sign in to comment.