Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a Moose Role for FFI #3

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Using a Moose Role for FFI #3

wants to merge 12 commits into from

Conversation

xsawyerx
Copy link
Owner

[The MooseX::Role::FFI is provided here to make the review of both
easier to do at the same time. It will be released separately if
we were to approve this PR.]

The MooseX::Role::FFI allows to specify the subroutine data and the
library name - as well as the fallback - as subroutines.

It will then generate the corresponding callback for each sub, which
can be reached via a method.

The after allows us to have direct access to the FFI object before
the callbacks are generated. (Which I think is when we need it for
the mangler to work, but I'm not sure.)

What I like about it:

  • Metadata, the rest is generated.
  • Alien support built-in.
  • We don't need to have class-level variables (though we could change
    it to it instead of subroutines.)
  • We don't need to check that it has been instantiated (like we do
    in _get_version and $HAS_SUBS)

What I don't like about it:

  • We need to make another method call to get the callback, depsite
    it being a definition of the class, not the instance.
  • If you have a typo in the callback name, you cannot catch it,
    because it's a string to retrieve it.

I'm not sure the benefits outweigh the drawbacks. I was thinking of
possibly providing another interface and saying "pick yours."

[The MooseX::Role::FFI is provided here to make the review of both
easier to do at the same time. It will be released separately if
we were to approve this PR.]

The MooseX::Role::FFI allows to specify the subroutine data and the
library name - as well as the fallback - as subroutines.

It will then generate the corresponding callback for each sub, which
can be reached via a method.

The `after` allows us to have direct access to the FFI object *before*
the callbacks are generated. (Which I think is when we need it for
the mangler to work, but I'm not sure.)

What I like about it:

* Metadata, the rest is generated.
* Alien support built-in.
* We don't need to have class-level variables (though we could change
  it to it instead of subroutines.)
* We don't need to check that it has been instantiated (like we do
  in _get_version and $HAS_SUBS)

What I don't like about it:

* We need to make another method call to get the callback, depsite
  it being a definition of the class, not the instance.
* If you have a typo in the callback name, you cannot catch it,
  because it's a string to retrieve it.

I'm not sure the benefits outweigh the drawbacks. I was thinking of
possibly providing *another* interface and saying "pick yours."
@xsawyerx xsawyerx requested a review from plicease June 27, 2019 10:28
@xsawyerx xsawyerx self-assigned this Jun 27, 2019
@@ -3,7 +3,7 @@ use warnings;
use Test::More;
use Audio::Chromaprint;

note "version = ", Audio::Chromaprint->get_version;
note "version = ", Audio::Chromaprint->new->get_version;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

or croak('Unable to get raw fingerprint (get_raw_fingerprint)');

# not espeically fast, but need a cast with a variable length array
my $fp = FFI::Platypus->new->cast( 'opaque' => "uint32[$size]", $ptr );
_dealloc($ptr);
my $fp = $self->ffi->cast( 'opaque' => "uint32[$size]", $ptr );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


return _get_version();
around '_build_ffi' => sub {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can do this with an optional ffi_mangler ?

# ABSTRACT: Easily create interfaces to FFI functions with Moose roles

use Moose::Role;
use FFI::Platypus 0.89_01;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please bump this to 0.90.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants