Debian Codegraph @

lib/FFI/Platypus/Closure.pm

raw
1 package FFI::Platypus::Closure;
2
3 use strict;
4 use warnings;
5 use 5.008004;
6 use FFI::Platypus;
7 use Scalar::Util qw( refaddr);
8 use Carp qw( croak );
9 use overload '&{}' => sub {
10 my $self = shift;
11 sub { $self->{code}->(@_) };
12 }, bool => sub { 1 }, fallback => 1;
13
14 # ABSTRACT: Platypus closure object
15 our $VERSION = '2.12'; # VERSION
16
17
18 sub new
19 {
20 my($class, $coderef) = @_;
21 croak "not a coderef" unless ref($coderef) eq 'CODE';
22 my $self = bless { code => $coderef, cbdata => {}, sticky => 0 }, $class;
23 $self;
24 }
25
26 sub add_data
27 {
28 my($self, $payload, $type) = @_;
29 $self->{cbdata}{$type} = bless \$payload, 'FFI::Platypus::ClosureData';
30 }
31
32 sub get_data
33 {
34 my($self, $type) = @_;
35
36 if (exists $self->{cbdata}->{$type}) {
37 return ${$self->{cbdata}->{$type}};
38 }
39
40 return 0;
41 }
42
43
44 sub call
45 {
46 my $self = shift;
47 $self->{code}->(@_)
48 }
49
50
51 sub sticky
52 {
53 my($self) = @_;
54 unless( $self->{sticky}) {
55 $self->{sticky} = 1;
56 $self->_sticky;
57 }
58 return $self;
59 }
60
61
62 sub unstick
63 {
64 my($self) = @_;
65 if( $self->{sticky} ) {
66 $self->{sticky} = 0;
67 $self->_unstick;
68 }
69 return $self;
70 }
71
72 package FFI::Platypus::ClosureData;
73
74 our $VERSION = '2.12'; # VERSION
75
76 1;
77
78 __END__
79
80 =pod
81
82 =encoding UTF-8
83
84 =head1 NAME
85
86 FFI::Platypus::Closure - Platypus closure object
87
88 =head1 VERSION
89
90 version 2.12
91
92 =head1 SYNOPSIS
93
94 create closure with OO interface
95
96 use FFI::Platypus::Closure;
97 my $closure = FFI::Platypus::Closure->new(sub { print "hello world\n" });
98
99 create closure from Platypus object
100
101 use FFI::Platypus 2.00;
102 my $ffi = FFI::Platypus->new( api => 2 );
103 my $closure = $ffi->closure(sub { print "hello world\n" });
104
105 use closure
106
107 $ffi->function(foo => ['()->void'] => 'void')->call($closure);
108
109 =head1 DESCRIPTION
110
111 This class represents a Perl code reference that can be called from compiled code.
112 When you create a closure object, you can pass it into any function that expects
113 a function pointer. Care needs to be taken with closures because compiled languages
114 typically have a different way of handling lifetimes of objects. You have to make
115 sure that if the compiled code is going to call a closure that the closure object
116 is still in scope somewhere, or has been made sticky, otherwise you may get a
117 segment violation or other mysterious crash.
118
119 =head1 CONSTRUCTOR
120
121 =head2 new
122
123 my $closure = FFI::Platypus::Closure->new($coderef);
124
125 Create a new closure object; C<$coderef> must be a subroutine code reference.
126
127 =head1 METHODS
128
129 =head2 call
130
131 $closure->call(@arguments);
132 $closure->(@arguments);
133
134 Call the closure from Perl space. May also be invoked by treating
135 the closure object as a code reference.
136
137 =head2 sticky
138
139 $closure->sticky;
140
141 Mark the closure sticky, meaning that it won't be free'd even if
142 all the reference of the object fall out of scope.
143
144 [version 2.12]
145
146 Returns C<$self>, so this method may be chained.
147
148 =head2 unstick
149
150 $closure->unstick;
151
152 Unmark the closure as sticky.
153
154 [version 2.12]
155
156 Returns C<$self>, so this method may be chained.
157
158 =head1 AUTHOR
159
160 Author: Graham Ollis E<lt>[email protected]<gt>
161
162 Contributors:
163
164 Bakkiaraj Murugesan (bakkiaraj)
165
166 Dylan Cali (calid)
167
168 pipcet
169
170 Zaki Mughal (zmughal)
171
172 Fitz Elliott (felliott)
173
174 Vickenty Fesunov (vyf)
175
176 Gregor Herrmann (gregoa)
177
178 Shlomi Fish (shlomif)
179
180 Damyan Ivanov
181
182 Ilya Pavlov (Ilya33)
183
184 Petr Písař (ppisar)
185
186 Mohammad S Anwar (MANWAR)
187
188 Håkon Hægland (hakonhagland, HAKONH)
189
190 Meredith (merrilymeredith, MHOWARD)
191
192 Diab Jerius (DJERIUS)
193
194 Eric Brine (IKEGAMI)
195
196 szTheory
197
198 José Joaquín Atria (JJATRIA)
199
200 Pete Houston (openstrike, HOUSTON)
201
202 Lukas Mai (MAUKE)
203
204 =head1 COPYRIGHT AND LICENSE
205
206 This software is copyright (c) 2015-2022 by Graham Ollis.
207
208 This is free software; you can redistribute it and/or modify it under
209 the same terms as the Perl 5 programming language system itself.
210
211 =cut