FFI-C-0.15/000755 000000 000000 00000000000 14334741503 012205 5ustar00rootroot000000 000000 FFI-C-0.15/Changes000644 000000 000000 00000003134 14334741503 013501 0ustar00rootroot000000 000000 Revision history for FFI-C 0.15 2022-11-15 09:50:07 -0700 - Update test for FreeBSD (gh#61) 0.14 2022-11-14 05:38:05 -0700 - Update test for NetBSD (gh#60) 0.12 2022-06-24 10:08:47 -0500 - Documentation improvements (gh#58) 0.11 2021-12-06 15:59:29 -0700 - Add addressof function to FFI::C::Util (gh#49) - Fix version prereq for Sub::Identify (gh#55) - Fix test on OpenBSD, probably because we are testing undefined behavior (gh#56) 0.10 2020-09-23 03:16:36 -0600 - Add tmpfile constructor for FFI::C::File (gh#41) 0.09 2020-09-22 14:28:36 -0600 - Move to PerlFFI org (gh#37) - Add FFI::C::File class (gh#38) - Add FFI::C::PosixFile class (gh#38) 0.08 2020-05-12 19:20:26 -0400 - Added enum method to FFI::C (gh#23, gh#36) 0.07 2020-05-11 19:50:38 -0400 - Added trim_string property to FFI::C::StructDef (gh#35) 0.06 2020-05-09 04:41:32 -0600 - Bumped Test2::Suite prereq to get float (gh#33) - Fixes for Perls with 32bit IV (gh#34) 0.05 2020-05-07 04:58:02 -0600 - Added set_array_count function to FFI::C::Util (gh#31) 0.04 2020-05-05 09:24:13 -0600 - non-generated struct/union objects properly support can (gh#28) - Added nullable property to FFI::C::Def class (gh#30) 0.03 2020-05-03 15:57:56 -0600 - Allow creating variable array with array ref initalization (gh#27) (that this was missing from the initial release was an oversight) 0.02 2020-05-03 07:57:00 -0600 - Fixed floating point tests with longdouble and quadmath Perls (gh#24, gh#25) 0.01 2020-05-02 17:15:38 -0600 - initial version FFI-C-0.15/INSTALL000644 000000 000000 00000004472 14334741503 013245 0ustar00rootroot000000 000000 This is the Perl distribution FFI-C. Installing FFI-C is straightforward. ## Installation with cpanm If you have cpanm, you only need one line: % cpanm FFI::C If it does not have permission to install modules to the current perl, cpanm will automatically set up and install to a local::lib in your home directory. See the local::lib documentation (https://metacpan.org/pod/local::lib) for details on enabling it in your environment. ## Installing with the CPAN shell Alternatively, if your CPAN shell is set up, you should just be able to do: % cpan FFI::C ## Manual installation As a last resort, you can manually install it. If you have not already downloaded the release tarball, you can find the download link on the module's MetaCPAN page: https://metacpan.org/pod/FFI::C Untar the tarball, install configure prerequisites (see below), then build it: % perl Makefile.PL % make && make test Then install it: % make install On Windows platforms, you should use `dmake` or `nmake`, instead of `make`. If your perl is system-managed, you can create a local::lib in your home directory to install modules to. For details, see the local::lib documentation: https://metacpan.org/pod/local::lib The prerequisites of this distribution will also have to be installed manually. The prerequisites are listed in one of the files: `MYMETA.yml` or `MYMETA.json` generated by running the manual build process described above. ## Configure Prerequisites This distribution requires other modules to be installed before this distribution's installer can be run. They can be found under the "configure_requires" key of META.yml or the "{prereqs}{configure}{requires}" key of META.json. ## Other Prerequisites This distribution may require additional modules to be installed after running Makefile.PL. Look for prerequisites in the following phases: * to run make, PHASE = build * to use the module code itself, PHASE = runtime * to run tests, PHASE = test They can all be found in the "PHASE_requires" key of MYMETA.yml or the "{prereqs}{PHASE}{requires}" key of MYMETA.json. ## Documentation FFI-C documentation is available as POD. You can run `perldoc` from a shell to read the documentation: % perldoc FFI::C For more information on installing Perl modules via CPAN, please see: https://www.cpan.org/modules/INSTALL.html FFI-C-0.15/LICENSE000644 000000 000000 00000043667 14334741503 013232 0ustar00rootroot000000 000000 This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Terms of the Perl programming language system itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --- The GNU General Public License, Version 1, February 1989 --- This software is Copyright (c) 2020-2022 by Graham Ollis. This is free software, licensed under: The GNU General Public License, Version 1, February 1989 GNU GENERAL PUBLIC LICENSE Version 1, February 1989 Copyright (C) 1989 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The license agreements of most software companies try to keep users at the mercy of those companies. By contrast, our General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. The General Public License applies to the Free Software Foundation's software and to any other program whose authors commit to using it. You can use it for your programs, too. When we speak of free software, we are referring to freedom, not price. Specifically, the General Public License is designed to make sure that you have the freedom to give away or sell copies of free software, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of a such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must tell them their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any work containing the Program or a portion of it, either verbatim or with modifications. Each licensee is addressed as "you". 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this General Public License and to the absence of any warranty; and give any other recipients of the Program a copy of this General Public License along with the Program. You may charge a fee for the physical act of transferring a copy. 2. You may modify your copy or copies of the Program or any portion of it, and copy and distribute such modifications under the terms of Paragraph 1 above, provided that you also do the following: a) cause the modified files to carry prominent notices stating that you changed the files and the date of any change; and b) cause the whole of any work that you distribute or publish, that in whole or in part contains the Program or any part thereof, either with or without modifications, to be licensed at no charge to all third parties under the terms of this General Public License (except that you may choose to grant warranty protection to some or all third parties, at your option). c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the simplest and most usual way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this General Public License. d) You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. Mere aggregation of another independent work with the Program (or its derivative) on a volume of a storage or distribution medium does not bring the other work under the scope of these terms. 3. You may copy and distribute the Program (or a portion or derivative of it, under Paragraph 2) in object code or executable form under the terms of Paragraphs 1 and 2 above provided that you also do one of the following: a) accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Paragraphs 1 and 2 above; or, b) accompany it with a written offer, valid for at least three years, to give any third party free (except for a nominal charge for the cost of distribution) a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Paragraphs 1 and 2 above; or, c) accompany it with the information you received as to where the corresponding source code may be obtained. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form alone.) Source code for a work means the preferred form of the work for making modifications to it. For an executable file, complete source code means all the source code for all modules it contains; but, as a special exception, it need not include source code for modules which are standard libraries that accompany the operating system on which the executable file runs, or for standard header files or definitions files that accompany that operating system. 4. You may not copy, modify, sublicense, distribute or transfer the Program except as expressly provided under this General Public License. Any attempt otherwise to copy, modify, sublicense, distribute or transfer the Program is void, and will automatically terminate your rights to use the Program under this License. However, parties who have received copies, or rights to use copies, from you under this General Public License will not have their licenses terminated so long as such parties remain in full compliance. 5. By copying, distributing or modifying the Program (or any work based on the Program) you indicate your acceptance of this license to do so, and all its terms and conditions. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. 7. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of the license which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the license, you may choose any version ever published by the Free Software Foundation. 8. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to humanity, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19xx name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (a program to direct compilers to make passes at assemblers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice That's all there is to it! --- The Artistic License 1.0 --- This software is Copyright (c) 2020-2022 by Graham Ollis. This is free software, licensed under: The Artistic License 1.0 The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End FFI-C-0.15/MANIFEST000644 000000 000000 00000002532 14334741503 013340 0ustar00rootroot000000 000000 # This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.025. Changes INSTALL LICENSE MANIFEST META.json META.yml Makefile.PL README author.yml corpus/examples/lib/main.pm dist.ini examples/synopsis/array.pl examples/synopsis/arraydef.c examples/synopsis/arraydef.pl examples/synopsis/c.c examples/synopsis/c.pl examples/synopsis/struct.pl examples/synopsis/structdef.c examples/synopsis/structdef.pl examples/synopsis/union.pl examples/synopsis/uniondef.c examples/synopsis/uniondef.pl examples/synopsis/util.pl examples/time.pl lib/FFI/C.pm lib/FFI/C/Array.pm lib/FFI/C/ArrayDef.pm lib/FFI/C/Def.pm lib/FFI/C/FFI.pm lib/FFI/C/File.pm lib/FFI/C/PosixFile.pm lib/FFI/C/Struct.pm lib/FFI/C/StructDef.pm lib/FFI/C/Union.pm lib/FFI/C/UnionDef.pm lib/FFI/C/Util.pm maint/cip-before-install maint/cip-before-install-inside maint/gh-macos maint/gh-windows.pl perlcriticrc t/00_diag.t t/ffi_c.t t/ffi_c_array.t t/ffi_c_arraydef.t t/ffi_c_def.t t/ffi_c_enumdef.t t/ffi_c_ffi.t t/ffi_c_file.t t/ffi_c_posixfile.t t/ffi_c_struct.t t/ffi_c_structdef.t t/ffi_c_union.t t/ffi_c_uniondef.t t/ffi_c_util.t xt/author/critic.t xt/author/eol.t xt/author/examples.t xt/author/no_tabs.t xt/author/pod.t xt/author/pod_coverage.t xt/author/pod_spelling_common.t xt/author/pod_spelling_system.t xt/author/strict.t xt/author/version.t xt/release/changes.t xt/release/fixme.t FFI-C-0.15/META.json000644 000000 000000 00000016156 14334741503 013637 0ustar00rootroot000000 000000 { "abstract" : "C data types for FFI", "author" : [ "Graham Ollis " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 6.025, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "FFI-C", "no_index" : { "directory" : [ "examples" ] }, "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0", "perl" : "5.008001" } }, "develop" : { "recommends" : { "Dist::Zilla::Plugin::Author::Plicease::Core" : "0", "Dist::Zilla::Plugin::Author::Plicease::Upload" : "0", "Dist::Zilla::Plugin::InsertExample" : "0", "Dist::Zilla::Plugin::MetaNoIndex" : "0", "Dist::Zilla::Plugin::Prereqs" : "0", "Dist::Zilla::Plugin::PruneFiles" : "0", "Dist::Zilla::PluginBundle::Author::Plicease" : "2.69", "Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep" : "0", "Perl::Critic::Policy::BuiltinFunctions::ProhibitStringyEval" : "0", "Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit" : "0", "Perl::Critic::Policy::BuiltinFunctions::ProhibitVoidGrep" : "0", "Perl::Critic::Policy::BuiltinFunctions::ProhibitVoidMap" : "0", "Perl::Critic::Policy::ClassHierarchies::ProhibitExplicitISA" : "0", "Perl::Critic::Policy::ClassHierarchies::ProhibitOneArgBless" : "0", "Perl::Critic::Policy::CodeLayout::ProhibitHardTabs" : "0", "Perl::Critic::Policy::CodeLayout::ProhibitTrailingWhitespace" : "0", "Perl::Critic::Policy::CodeLayout::RequireConsistentNewlines" : "0", "Perl::Critic::Policy::Community::ArrayAssignAref" : "0", "Perl::Critic::Policy::Community::BarewordFilehandles" : "0", "Perl::Critic::Policy::Community::ConditionalDeclarations" : "0", "Perl::Critic::Policy::Community::ConditionalImplicitReturn" : "0", "Perl::Critic::Policy::Community::DeprecatedFeatures" : "0", "Perl::Critic::Policy::Community::DiscouragedModules" : "0", "Perl::Critic::Policy::Community::DollarAB" : "0", "Perl::Critic::Policy::Community::Each" : "0", "Perl::Critic::Policy::Community::EmptyReturn" : "0", "Perl::Critic::Policy::Community::IndirectObjectNotation" : "0", "Perl::Critic::Policy::Community::LexicalForeachIterator" : "0", "Perl::Critic::Policy::Community::LoopOnHash" : "0", "Perl::Critic::Policy::Community::ModPerl" : "0", "Perl::Critic::Policy::Community::OpenArgs" : "0", "Perl::Critic::Policy::Community::OverloadOptions" : "0", "Perl::Critic::Policy::Community::POSIXImports" : "0", "Perl::Critic::Policy::Community::PackageMatchesFilename" : "0", "Perl::Critic::Policy::Community::PreferredAlternatives" : "0", "Perl::Critic::Policy::Community::StrictWarnings" : "0", "Perl::Critic::Policy::Community::Threads" : "0", "Perl::Critic::Policy::Community::Wantarray" : "0", "Perl::Critic::Policy::Community::WarningsSwitch" : "0", "Perl::Critic::Policy::Community::WhileDiamondDefaultAssignment" : "0", "Perl::Critic::Policy::ControlStructures::ProhibitLabelsWithSpecialBlockNames" : "0", "Perl::Critic::Policy::ControlStructures::ProhibitMutatingListFunctions" : "0", "Perl::Critic::Policy::ControlStructures::ProhibitUnreachableCode" : "0", "Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles" : "0", "Perl::Critic::Policy::InputOutput::ProhibitJoinedReadline" : "0", "Perl::Critic::Policy::InputOutput::ProhibitTwoArgOpen" : "0", "Perl::Critic::Policy::Miscellanea::ProhibitFormats" : "0", "Perl::Critic::Policy::Miscellanea::ProhibitUselessNoCritic" : "0", "Perl::Critic::Policy::Modules::ProhibitConditionalUseStatements" : "0", "Perl::Critic::Policy::Modules::RequireNoMatchVarsWithUseEnglish" : "0", "Perl::Critic::Policy::Objects::ProhibitIndirectSyntax" : "0", "Perl::Critic::Policy::RegularExpressions::ProhibitUselessTopic" : "0", "Perl::Critic::Policy::Subroutines::ProhibitNestedSubs" : "0", "Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros" : "0", "Perl::Critic::Policy::ValuesAndExpressions::ProhibitMixedBooleanOperators" : "0", "Perl::Critic::Policy::ValuesAndExpressions::ProhibitSpecialLiteralHeredocTerminator" : "0", "Perl::Critic::Policy::ValuesAndExpressions::RequireUpperCaseHeredocTerminator" : "0", "Perl::Critic::Policy::Variables::ProhibitPerl4PackageNames" : "0", "Perl::Critic::Policy::Variables::ProhibitUnusedVariables" : "0", "Software::License::Perl_5" : "0" }, "requires" : { "Capture::Tiny" : "0", "FFI::Build" : "0", "File::Glob" : "0", "File::Spec" : "0", "FindBin" : "0", "Path::Tiny" : "0", "Perl::Critic" : "0", "Test2::Require::Module" : "0.000121", "Test2::Tools::PerlCritic" : "0", "Test2::V0" : "0.000121", "Test::CPAN::Changes" : "0", "Test::EOL" : "0", "Test::Fixme" : "0.07", "Test::More" : "0.98", "Test::NoTabs" : "0", "Test::Pod" : "0", "Test::Pod::Coverage" : "0", "Test::Pod::Spelling::CommonMistakes" : "0", "Test::Script" : "0", "Test::Spelling" : "0", "Test::Strict" : "0", "YAML" : "0" } }, "runtime" : { "requires" : { "Class::Inspector" : "0", "FFI::Platypus" : "1.24", "FFI::Platypus::Memory" : "0", "FFI::Platypus::Type::Enum" : "0.03", "Ref::Util" : "0", "Sub::Identify" : "0.05", "Sub::Install" : "0", "Sub::Util" : "0", "perl" : "5.008001" } }, "test" : { "requires" : { "Capture::Tiny" : "0", "FFI::Platypus::Record" : "0", "Path::Tiny" : "0", "Test2::V0" : "0.000081", "perl" : "5.008001" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/PerlFFI/FFI-C/issues" }, "homepage" : "https://metacpan.org/pod/FFI::C", "repository" : { "type" : "git", "url" : "git://github.com/PerlFFI/FFI-C.git", "web" : "https://github.com/PerlFFI/FFI-C" }, "x_IRC" : "irc://irc.perl.org/#native" }, "version" : "0.15", "x_generated_by_perl" : "v5.37.4", "x_serialization_backend" : "Cpanel::JSON::XS version 4.32", "x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later", "x_use_unsafe_inc" : 0 } FFI-C-0.15/META.yml000644 000000 000000 00000002137 14334741503 013461 0ustar00rootroot000000 000000 --- abstract: 'C data types for FFI' author: - 'Graham Ollis ' build_requires: Capture::Tiny: '0' FFI::Platypus::Record: '0' Path::Tiny: '0' Test2::V0: '0.000081' perl: '5.008001' configure_requires: ExtUtils::MakeMaker: '0' perl: '5.008001' dynamic_config: 0 generated_by: 'Dist::Zilla version 6.025, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: FFI-C no_index: directory: - examples requires: Class::Inspector: '0' FFI::Platypus: '1.24' FFI::Platypus::Memory: '0' FFI::Platypus::Type::Enum: '0.03' Ref::Util: '0' Sub::Identify: '0.05' Sub::Install: '0' Sub::Util: '0' perl: '5.008001' resources: IRC: irc://irc.perl.org/#native bugtracker: https://github.com/PerlFFI/FFI-C/issues homepage: https://metacpan.org/pod/FFI::C repository: git://github.com/PerlFFI/FFI-C.git version: '0.15' x_generated_by_perl: v5.37.4 x_serialization_backend: 'YAML::Tiny version 1.73' x_spdx_expression: 'Artistic-1.0-Perl OR GPL-1.0-or-later' x_use_unsafe_inc: 0 FFI-C-0.15/Makefile.PL000644 000000 000000 00000007322 14334741503 014163 0ustar00rootroot000000 000000 BEGIN { use strict; use warnings; my %missing = map { eval qq{ require $_ }; $@ ? ($_=>1) : () } qw( Carp Exporter Scalar::Util base bytes constant overload strict warnings ); if(%missing) { print "Your Perl is missing core modules: @{[ sort keys %missing ]}\n"; print "Ideally if you are using the system Perl you can install the appropriate\n"; print "package which includes the core Perl modules. On at least some versions\n"; print "of Fedora, CentOS and RHEL, this is the `perl-core` package.\n"; print "\n"; print " % dnf install perl-core\n"; print " ~ or ~\n"; print " % yum install perl-core\n"; print "\n"; print "If you really want to install dual-life modules from CPAN, then you can\n"; print "use cpanm:\n"; print "\n"; print " % cpanm @{[ sort keys %missing ]}\n"; print "\n"; print "Note that some core modules are not available from CPAN.\n"; exit; } } BEGIN { use strict; use warnings; unless(eval q{ use 5.008001; 1}) { print "Perl 5.008001 or better required\n"; exit; } } # This file was automatically generated by Dist::Zilla::Plugin::Author::Plicease::MakeMaker v2.72. use strict; use warnings; use 5.008001; use ExtUtils::MakeMaker; my %WriteMakefileArgs = ( "ABSTRACT" => "C data types for FFI", "AUTHOR" => "Graham Ollis ", "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => 0 }, "DISTNAME" => "FFI-C", "LICENSE" => "perl", "MIN_PERL_VERSION" => "5.008001", "NAME" => "FFI::C", "PM" => { "lib/FFI/C.pm" => "\$(INST_LIB)/FFI/C.pm", "lib/FFI/C/Array.pm" => "\$(INST_LIB)/FFI/C/Array.pm", "lib/FFI/C/ArrayDef.pm" => "\$(INST_LIB)/FFI/C/ArrayDef.pm", "lib/FFI/C/Def.pm" => "\$(INST_LIB)/FFI/C/Def.pm", "lib/FFI/C/FFI.pm" => "\$(INST_LIB)/FFI/C/FFI.pm", "lib/FFI/C/File.pm" => "\$(INST_LIB)/FFI/C/File.pm", "lib/FFI/C/PosixFile.pm" => "\$(INST_LIB)/FFI/C/PosixFile.pm", "lib/FFI/C/Struct.pm" => "\$(INST_LIB)/FFI/C/Struct.pm", "lib/FFI/C/StructDef.pm" => "\$(INST_LIB)/FFI/C/StructDef.pm", "lib/FFI/C/Union.pm" => "\$(INST_LIB)/FFI/C/Union.pm", "lib/FFI/C/UnionDef.pm" => "\$(INST_LIB)/FFI/C/UnionDef.pm", "lib/FFI/C/Util.pm" => "\$(INST_LIB)/FFI/C/Util.pm" }, "PREREQ_PM" => { "Class::Inspector" => 0, "FFI::Platypus" => "1.24", "FFI::Platypus::Memory" => 0, "FFI::Platypus::Type::Enum" => "0.03", "Ref::Util" => 0, "Sub::Identify" => "0.05", "Sub::Install" => 0, "Sub::Util" => 0 }, "TEST_REQUIRES" => { "Capture::Tiny" => 0, "FFI::Platypus::Record" => 0, "Path::Tiny" => 0, "Test2::V0" => "0.000081" }, "VERSION" => "0.15", "test" => { "TESTS" => "t/*.t" } ); my %FallbackPrereqs = ( "Capture::Tiny" => 0, "Class::Inspector" => 0, "FFI::Platypus" => "1.24", "FFI::Platypus::Memory" => 0, "FFI::Platypus::Record" => 0, "FFI::Platypus::Type::Enum" => "0.03", "Path::Tiny" => 0, "Ref::Util" => 0, "Sub::Identify" => "0.05", "Sub::Install" => 0, "Sub::Util" => 0, "Test2::V0" => "0.000081" ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { delete $WriteMakefileArgs{TEST_REQUIRES}; delete $WriteMakefileArgs{BUILD_REQUIRES}; $WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs; } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs);FFI-C-0.15/README000644 000000 000000 00000016232 14334741503 013071 0ustar00rootroot000000 000000 NAME FFI::C - C data types for FFI VERSION version 0.15 SYNOPSIS In C: #include typedef struct { uint8_t red; uint8_t green; uint8_t blue; } color_value_t; typedef struct { char name[22]; color_value_t value; } named_color_t; typedef named_color_t array_named_color_t[4]; typedef union { uint8_t u8; uint16_t u16; uint32_t u32; uint64_t u64; } anyint_t; In Perl: use FFI::C; package ColorValue { FFI::C->struct([ red => 'uint8', green => 'uint8', blue => 'uint8', ]); } package NamedColor { FFI::C->struct([ name => 'string(22)', value => 'color_value_t', ]); } package ArrayNamedColor { FFI::C->array(['named_color_t' => 4]); }; my $array = ArrayNamedColor->new([ { name => "red", value => { red => 255 } }, { name => "green", value => { green => 255 } }, { name => "blue", value => { blue => 255 } }, { name => "purple", value => { red => 255, blue => 255 } }, ]); # dim each color by 1/2 foreach my $color (@$array) { $color->value->red ( $color->value->red / 2 ); $color->value->green( $color->value->green / 2 ); $color->value->blue ( $color->value->blue / 2 ); } # print out the colors foreach my $color (@$array) { printf "%s [%02x %02x %02x]\n", $color->name, $color->value->red, $color->value->green, $color->value->blue; } package AnyInt { FFI::C->union([ u8 => 'uint8', u16 => 'uint16', u32 => 'uint32', u64 => 'uint64', ]); } my $int = AnyInt->new({ u8 => 42 }); print $int->u32; DESCRIPTION This distribution provides tools for building classes to interface for common C data types. Arrays, struct, union and nested types based on those are supported. Core FFI::Platypus also provides FFI::Platypus::Record for manipulating and passing structured data. Typically you want to use FFI::C instead, the main exception is when you need to pass structured data by value instead of by reference. To work with C APIs that work with C file pointers you can use FFI::C::File and FFI::C::PosixFile. For C APIs that expose the POSIX stat structure use FFI::C::Stat. METHODS ffi FFI::C->ffi($ffi); my $ffi = FFI::C->ffi; Get or set the FFI::Platypus instance used for the current Perl file (.pl or .pm). By default a new Platypus instance is created the on the first call to ffi, or when a new type is created with struct, union or array below, so if you want to use your own Platypus instance make sure that you set it as soon as possible. The Platypus instance is file scoped because scoping on just one package doesn't make sense if you are defining multiple types in one file since each type must be in its own package. It also doesn't make sense to make the Platypus instance global, because different distributions would conflict. struct FFI::C->struct($name, \@members); FFI::C->struct(\@members); Generate a new FFI::C::Struct class with the given @members into the calling package. (@members should be a list of name/type pairs). You may optionally give a $name which will be used for the FFI::Platypus type name for the generated class. If you do not specify a $name, a C style name will be generated from the last segment in the calling package name by converting to snake case and appending a _t to the end. As an example, given: package MyLibrary::FooBar { FFI::C->struct([ a => 'uint8', b => 'float', ]); }; You can use MyLibrary::FooBar via the file scoped FFI::Platypus instance using the type foo_bar_t. my $foobar = MyLibrary::FooBar->new({ a => 1, b => 3.14 }); $ffi->function( my_library_func => [ 'foo_bar_t' ] => 'void' )->call($foobar); union FFI::C->union($name, \@members); FFI::C->union(\@members); This works exactly like the struct method above, except a FFI::C::Union class is generated instead. array FFI::C->array($name, [$type, $count]); FFI::C->array($name, [$type]); FFI::C->array([$type, $count]); FFI::C->array([$type]); This is similar to struct and union above, except FFI::C::Array is generated. For an array you give it the member type and the element count. The element count is optional for variable length arrays, but keep in mind that when you create such an array you do need to provide a size. enum FFI::C->enum($name, \@values, \%config); FFI::C->enum(\@values, \%config); FFI::C->enum(\@values, \%config); FFI::C->enum(\@values); Defines an enum. The @values and %config are passed to FFI::Platypus::Type::Enum, except the constants are exported to the calling package by default. EXAMPLES unix time struct use FFI::Platypus 1.00; use FFI::C; my $ffi = FFI::Platypus->new( api => 1, lib => [undef], ); FFI::C->ffi($ffi); package Unix::TimeStruct { FFI::C->struct(tm => [ tm_sec => 'int', tm_min => 'int', tm_hour => 'int', tm_mday => 'int', tm_mon => 'int', tm_year => 'int', tm_wday => 'int', tm_yday => 'int', tm_isdst => 'int', tm_gmtoff => 'long', _tm_zone => 'opaque', ]); # For now 'string' is unsupported by FFI::C, but we # can cast the time zone from an opaque pointer to # string. sub tm_zone { my $self = shift; $ffi->cast('opaque', 'string', $self->_tm_zone); } # attach the C localtime function $ffi->attach( localtime => ['time_t*'] => 'tm', sub { my($inner, $class, $time) = @_; $time = time unless defined $time; $inner->(\$time); }); } # now we can actually use our My::UnixTime class my $time = Unix::TimeStruct->localtime; printf "time is %d:%d:%d %s\n", $time->tm_hour, $time->tm_min, $time->tm_sec, $time->tm_zone; CAVEATS FFI::C objects must be passed into C via FFI::Platypus by pointers. So-called "pass-by-value" is not and will not be supported. For "pass-by-value" record types, you should instead use FFI::Platypus::Record. SEE ALSO FFI::C FFI::C::Array FFI::C::ArrayDef FFI::C::Def FFI::C::File FFI::C::PosixFile FFI::C::Struct FFI::C::StructDef FFI::C::Union FFI::C::UnionDef FFI::C::Util FFI::Platypus::Record AUTHOR Graham Ollis COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. FFI-C-0.15/author.yml000644 000000 000000 00000000644 14334741503 014236 0ustar00rootroot000000 000000 --- pod_spelling_system: skip: 0 # list of words that are spelled correctly # (regardless of what spell check thinks) # or stuff that I like to spell incorrectly # intentionally stopwords: - ffi - nullable - addressof - macOS pod_coverage: skip: 0 # format is "Class#method" or "Class",regex allowed # for either Class or method. private: - FFI::C::FFI - FFI::C::Struct#can FFI-C-0.15/corpus/000755 000000 000000 00000000000 14334741503 013520 5ustar00rootroot000000 000000 FFI-C-0.15/corpus/examples/000755 000000 000000 00000000000 14334741503 015336 5ustar00rootroot000000 000000 FFI-C-0.15/corpus/examples/lib/000755 000000 000000 00000000000 14334741503 016104 5ustar00rootroot000000 000000 FFI-C-0.15/corpus/examples/lib/main.pm000644 000000 000000 00000000057 14334741503 017370 0ustar00rootroot000000 000000 package main; use strict; use warnings; 1; FFI-C-0.15/dist.ini000644 000000 000000 00000001322 14334741503 013647 0ustar00rootroot000000 000000 name = FFI-C author = Graham Ollis license = Perl_5 copyright_holder = Graham Ollis copyright_year = 2020-2022 version = 0.15 [@Author::Plicease] :version = 2.69 release_tests = 1 installer = Author::Plicease::MakeMaker github_user = PerlFFI test2_v0 = 1 workflow = linux workflow = windows workflow = macos irc = irc://irc.perl.org/#native diag = +Math::Int64 [Author::Plicease::Core] [Author::Plicease::Upload] cpan = 1 [InsertExample] remove_boiler = 1 [PruneFiles] match = ^corpus/examples/arch match = /_build [MetaNoIndex] directory = examples [Prereqs] FFI::Platypus::Type::Enum = 0.03 FFI-C-0.15/examples/000755 000000 000000 00000000000 14334741503 014023 5ustar00rootroot000000 000000 FFI-C-0.15/examples/synopsis/000755 000000 000000 00000000000 14334741503 015712 5ustar00rootroot000000 000000 FFI-C-0.15/examples/synopsis/array.pl000644 000000 000000 00000001711 14334741503 017365 0ustar00rootroot000000 000000 use strict; use warnings; use FFI::C::ArrayDef; use FFI::C::StructDef; my $point_def = FFI::C::StructDef->new( name => 'point_t', class => 'Point', members => [ x => 'double', y => 'double', ], ); my $rect_def = FFI::C::ArrayDef->new( name => 'rectangle_t', class => 'Rectangle', members => [ $point_def, 2, ] ); # create a rectangle using the def's create method my $square = $rect_def->create([ { x => 1.0, y => 1.0 }, { x => 2.0, y => 2.0 }, ]); printf "[[%d %d][%d %d]]\n", $square->[0]->x, $square->[0]->y, $square->[1]->x, $square->[1]->y; # [[1 1][2 2]] # move square by 1 on the x axis $square->[$_]->x( $square->[$_]->x + 1 ) for 0..1; printf "[[%d %d][%d %d]]\n", $square->[0]->x, $square->[0]->y, $square->[1]->x, $square->[1]->y; # [[2 1][3 2]] # Create a rectange usingn the generated class my $rect = Rectangle->new; $rect->[0]->x(1.0); $rect->[0]->y(1.0); $rect->[1]->x(2.0); $rect->[1]->y(3.0); FFI-C-0.15/examples/synopsis/arraydef.c000644 000000 000000 00000000275 14334741503 017657 0ustar00rootroot000000 000000 #include typedef struct { double x, y; } point_t; void print_rectangle(point_t rec[2]) { printf("[[%g %g] [%g %g]]\n", rec[0].x, rec[0].y, rec[1].x, rec[1].y ); } FFI-C-0.15/examples/synopsis/arraydef.pl000644 000000 000000 00000001457 14334741503 020053 0ustar00rootroot000000 000000 use strict; use warnings; use FFI::Platypus 1.00; use FFI::C::ArrayDef; use FFI::C::StructDef; my $ffi = FFI::Platypus->new( api => 1 ); # See FFI::Platypus::Bundle for how bundle works. $ffi->bundle; my $point_def = FFI::C::StructDef->new( $ffi, name => 'point_t', class => 'Point', members => [ x => 'double', y => 'double', ], ); my $rect_def = FFI::C::ArrayDef->new( $ffi, name => 'rectangle_t', class => 'Rectangle', members => [ $point_def, 2, ] ); $ffi->attach( print_rectangle => ['rectangle_t'] ); my $rect = Rectangle->new([ { x => 1.5, y => 2.0 }, { x => 3.14, y => 11.0 }, ]); print_rectangle($rect); # [[1.5 2] [3.14 11]] # move rectangle on the y axis $rect->[$_]->y( $rect->[$_]->y + 1.0 ) for 0..1; print_rectangle($rect); # [[1.5 3] [3.14 12]] FFI-C-0.15/examples/synopsis/c.c000644 000000 000000 00000000475 14334741503 016306 0ustar00rootroot000000 000000 #include typedef struct { uint8_t red; uint8_t green; uint8_t blue; } color_value_t; typedef struct { char name[22]; color_value_t value; } named_color_t; typedef named_color_t array_named_color_t[4]; typedef union { uint8_t u8; uint16_t u16; uint32_t u32; uint64_t u64; } anyint_t; FFI-C-0.15/examples/synopsis/c.pl000644 000000 000000 00000002260 14334741503 016471 0ustar00rootroot000000 000000 use strict; use warnings; use FFI::C; package ColorValue { FFI::C->struct([ red => 'uint8', green => 'uint8', blue => 'uint8', ]); } package NamedColor { FFI::C->struct([ name => 'string(22)', value => 'color_value_t', ]); } package ArrayNamedColor { FFI::C->array(['named_color_t' => 4]); }; my $array = ArrayNamedColor->new([ { name => "red", value => { red => 255 } }, { name => "green", value => { green => 255 } }, { name => "blue", value => { blue => 255 } }, { name => "purple", value => { red => 255, blue => 255 } }, ]); # dim each color by 1/2 foreach my $color (@$array) { $color->value->red ( $color->value->red / 2 ); $color->value->green( $color->value->green / 2 ); $color->value->blue ( $color->value->blue / 2 ); } # print out the colors foreach my $color (@$array) { printf "%s [%02x %02x %02x]\n", $color->name, $color->value->red, $color->value->green, $color->value->blue; } package AnyInt { FFI::C->union([ u8 => 'uint8', u16 => 'uint16', u32 => 'uint32', u64 => 'uint64', ]); } my $int = AnyInt->new({ u8 => 42 }); print $int->u32; FFI-C-0.15/examples/synopsis/struct.pl000644 000000 000000 00000001205 14334741503 017571 0ustar00rootroot000000 000000 use strict; use warnings; use FFI::C::StructDef; my $def = FFI::C::StructDef->new( name => 'color_t', class => 'Color', members => [ red => 'uint8', green => 'uint8', blue => 'uint8', ], ); my $red = $def->create({ red => 255 }); # creates a FFI::C::Stuct printf "[%02x %02x %02x]\n", $red->red, $red->green, $red->blue; # [ff 00 00] # that red is too bright! $red->red(200); printf "[%02x %02x %02x]\n", $red->red, $red->green, $red->blue; # [c8 00 00] my $green = Color->new({ green => 255 }); # creates a FFI::C::Stuct printf "[%02x %02x %02x]\n", $green->red, $green->green, $green->blue; # [00 ff 00] FFI-C-0.15/examples/synopsis/structdef.c000644 000000 000000 00000000340 14334741503 020056 0ustar00rootroot000000 000000 #include #include typedef struct { uint8_t red; uint8_t green; uint8_t blue; } color_t; void print_color(color_t *c) { printf("[%02x %02x %02x]\n", c->red, c->green, c->blue ); } FFI-C-0.15/examples/synopsis/structdef.pl000644 000000 000000 00000001170 14334741503 020251 0ustar00rootroot000000 000000 use strict; use warnings; use FFI::Platypus 1.00; use FFI::C::StructDef; my $ffi = FFI::Platypus->new( api => 1 ); # See FFI::Platypus::Bundle for how bundle works. $ffi->bundle; my $def = FFI::C::StructDef->new( $ffi, name => 'color_t', class => 'Color', members => [ red => 'uint8', green => 'uint8', blue => 'uint8', ], ); my $red = Color->new({ red => 255 }); my $green = Color->new({ green => 255 }); $ffi->attach( print_color => ['color_t'] ); print_color($red); # [ff 00 00] print_color($green); # [00 ff 00] # that red is a tad bright! $red->red( 200 ); print_color($red); # [c8 00 00] FFI-C-0.15/examples/synopsis/union.pl000644 000000 000000 00000000446 14334741503 017403 0ustar00rootroot000000 000000 use strict; use warnings; use FFI::C::UnionDef; my $def = FFI::C::UnionDef->new( name => 'anyint_t', class => 'AnyInt', members => [ u8 => 'uint8', u16 => 'uint16', u32 => 'uint32', ], ); my $int = AnyInt->new({ u8 => 42 }); printf "0x%x\n", $int->u32; # 0x2a on Intel FFI-C-0.15/examples/synopsis/uniondef.c000644 000000 000000 00000000276 14334741503 017672 0ustar00rootroot000000 000000 #include #include typedef union { uint8_t u8; uint16_t u16; uint32_t u32; } anyint_t; void print_anyint_as_u32(anyint_t *any) { printf("0x%x\n", any->u32); } FFI-C-0.15/examples/synopsis/uniondef.pl000644 000000 000000 00000000746 14334741503 020065 0ustar00rootroot000000 000000 use strict; use warnings; use FFI::Platypus 1.00; use FFI::C::UnionDef; my $ffi = FFI::Platypus->new( api => 1 ); # See FFI::Platypus::Bundle for how bundle works. $ffi->bundle; my $def = FFI::C::UnionDef->new( $ffi, name => 'anyint_t', class => 'AnyInt', members => [ u8 => 'uint8', u16 => 'uint16', u32 => 'uint32', ], ); $ffi->attach( print_anyint_as_u32 => ['anyint_t'] ); my $int = AnyInt->new({ u8 => 42 }); print_anyint_as_u32($int); # 0x2a on Intel, FFI-C-0.15/examples/synopsis/util.pl000644 000000 000000 00000000653 14334741503 017230 0ustar00rootroot000000 000000 use strict; use warnings; use FFI::C::Util qw( perl_to_c take ); use FFI::C::StructDef; use FFI::Platypus::Memory qw( free ); my $def = FFI::C::StructDef->new( members => [ x => 'uint8', y => 'sint64', ], ); my $inst = $def->create; # initalize members perl_to_c($inst, { x => 1, y => 2 }); # take ownership my $ptr = take $inst; # since we took ownership, we are responsible for freeing the memory: free $ptr; FFI-C-0.15/examples/time.pl000644 000000 000000 00000002066 14334741503 015322 0ustar00rootroot000000 000000 use strict; use warnings; use FFI::Platypus 1.00; use FFI::C; my $ffi = FFI::Platypus->new( api => 1, lib => [undef], ); FFI::C->ffi($ffi); package Unix::TimeStruct { FFI::C->struct(tm => [ tm_sec => 'int', tm_min => 'int', tm_hour => 'int', tm_mday => 'int', tm_mon => 'int', tm_year => 'int', tm_wday => 'int', tm_yday => 'int', tm_isdst => 'int', tm_gmtoff => 'long', _tm_zone => 'opaque', ]); # For now 'string' is unsupported by FFI::C, but we # can cast the time zone from an opaque pointer to # string. sub tm_zone { my $self = shift; $ffi->cast('opaque', 'string', $self->_tm_zone); } # attach the C localtime function $ffi->attach( localtime => ['time_t*'] => 'tm', sub { my($inner, $class, $time) = @_; $time = time unless defined $time; $inner->(\$time); }); } # now we can actually use our My::UnixTime class my $time = Unix::TimeStruct->localtime; printf "time is %d:%d:%d %s\n", $time->tm_hour, $time->tm_min, $time->tm_sec, $time->tm_zone; FFI-C-0.15/lib/000755 000000 000000 00000000000 14334741503 012753 5ustar00rootroot000000 000000 FFI-C-0.15/lib/FFI/000755 000000 000000 00000000000 14334741503 013357 5ustar00rootroot000000 000000 FFI-C-0.15/lib/FFI/C.pm000644 000000 000000 00000022213 14334741503 014077 0ustar00rootroot000000 000000 package FFI::C; use strict; use warnings; use 5.008001; use Carp (); use Ref::Util qw( is_ref is_plain_arrayref is_plain_hashref ); # ABSTRACT: C data types for FFI our $VERSION = '0.15'; # VERSION our %ffi; sub _ffi_get { my($filename) = @_; $ffi{$filename} ||= do { require FFI::Platypus; FFI::Platypus->new( api => 1 ); }; } sub ffi { my($class, $new) = @_; my(undef, $filename) = caller; if($new) { Carp::croak("Already have an FFI::Platypus instance for $filename") if defined $ffi{$filename}; return $ffi{$filename} = $new; } _ffi_get($filename); } our $def_class; sub _gen { shift; my($class, $filename) = caller; my($name, $members); my %extra = is_plain_hashref $_[-1] ? %{ pop() } : (); if(@_ == 2 && !is_ref $_[0] && is_plain_arrayref $_[1]) { ($name, $members) = @_; } elsif(@_ == 1 && is_plain_arrayref $_[0]) { $name = lcfirst [split /::/, $class]->[-1]; $name =~ s/([A-Z]+)/'_' . lc($1)/ge; $name .= "_t"; ($members) = @_; } else { my($method) = map { lc $_ } $def_class =~ /::([A-Za-z]+)Def$/; Carp::croak("usage: FFI::C->$method([\$name], \\\@members)"); } $def_class->new( _ffi_get($filename), %extra, name => $name, class => $class, members => $members, ); } sub struct { require FFI::C::StructDef; $def_class = 'FFI::C::StructDef'; goto &_gen; } sub union { require FFI::C::UnionDef; $def_class = 'FFI::C::UnionDef'; goto &_gen; } sub array { require FFI::C::ArrayDef; $def_class = 'FFI::C::ArrayDef'; goto &_gen; } sub enum { (undef) = shift; my $name = defined $_[0] && !is_ref $_[0] ? shift : undef; my @values = defined $_[0] && is_plain_arrayref $_[0] ? @{shift()} : (); my %config = defined $_[0] && is_plain_hashref $_[0] ? %{shift()} : (); my($class, $filename) = caller; unless(defined $name) { $name = lcfirst [split /::/, $class]->[-1]; $name =~ s/([A-Z]+)/'_' . lc($1)/ge; $name .= "_t"; } my $ffi = _ffi_get($filename), $config{package} ||= $class; my @maps; $config{maps} = \@maps; my $rev = $config{rev} ||= 'str'; $ffi->load_custom_type('::Enum', $name, \%config, @values); my($str_lookup, $int_lookup, $type) = @maps; require FFI::C::Def; $ffi->def('FFI::C::EnumDef', $name, FFI::C::EnumDef->new( str_lookup => $str_lookup, int_lookup => $int_lookup, type => $type, rev => $rev, ) ); } 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C - C data types for FFI =head1 VERSION version 0.15 =head1 SYNOPSIS In C: #include typedef struct { uint8_t red; uint8_t green; uint8_t blue; } color_value_t; typedef struct { char name[22]; color_value_t value; } named_color_t; typedef named_color_t array_named_color_t[4]; typedef union { uint8_t u8; uint16_t u16; uint32_t u32; uint64_t u64; } anyint_t; In Perl: use FFI::C; package ColorValue { FFI::C->struct([ red => 'uint8', green => 'uint8', blue => 'uint8', ]); } package NamedColor { FFI::C->struct([ name => 'string(22)', value => 'color_value_t', ]); } package ArrayNamedColor { FFI::C->array(['named_color_t' => 4]); }; my $array = ArrayNamedColor->new([ { name => "red", value => { red => 255 } }, { name => "green", value => { green => 255 } }, { name => "blue", value => { blue => 255 } }, { name => "purple", value => { red => 255, blue => 255 } }, ]); # dim each color by 1/2 foreach my $color (@$array) { $color->value->red ( $color->value->red / 2 ); $color->value->green( $color->value->green / 2 ); $color->value->blue ( $color->value->blue / 2 ); } # print out the colors foreach my $color (@$array) { printf "%s [%02x %02x %02x]\n", $color->name, $color->value->red, $color->value->green, $color->value->blue; } package AnyInt { FFI::C->union([ u8 => 'uint8', u16 => 'uint16', u32 => 'uint32', u64 => 'uint64', ]); } my $int = AnyInt->new({ u8 => 42 }); print $int->u32; =head1 DESCRIPTION This distribution provides tools for building classes to interface for common C data types. Arrays, C, C and nested types based on those are supported. Core L also provides L for manipulating and passing structured data. Typically you want to use L instead, the main exception is when you need to pass structured data by value instead of by reference. To work with C APIs that work with C file pointers you can use L and L. For C APIs that expose the POSIX C structure use L. =head1 METHODS =head2 ffi FFI::C->ffi($ffi); my $ffi = FFI::C->ffi; Get or set the L instance used for the current Perl file (C<.pl> or C<.pm>). By default a new Platypus instance is created the on the first call to C, or when a new type is created with C, C or C below, so if you want to use your own Platypus instance make sure that you set it as soon as possible. The Platypus instance is file scoped because scoping on just one package doesn't make sense if you are defining multiple types in one file since each type must be in its own package. It also doesn't make sense to make the Platypus instance global, because different distributions would conflict. =head2 struct FFI::C->struct($name, \@members); FFI::C->struct(\@members); Generate a new L class with the given C<@members> into the calling package. (C<@members> should be a list of name/type pairs). You may optionally give a C<$name> which will be used for the L type name for the generated class. If you do not specify a C<$name>, a C style name will be generated from the last segment in the calling package name by converting to snake case and appending a C<_t> to the end. As an example, given: package MyLibrary::FooBar { FFI::C->struct([ a => 'uint8', b => 'float', ]); }; You can use C via the file scoped L instance using the type C. my $foobar = MyLibrary::FooBar->new({ a => 1, b => 3.14 }); $ffi->function( my_library_func => [ 'foo_bar_t' ] => 'void' )->call($foobar); =head2 union FFI::C->union($name, \@members); FFI::C->union(\@members); This works exactly like the C method above, except a L class is generated instead. =head2 array FFI::C->array($name, [$type, $count]); FFI::C->array($name, [$type]); FFI::C->array([$type, $count]); FFI::C->array([$type]); This is similar to C and C above, except L is generated. For an array you give it the member type and the element count. The element count is optional for variable length arrays, but keep in mind that when you create such an array you do need to provide a size. =head2 enum FFI::C->enum($name, \@values, \%config); FFI::C->enum(\@values, \%config); FFI::C->enum(\@values, \%config); FFI::C->enum(\@values); Defines an enum. The C<@values> and C<%config> are passed to L, except the constants are exported to the calling package by default. =head1 EXAMPLES =head2 unix time struct use FFI::Platypus 1.00; use FFI::C; my $ffi = FFI::Platypus->new( api => 1, lib => [undef], ); FFI::C->ffi($ffi); package Unix::TimeStruct { FFI::C->struct(tm => [ tm_sec => 'int', tm_min => 'int', tm_hour => 'int', tm_mday => 'int', tm_mon => 'int', tm_year => 'int', tm_wday => 'int', tm_yday => 'int', tm_isdst => 'int', tm_gmtoff => 'long', _tm_zone => 'opaque', ]); # For now 'string' is unsupported by FFI::C, but we # can cast the time zone from an opaque pointer to # string. sub tm_zone { my $self = shift; $ffi->cast('opaque', 'string', $self->_tm_zone); } # attach the C localtime function $ffi->attach( localtime => ['time_t*'] => 'tm', sub { my($inner, $class, $time) = @_; $time = time unless defined $time; $inner->(\$time); }); } # now we can actually use our My::UnixTime class my $time = Unix::TimeStruct->localtime; printf "time is %d:%d:%d %s\n", $time->tm_hour, $time->tm_min, $time->tm_sec, $time->tm_zone; =head1 CAVEATS L objects must be passed into C via L by pointers. So-called "pass-by-value" is not and will not be supported. For "pass-by-value" record types, you should instead use L. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/000755 000000 000000 00000000000 14334741503 013541 5ustar00rootroot000000 000000 FFI-C-0.15/lib/FFI/C/Array.pm000644 000000 000000 00000007546 14334741503 015171 0ustar00rootroot000000 000000 package FFI::C::Array; use strict; use warnings; use FFI::C::FFI (); use overload '@{}' => \&tie, bool => sub { 1 }, fallback => 1; # ABSTRACT: Array instance for FFI our $VERSION = '0.15'; # VERSION sub get { my($self, $index) = @_; Carp::croak("Negative array index") if $index < 0; Carp::croak("OOB array index") if $self->{count} && $index >= $self->{count}; my $member = $self->{def}->{members}->{member}; my $ptr = $self->{ptr} + $member->size * $index; $member->create($ptr,$self->{owner} || $self); } sub count { shift->{count} } sub tie { my @a; CORE::tie @a, 'FFI::C::Array', shift; \@a; } sub DESTROY { my($self) = @_; if($self->{ptr} && !$self->{owner}) { FFI::C::FFI::free(delete $self->{ptr}); } } sub TIEARRAY { my($class, $array) = @_; $array; } sub FETCH { my($self, $index) = @_; $self->get($index); } sub STORE { Carp::croak("Cannot set"); } sub FETCHSIZE { my($self) = @_; $self->count; } sub STORESIZE { my($self) = @_; $self->count; } sub CLEAR { Carp::croak("Cannot clear"); } 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::Array - Array instance for FFI =head1 VERSION version 0.15 =head1 SYNOPSIS use FFI::C::ArrayDef; use FFI::C::StructDef; my $point_def = FFI::C::StructDef->new( name => 'point_t', class => 'Point', members => [ x => 'double', y => 'double', ], ); my $rect_def = FFI::C::ArrayDef->new( name => 'rectangle_t', class => 'Rectangle', members => [ $point_def, 2, ] ); # create a rectangle using the def's create method my $square = $rect_def->create([ { x => 1.0, y => 1.0 }, { x => 2.0, y => 2.0 }, ]); printf "[[%d %d][%d %d]]\n", $square->[0]->x, $square->[0]->y, $square->[1]->x, $square->[1]->y; # [[1 1][2 2]] # move square by 1 on the x axis $square->[$_]->x( $square->[$_]->x + 1 ) for 0..1; printf "[[%d %d][%d %d]]\n", $square->[0]->x, $square->[0]->y, $square->[1]->x, $square->[1]->y; # [[2 1][3 2]] # Create a rectange usingn the generated class my $rect = Rectangle->new; $rect->[0]->x(1.0); $rect->[0]->y(1.0); $rect->[1]->x(2.0); $rect->[1]->y(3.0); =head1 DESCRIPTION This class represents an instance of a C an array. This class can be created using C on the generated class, if that was specified for the L, or by using the C method on L. Each element of the array can be accessed using the C method below, or by using the object as an array reference, thanks to magical Perl ties. =head1 CONSTRUCTOR =head2 new FFI::C::ArrayDef->new( class => 'User::Array::Class', ... ); my $instance = User::Array::Class->new; my $instance = User::Array::Class->new($count); Creates a new instance of the array. If C<$count> is specified, that will be used as the element count, overriding the count defined by the def. If the def did not specify a count then you MUST provide a count. =head1 METHODS =head2 get my $element = $instance->get($index); my $element = $instance->[$index]; Gets the element at the given C<$index>. =head2 count my $count = $instance->count; Returns the number of elements in the array, if known. =head2 tie my $arrayref = $instance->tie; Returns a Perl array reference tied to the C array. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/ArrayDef.pm000644 000000 000000 00000014450 14334741503 015600 0ustar00rootroot000000 000000 package FFI::C::ArrayDef; use strict; use warnings; use 5.008001; use Ref::Util qw( is_blessed_ref is_ref is_plain_arrayref ); use FFI::C::Array; use Sub::Install (); use Sub::Util (); use Scalar::Util qw( refaddr ); use base qw( FFI::C::Def ); our @CARP_NOT = qw( FFI::C ); # ABSTRACT: Array data definition for FFI our $VERSION = '0.15'; # VERSION sub new { my $self = shift->SUPER::new(@_); my %args = %{ delete $self->{args} }; my $member; my $count = 0; my @members = @{ delete $args{members} || [] }; if(@members == 1) { ($member) = @members; } elsif(@members == 2) { ($member, $count) = @members; } else { Carp::croak("The members argument should be a struct/union type and an optional element count"); } if(my $def = $self->ffi->def('FFI::C::Def', $member)) { $member = $def; } Carp::croak("Canot nest an array def inside of itself") if refaddr($member) == refaddr($self); Carp::croak("Illegal member") unless defined $member && is_blessed_ref($member) && $member->isa("FFI::C::Def"); Carp::croak("The element count must be a positive integer") if defined $count && $count !~ /^[1-9]*[0-9]$/; $self->{size} = $member->size * $count; $self->{align} = $member->align; $self->{members}->{member} = $member; $self->{members}->{count} = $count; Carp::carp("Unknown argument: $_") for sort keys %args; if($self->class) { # not handled by the superclass: # 3. Any nested cdefs must have Perl classes. { my $member = $self->{members}->{member}; my $accessor = $self->class . '::get'; Carp::croak("Missing Perl class for $accessor") if $member->{nest} && !$member->{nest}->{class}; } $self->_generate_class(qw( get )); { my $member_class = $self->{members}->{member}->class; my $member_size = $self->{members}->{member}->size; my $code = sub { my($self, $index) = @_; Carp::croak("Negative array index") if $index < 0; Carp::croak("OOB array index") if $self->{count} && $index >= $self->{count}; my $ptr = $self->{ptr} + $member_size * $index; $member_class->new($ptr,$self); }; Sub::Util::set_subname(join('::', $self->class), $code); Sub::Install::install_sub({ code => $code, into => $self->class, as => 'get', }); } { no strict 'refs'; push @{ join '::', $self->class, 'ISA' }, 'FFI::C::Array'; } } $self; } sub create { my $self = shift; return $self->class->new(@_) if $self->class; local $self->{size} = $self->{size}; my $count = $self->{members}->{count}; if(@_ == 1) { if(! is_ref $_[0]) { $count = shift; } elsif(is_plain_arrayref $_[0]) { $count = scalar @{$_[0]}; } if($count) { $self->{size} = $self->{members}->{member}->size * $count; } } if( (@_ == 2 && ! is_ref $_[0]) || ($self->size) ) { my $array = $self->SUPER::create(@_); $array->{count} = $count; FFI::C::Util::perl_to_c($array, $_[0]) if @_ == 1 && is_plain_arrayref $_[0]; return $array; } Carp::croak("Cannot create array without knowing the number of elements"); } 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::ArrayDef - Array data definition for FFI =head1 VERSION version 0.15 =head1 SYNOPSIS In your C code: #include typedef struct { double x, y; } point_t; void print_rectangle(point_t rec[2]) { printf("[[%g %g] [%g %g]]\n", rec[0].x, rec[0].y, rec[1].x, rec[1].y ); } In your Perl code: use FFI::Platypus 1.00; use FFI::C::ArrayDef; use FFI::C::StructDef; my $ffi = FFI::Platypus->new( api => 1 ); # See FFI::Platypus::Bundle for how bundle works. $ffi->bundle; my $point_def = FFI::C::StructDef->new( $ffi, name => 'point_t', class => 'Point', members => [ x => 'double', y => 'double', ], ); my $rect_def = FFI::C::ArrayDef->new( $ffi, name => 'rectangle_t', class => 'Rectangle', members => [ $point_def, 2, ] ); $ffi->attach( print_rectangle => ['rectangle_t'] ); my $rect = Rectangle->new([ { x => 1.5, y => 2.0 }, { x => 3.14, y => 11.0 }, ]); print_rectangle($rect); # [[1.5 2] [3.14 11]] # move rectangle on the y axis $rect->[$_]->y( $rect->[$_]->y + 1.0 ) for 0..1; print_rectangle($rect); # [[1.5 3] [3.14 12]] =head1 DESCRIPTION This class creates a def for a C array of structured data. Usually the def contains a L or L and optionally a number of elements. =head1 CONSTRUCTOR =head2 new my $def = FFI::C::ArrayDef->new(%opts); my $def = FFI::C::ArrayDef->new($ffi, %opts); For standard def options, see L. =over 4 =item members This should be an array reference the member type, and optionally the number of elements. Examples: my $struct = FFI::C::StructDef->new(...); my $fixed = FFI::C::ArrayDef->new( members => [ $struct, 10 ], ); my $var = FFI::C::ArrayDef->new( members => [ $struct ], ); =back =head1 METHODS =head2 create my $instance = $def->create; my $instance = $def->class->new; # if class was specified my $instance = $def->create($count); my $instance = $def->class->new($count); # if class was specified my $instance = $def->create(\@init); my $instance = $def->class->new(\@init); # if class was specified This creates an instance of the array. If C<$count> is given, this is used for the element count, possibly overriding what was specified when the def was created. If the def doesn't have an element count specified, then you MUST provide it here. Returns a L. You can optionally initialize member values using C<@init>. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/Def.pm000644 000000 000000 00000026221 14334741503 014600 0ustar00rootroot000000 000000 package FFI::C::Def; use strict; use warnings; use 5.008001; use FFI::C::FFI qw( malloc memset ); use FFI::C::Util; use Ref::Util qw( is_blessed_ref is_ref is_plain_hashref is_plain_arrayref ); use Sub::Install (); use Sub::Util (); our @CARP_NOT = qw( FFI::C ); # ABSTRACT: Data definition for FFI our $VERSION = '0.15'; # VERSION sub new { my $class = shift; Carp::croak("Attempt to call new on a def object (did you mean ->create?)") if is_blessed_ref $class; my $ffi = is_blessed_ref($_[0]) && $_[0]->isa('FFI::Platypus') ? shift : FFI::Platypus->new( api => 1 ); my %args = @_; Carp::croak("Only works with FFI::Platypus api level 1 or better") unless $ffi->api >= 1; Carp::croak("FFI::C::Def is an abstract class") if $class eq 'FFI::C::Def'; my $self = bless { ffi => $ffi, name => delete $args{name}, class => delete $args{class}, nullable => delete $args{nullable}, members => {}, align => 0, size => 0, args => \%args, }, $class; if($self->name) { my $cdef = ref($self); $cdef =~ s/Def$//; $ffi->load_custom_type('::CDef' => $self->name, name => $self->name, class => $self->class, nullable => $self->nullable, def => $self, cdef => $cdef, ); $ffi->def('FFI::C::Def', $self->name, $self); } $self; } sub _generate_class { my($self, @accessors) = @_; # first run through all the members, and make sure that we # can generate a class based on the def. That means that: # 1. there is no constructor or destructor defined yet. # 2. none of the member accessors already exist # 3. Any nested cdefs have Perl classes, this will be done # in the subclass foreach my $method (qw( new DESTROY )) { my $accessor = join '::', $self->class, $method; Carp::croak("$accessor already defined") if $self->class->can($method); } foreach my $name (@accessors) { next if $name =~ /^:/; my $accessor = $self->class . '::' . $name; Carp::croak("$accessor already exists") if $self->class->can($name); } require FFI::Platypus::Memory; if($self->isa('FFI::C::ArrayDef')) { my $size = $self->size; my $count = $self->{members}->{count}; my $member_size = $self->{members}->{member}->size; my $code = sub { my $class = shift; my($ptr, $owner); my $size = $size; my $count = $count; if(@_ == 1) { if(!is_ref $_[0]) { $count = shift; } elsif(is_plain_arrayref $_[0]) { $count = scalar @{$_[0]}; } if($count) { $size = $member_size * $count; } } if(@_ == 2 && ! is_ref $_[0]) { ($ptr, $owner) = @_; } else { Carp::croak("Cannot create array without knowing the number of elements") unless $size; $ptr = FFI::Platypus::Memory::malloc($size); FFI::Platypus::Memory::memset($ptr, 0, $size); } my $self = bless { ptr => $ptr, owner => $owner, count => $count, }, $class; FFI::C::Util::perl_to_c($self, $_[0]) if @_ == 1 && is_ref $_[0]; $self; }; Sub::Util::set_subname(join('::', $self->class, 'new'), $code); Sub::Install::install_sub({ code => $code, into => $self->class, as => 'new', }); } else { my $size = $self->size; $size = 1 unless $size > 0; my $code = sub { my $class = shift; my($ptr, $owner); if(@_ == 2 && ! is_ref $_[0]) { ($ptr, $owner) = @_; } else { $ptr = FFI::Platypus::Memory::malloc($size); FFI::Platypus::Memory::memset($ptr, 0, $size); } my $self = bless { ptr => $ptr, owner => $owner, }, $class; FFI::C::Util::perl_to_c($self, $_[0]) if @_ == 1 && is_ref $_[0]; $self; }; Sub::Util::set_subname(join('::', $self->class, 'new'), $code); Sub::Install::install_sub({ code => $code, into => $self->class, as => 'new', }); } Sub::Install::install_sub({ code => \&_common_destroy, into => $self->class, as => 'DESTROY', }); } sub _common_destroy { my($self) = @_; if($self->{ptr} && !$self->{owner}) { FFI::Platypus::Memory::free(delete $self->{ptr}); } } sub name { shift->{name} } sub class { shift->{class} } sub ffi { shift->{ffi} } sub size { shift->{size} } sub align { shift->{align} } sub nullable { shift->{nullable} } sub create { my $self = shift; return $self->class->new(@_) if $self->class; my $ptr; my $owner; if(@_ == 2 && ! is_ref $_[0]) { ($ptr, $owner) = @_; } else { # TODO: we use 1 byte for size 0 # this is needed if malloc(0) returns undef. # we could special case for platforms where malloc(0) # returns a constant pointer that can be free()'d $ptr = malloc($self->size ? $self->size : 1); memset($ptr, 0, $self->size); } my $class = ref($self); $class =~ s/Def$//; my $inst = bless { ptr => $ptr, def => $self, owner => $owner, }, $class; FFI::C::Util::perl_to_c($inst, $_[0]) if @_ == 1 && is_plain_hashref $_[0]; $inst; } package FFI::Platypus::Type::CDef; use Ref::Util qw( is_blessed_ref ); push @FFI::Platypus::CARP_NOT, __PACKAGE__; sub ffi_custom_type_api_1 { my(undef, undef, %args) = @_; my $perl_to_native; my $native_to_perl; my $name = $args{name}; my $class = $args{class}; my $def = $args{def} || Carp::croak("no def defined"); my $cdef = $args{cdef} || Carp::croak("no cdef defined"); my $nullable = $args{nullable}; if($class) { $perl_to_native = sub { return undef if !defined $_[0] && $nullable; Carp::croak("argument is not a $class") unless is_blessed_ref $_[0] && $_[0]->isa($class); my $ptr = $_[0]->{ptr}; Carp::croak("pointer for $name went away") unless defined $ptr; $ptr; }; $native_to_perl = sub { defined $_[0] ? bless { ptr => $_[0], owner => 1 }, $class : undef; }; } elsif($name) { $perl_to_native = sub { return undef if !defined $_[0] && $nullable; Carp::croak("argument is not a $name") unless is_blessed_ref $_[0] && ref($_[0]) eq $cdef && $_[0]->{def}->{name} eq $name; my $ptr = $_[0]->{ptr}; Carp::croak("pointer for $name went away") unless defined $ptr; $ptr; }; $native_to_perl = sub { defined $_[0] ? bless { ptr => $_[0], def => $def, owner => 1 }, $cdef : undef; }; } return { native_type => 'opaque', perl_to_native => $perl_to_native, native_to_perl => $native_to_perl, } } package FFI::C::EnumDef; sub new { my($class, %self) = @_; bless \%self, $class; } sub str_lookup { shift->{str_lookup} } sub int_lookup { shift->{int_lookup} } sub type { shift->{type} } sub rev { shift->{rev} } 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::Def - Data definition for FFI =head1 VERSION version 0.15 =head1 SYNOPSIS In your C code: #include #include typedef struct { uint8_t red; uint8_t green; uint8_t blue; } color_t; void print_color(color_t *c) { printf("[%02x %02x %02x]\n", c->red, c->green, c->blue ); } In your Perl code: use FFI::Platypus 1.00; use FFI::C::StructDef; my $ffi = FFI::Platypus->new( api => 1 ); # See FFI::Platypus::Bundle for how bundle works. $ffi->bundle; my $def = FFI::C::StructDef->new( $ffi, name => 'color_t', class => 'Color', members => [ red => 'uint8', green => 'uint8', blue => 'uint8', ], ); my $red = Color->new({ red => 255 }); my $green = Color->new({ green => 255 }); $ffi->attach( print_color => ['color_t'] ); print_color($red); # [ff 00 00] print_color($green); # [00 ff 00] # that red is a tad bright! $red->red( 200 ); print_color($red); # [c8 00 00] =head1 DESCRIPTION This class is the base class for all def classes in the L collection. The def classes are for defining C C, C and array types that can be used from Perl and passed to C via L. You don't create an instance of this class directly, rather one of the subclasses: L, L, L. =head1 CONSTRUCTOR =head2 new my $def = FFI::C::StructDef->new(%opts); my $def = FFI::C::StructDef->new($ffi, %opts); my $def = FFI::C::UnionDef->new(%opts); my $def = FFI::C::UnionDef->new($ffi, %opts); my $def = FFI::C::ArrayDef->new(%opts); my $def = FFI::C::ArrayDef->new($ffi, %opts); The constructor for this class shouldn't be invoked directly. If you try and exception will be thrown. For subclasses, the first argument should be the L instance that you want to use with the def. If you do not provide it, then one will be created internally for you. All def classes accept these standard options: =over 4 =item name The L alias for this def. This name can be used in function signatures when creating or attaching functions in L. =item class The Perl class for this def. The Perl class can be used to create an instance of this def instead of invoking the C method below. =item members This is an array reference, which specifies the member fields for the def. How exactly it works depends on the subclass, so see the documentation for the specific def class that you are using. =item nullable If true, then the type can be C when passed into C. C will be translated to C. =back =head1 METHODS =head2 ffi my $ffi = $def->ffi; Returns the L instance used by this def. =head2 name my $name = $def->name; Return the L alias for this def. This name can be used in function signatures when creating or attaching functions in L. =head2 class my $class = $def->class; Returns the Perl class for this def, if one was specified. The Perl class can be used to create an instance of this def instead of invoking the C method below. =head2 size my $size = $def->size; Returns the size of the def in bytes. =head2 align my $align = $def->align; Returns the alignment in bytes of the def. =head2 nullable my $bool = $def->nullable; Returns true if C is allowed to be passed in to C functions. C will be translated to C. =head2 create my $instance = $def->create; my $instance = $def->class->new; # if class was specified Creates an instance of the def. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/FFI.pm000644 000000 000000 00000003362 14334741503 014507 0ustar00rootroot000000 000000 package FFI::C::FFI; use strict; use warnings; use FFI::Platypus 1.24; use constant (); use base qw( Exporter ); # ABSTRACT: Private module for FFI::C our $VERSION = '0.15'; # VERSION our @EXPORT_OK = qw( malloc free memset memcpy_addr ); my $ffi = FFI::Platypus->new( api => 1, lib => [undef] ); constant->import( memcpy_addr => $ffi->find_symbol( 'memcpy' ) ); $ffi->attach( malloc => ['size_t'] => 'opaque', '$' ); $ffi->attach( free => ['opaque'] => 'void', '$' ); $ffi->attach( memset => ['opaque','int','size_t'] => 'opaque', '$$$' ); ## should this be configurable for when we hunt for memory leaks? #sub malloc ($) #{ # $ffi->function( malloc => ['size_t'] => 'opaque' ) # ->call(@_); #} # #sub free ($) #{ # $ffi->function( free => ['opaque'] => 'void' ) # ->call(@_); #} # #sub memset ($$$) #{ # $ffi->function( memset => ['opaque','int','size_t'] => 'opaque' ) # ->call(@_); #} 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::FFI - Private module for FFI::C =head1 VERSION version 0.15 =head1 SYNOPSIS perldoc FFI::C =head1 DESCRIPTION This module is private for L =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/File.pm000644 000000 000000 00000020655 14334741503 014766 0ustar00rootroot000000 000000 package FFI::C::File; use strict; use warnings; use Carp qw( croak ); use FFI::Platypus 1.00; use base qw( Exporter ); our @EXPORT_OK = qw( SEEK_SET SEEK_CUR SEEK_END ); # ABSTRACT: Perl interface to C File pointer our $VERSION = '0.15'; # VERSION our $ffi = FFI::Platypus->new( api => 1, lib => [undef] ); $ffi->type( 'object(FFI::C::File)' => 'FILE' ); $ffi->load_custom_type('::Enum', 'whence', { package => 'FFI::C::File', prefix => 'SEEK_', type => 'int' }, 'set', 'cur', 'end', ); $ffi->attach( fopen => [ 'string', 'string' ] => 'opaque' => sub { my($xsub, $class, $filename, $mode) = @_; my $ptr = $xsub->($filename, $mode); unless(defined $ptr) { croak "Error opening $filename with mode $mode: $!"; } bless \$ptr, $class; }); $ffi->attach( tmpfile => [] => 'opaque' => sub { my($xsub, $class) = @_; if(my $ptr = $xsub->()) { return bless \$ptr, $class; } else { croak "Error opening temp file: $!" } }); sub new { my($class, $ptr) = @_; bless \$ptr, $class; } $ffi->attach( freopen => ['string','string','FILE'] => 'opaque' => sub { my($xsub, $self, $filename, $mode) = @_; if(my $ptr = $xsub->($filename, $mode, $self)) { $$self = $ptr; } else { $$self = undef; $filename = 'undef' unless defined $filename; croak "Error opening $filename with mode $mode: $!"; } }); sub _read_write_wrapper { my($xsub, $self, $buffer, $size) = @_; $self->clearerr; my $ret = $xsub->($$buffer, 1, $size, $self); if($ret != $size) { if(my $error = $self->ferror) { croak "File error: $!"; } } return $ret; } $ffi->attach( fread => ['string', 'size_t', 'size_t', 'FILE'] => 'size_t' => \&_read_write_wrapper ); $ffi->attach( fwrite => ['string', 'size_t', 'size_t', 'FILE'] => 'size_t' => \&_read_write_wrapper ); $ffi->attach( fseek => ['FILE', 'long', 'whence'] => 'int', sub { my $xsub = shift; $xsub->(@_) and croak "Error seeking file: $!"; }); $ffi->attach( ftell => ['FILE'] => 'long' ); $ffi->attach( rewind => ['FILE'] ); $ffi->attach( fflush => ['FILE'] => 'int' => sub { my($xsub, $self) = @_; my $ret = $xsub->($self); die 'fixme' unless $ret == 0; return; }); $ffi->attach( clearerr => ['FILE'] ); $ffi->attach( feof => ['FILE'] => 'int' ); $ffi->attach( ferror => ['FILE'] => 'int' ); sub take { my($self) = @_; my $ptr = $$self; $$self = undef; $ptr; } $ffi->attach( fclose => [ 'FILE' ] => 'int' => sub { my($xsub, $self) = @_; my $ret = $xsub->($self); if($ret != 0) { croak "Error closing file: $!"; } $$self = undef; }); sub DESTROY { my($self) = @_; $self->fclose if defined $$self; } 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::File - Perl interface to C File pointer =head1 VERSION version 0.15 =head1 SYNOPSIS use FFI::C::File; my $file1 = FFI::C::File->fopen("foo.txt", "w"); my $content1 = "hello world!\n"; $file1->fwrite(\$content1, length $content); $file1->fclose; my $file2 = FFI::C::File->fopen("foo.txt", "r"); # take gets the file pointer, $file2 is no longer # usable. my $ptr = $file2->take; # reconstitute the File object using the same file # pointer my $file3 = FFI::C::File->new($ptr); my $content3 = "\0" x length $content; $file3->fread(\$content3, length $content); print $content3; # "hello world!\n"; =head1 DESCRIPTION This class provides an interface to the standard C library file pointers. Normally from Perl you want to use the native Perl file interfaces, but sometimes you might be working with a C library that uses C library file pointers (anytime you see the C type this is the case), and having C native interface can be useful. For example, if you have a C function that takes a file pointer: void foo(FILE *fp); You can use it from your Perl code like so: use FFI::Platypus 1.00; use FFI::C::File; my $ffi = FFI::Platypus->new( api => 1 ); $ffi->attach( foo => ['object(FFI::C::File)'] ); my $file = FFI::C::File->fopen("foo.txt", "r"); foo($file); As long as this class "owns" the file pointer it will close it automatically when it falls out of scope. If the C API you are calling is taking ownership of the file pointer and is expected to close the file itself, then you can use the take method to take the file pointer. Once this method is called, the file object is no longer usable (though it can be later reconstituted using the C constructor). use FFI::Platypus 1.00; use FFI::C::File; my $ffi = FFI::Platypus->new( api => 1 ); $ffi->attach( foo => ['opaque'] ); my $file = FFI::C::File->fopen("foo.txt", "r"); my $ptr = $file->ptr; foo($ptr); Likewise, if a C API returns a file pointer that you are expected to close you can create a new File object from the opaque pointer using the C constructor. C: FILE *bar(); Perl: use FFI::Platypus 1.00; use FFI::C::File; my $ffi = FFI::Platypus->new( api => 1 ); $ffi->attach( bar => [] => 'opaque' ); my $ptr = bar(); my $file = FFI::C::File->new($ptr); # can now read/write etc to/from $file Constructors and methods will throw an exception on errors. End-of-File (EOF) is not considered an error. The subclass L extends this class by adding some POSIX extensions for platforms that support them. =head1 CONSTRUCTOR =head2 fopen my $file = FFI::C::File->fopen($filename, $mode); Opens the file with the given mode. See your standard library C documentation for the exact format of C<$mode>. =head2 tmpfile my $file = FFI::C::File->tmpfile; Creates and opens a temporary file. The file is opened as binary file for update. On Windows this may require administrator privileges. =head2 new my $file = FFI::C::File->new($ptr); Create a new File instance object from the opaque pointer. Note that it isn't possible to do any error checking on the type, so make sure that the pointer you are providing really is a C file pointer. =head1 METHODS =head2 freopen $file->freopen($filename, $mode); Re-open the file stream. If C<$filename> is C, then the same file is reopened. This can be useful for reopening a file in a different mode. Note that the mode changes that are allowed are platform dependent. On some platforms (Linux, macOS and possibly some others) you can pass C as the C<$filename>. This is a way to change the C<$mode> without changing the file. =head2 fread my $bytes = $file->fread(\$buffer, $size); Read up to C<$size> bytes into C<$buffer>. C<$buffer> must be preallocated, otherwise memory corruption will happen. Returns the number of bytes actually read, which may be fewer than the number of bytes requested if the end of file is reached. =head2 fwrite my $bytes = $file->fwrite(\$buffer, $size); Write up to C<$size> bytes from C<$buffer>. Returns the number of bytes actually written. =head2 fseek $file->fseek($offset, $whence); Seek to the specified location in the file. C<$whence> should be one of the following (either strings, or constants can be used, the constants can be imported from this module): =over 4 =item C<'set'> | SEEK_SET Relative to the start of the file =item C<'cur'> | SEEK_CUR Relative to the current location of the file pointer. =item C<'end'> | SEEK_END Relative to the end of the file. =back =head2 ftell my $offset = $file->ftell; Returns the file position indicator for the file pointer. =head2 rewind $file->rewind; Moves the file position indicator to the beginning of the file. =head2 fflush $file->fflush; Flush the file stream. =head2 clearerr $file->clearerr; Clear the error flag for the file stream. =head2 feof my $bool = $file->feof; Returns true if the end of file has been reached. False otherwise. =head2 ferror my $error = $file->ferror; Returns the file error code. =head2 take my $ptr = $file->take; Takes ownership of the file from the object and returns the opaque file pointer. =head2 fclose $file->close; Close the file. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/PosixFile.pm000644 000000 000000 00000005306 14334741503 016005 0ustar00rootroot000000 000000 package FFI::C::PosixFile; use strict; use warnings; use Carp qw( croak ); use base qw( FFI::C::File ); # ABSTRACT: Perl interface to C File pointer with POSIX extensions our $VERSION = '0.15'; # VERSION our $ffi = $FFI::C::File::ffi; if($ffi->find_symbol('fdopen') && $ffi->find_symbol('fileno')) { $ffi->attach( fdopen => [ 'int', 'string' ] => 'opaque' => sub { my($xsub, $class, $fd, $mode) = @_; if(my $ptr = $xsub->($fd, $mode)) { return bless \$ptr, $class; } else { croak "Error opening fd $fd with mode $mode: $!"; } }); $ffi->attach( fileno => [ 'FILE' ] => 'int' ); } else { require Sub::Install; foreach my $ctor (qw( fopen freopen new fdopen tmpfile )) { Sub::Install::install_sub({ code => sub { croak "FFI::C::PosixFile not supported on this platform"; }, into => __PACKAGE__, as => $ctor, }); } } 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::PosixFile - Perl interface to C File pointer with POSIX extensions =head1 VERSION version 0.15 =head1 SYNOPSIS use FFI::C::PosixFile; my $stdout = FFI::C::PosixFile->fdopen(1, "w"); say $stdout->fileno; # prints 1 =head1 DESCRIPTION This is a subclass of L which adds a couple of useful POSIX extensions that may not be available on non-POSIX systems. Trying to create an instance of this class will fail on platforms that do not support the extensions. =head1 CONSTRUCTOR =head2 fopen my $file = FFI::C::PosixFile->fopen($filename, $mode); Opens the file with the given mode. See your standard library C documentation for the exact format of C<$mode>. =head2 new my $file = FFI::C::PosixFile->new($ptr); Create a new File instance object from the opaque pointer. Note that it isn't possible to do any error checking on the type, so make sure that the pointer you are providing really is a C file pointer. =head2 fdopen my $file = FFI::C::PosixFile->fdopen($fd, $mode); Create a new File instance from a POSIX file descriptor. =head1 METHODS =head2 fileno my $fd = $file->fileno; Returns the POSIX file descriptor for the file pointer. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/Struct.pm000644 000000 000000 00000013306 14334741503 015366 0ustar00rootroot000000 000000 package FFI::C::Struct; use strict; use warnings; use FFI::C::Util; use FFI::C::FFI (); use Ref::Util qw( is_ref is_plain_arrayref ); # ABSTRACT: Structured data instance for FFI our $VERSION = '0.15'; # VERSION sub AUTOLOAD { our $AUTOLOAD; my $self = shift; my $name = $AUTOLOAD; $name=~ s/^.*:://; if(my $member = $self->{def}->{members}->{$name}) { my $ptr = $self->{ptr} + $member->{offset}; if($member->{nest}) { my $m = $member->{nest}->create($ptr,$self->{owner} || $self); FFI::C::Util::perl_to_c($m, $_[0]) if @_; return $m; } my $ffi = $self->{def}->ffi; if(defined $member->{count}) { if(defined $_[0]) { if(! is_ref $_[0]) { my $index = shift; Carp::croak("$name Negative index on array member") if $index < 0; Carp::croak("$name OOB index on array member") if $index >= $member->{count}; $ptr += $index * $member->{unitsize}; } elsif(is_plain_arrayref $_[0]) { my $array = shift; Carp::croak("$name OOB index on array member") if @$array > $member->{count}; my $asize = @$array * $member->{unitsize}; $ffi->function( FFI::C::FFI::memcpy_addr() => [ 'opaque', $member->{spec} . "[@{[ scalar @$array ]}]", 'size_t' ] => 'opaque' ) ->call($ptr, $array, $asize); my @a; tie @a, 'FFI::C::Struct::MemberArrayTie', $self, $name, $member->{count}; return \@a; } else { Carp::croak("$name tried to set element to non-scalar"); } } else { my @a; tie @a, 'FFI::C::Struct::MemberArrayTie', $self, $name, $member->{count}; return \@a; } } if(@_) { Carp::croak("$name tried to set member to non-scalar") if is_ref $_[0]; my $src = \$_[0]; # For fixed strings, pad short strings with NULLs $src = \($_[0] . ("\0" x ($member->{size} - do { use bytes; length $_[0] }))) if $member->{rec} && $member->{size} > do { use bytes; length $_[0] }; if(my $enum = $member->{enum}) { if(exists $enum->str_lookup->{$$src}) { $src = \($enum->str_lookup->{$$src}); } elsif(exists $enum->int_lookup->{$$src}) { # nothing } else { Carp::croak("$name tried to set member to invalid enum value"); } } $ffi->function( FFI::C::FFI::memcpy_addr() => [ 'opaque', $member->{spec} . "*", 'size_t' ] => 'opaque' ) ->call($ptr, $src, $member->{unitsize} || $member->{size}); } my $value = $ffi->cast( 'opaque' => $member->{spec} . "*", $ptr ); $value = $$value unless $member->{rec}; $value =~ s/\0.*$// if $member->{trim_string}; if(my $enum = $member->{enum}) { if($enum->rev eq 'str') { if(exists $enum->int_lookup->{$value}) { $value = $enum->int_lookup->{$value}; } } } return $value; } else { Carp::croak("No such member: $name"); } } sub can { my($self, $name) = @_; $self->{def}->{members}->{$name} ? sub { shift->$name(@_) } : $self->SUPER::can($name); } sub DESTROY { my($self) = @_; if($self->{ptr} && !$self->{owner}) { FFI::C::FFI::free(delete $self->{ptr}); } } package FFI::C::Struct::MemberArrayTie; sub TIEARRAY { my($class, $struct, $name, $count) = @_; bless [ $struct, $name, $count ], $class; } sub FETCH { my($self, $index) = @_; my($struct, $name) = @$self; $struct->$name($index); } sub STORE { my($self, $index, $value) = @_; my($struct, $name) = @$self; $struct->$name($index, $value); } sub FETCHSIZE { my($self) = @_; $self->[2]; } sub STORESIZE { my($self) = @_; $self->[2]; } sub CLEAR { Carp::croak("Cannot clear"); } 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::Struct - Structured data instance for FFI =head1 VERSION version 0.15 =head1 SYNOPSIS use FFI::C::StructDef; my $def = FFI::C::StructDef->new( name => 'color_t', class => 'Color', members => [ red => 'uint8', green => 'uint8', blue => 'uint8', ], ); my $red = $def->create({ red => 255 }); # creates a FFI::C::Stuct printf "[%02x %02x %02x]\n", $red->red, $red->green, $red->blue; # [ff 00 00] # that red is too bright! $red->red(200); printf "[%02x %02x %02x]\n", $red->red, $red->green, $red->blue; # [c8 00 00] my $green = Color->new({ green => 255 }); # creates a FFI::C::Stuct printf "[%02x %02x %02x]\n", $green->red, $green->green, $green->blue; # [00 ff 00] =head1 DESCRIPTION This class represents an instance of a C C. This class can be created using C on the generated class, if that was specified for the L, or by using the C method on L. For each member defined in the L there is an accessor for the L instance. =head1 CONSTRUCTOR =head2 new FFI::C::StructDef->new( class => 'User::Struct::Class', ... ); my $instance = User::Struct::Class->new; Creates a new instance of the C. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/StructDef.pm000644 000000 000000 00000031363 14334741503 016010 0ustar00rootroot000000 000000 package FFI::C::StructDef; use strict; use warnings; use 5.008001; use FFI::C::Util; use FFI::C::Struct; use FFI::C::FFI (); use FFI::Platypus 1.24; use Ref::Util qw( is_blessed_ref is_plain_arrayref is_ref ); use Carp (); use Sub::Install (); use Sub::Util (); use Scalar::Util qw( refaddr ); use constant _is_union => 0; use base qw( FFI::C::Def ); our @CARP_NOT = qw( FFI::C::Util FFI::C ); # ABSTRACT: Structured data definition for FFI our $VERSION = '0.15'; # VERSION sub _is_kind { my($self, $name, $want) = @_; my $kind = eval { $self->ffi->kindof($name) }; return undef unless defined $kind; return $kind eq $want; } sub new { my $self = shift->SUPER::new(@_); my %args = %{ delete $self->{args} }; $self->{trim_string} = delete $args{trim_string} ? 1 : 0; my $offset = 0; my $alignment = 0; my $anon = 0; if(my @members = @{ delete $args{members} || [] }) { Carp::croak("Odd number of arguments in member spec") if scalar(@members) % 2; while(@members) { my $name = shift @members; my $spec = shift @members; my %member; if($name ne ':' && $self->{members}->{$name}) { Carp::croak("More than one member with the name $name"); } if($name eq ':') { $name .= (++$anon); } elsif($name !~ /^[A-Za-z_][A-Za-z_0-9]*$/) { Carp::croak("Illegal member name"); } elsif($name eq 'new') { Carp::croak("new now allowed as a member name"); } if(my $def = $self->ffi->def('FFI::C::Def', $spec)) { $spec = $def; } elsif($def = $self->ffi->def('FFI::C::EnumDef', $spec)) { $spec = $def; } if(is_blessed_ref $spec) { if($spec->isa('FFI::C::Def')) { Carp::croak("Canot nest a struct or union def inside of itself") if refaddr($spec) == refaddr($self); $member{nest} = $spec; $member{size} = $spec->size; $member{align} = $spec->align; } elsif($spec->isa('FFI::C::EnumDef')) { $member{spec} = $spec->type; $member{size} = $self->ffi->sizeof($spec->type); $member{align} = $self->ffi->alignof($spec->type); $member{enum} = $spec; } } elsif($self->_is_kind($spec, 'scalar')) { $member{spec} = $spec; $member{size} = $self->ffi->sizeof($spec); $member{align} = $self->ffi->alignof($spec); } elsif($self->_is_kind($spec, 'array')) { $member{spec} = $self->ffi->unitof($spec); $member{count} = $self->ffi->countof($spec); $member{size} = $self->ffi->sizeof($spec); $member{unitsize} = $self->ffi->sizeof($member{spec}); $member{align} = $self->ffi->alignof($spec); Carp::croak("array members must have at least one element") unless $member{count} > 0; } elsif($self->_is_kind("$spec*", 'record')) { local $@; $member{align} = eval { $self->ffi->alignof("$spec*") }; $member{trim_string} = 1 if $self->{trim_string}; $member{spec} = $spec; $member{rec} = 1; $member{size} = $self->ffi->sizeof("$spec*"); Carp::croak("undefined, or unsupported type: $spec") if $@; } else { Carp::croak("undefined or unsupported type: $spec"); } $self->{align} = $member{align} if $member{align} > $self->{align}; if($self->_is_union) { $self->{size} = $member{size} if $member{size} > $self->{size}; $member{offset} = 0; } else { $offset++ while $offset % $member{align}; $member{offset} = $offset; $offset += $member{size}; } $self->{members}->{$name} = \%member; } } $self->{size} = $offset unless $self->_is_union; Carp::carp("Unknown argument: $_") for sort keys %args; if($self->class) { # not handled by the superclass: # 3. Any nested cdefs must have Perl classes. foreach my $name (keys %{ $self->{members} }) { next if $name =~ /^:/; my $member = $self->{members}->{$name}; my $accessor = $self->class . '::' . $name; Carp::croak("Missing Perl class for $accessor") if $member->{nest} && !$member->{nest}->{class}; } $self->_generate_class(keys %{ $self->{members} }); { my $ffi = $self->ffi; foreach my $name (keys %{ $self->{members} }) { my $offset = $self->{members}->{$name}->{offset}; my $code; if($self->{members}->{$name}->{nest}) { my $class = $self->{members}->{$name}->{nest}->{class}; $code = sub { my $self = shift; my $ptr = $self->{ptr} + $offset; my $m = $class->new($ptr,$self); FFI::C::Util::perl_to_c($m, $_[0]) if @_; $m; }; } else { my $type = $self->{members}->{$name}->{spec} . '*'; my $size = $self->{members}->{$name}->{size}; my $set = $ffi->function( FFI::C::FFI::memcpy_addr() => ['opaque',$type,'size_t'] => $type)->sub_ref; my $get = $ffi->function( 0 => ['opaque'] => $type)->sub_ref; if($self->{members}->{$name}->{rec}) { if($self->{trim_string}) { unless(__PACKAGE__->can('_cast_string')) { $ffi->attach_cast('_cast_string', 'opaque', 'string'); } $set = $ffi->function( FFI::C::FFI::memcpy_addr() => ['opaque',$type,'size_t'] => 'string')->sub_ref; $get = \&_cast_string; } $code = sub { my $self = shift; my $ptr = $self->{ptr} + $offset; if(@_) { my $length = do { use bytes; length $_[0] }; my $src = \($size > $length ? $_[0] . ("\0" x ($size-$length)) : $_[0]); return $set->($ptr, $src, $size); } $get->($ptr) }; } elsif(my $count = $self->{members}->{$name}->{count}) { my $unitsize = $self->{members}->{$name}->{unitsize}; my $atype = $self->{members}->{$name}->{spec} . "[$count]"; my $all = $ffi->function( FFI::C::FFI::memcpy_addr() => ['opaque',$atype,'size_t'] => 'void' ); $code = sub { my $self = shift; if(defined $_[0]) { if(is_plain_arrayref $_[0]) { my $array = shift; Carp::croak("$name OOB index on array member") if @$array > $count; my $ptr = $self->{ptr} + $offset; my $size = (@$array ) * $unitsize; $all->($ptr, $array, (@$array * $unitsize)); # we don't want to have to get the array and tie it if # it isn't going to be used anyway. return unless defined wantarray; ## no critic (Community::Wantarray) } elsif(! is_ref $_[0]) { my $index = shift; Carp::croak("$name Negative index on array member") if $index < 0; Carp::croak("$name OOB index on array member") if $index >= $count; my $ptr = $self->{ptr} + $offset + $index * $unitsize; return @_ ? ${ $set->($ptr,\$_[0],$unitsize) } : ${ $get->($ptr) }; } else { Carp::croak("$name tried to set element to non-scalar"); } } my @a; tie @a, 'FFI::C::Struct::MemberArrayTie', $self, $name, $count; return \@a; }; } elsif(my $enum = $self->{members}->{$name}->{enum}) { my $str_lookup = $enum->str_lookup; my $int_lookup = $enum->int_lookup; if($enum->rev eq 'str') { $code = sub { my $self = shift; my $ptr = $self->{ptr} + $offset; Carp::croak("$name tried to set member to non-scalar") if @_ && is_ref $_[0]; my $ret = @_ ? do { my $arg = exists $str_lookup->{$_[0]} ? $str_lookup->{$_[0]} : exists $int_lookup->{$_[0]} ? $_[0] : Carp::croak("No such value for $name: $_[0]"); ${ $set->($ptr,\$arg,$size) } } : ${ $get->($ptr) }; $int_lookup->{$ret} ? $int_lookup->{$ret} : $ret; }; } else { $code = sub { my $self = shift; my $ptr = $self->{ptr} + $offset; Carp::croak("$name tried to set member to non-scalar") if @_ && is_ref $_[0]; @_ ? do { my $arg = exists $str_lookup->{$_[0]} ? $str_lookup->{$_[0]} : exists $int_lookup->{$_[0]} ? $_[0] : Carp::croak("No such value for $name: $_[0]"); ${ $set->($ptr,\$arg,$size) } } : ${ $get->($ptr) }; }; } } else { $code = sub { my $self = shift; my $ptr = $self->{ptr} + $offset; Carp::croak("$name tried to set member to non-scalar") if @_ && is_ref $_[0]; @_ ? ${ $set->($ptr,\$_[0],$size) } : ${ $get->($ptr) }; }; } } Sub::Util::set_subname(join('::', $self->class, $name), $code); Sub::Install::install_sub({ code => $code, into => $self->class, as => $name, }); } } } $self; } sub trim_string { shift->{trim_string} } 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::StructDef - Structured data definition for FFI =head1 VERSION version 0.15 =head1 SYNOPSIS In your C code: #include #include typedef struct { uint8_t red; uint8_t green; uint8_t blue; } color_t; void print_color(color_t *c) { printf("[%02x %02x %02x]\n", c->red, c->green, c->blue ); } In your Perl code: use FFI::Platypus 1.00; use FFI::C::StructDef; my $ffi = FFI::Platypus->new( api => 1 ); # See FFI::Platypus::Bundle for how bundle works. $ffi->bundle; my $def = FFI::C::StructDef->new( $ffi, name => 'color_t', class => 'Color', members => [ red => 'uint8', green => 'uint8', blue => 'uint8', ], ); my $red = Color->new({ red => 255 }); my $green = Color->new({ green => 255 }); $ffi->attach( print_color => ['color_t'] ); print_color($red); # [ff 00 00] print_color($green); # [00 ff 00] # that red is a tad bright! $red->red( 200 ); print_color($red); # [c8 00 00] =head1 DESCRIPTION This class creates a def for a C C. =head1 CONSTRUCTOR =head2 new my $def = FFI::C::StructDef->new(%opts); my $def = FFI::C::StructDef->new($ffi, %opts); For standard def options, see L. =over 4 =item members This should be an array reference containing name, type pairs, in the order that they will be stored in the struct. =item trim_string If true, fixed-length strings should be treated as null terminated strings and be trimmed. =back =head1 METHODS =head2 create my $instance = $def->create; my $instance = $def->class->new; # if class was specified my $instance = $def->create(\%init); my $instance = $def->class->new(\%init); # if class was specified This creates an instance of the C, returns a L. You can optionally initialize member values using C<%init>. =head2 trim_string my $bool = $def->trim_string; Returns true if fixed-length strings should be treated as null terminated strings and be trimmed. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/Union.pm000644 000000 000000 00000003272 14334741503 015173 0ustar00rootroot000000 000000 package FFI::C::Union; use strict; use warnings; use base qw( FFI::C::Struct ); # ABSTRACT: Union data instance for FFI our $VERSION = '0.15'; # VERSION 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::Union - Union data instance for FFI =head1 VERSION version 0.15 =head1 SYNOPSIS use FFI::C::UnionDef; my $def = FFI::C::UnionDef->new( name => 'anyint_t', class => 'AnyInt', members => [ u8 => 'uint8', u16 => 'uint16', u32 => 'uint32', ], ); my $int = AnyInt->new({ u8 => 42 }); printf "0x%x\n", $int->u32; # 0x2a on Intel =head1 DESCRIPTION This class represents an instance of a C C. This class can be created using C on the generated class, if that was specified for the L, or by using the C method on L. For each member defined in the L there is an accessor for the L instance. =head1 CONSTRUCTOR =head2 new FFI::C::UnionDef->new( class => 'User::Union::Class', ... ); my $instance = User::Union::Class->new; Creates a new instance of the C. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/UnionDef.pm000644 000000 000000 00000004644 14334741503 015616 0ustar00rootroot000000 000000 package FFI::C::UnionDef; use strict; use warnings; use 5.008001; use FFI::C::Union; use FFI::Platypus 1.24; use constant _is_union => 1; use base qw( FFI::C::StructDef ); # ABSTRACT: Union data definition for FFI our $VERSION = '0.15'; # VERSION 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::UnionDef - Union data definition for FFI =head1 VERSION version 0.15 =head1 SYNOPSIS In your C code: #include #include typedef union { uint8_t u8; uint16_t u16; uint32_t u32; } anyint_t; void print_anyint_as_u32(anyint_t *any) { printf("0x%x\n", any->u32); } In your Perl code: use FFI::Platypus 1.00; use FFI::C::UnionDef; my $ffi = FFI::Platypus->new( api => 1 ); # See FFI::Platypus::Bundle for how bundle works. $ffi->bundle; my $def = FFI::C::UnionDef->new( $ffi, name => 'anyint_t', class => 'AnyInt', members => [ u8 => 'uint8', u16 => 'uint16', u32 => 'uint32', ], ); $ffi->attach( print_anyint_as_u32 => ['anyint_t'] ); my $int = AnyInt->new({ u8 => 42 }); print_anyint_as_u32($int); # 0x2a on Intel, =head1 DESCRIPTION This class creates a def for a C C. =head1 CONSTRUCTOR =head2 new my $def = FFI::C::UnionDef->new(%opts); my $def = FFI::C::UnionDef->new($ffi, %opts); For standard def options, see L. =over 4 =item members This should be an array reference containing name, type pairs. For a union, the order doesn't matter. =back =head1 METHODS =head2 create my $instance = $def->create; my $instance = $def->class->new; # if class was specified my $instance = $def->create(\%init); my $instance = $def->class->new(\%init); # if class was specified This creates an instance of the C, returns a L. You can optionally initialize member values using C<%init>. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/lib/FFI/C/Util.pm000644 000000 000000 00000014211 14334741503 015013 0ustar00rootroot000000 000000 package FFI::C::Util; use strict; use warnings; use 5.008001; use Ref::Util qw( is_blessed_ref is_plain_arrayref is_plain_hashref is_ref is_blessed_hashref ); use Sub::Identify 0.05 (); use Carp (); use Class::Inspector; use base qw( Exporter ); our @EXPORT_OK = qw( perl_to_c c_to_perl take owned set_array_count addressof ); # ABSTRACT: Utility functions for dealing with structured C data our $VERSION = '0.15'; # VERSION sub perl_to_c ($$) { my($inst, $values) = @_; if(is_blessed_ref $inst && $inst->isa('FFI::C::Array')) { Carp::croak("Tried to initalize a @{[ ref $inst ]} with something other than an array reference") unless is_plain_arrayref $values; &perl_to_c($inst->get($_), $values->[$_]) for 0..$#$values; } elsif(is_blessed_ref $inst) { Carp::croak("Tried to initalize a @{[ ref $inst ]} with something other than an hash reference") unless is_plain_hashref $values; foreach my $name (keys %$values) { my $value = $values->{$name}; $inst->$name($value); } } else { Carp::croak("Not an object"); } } sub c_to_perl ($) { my $inst = shift; Carp::croak("Not an object") unless is_blessed_ref($inst); if($inst->isa("FFI::C::Array")) { return [map { &c_to_perl($_) } @$inst] } elsif($inst->isa("FFI::C::Struct")) { my $def = $inst->{def}; my %h; foreach my $key (keys %{ $def->{members} }) { next if $key =~ /^:/; my $value = $inst->$key; $value = &c_to_perl($value) if is_blessed_ref $value; $value = [@$value] if is_plain_arrayref $value; $h{$key} = $value; } return \%h; } else { my %h; my $df = $INC{'FFI/C/StructDef.pm'}; foreach my $key (@{ Class::Inspector->methods(ref $inst) }) { next if $key =~ /^(new|DESTROY)$/; # we only want to recurse on generated methods. my ($file) = Sub::Identify::get_code_location( $inst->can($key) ); next unless $file eq $df; # get the value; my $value = $inst->$key; $value = &c_to_perl($value) if is_blessed_hashref $value; $value = [@$value] if is_plain_arrayref $value; $h{$key} = $value; } return \%h; } } sub owned ($) { my $inst = shift; !!($inst->{ptr} && !$inst->{owner}); } sub take ($) { my $inst = shift; Carp::croak("Not an object") unless is_blessed_ref $inst; Carp::croak("Object is owned by someone else") if $inst->{owner}; my $ptr = delete $inst->{ptr}; Carp::croak("Object pointer went away") unless $ptr; $ptr; } sub addressof ($) { my $inst = shift; Carp::croak("Not an object") unless is_blessed_ref $inst; my $ptr = $inst->{ptr}; Carp::croak("Object pointer went away") unless $ptr; $ptr; } sub set_array_count ($$) { my($inst, $count) = @_; Carp::croak("Not a FFI::C::Array") unless is_blessed_ref $inst && $inst->isa('FFI::C::Array'); Carp::croak("This array already has a size") if $inst->{count}; $inst->{count} = $count; } 1; __END__ =pod =encoding UTF-8 =head1 NAME FFI::C::Util - Utility functions for dealing with structured C data =head1 VERSION version 0.15 =head1 SYNOPSIS use FFI::C::Util qw( perl_to_c take ); use FFI::C::StructDef; use FFI::Platypus::Memory qw( free ); my $def = FFI::C::StructDef->new( members => [ x => 'uint8', y => 'sint64', ], ); my $inst = $def->create; # initalize members perl_to_c($inst, { x => 1, y => 2 }); # take ownership my $ptr = take $inst; # since we took ownership, we are responsible for freeing the memory: free $ptr; =head1 DESCRIPTION This module provides some useful utility functions for dealing with the various def instances provided by L =head1 FUNCTIONS =head2 perl_to_c perl_to_c $instance, \%values; # for Struct/Union perl_to_c $instance, \@values; # for Array This function initializes the members of an instance. =head2 c_to_perl my $perl = c_to_perl $instance; This function takes an instance and returns the nested members as Perl data structures. =head2 owned my $bool = owned $instance; Returns true of the C<$instance> owns its allocated memory. That is, it will free up the allocated memory when it falls out of scope. Reasons an instance might not be owned are: =over 4 =item the instance is nested inside another object that owns the memory =item the instance was returned from a C function that owns the memory =item ownership was taken away by the C function below. =back =head2 take my $ptr = take $instance; This function takes ownership of the instance pointer, and returns the opaque pointer. This means a couple of things: =over 4 =item C<$instance> will not free its data automatically You should call C on it manually to free the memory it is using. =item C<$instance> cannot be used anymore So don't try to get/set any of its members, or pass it into a function. =back The returned pointer can be cast into something else or passed into a function that takes an C argument. =head2 addressof [version 0.11] my $ptr = addressof $instance; This function returns the address (as an C type) of the instance object. This is similar to C above in that it gets you the address of the object, but doesn't take ownership of it, so care needs to be taken when using C<$ptr> that the object is still allocated. =head2 set_array_count set_array_count $inst, $count; This function sets the element count on a variable array returned from C (where normally there is no way to know from just the return value). If you try to set a count on a non-array or a fixed sized array an exception will be thrown. =head1 SEE ALSO =over 4 =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =item L =back =head1 AUTHOR Graham Ollis =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2020-2022 by Graham Ollis. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut FFI-C-0.15/maint/000755 000000 000000 00000000000 14334741503 013315 5ustar00rootroot000000 000000 FFI-C-0.15/maint/cip-before-install000755 000000 000000 00000000246 14334741503 016724 0ustar00rootroot000000 000000 #!/bin/bash set -ex cip sudo apt-get update cip sudo apt-get install libffi-dev cip sudo chown -R cip.cip /home/cip/dzil cip exec ./maint/cip-before-install-inside FFI-C-0.15/maint/cip-before-install-inside000755 000000 000000 00000000375 14334741503 020200 0ustar00rootroot000000 000000 #!/bin/bash set -ex mkdir -p ~/tmp cd $(env TMPDIR=$HOME/tmp mktemp -d) curl -L https://github.com/PerlFFI/FFI-Platypus/tarball/main | tar zxf - cd $(ls -1) dzil-dzil authordeps --missing | dzil-cpanm -n dzil-dzil build cpanm -n FFI-Platypus*.tar.gz FFI-C-0.15/maint/gh-macos000755 000000 000000 00000000255 14334741503 014743 0ustar00rootroot000000 000000 #!/bin/bash set -ex cd $(mktemp -d) curl -L https://github.com/PerlFFI/FFI-Platypus/tarball/main | tar zxf - cd $(ls -1) dzil authordeps --missing | cpanm -n dzil install FFI-C-0.15/maint/gh-windows.pl000644 000000 000000 00000000532 14334741503 015740 0ustar00rootroot000000 000000 use strict; use warnings; use File::chdir; sub run { my @cmd = @_; print "+@cmd\n"; system @cmd; exit 2 if $?; } run( qw( git clone https://github.com/PerlFFI/FFI-Platypus.git \FFI-Platypus ) ); { local $CWD = "/FFI-Platypus"; run( "dzil authordeps --missing | cpanm -n" ); run( qw( dzil install ) ); run( "bogus command" ); } FFI-C-0.15/perlcriticrc000644 000000 000000 00000003722 14334741503 014621 0ustar00rootroot000000 000000 severity = 1 only = 1 [Community::ArrayAssignAref] [Community::BarewordFilehandles] [Community::ConditionalDeclarations] [Community::ConditionalImplicitReturn] [Community::DeprecatedFeatures] [Community::DiscouragedModules] [Community::DollarAB] [Community::Each] [Community::EmptyReturn] [Community::IndirectObjectNotation] [Community::LexicalForeachIterator] [Community::LoopOnHash] [Community::ModPerl] [Community::OpenArgs] [Community::OverloadOptions] [Community::POSIXImports] [Community::PackageMatchesFilename] [Community::PreferredAlternatives] [Community::StrictWarnings] extra_importers = Test2::V0 [Community::Threads] [Community::Wantarray] [Community::WarningsSwitch] [Community::WhileDiamondDefaultAssignment] [BuiltinFunctions::ProhibitBooleanGrep] [BuiltinFunctions::ProhibitStringyEval] [BuiltinFunctions::ProhibitStringySplit] [BuiltinFunctions::ProhibitVoidGrep] [BuiltinFunctions::ProhibitVoidMap] [ClassHierarchies::ProhibitExplicitISA] [ClassHierarchies::ProhibitOneArgBless] [CodeLayout::ProhibitHardTabs] allow_leading_tabs = 0 [CodeLayout::ProhibitTrailingWhitespace] [CodeLayout::RequireConsistentNewlines] [ControlStructures::ProhibitLabelsWithSpecialBlockNames] [ControlStructures::ProhibitMutatingListFunctions] [ControlStructures::ProhibitUnreachableCode] [InputOutput::ProhibitBarewordFileHandles] [InputOutput::ProhibitJoinedReadline] [InputOutput::ProhibitTwoArgOpen] [Miscellanea::ProhibitFormats] [Miscellanea::ProhibitUselessNoCritic] [Modules::ProhibitConditionalUseStatements] ;[Modules::RequireEndWithOne] [Modules::RequireNoMatchVarsWithUseEnglish] [Objects::ProhibitIndirectSyntax] [RegularExpressions::ProhibitUselessTopic] [Subroutines::ProhibitNestedSubs] [ValuesAndExpressions::ProhibitLeadingZeros] [ValuesAndExpressions::ProhibitMixedBooleanOperators] [ValuesAndExpressions::ProhibitSpecialLiteralHeredocTerminator] [ValuesAndExpressions::RequireUpperCaseHeredocTerminator] [Variables::ProhibitPerl4PackageNames] [Variables::ProhibitUnusedVariables] FFI-C-0.15/t/000755 000000 000000 00000000000 14334741503 012450 5ustar00rootroot000000 000000 FFI-C-0.15/t/00_diag.t000644 000000 000000 00000002716 14334741503 014046 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use Config; eval { require 'Test/More.pm' }; # This .t file is generated. # make changes instead to dist.ini my %modules; my $post_diag; $modules{$_} = $_ for qw( Capture::Tiny Class::Inspector ExtUtils::MakeMaker FFI::Platypus FFI::Platypus::Memory FFI::Platypus::Record FFI::Platypus::Type::Enum Math::Int64 Path::Tiny Ref::Util Sub::Identify Sub::Install Sub::Util Test2::V0 ); my @modules = sort keys %modules; sub spacer () { diag ''; diag ''; diag ''; } pass 'okay'; my $max = 1; $max = $_ > $max ? $_ : $max for map { length $_ } @modules; our $format = "%-${max}s %s"; spacer; my @keys = sort grep /(MOJO|PERL|\A(LC|HARNESS)_|\A(SHELL|LANG)\Z)/i, keys %ENV; if(@keys > 0) { diag "$_=$ENV{$_}" for @keys; if($ENV{PERL5LIB}) { spacer; diag "PERL5LIB path"; diag $_ for split $Config{path_sep}, $ENV{PERL5LIB}; } elsif($ENV{PERLLIB}) { spacer; diag "PERLLIB path"; diag $_ for split $Config{path_sep}, $ENV{PERLLIB}; } spacer; } diag sprintf $format, 'perl', "$] $^O $Config{archname}"; foreach my $module (sort @modules) { my $pm = "$module.pm"; $pm =~ s{::}{/}g; if(eval { require $pm; 1 }) { my $ver = eval { $module->VERSION }; $ver = 'undef' unless defined $ver; diag sprintf $format, $module, $ver; } else { diag sprintf $format, $module, '-'; } } if($post_diag) { spacer; $post_diag->(); } spacer; done_testing; FFI-C-0.15/t/ffi_c.t000644 000000 000000 00000007011 14334741503 013702 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::C; use FFI::C::Util qw( c_to_perl ); use FFI::Platypus; use Capture::Tiny qw( capture_merged ); my $ffi = FFI::Platypus->new( api => 1 ); is( FFI::C->ffi($ffi), object { call [ isa => 'FFI::Platypus' ] => T(); }, 'FFI::C->ffi first set ok', ); is( dies { FFI::C->ffi($ffi) }, match qr/Already have an FFI::Platypus instance for/, 'FFI::C->ffi second call dies', ); is( FFI::C->ffi, object { call [ isa => 'FFI::Platypus' ] => T(); }, 'FFI::C->ffi can call get as many times as we like', ); subtest 'example' => sub { skip_all 'test requires Perl 5.14 or better' unless $] >= 5.014; { my($out, $ret) = capture_merged { require './examples/synopsis/c.pl'; }; is $ret, T(), 'example compiles'; note $out; } is( c_to_perl(ArrayNamedColor->new([ { name => "red", value => { red => 255 } }, { name => "green", value => { green => 255 } }, { name => "blue", value => { blue => 255 } }, { name => "purple", value => { red => 255, blue => 255 } }, ])), [ { name => match qr/^red\0+$/, value => { red => 255, blue => 0, green => 0 } }, { name => match qr/^green\0+$/, value => { red => 0, blue => 0, green => 255 } }, { name => match qr/^blue\0+$/, value => { red => 0, blue => 255, green => 0 } }, { name => match qr/^purple\0+$/, value => { red => 255, blue => 255, green => 0 } }, ], 'create instance array + struct', ); is( c_to_perl(AnyInt->new({ u8 => 42 })), hash { field u8 => 42; field u16 => match qr/^[0-9]+$/; field u32 => match qr/^[0-9]+$/; field u64 => D(); # on 32bit this can be a scalar ref that # confuses Test2 end; }, 'create instance union' ); }; subtest 'with type name' => sub { { package ColorValue1; FFI::C->struct( color_value_t => [ red => 'uint8', green => 'uint8', blue => 'uint8', ]); } { package NamedColor1; FFI::C->struct( named_color_t => [ name => 'string(22)', value => 'color_value_t', ]); } { package ArrayNamedColor1; FFI::C->array(array_named_color_t => ['named_color_t' => 4]); }; is( c_to_perl(ArrayNamedColor1->new([ { name => "red", value => { red => 255 } }, { name => "green", value => { green => 255 } }, { name => "blue", value => { blue => 255 } }, { name => "purple", value => { red => 255, blue => 255 } }, ])), [ { name => match qr/^red\0+$/, value => { red => 255, blue => 0, green => 0 } }, { name => match qr/^green\0+$/, value => { red => 0, blue => 0, green => 255 } }, { name => match qr/^blue\0+$/, value => { red => 0, blue => 255, green => 0 } }, { name => match qr/^purple\0+$/, value => { red => 255, blue => 255, green => 0 } }, ], 'create instance array + struct', ); { package AnyInt1; FFI::C->union(any_int_t => [ u8 => 'uint8', u16 => 'uint16', u32 => 'uint32', u64 => 'uint64', ]); } is( c_to_perl(AnyInt1->new({ u8 => 42 })), hash { field u8 => 42; field u16 => match qr/^[0-9]+$/; field u32 => match qr/^[0-9]+$/; field u64 => D(); # On 32bit this can be a scalar ref # (blessed) that confuses Test2 end; }, 'create instance union' ); }; done_testing; FFI-C-0.15/t/ffi_c_array.t000644 000000 000000 00000003631 14334741503 015104 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::Platypus 1.24; use FFI::C::StructDef; use FFI::C::ArrayDef; use FFI::C::Array; { package Foo; use FFI::Platypus::Record; record_layout_1( sint64 => 'bar', ); } subtest 'basic' => sub { my $def = FFI::C::ArrayDef->new( name => "foo_t", members => [ FFI::C::StructDef->new( members => [ bar => 'sint64', ]), 1, ], ); is( do { my $o = $def->create; $o->[0]->bar(-42); $def->ffi->cast('foo_t' => 'record(Foo)*', $o); }, object { call [ isa => 'Foo' ] => T(); call bar => -42; }, 'object argument', ); is( do { our $r = Foo->new( bar => -47 ); $def->ffi->cast('record(Foo)*', 'foo_t', $r); }, object { call [ isa => 'FFI::C::Array' ] => T(); call [ get => 0 ] => object { call bar => -47; }; field owner => 1; field def => object { call [ isa => 'FFI::C::ArrayDef' ] => T(); call name => 'foo_t'; }; etc; }, 'object return', ); }; subtest 'var' => sub { my $def = FFI::C::ArrayDef->new( name => "foo_t", members => [ FFI::C::StructDef->new( members => [ bar => 'sint64', ]), ], ); is( do { my $o = $def->create([{ bar => -42 }]); $def->ffi->cast('foo_t' => 'record(Foo)*', $o); }, object { call [ isa => 'Foo' ] => T(); call bar => -42; }, 'object argument', ); is( do { our $r = Foo->new( bar => -47 ); $def->ffi->cast('record(Foo)*', 'foo_t', $r); }, object { call [ isa => 'FFI::C::Array' ] => T(); call [ get => 0 ] => object { call bar => -47; }; field owner => 1; field def => object { call [ isa => 'FFI::C::ArrayDef' ] => T(); call name => 'foo_t'; }; etc; }, 'object return', ); }; done_testing; FFI-C-0.15/t/ffi_c_arraydef.t000644 000000 000000 00000014031 14334741503 015557 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::C::FFI qw( malloc ); use FFI::C::ArrayDef; use FFI::C::StructDef; is( FFI::C::ArrayDef->new( name => 'foo', members => [ FFI::C::StructDef->new( members => [ u64 => 'uint64', ]), 10, ]), object { call [ isa => 'FFI::C::ArrayDef' ] => T(); call ffi => object { call [ isa => 'FFI::Platypus' ] => T() }; call size => 80; call align => match qr/^[0-9]+$/; call name => 'foo'; call create => object { call [ isa => 'FFI::C::Array' ] => T(); call [ get => 5] => object { call [ isa => 'FFI::C::Struct' ] => T(); call u64 => 0; call [ u64 => 10 ] => 10; call u64 => 10; }; call [ get => 4] => object { call [ isa => 'FFI::C::Struct' ] => T(); call u64 => 0; call [ u64 => 6 ] => 6; call u64 => 6; }; call [ get => 5] => object { call [ isa => 'FFI::C::Struct' ] => T(); call u64 => 10; }; call [ get => 4] => object { call [ isa => 'FFI::C::Struct' ] => T(); call u64 => 6; }; call_list sub { map { $_->u64 } @{ shift() } } => [ 0, 0, 0, 0, 6, 10, 0, 0, 0, 0 ]; call tie => array { item object { call [ isa => 'FFI::C::Struct' ] => T(); call u64 => $_; } for ( 0, 0, 0, 0, 6, 10, 0, 0, 0, 0 ); end; }; }; call [ create => [ map { { u64 => $_ * 100 } } (1..10) ] ] => object { call [ isa => 'FFI::C::Array' ] => T(); call [ get => $_-1 ] => object { call u64 => $_*100 } for 1..10; }; }, 'simple' ); { FFI::C::ArrayDef->new( name => 'color_array_t', class => 'Color::Array', members => [ FFI::C::StructDef->new( name => 'color_value_t', class => 'Color::Value', members => [ red => 'uint8', green => 'uint8', blue => 'uint8', ], ), 2, ], ); is( Color::Array->new([ { red => 1, green => 2, blue => 3 }, { red => 4, green => 5, blue => 6 }]), object { call [ isa => 'Color::Array' ] => T(); call [ get => 0 ] => object { call [ isa => 'Color::Value' ] => T(); call red => 1; call green => 2; call blue => 3; }; call [ get => 1 ] => object { call [ isa => 'Color::Value' ] => T(); call red => 4; call green => 5; call blue => 6; }; }, 'initalizers', ); is( Color::Array->new, object { call [ isa => 'Color::Array' ] => T(); call [ isa => 'FFI::C::Array' ] => T(); call [ get => 0 ] => object { call [ isa => 'Color::Value' ] => T(); call red => 0; call green => 0; call blue => 0; call [ red => 0xff ] => 0xff; call red => 0xff; call green => 0; call blue => 0; }; call [ get => 1 ] => object { call [ isa => 'Color::Value' ] => T(); call red => 0; call green => 0; call blue => 0; }; call [ get => 0 ] => object { call [ isa => 'Color::Value' ] => T(); call red => 0xff; call green => 0; call blue => 0; }; call [ get => 1 ] => object { call [ isa => 'Color::Value' ] => T(); call red => 0; call green => 0; call blue => 0; }; call count => 2; call sub { my $self = shift; dies { $self->get(2) } } => match qr/OOB array index/; call sub { my $self = shift; dies { $self->get(-1) } } => match qr/Negative array index/; }, 'default count' ); is( Color::Array->new(3), object { call [ isa => 'Color::Array' ] => T(); call [ isa => 'FFI::C::Array' ] => T(); call count => 3; call [ get => 0 ] => object {}; call [ get => 1 ] => object {}; call [ get => 2 ] => object {}; call sub { my $self = shift; dies { $self->get(3) } } => match qr/OOB array index/; }, 'override count' ); } { my $ffi = FFI::Platypus->new( api => 1 ); my $vdef = FFI::C::StructDef->new( $ffi, name => 'color_value_t', class => 'Color::VarValue', members => [ red => 'uint8', green => 'uint8', blue => 'uint8', ], ); FFI::C::ArrayDef->new( $ffi, name => 'color_array_t', class => 'Color::VarArray', members => [ 'color_value_t', ], ); is( dies { Color::VarArray->new }, match qr/Cannot create array without knowing the number of elements/, 'var array dies without size', ); is( Color::VarArray->new(2), object { call [ isa => 'Color::VarArray' ] => T(); call [ get => 0 ] => object {}; call [ get => 1 ] => object {}; call sub { my $self = shift; dies { $self->get(2) } } => match qr/OOB array index/; call sub { my $self = shift; dies { $self->get(-1) } } => match qr/Negative array index/; }, 'Create var array with size' ); is( Color::VarArray->new([{ red => 255, green => 128, blue => 25 },{}]), object { call [ isa => 'Color::VarArray' ] => T(); call [ get => 0 ] => object {}; call [ get => 1 ] => object {}; call sub { my $self = shift; dies { $self->get(2) } } => match qr/OOB array index/; call sub { my $self = shift; dies { $self->get(-1) } } => match qr/Negative array index/; }, 'Create var away with array ref', ); is( $ffi->cast('opaque', 'color_array_t', malloc(10 * $vdef->size)), object { call [ isa => 'Color::VarArray' ] => T(); call [ get => 0 ] => object {}; call [ get => 1 ] => object {}; call sub { my $self = shift; dies { $self->get(-1) } } => match qr/Negative array index/; }, 'create from pointer', ); } is( dies { my $ffi = FFI::Platypus->new( api => 1 ); FFI::C::ArrayDef->new( name => 'self_nest_t', members => [ 'self_nest_t', ], ); }, match qr/Canot nest an array def inside of itself/, 'Canot nest an array def inside of itself', ); done_testing; FFI-C-0.15/t/ffi_c_def.t000644 000000 000000 00000000254 14334741503 014522 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::C::Def; is( dies { FFI::C::Def->new }, match qr/FFI::C::Def is an abstract class/, 'cannot create instance' ); done_testing; FFI-C-0.15/t/ffi_c_enumdef.t000644 000000 000000 00000003627 14334741503 015416 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::C; my $ffi = FFI::C->ffi; { package MyEnum1; FFI::C->enum(['foo','bar',[baz=>12]]); package MyStruct1; FFI::C->struct([foo => 'my_enum1_t']); } is(MyEnum1::FOO(), 0); is(MyEnum1::BAR(), 1); is(MyEnum1::BAZ(), 12); { my $m = MyStruct1->new; is($m->foo, 'foo'); is($ffi->cast('my_struct1_t' => 'enum*', $m), \0); is($m->foo('bar'), 'bar'); is($m->foo, 'bar'); is($ffi->cast('my_struct1_t' => 'enum*', $m), \1); is($m->foo(12), 'baz'); is($m->foo, 'baz'); is($ffi->cast('my_struct1_t' => 'enum*', $m), \12); } { package MyEnum3; FFI::C->enum(['foo','bar',[baz=>12]], { rev => 'int' }); package MyStruct3; FFI::C->struct([foo => 'my_enum3_t']); } is(MyEnum3::FOO(), 0); is(MyEnum3::BAR(), 1); is(MyEnum3::BAZ(), 12); { my $m = MyStruct3->new; is($m->foo, 0); is($ffi->cast('my_struct3_t' => 'enum*', $m), \0); is($m->foo('bar'), 1); is($m->foo, 1); is($ffi->cast('my_struct3_t' => 'enum*', $m), \1); is($m->foo(12), 12); is($m->foo, 12); is($ffi->cast('my_struct3_t' => 'enum*', $m), \12); } require FFI::C::StructDef; { my $m = FFI::C::StructDef->new( $ffi, name => 'my_struct2_t', members => [ 'foo' => 'my_enum1_t' ], )->create; is($m->foo, 'foo'); is($ffi->cast('my_struct2_t' => 'enum*', $m), \0); is($m->foo('bar'), 'bar'); is($m->foo, 'bar'); is($ffi->cast('my_struct2_t' => 'enum*', $m), \1); is($m->foo(12), 'baz'); is($m->foo, 'baz'); is($ffi->cast('my_struct2_t' => 'enum*', $m), \12); } { my $m = FFI::C::StructDef->new( $ffi, name => 'my_struct4_t', members => [ 'foo' => 'my_enum3_t' ], )->create; is($m->foo, 0); is($ffi->cast('my_struct4_t' => 'enum*', $m), \0); is($m->foo('bar'), 1); is($m->foo, 1); is($ffi->cast('my_struct4_t' => 'enum*', $m), \1); is($m->foo(12), 12); is($m->foo, 12); is($ffi->cast('my_struct4_t' => 'enum*', $m), \12); } done_testing; FFI-C-0.15/t/ffi_c_ffi.t000644 000000 000000 00000000345 14334741503 014531 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::C::FFI qw( malloc memset memcpy_addr ); imported_ok 'malloc'; imported_ok 'memset'; imported_ok 'memcpy_addr'; my $ptr = malloc(100); is $ptr, match qr/^[0-9]+$/, 'malloc'; done_testing; FFI-C-0.15/t/ffi_c_file.t000644 000000 000000 00000005504 14334741503 014706 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::C::File; use Path::Tiny qw( path tempfile ); subtest 'basic read' => sub { my $expected = path(__FILE__)->slurp_raw; my $bytes = length($expected); subtest 'explicit close' => sub { my $file = FFI::C::File->fopen(__FILE__, "r"); isa_ok $file, 'FFI::C::File'; is( $file->feof, F() ); my $content = "\0" x $bytes; is( $file->fread(\$content, $bytes), $bytes ); is( $content, $expected ); is( $file->feof, F() ); is( $file->fread(\$content, $bytes), 0 ); is( $file->feof, T() ); $file->fclose; }; subtest 'implicit close' => sub { my $file = FFI::C::File->fopen(__FILE__, "r"); isa_ok $file, 'FFI::C::File'; my $content = "\0" x $bytes; is( $file->fread(\$content, $bytes), $bytes ); is( $content, $expected ); }; subtest 'take / new' => sub { my $file = FFI::C::File->fopen(__FILE__, "r"); isa_ok $file, 'FFI::C::File'; my $ptr = $file->take; undef $file; $file = FFI::C::File->new($ptr); isa_ok $file, 'FFI::C::File'; my $content = "\0" x $bytes; is( $file->fread(\$content, $bytes), $bytes ); is( $content, $expected ); }; }; subtest 'tmpfile' => sub { skip_all 'tricky permissions on windows' if $^O eq 'MSWin32'; my $file = FFI::C::File->tmpfile; isa_ok $file, 'FFI::C::File'; }; subtest 'basic write' => sub { my $path = tempfile; my $content = "hello world\0there"; my $bytes = length $content; my $file = FFI::C::File->fopen("$path", "w"); is($file->fwrite(\$content, $bytes), $bytes); $file->fflush; is($path->slurp_raw, $content); }; subtest 'freopen' => sub { my $path1 = tempfile; my $path2 = tempfile; my $file = FFI::C::File->fopen("$path1", "a"); $file->fwrite(\"foo", 3); $file->freopen("$path2", "a"); $file->fwrite(\"bar", 3); if($^O =~ /^(darwin|linux)$/) { note 'testing NULL to freopen'; $file->freopen(undef, "a"); $file->fwrite(\"baz", 3); $file->fflush; is($path1->slurp_raw, "foo"); is($path2->slurp_raw, "barbaz"); } else { note 'not testing NULL to freopen'; $file->fflush; is($path1->slurp_raw, "foo"); is($path2->slurp_raw, "bar"); } is dies { $file->freopen("bogus.txt", "r") }, match qr/^Error opening bogus\.txt with mode r:/; }; subtest 'fseek / ftell / rewind' => sub { my $file = FFI::C::File->fopen(__FILE__, "r"); is $file->ftell, 0; $file->fseek(0, 'end'); cmp_ok $file->ftell, '>', 0; note "ftell = ", $file->ftell; $file->fseek(0, 'set'); is $file->ftell, 0; $file->fseek(10, 'cur'); is $file->ftell, 10; $file->fseek(-5, 'cur'); is $file->ftell, 5; $file->rewind; is $file->ftell, 0; }; subtest 'exceptions' => sub { is dies { FFI::C::File->fopen("bogus.txt", "r") }, match qr/^Error opening bogus\.txt with mode r:/; }; done_testing; FFI-C-0.15/t/ffi_c_posixfile.t000644 000000 000000 00000001256 14334741503 015771 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::C::PosixFile; skip_all 'Test requires POSIX extensions to libc File pointers' unless FFI::C::PosixFile->can('fileno'); subtest 'fileno' => sub { my $file = FFI::C::PosixFile->fopen(__FILE__, "r"); isa_ok $file, 'FFI::C::File'; isa_ok $file, 'FFI::C::PosixFile'; is $file->fileno, match qr/^[0-9]+$/; }; subtest 'fdopen' => sub { my $file = FFI::C::PosixFile->fdopen(0, 'r'); isa_ok $file, 'FFI::C::File'; isa_ok $file, 'FFI::C::PosixFile'; is $file->fileno, 0; $file = FFI::C::PosixFile->fdopen(1, 'w'); isa_ok $file, 'FFI::C::File'; isa_ok $file, 'FFI::C::PosixFile'; is $file->fileno, 1; }; done_testing; FFI-C-0.15/t/ffi_c_struct.t000644 000000 000000 00000002071 14334741503 015307 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::Platypus 1.24; use FFI::C::StructDef; use FFI::C::Struct; { package Foo; use FFI::Platypus::Record; record_layout_1( sint64 => 'bar', uint8 => 'baz', ); } subtest 'basic' => sub { my $def = FFI::C::StructDef->new( name => "foo_t", members => [ bar => 'sint64', baz => 'uint8', ], ); is( do { my $o = $def->create; $o->bar(-42); $o->baz(200); $def->ffi->cast('foo_t' => 'record(Foo)*', $o); }, object { call [ isa => 'Foo' ] => T(); call bar => -42; call baz => 200; }, 'object argument', ); is( do { our $r = Foo->new( bar => -47, baz => 199 ); $def->ffi->cast('record(Foo)*', 'foo_t', $r); }, object { call [ isa => 'FFI::C::Struct' ] => T(); call bar => -47; call baz => 199; field owner => 1; field def => object { call [ isa => 'FFI::C::StructDef' ] => T(); call name => 'foo_t'; }; etc; }, 'object return', ); }; done_testing; FFI-C-0.15/t/ffi_c_structdef.t000644 000000 000000 00000027207 14334741503 015776 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::Platypus 1.24; use FFI::Platypus::Memory qw( malloc ); use FFI::Platypus::Record; use FFI::C::StructDef; { my $count = 1; sub record { my $struct = shift; my $perl = qq{ package Rec$count; use FFI::Platypus::Record; record_layout_1(\@_); }; eval $perl; ## no critic (BuiltinFunctions::ProhibitStringyEval) die $@ if $@; my $rec = FFI::Platypus->new( api => 1 )->cast( 'opaque' => "record(Rec$count)*", $struct->{ptr} ); $count++; $rec; } } is( FFI::C::StructDef->new, object { call [ isa => 'FFI::C::StructDef' ] => T(); call name => U(); call ffi => object { call [ isa => 'FFI::Platypus' ] => T(); }; call size => 0; call align => match qr/^[0-9]+$/; call create => object { call [ isa => 'FFI::C::Struct' ] => T(); call [ isa => 'FFI::C::Struct' ] => T(); call sub { my $self = shift; dies { $self->foo } } => match qr/No such member/; }; call trim_string => F(); }, 'unnamed, empty struct', ); is( FFI::C::StructDef->new( name => 'foo_t' ), object { call [ isa => 'FFI::C::StructDef' ] => T(); call name => 'foo_t'; call ffi => object { call [ isa => 'FFI::Platypus' ] => T(); }; call size => 0; call align => match qr/^[0-9]+$/; call create => object { call [ isa => 'FFI::C::Struct' ] => T(); call [ isa => 'FFI::C::Struct' ] => T(); call sub { my $self = shift; dies { $self->foo } } => match qr/No such member/; }; call trim_string => F(); }, 'named, empty struct', ); is( FFI::C::StructDef->new( FFI::Platypus->new( api => 1 ), name => 'foo_t' ), object { call [ isa => 'FFI::C::StructDef' ] => T(); call name => 'foo_t'; call ffi => object { call [ isa => 'FFI::Platypus' ] => T(); }; call size => 0; call align => match qr/^[0-9]+$/; call create => object { call [ isa => 'FFI::C::Struct' ] => T(); call [ isa => 'FFI::C::Struct' ] => T(); call sub { my $self = shift; dies { $self->foo } } => match qr/No such member/; }; call trim_string => F(); }, 'named, empty struct, explicit Platypus', ); my $ptr = malloc(10); is( FFI::C::StructDef->new( members => [ foo => 'uint8', bar => 'uint32', baz => 'sint64', roger => 'opaque', ]), object { call [ isa => 'FFI::C::StructDef' ] => T(); call create => object { call [ isa => 'FFI::C::Struct' ] => T(); call foo => 0; call bar => 0; call baz => 0; call roger => U(); call [ foo => 22 ] => 22; call [ bar => 1900 ] => 1900; call [ baz => -500 ] => -500; call [ roger => $ptr ] => $ptr; call foo => 22; call bar => 1900; call baz => -500; call roger => $ptr; call sub { record(shift, qw( uint8 foo uint32 bar sint64 baz opaque roger ) ) } => object { call foo => 22; call bar => 1900; call baz => -500; call roger => $ptr; }; call [ roger => undef ] => U(); call roger => U(); }; call trim_string => F(); }, 'with members', ); is( FFI::C::StructDef->new( members => [ foo => 'uint8', bar => FFI::C::StructDef->new( members => [ baz => 'sint32', ]), ]), object { call [ isa => 'FFI::C::StructDef' ] => T(); call create => object { call foo => 0; call bar => object { call baz => 0; }; call [ foo => 200 ] => 200; call bar => object { call [baz => -9999 ] => -9999; }; call foo => 200; call bar => object { call baz => -9999; }; }; call trim_string => F(); }, 'nested' ); is( FFI::C::StructDef->new( members => [ foo => 'uint8', bar => FFI::C::StructDef->new( members => [ baz => 'sint32', ]), ]), object { call [ isa => 'FFI::C::StructDef' ] => T(); call [ create => { foo => 200, bar => { baz => -9999 } } ] => object { call foo => 200; call bar => object { call baz => -9999; } }; call trim_string => F(); }, 'nested' ); is( FFI::C::StructDef->new( members => [ foo => 'string(10)', ]), object { call [ isa => 'FFI::C::StructDef' ] => T(); call create => object { call foo => "\0\0\0\0\0\0\0\0\0\0"; call [ foo => "hello" ] => "hello\0\0\0\0\0"; call foo => "hello\0\0\0\0\0"; }; call trim_string => F(); }, 'fixed string', ); is( FFI::C::StructDef->new( members => [ foo => 'string(10)', ], trim_string => 1, ), object { call [ isa => 'FFI::C::StructDef' ] => T(); call create => object { call foo => ""; call [ foo => "hello" ] => "hello"; call foo => "hello"; }; call trim_string => T(); }, 'fixed string, trim', ); is( do { FFI::C::StructDef->new( class => 'String10', members => [ foo => 'string(10)' ], trim_string => 1, ); String10->new, }, object { call [ isa => 'String10' ] => T(); call foo => ""; call [ foo => "hello" ] => "hello"; call foo => "hello"; }, 'fixed string, generated, trim', ); { my $ffi = FFI::Platypus->new( api => 1 ); FFI::C::StructDef->new( $ffi, name => 'value_color_t', class => 'Color::Value', members => [ red => 'uint8', green => 'uint8', blue => 'uint8', ] ); is( Color::Value->new({ red => 1, green => 2, blue => 3 }), object { call [ isa => 'Color::Value' ] => T(); call red => 1; call green => 2; call blue => 3; }, 'initalizers', ); FFI::C::StructDef->new( $ffi, name => 'named_color_t', class => 'Color::Named', members => [ name => 'string(5)', value => 'value_color_t', ], ); is( Color::Named->new, object { call [ isa => 'Color::Named' ] => T(); call name => "\0\0\0\0\0"; call [ name => "red" ] => "red\0\0"; call name => "red\0\0"; call value => object { call [ isa => 'Color::Value' ] => T(); call red => 0; call [ red => 255] => 255; call red => 255; call green => 0; call blue => 0; }; }, 'named color', ); { my $def = FFI::C::StructDef->new( $ffi, name => 'byte_array1_t', members => [ e => 'uint8[3]', ], ); { my $ar = $def->create; is($ar->e(0), 0, 'a.get.0 = 0'); is($ar->e(1), 0, 'a.get.1 = 0'); is($ar->e(2), 0, 'a.get.2 = 0'); is($ar->e(0,1), 1, 'a.set.0,1 = 1'); is($ar->e(1,2), 2, 'a.set.0,2 = 2'); is($ar->e(2,3), 3, 'a.set.0,3 = 3'); is($ar->e(0), 1, 'a.get.0 = 1'); is($ar->e(1), 2, 'a.get.1 = 2'); is($ar->e(2), 3, 'a.get.2 = 3'); is($ar->e, [1,2,3], 'a = [1,2,3]'); is( dies { $ar->e(-1) }, match qr/Negative index on array member/, 'disallow negative index', ); is( dies { $ar->e(3) }, match qr/OOB index on array member/, 'disallow oob index', ); my $c = $ffi->cast('byte_array1_t' => 'value_color_t', $ar); is( $c, object { call [ isa => 'Color::Value' ] => T(); call red => 1; call green => 2; call blue => 3; }, 'cast from bytes to color worked' ); } { my $ar = $def->create; is($ar->e->[0], 0, 'a.get.0 = 0'); is($ar->e->[1], 0, 'a.get.1 = 0'); is($ar->e->[2], 0, 'a.get.2 = 0'); is($ar->e->[0] = 1, 1, 'a.set.0,1 = 1'); is($ar->e->[1] = 2, 2, 'a.set.0,2 = 2'); is($ar->e->[2] = 3, 3, 'a.set.0,3 = 3'); is($ar->e->[0], 1, 'a.get.0 = 1'); is($ar->e->[1], 2, 'a.get.1 = 2'); is($ar->e->[2], 3, 'a.get.2 = 3'); is(scalar @{ $ar->e }, 3, 'a.length = 3'); is($ar->e, [1,2,3], 'a = [1,2,3]'); is( dies { $ar->e(-1) }, match qr/Negative index on array member/, 'disallow negative index', ); is( dies { $ar->e(3) }, match qr/OOB index on array member/, 'disallow oob index', ); my $c = $ffi->cast('byte_array1_t' => 'value_color_t', $ar); is( $c, object { call [ isa => 'Color::Value' ] => T(); call red => 1; call green => 2; call blue => 3; }, 'cast from bytes to color worked' ); is($ar->e([4,5,6]), [4,5,6], 'a = [4,5,6]'); is($ar->e, [4,5,6], 'a == [4,5,6]'); } } { my $def = FFI::C::StructDef->new( $ffi, name => 'byte_array2_t', class => 'Byte::Array2', members => [ e => 'uint8[3]', ], ); { my $ar = Byte::Array2->new; is($ar->e(0), 0, 'a.get.0 = 0'); is($ar->e(1), 0, 'a.get.1 = 0'); is($ar->e(2), 0, 'a.get.2 = 0'); is($ar->e(0,1), 1, 'a.set.0,1 = 1'); is($ar->e(1,2), 2, 'a.set.0,2 = 2'); is($ar->e(2,3), 3, 'a.set.0,3 = 3'); is($ar->e(0), 1, 'a.get.0 = 1'); is($ar->e(1), 2, 'a.get.1 = 2'); is($ar->e(2), 3, 'a.get.2 = 3'); is($ar->e, [1,2,3], 'a = [1,2,3]'); is( dies { $ar->e(-1) }, match qr/Negative index on array member/, 'disallow negative index', ); is( dies { $ar->e(3) }, match qr/OOB index on array member/, 'disallow oob index', ); my $c = $ffi->cast('byte_array2_t' => 'value_color_t', $ar); is( $c, object { call [ isa => 'Color::Value' ] => T(); call red => 1; call green => 2; call blue => 3; }, 'cast from bytes to color worked' ); } { my $ar = Byte::Array2->new; is($ar->e->[0], 0, 'a.get.0 = 0'); is($ar->e->[1], 0, 'a.get.1 = 0'); is($ar->e->[2], 0, 'a.get.2 = 0'); is($ar->e->[0] = 1, 1, 'a.set.0,1 = 1'); is($ar->e->[1] = 2, 2, 'a.set.0,2 = 2'); is($ar->e->[2] = 3, 3, 'a.set.0,3 = 3'); is($ar->e->[0], 1, 'a.get.0 = 1'); is($ar->e->[1], 2, 'a.get.1 = 2'); is($ar->e->[2], 3, 'a.get.2 = 3'); is(scalar @{ $ar->e }, 3, 'a.length = 3'); is($ar->e, [1,2,3], 'a = [1,2,3]'); is( dies { $ar->e(-1) }, match qr/Negative index on array member/, 'disallow negative index', ); is( dies { $ar->e(3) }, match qr/OOB index on array member/, 'disallow oob index', ); my $c = $ffi->cast('byte_array2_t' => 'value_color_t', $ar); is( $c, object { call [ isa => 'Color::Value' ] => T(); call red => 1; call green => 2; call blue => 3; }, 'cast from bytes to color worked' ); is($ar->e([4,5,6]), [4,5,6], 'a = [4,5,6]'); is($ar->e, [4,5,6], 'a == [4,5,6]'); } } } is( dies { my $ffi = FFI::Platypus->new( api => 1 ); FFI::C::StructDef->new( name => 'self_nest_t', members => [ self => 'self_nest_t', ], ); }, match qr/Canot nest a struct or union def inside of itself/, 'Canot nest a struct or union def inside of itself', ); is( do { my $ffi = FFI::Platypus->new( api => 1 ); FFI::C::StructDef->new( $ffi, name => 'foo_t', members => [ x => 'uint8' ], nullable => 1, ); $ffi->cast('foo_t' => 'opaque' => undef); }, U(), 'nullable okay' ); done_testing; FFI-C-0.15/t/ffi_c_union.t000644 000000 000000 00000001721 14334741503 015114 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::Platypus 1.24; use FFI::C::UnionDef; use FFI::C::Union; { package Foo; use FFI::Platypus::Record; record_layout_1( sint64 => 'bar', ); } subtest 'basic' => sub { my $def = FFI::C::UnionDef->new( name => "foo_t", members => [ bar => 'sint64', baz => 'uint8', ], ); is( do { my $o = $def->create; $o->bar(-42); $def->ffi->cast('foo_t' => 'record(Foo)*', $o); }, object { call [ isa => 'Foo' ] => T(); call bar => -42; }, 'object argument', ); is( do { our $r = Foo->new( bar => -47 ); $def->ffi->cast('record(Foo)*', 'foo_t', $r); }, object { call [ isa => 'FFI::C::Union' ] => T(); call bar => -47; field owner => 1; field def => object { call [ isa => 'FFI::C::UnionDef' ] => T(); call name => 'foo_t'; }; etc; }, 'object return', ); }; done_testing; FFI-C-0.15/t/ffi_c_uniondef.t000644 000000 000000 00000004474 14334741503 015603 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use FFI::C::UnionDef; is( FFI::C::UnionDef->new, object { call [ isa => 'FFI::C::UnionDef' ] => T(); call name => U(); call ffi => object { call [ isa => 'FFI::Platypus' ] => T(); }; call size => 0; call align => match qr/^[0-9]+$/; call create => object { call [ isa => 'FFI::C::Union' ] => T(); call sub { my $self = shift; dies { $self->foo } } => match qr/No such member/; }; }, 'unnamed, empty union', ); is( FFI::C::UnionDef->new( name => 'foo_t' ), object { call [ isa => 'FFI::C::UnionDef' ] => T(); call name => 'foo_t'; call ffi => object { call [ isa => 'FFI::Platypus' ] => T(); }; call size => 0; call align => match qr/^[0-9]+$/; call create => object { call [ isa => 'FFI::C::Union' ] => T(); call sub { my $self = shift; dies { $self->foo } } => match qr/No such member/; }; }, 'named, empty union', ); is( FFI::C::UnionDef->new( FFI::Platypus->new( api => 1 ), name => 'foo_t' ), object { call [ isa => 'FFI::C::UnionDef' ] => T(); call name => 'foo_t'; call ffi => object { call [ isa => 'FFI::Platypus' ] => T(); }; call size => 0; call align => match qr/^[0-9]+$/; call create => object { call [ isa => 'FFI::C::Union' ] => T(); call sub { my $self = shift; dies { $self->foo } } => match qr/No such member/; }; }, 'named, empty union, explicit Platypus', ); is( FFI::C::UnionDef->new( members => [ u8 => 'uint8', u16 => 'uint16', u32 => 'uint32', u64 => 'uint64', ]), object { call [ isa => 'FFI::C::UnionDef' ] => T(); # I don't think there is any arch out there where 8-64 ints # are more than 8 byte aligned? call size => 8; call create => object { call [ isa => 'FFI::C::Union' ] => T(); call u8 => 0; call u16 => 0; call u32 => 0; call u64 => 0; call [ u8 => 22 ] => 22; call u8 => 22; call [ u16 => 1024 ] => 1024; call u16 => 1024; call [ u32 => 999999 ] => 999999; call u32 => 999999; call [ u64 => 55 ] => 55; call u64 => 55; }; }, 'union with members', ); done_testing; FFI-C-0.15/t/ffi_c_util.t000644 000000 000000 00000013376 14334741503 014752 0ustar00rootroot000000 000000 use Test2::V0 0.000081 -no_srand => 1; use FFI::C::Util qw( owned take perl_to_c c_to_perl set_array_count addressof ); use FFI::Platypus::Memory qw( free ); use FFI::C::StructDef; use FFI::C::UnionDef; use FFI::C::ArrayDef; subtest 'owned / take' => sub { imported_ok 'take'; imported_ok 'owned'; my $def = FFI::C::StructDef->new( name => 'foo_t', members => [], ); my $inst = $def->create; is $inst, object { call [ isa => 'FFI::C::Struct' ] => T(); field ptr => match qr/^[0-9]+$/; etc; }, 'object before take', ; is owned($inst), T(), 'instance is owned'; my $ptr1 = addressof $inst; is $ptr1, match qr/^[0-9]+$/, 'addressof is a pointer'; is $inst, object { call [ isa => 'FFI::C::Struct' ] => T(); field ptr => match qr/^[0-9]+$/; etc; }, 'object after addressof', ; my $ptr2 = take $inst; is $ptr2, match qr/^[0-9]+$/, 'gave us a pointer'; is $ptr2, $ptr1, 'pointer from take and addressof are the same'; is $inst, object { call [ isa => 'FFI::C::Struct' ] => T(); field ptr => U(); etc; }, 'object after take', ; is owned($inst), F(), 'instance is unowned'; }; subtest 'perl_to_c / c_to_perl' => sub { imported_ok 'perl_to_c'; imported_ok 'c_to_perl'; subtest 'generated classes' => sub { my $def = FFI::C::StructDef->new( class => 'Class1', members => [ x => 'uint8', y => FFI::C::ArrayDef->new( class => 'Class2', members => [ FFI::C::StructDef->new( class => 'Class3', members => [ foo => 'sint16', bar => 'uint32', baz => 'double', ], ), 2, ], ), z => 'sint16[3]', a => FFI::C::UnionDef->new( class => 'Class4', members => [ u8 => 'uint8', u16 => 'uint16', ], ), ], ); my $inst = $def->create; perl_to_c($inst, { x => 1, y => [ { foo => 2, bar => 3, baz => 5.5 }, { foo => 6, bar => 7, baz => 8.8 }, ], z => [ 1, 2, 3 ], a => { u16 => 900 }, }); is( $inst, object { call [ isa => 'Class1' ] => T(); call x => 1; call y => object { call [ isa => 'Class2' ] => T(); call [ get => 0 ] => object { call [ isa => 'Class3' ] => T(); }; call [ get => 1 ] => object { call [ isa => 'Class3' ] => T(); }; }; call a => object { call [ isa => 'Class4' ] => T(); call u16 => 900; }; }, 'value converted to c', ); { no warnings 'once'; *Class1::blow_up = sub { die } } is( c_to_perl($inst), { x => 1, y => [ { foo => 2, bar => 3, baz => float(5.5, tolerance => 0.01) }, { foo => 6, bar => 7, baz => float(8.8, tolerance => 0.01) }, ], z => [ 1, 2, 3 ], a => { u8 => match qr/^[0-9]+$/, u16 => 900 }, }, 'c_to_perl' ); }; subtest 'ungenerated types' => sub { my $def = FFI::C::StructDef->new( name => 'Class1', members => [ x => 'uint8', y => FFI::C::ArrayDef->new( name => 'Class2', members => [ FFI::C::StructDef->new( name => 'Class3', members => [ foo => 'sint16', bar => 'uint32', baz => 'double', ], ), 2, ], ), z => 'sint16[3]', a => FFI::C::UnionDef->new( name => 'Class4', members => [ u8 => 'uint8', u16 => 'uint16', ], ), ], ); my $inst = $def->create; perl_to_c($inst, { x => 1, y => [ { foo => 2, bar => 3, baz => 5.5 }, { foo => 6, bar => 7, baz => 8.8 }, ], z => [ 1, 2, 3 ], a => { u16 => 900 }, }); is( c_to_perl($inst), { x => 1, y => [ { foo => 2, bar => 3, baz => float(5.5, tolerance => 0.01) }, { foo => 6, bar => 7, baz => float(8.8, tolerance => 0.01) }, ], z => [ 1, 2, 3 ], a => { u8 => match qr/^[0-9]+$/, u16 => 900 }, }, 'c_to_perl' ); }; }; subtest 'var-to-fixed' => sub { imported_ok 'set_array_count'; my $ffi = FFI::Platypus->new( api => 1 ); my $def = FFI::C::ArrayDef->new( $ffi, name => 'array_t', members => [ FFI::C::StructDef->new( members => [ x => 'uint32' ], ), ], ); my $fixed = $def->create([ map { { x => $_ } } 0..9 ]); is( $fixed, object { call [ isa => 'FFI::C::Array' ] => T(); call count => 10; }, 'fixed is okay' ); is( dies { set_array_count $fixed, 5 }, match qr/This array already has a size/, 'Trying to set count on already existing array fails', ); # casting looses the count. my $huh = $ffi->cast('array_t','array_t', $fixed); is( $huh, object { call [ isa => 'FFI::C::Array' ] => T(); call count => U(); call [ get => $_ ] => object { call [ isa => 'FFI::C::Struct' ] => T(); call x => $_; } for 0..9; }, 'huh is okay', ); set_array_count $huh, 5; is( $huh, object { call [ isa => 'FFI::C::Array' ] => T(); call count => 5; call [ get => $_ ] => object { call [ isa => 'FFI::C::Struct' ] => T(); call x => $_; } for 0..4; }, 'huh is okay', ); is( dies { $huh->get(5) }, match qr/OOB array index/, 'oob error on set count' ); }; done_testing; FFI-C-0.15/xt/000755 000000 000000 00000000000 14334741503 012640 5ustar00rootroot000000 000000 FFI-C-0.15/xt/author/000755 000000 000000 00000000000 14334741503 014142 5ustar00rootroot000000 000000 FFI-C-0.15/xt/author/critic.t000644 000000 000000 00000000517 14334741503 015607 0ustar00rootroot000000 000000 use Test2::Require::Module 'Test2::Tools::PerlCritic'; use Test2::Require::Module 'Perl::Critic'; use Test2::Require::Module 'Perl::Critic::Freenode'; use Test2::V0; use Perl::Critic; use Test2::Tools::PerlCritic; my $critic = Perl::Critic->new( -profile => 'perlcriticrc', ); perl_critic_ok ['lib','t'], $critic; done_testing; FFI-C-0.15/xt/author/eol.t000644 000000 000000 00000000510 14334741503 015102 0ustar00rootroot000000 000000 use strict; use warnings; use Test::More; BEGIN { plan skip_all => 'test requires Test::EOL' unless eval q{ use Test::EOL; 1 }; }; use Test::EOL; use FindBin; use File::Spec; chdir(File::Spec->catdir($FindBin::Bin, File::Spec->updir, File::Spec->updir)); all_perl_files_ok(grep { -e $_ } qw( bin lib t Makefile.PL )); FFI-C-0.15/xt/author/examples.t000644 000000 000000 00000002724 14334741503 016152 0ustar00rootroot000000 000000 use Test2::V0 -no_srand => 1; use Test2::Require::Module 'Test::Script'; use Test::Script; use Path::Tiny qw( path ); use File::Glob qw( bsd_glob ); use Capture::Tiny qw( capture_merged ); skip_all 'not tested with ciperl:static' if defined $ENV{CIPSTATIC} && $ENV{CIPSTATIC} eq 'true'; $ENV{PERL5LIB} = path('corpus/examples/lib')->absolute; my @dirs = qw( examples examples/synopsis ); is( do { my $lib; my($out) = capture_merged { require FFI::Build; my $build = FFI::Build->new( 'main', dir => 'corpus/examples/arch/auto/main', verbose => 2, ); $build->source(map { bsd_glob "$_/*.c" } @dirs); $lib = eval { $build->build }; }; note $out if $out ne ''; my $txt = path('corpus/examples/arch/auto/main/main.txt'); $txt->parent->mkpath; $txt->spew("FFI::Build\@" . path($lib->path)->relative('corpus/examples/lib')->stringify); $lib; }, object { call [ isa => 'FFI::Build::File::Library' ] => T(); }, 'build c example files', ); foreach my $example (map { bsd_glob "$_/*.pl" } @dirs) { my $basename = path($example)->basename; subtest $basename => sub { skip_all 'test requires Perl 5.14 or better' unless $basename ne 'c.pl' || $] >= 5.014; my $out = ''; my $err = ''; script_compiles $example; script_runs $example, { stdout => \$out, stderr => \$err }; note "[out]\n$out" if $out ne ''; note "[err]\n$err" if $err ne ''; }; } done_testing; FFI-C-0.15/xt/author/no_tabs.t000644 000000 000000 00000000522 14334741503 015753 0ustar00rootroot000000 000000 use strict; use warnings; use Test::More; BEGIN { plan skip_all => 'test requires Test::NoTabs' unless eval q{ use Test::NoTabs; 1 }; }; use Test::NoTabs; use FindBin; use File::Spec; chdir(File::Spec->catdir($FindBin::Bin, File::Spec->updir, File::Spec->updir)); all_perl_files_ok( grep { -e $_ } qw( bin lib t Makefile.PL )); FFI-C-0.15/xt/author/pod.t000644 000000 000000 00000000472 14334741503 015114 0ustar00rootroot000000 000000 use strict; use warnings; use Test::More; BEGIN { plan skip_all => 'test requires Test::Pod' unless eval q{ use Test::Pod; 1 }; }; use Test::Pod; use FindBin; use File::Spec; chdir(File::Spec->catdir($FindBin::Bin, File::Spec->updir, File::Spec->updir)); all_pod_files_ok( grep { -e $_ } qw( bin lib )); FFI-C-0.15/xt/author/pod_coverage.t000644 000000 000000 00000004001 14334741503 016757 0ustar00rootroot000000 000000 use strict; use warnings; use Test::More; BEGIN { plan skip_all => 'test requires 5.010 or better' unless $] >= 5.010; plan skip_all => 'test requires Test::Pod::Coverage' unless eval q{ use Test::Pod::Coverage; 1 }; plan skip_all => 'test requires YAML' unless eval q{ use YAML; 1; }; plan skip_all => 'test does not always work in cip check' if defined $ENV{CIPSTATIC} && $ENV{CIPSTATIC} eq 'true'; }; use Test::Pod::Coverage; use YAML qw( LoadFile ); use FindBin; use File::Spec; my $config_filename = File::Spec->catfile( $FindBin::Bin, File::Spec->updir, File::Spec->updir, 'author.yml' ); my $config; $config = LoadFile($config_filename) if -r $config_filename; plan skip_all => 'disabled' if $config->{pod_coverage}->{skip}; chdir(File::Spec->catdir($FindBin::Bin, File::Spec->updir, File::Spec->updir)); my @private_classes; my %private_methods; push $config->{pod_coverage}->{private}->@*, 'Alien::.*::Install::Files#Inline'; foreach my $private ($config->{pod_coverage}->{private}->@*) { my($class,$method) = split /#/, $private; if(defined $class && $class ne '') { my $regex = eval 'qr{^' . $class . '$}'; if(defined $method && $method ne '') { push @private_classes, { regex => $regex, method => $method }; } else { push @private_classes, { regex => $regex, all => 1 }; } } elsif(defined $method && $method ne '') { $private_methods{$_} = 1 for split /,/, $method; } } my @classes = all_modules; plan tests => scalar @classes; foreach my $class (@classes) { SKIP: { my($is_private_class) = map { 1 } grep { $class =~ $_->{regex} && $_->{all} } @private_classes; skip "private class: $class", 1 if $is_private_class; my %methods = map {; $_ => 1 } map { split /,/, $_->{method} } grep { $class =~ $_->{regex} } @private_classes; $methods{$_} = 1 for keys %private_methods; my $also_private = eval 'qr{^' . join('|', keys %methods ) . '$}'; pod_coverage_ok $class, { also_private => [$also_private] }; }; } FFI-C-0.15/xt/author/pod_spelling_common.t000644 000000 000000 00000001350 14334741503 020355 0ustar00rootroot000000 000000 use strict; use warnings; use Test::More; BEGIN { plan skip_all => 'test requires Test::Pod::Spelling::CommonMistakes' unless eval q{ use Test::Pod::Spelling::CommonMistakes; 1 }; plan skip_all => 'test requires YAML' unless eval q{ use YAML qw( LoadFile ); 1 }; }; use Test::Pod::Spelling::CommonMistakes; use FindBin; use File::Spec; my $config_filename = File::Spec->catfile( $FindBin::Bin, File::Spec->updir, File::Spec->updir, 'author.yml' ); my $config; $config = LoadFile($config_filename) if -r $config_filename; plan skip_all => 'disabled' if $config->{pod_spelling_common}->{skip}; chdir(File::Spec->catdir($FindBin::Bin, File::Spec->updir, File::Spec->updir)); # TODO: test files in bin too. all_pod_files_ok; FFI-C-0.15/xt/author/pod_spelling_system.t000644 000000 000000 00000002367 14334741503 020422 0ustar00rootroot000000 000000 use strict; use warnings; use Test::More; BEGIN { plan skip_all => 'test requires Test::Spelling' unless eval q{ use Test::Spelling; 1 }; plan skip_all => 'test requires YAML' unless eval q{ use YAML; 1; }; }; use Test::Spelling; use YAML qw( LoadFile ); use FindBin; use File::Spec; my $config_filename = File::Spec->catfile( $FindBin::Bin, File::Spec->updir, File::Spec->updir, 'author.yml' ); my $config; $config = LoadFile($config_filename) if -r $config_filename; plan skip_all => 'disabled' if $config->{pod_spelling_system}->{skip}; chdir(File::Spec->catdir($FindBin::Bin, File::Spec->updir, File::Spec->updir)); add_stopwords($config->{pod_spelling_system}->{stopwords}->@*); add_stopwords(qw( Plicease stdout stderr stdin subref loopback username os Ollis Mojolicious plicease CPAN reinstall TODO filename filenames login callback callbacks standalone VMS hostname hostnames TCP UDP IP API MSWin32 OpenBSD FreeBSD NetBSD unencrypted WebSocket WebSockets timestamp timestamps poney BackPAN portably RedHat AIX BSD XS FFI perlish optimizations subdirectory RESTful SQLite JavaScript dir plugins munge jQuery namespace PDF PDFs usernames DBI pluggable APIs SSL JSON YAML uncommented Solaris OpenVMS URI URL CGI )); all_pod_files_spelling_ok; FFI-C-0.15/xt/author/strict.t000644 000000 000000 00000001031 14334741503 015632 0ustar00rootroot000000 000000 use strict; use warnings; use Test::More; BEGIN { plan skip_all => 'test requires Test::Strict' unless eval q{ use Test::Strict; 1 }; }; use Test::Strict; use FindBin; use File::Spec; chdir(File::Spec->catdir($FindBin::Bin, File::Spec->updir, File::Spec->updir)); unshift @Test::Strict::MODULES_ENABLING_STRICT, 'ozo', 'Test2::Bundle::SIPS', 'Test2::V0', 'Test2::Bundle::Extended'; note "enabling strict = $_" for @Test::Strict::MODULES_ENABLING_STRICT; all_perl_files_ok( grep { -e $_ } qw( bin lib t Makefile.PL )); FFI-C-0.15/xt/author/version.t000644 000000 000000 00000001473 14334741503 016021 0ustar00rootroot000000 000000 use strict; use warnings; use Test::More; use FindBin (); BEGIN { plan skip_all => "test requires Test::Version 2.00" unless eval q{ use Test::Version 2.00 qw( version_all_ok ), { has_version => 1, filename_match => sub { $_[0] !~ m{/(ConfigData|Install/Files)\.pm$} }, }; 1 }; plan skip_all => 'test requires YAML' unless eval q{ use YAML; 1; }; } use YAML qw( LoadFile ); use FindBin; use File::Spec; my $config_filename = File::Spec->catfile( $FindBin::Bin, File::Spec->updir, File::Spec->updir, 'author.yml' ); my $config; $config = LoadFile($config_filename) if -r $config_filename; if($config->{version}->{dir}) { note "using dir " . $config->{version}->{dir} } version_all_ok($config->{version}->{dir} ? ($config->{version}->{dir}) : ()); done_testing; FFI-C-0.15/xt/release/000755 000000 000000 00000000000 14334741503 014260 5ustar00rootroot000000 000000 FFI-C-0.15/xt/release/changes.t000644 000000 000000 00000001113 14334741503 016051 0ustar00rootroot000000 000000 use strict; use warnings; use Test::More; BEGIN { plan skip_all => 'test requires Test::CPAN::Changes' unless eval q{ use Test::CPAN::Changes; 1 }; }; use Test::CPAN::Changes; use FindBin; use File::Spec; chdir(File::Spec->catdir($FindBin::Bin, File::Spec->updir, File::Spec->updir)); do { my $old = \&Test::Builder::carp; my $new = sub { my($self, @messages) = @_; return if $messages[0] =~ /^Date ".*" is not in the recommend format/; $old->($self, @messages); }; no warnings 'redefine'; *Test::Builder::carp = $new; }; changes_file_ok; done_testing; FFI-C-0.15/xt/release/fixme.t000644 000000 000000 00000000616 14334741503 015560 0ustar00rootroot000000 000000 use strict; use warnings; use Test::More; BEGIN { plan skip_all => 'test requires Test::Fixme' unless eval q{ use Test::Fixme 0.14; 1 }; }; use Test::Fixme 0.07; use FindBin; use File::Spec; chdir(File::Spec->catdir($FindBin::Bin, File::Spec->updir, File::Spec->updir)); run_tests( match => qr/FIXME/, where => [ grep { -e $_ } qw( bin lib t Makefile.PL Build.PL )], warn => 1, );