perlffi / ffi-c Goto Github PK
View Code? Open in Web Editor NEWStructured data types for FFI
Structured data types for FFI
https://en.cppreference.com/w/c/io/setbuf
https://en.cppreference.com/w/c/io/setvbuf
I think these might require some constants that aren't easy to know without doing a probe, so FFI::Platypus might have to do that probe for us...
If perl was compiled with -Duselongdouble, then the test suite fails:
# Failed test 'c_to_perl'
# at t/ffi_c_util.t line 122.
# +-------------+---------------------+----+-------+
# | PATH | GOT | OP | CHECK |
# +-------------+---------------------+----+-------+
# | {y}[1]{baz} | 8.80000000000000071 | eq | 8.8 |
# +-------------+---------------------+----+-------+
# Failed test 'generated classes'
# at t/ffi_c_util.t line 135.
# Failed test 'c_to_perl'
# at t/ffi_c_util.t line 180.
# +-------------+---------------------+----+-------+
# | PATH | GOT | OP | CHECK |
# +-------------+---------------------+----+-------+
# | {y}[1]{baz} | 8.80000000000000071 | eq | 8.8 |
# +-------------+---------------------+----+-------+
# Failed test 'ungenerated types'
# at t/ffi_c_util.t line 194.
# Failed test 'perl_to_c / c_to_perl'
# at t/ffi_c_util.t line 196.
t/ffi_c_util.t .......
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/2 subtests
Have to think about the interface though.
I'm trying to find a way to pass an array of ::Struct based objects into a newly created ::Array. Here's an example of my goal based on the synopsis from FFI::C::Array:
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([
Point->new( {x => 1.0, y => 1.0} ), # Obtained somehow as full objects...
Point->new( {x => 2.0, y => 2.0} ) # ...rather than hash references
]);
printf "[[%d %d][%d %d]]\n",
$square->[0]->x, $square->[0]->y,
$square->[1]->x, $square->[1]->y; # [[1 1][2 2]]
In my project, I would have these Point
objects returned from C by yet another attached function. My only solution to FFI::C::Util::perl_to_c(...)
croaking on this is to map { ... }
the objects back to plain hash references and let FFI::C::Def instantiate brand new objects with the exposed data (this would break on complex C objects). Before I start looking for a place to do this quickly and cleanly enough for a PR (maybe), am I missing something obvious?
I cheated and set the count manually in Wasm, but this should be fixed to use a public interface!
The init
util function takes perl data structures and initializes the members of an instance object. It would be helpful, if for nothing else debugging, to be able to do the reverse. That is, given an instance object, return the nested perl data structure that could re-init a new object. It's not ever going to be completely round-trip safe because of anonymous members and the fact that init doesn't write unspecified members, but it is probably good enough.
Format and nesting TBD
This should have been a PR to merge 6d4df16 but accidentally pushed to main
.
This would wrap around a pointer to a null terminated C style string. Some integration with opaque member on a struct/union would be helpful.
This is the only feature that FFI::Platypus::Record
has that FFI::C
is missing.
https://metacpan.org/pod/FFI::C::Struct
This is ambiguous. Synopsis describe FFI::C::StructDef
, methods do same.
What is FFI::C::Struct
?
when testing this PR uperl/Net-Swirl-CurlEasy#56 I noticed passing a file pointer opened with FFI::C::File->tmpfile
into $curl->setopt(stderr => $fp)
caused a segfault. Valgrind suggested it was the file pointer that was the problem.
It's possible this is with curl?
How can I use string
for char *
?
FFI::C::StructDef->new(
$ffi,
nullable => 1,
name => 'SDL_RendererInfo',
class => 'SDL2::RendererInfo',
members => [
name => 'string',
flags => 'uint32',
num_texture_formats => 'uint32',
texture_formats => 'uint32[16]',
max_texture_width => 'int',
max_texture_height => 'int',
],
);
/**
* \brief Information on the capabilities of a render driver or context.
*/
typedef struct SDL_RendererInfo
{
const char *name; /**< The name of the renderer */
Uint32 flags; /**< Supported ::SDL_RendererFlags */
Uint32 num_texture_formats; /**< The number of available texture formats */
Uint32 texture_formats[16]; /**< The available texture formats */
int max_texture_width; /**< The maximum texture width */
int max_texture_height; /**< The maximum texture height */
} SDL_RendererInfo;
This and a couple of tweaks to FFI::Platypus
are the only blockers to sending this to CPAN tbh.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.