nativecall

Passing an array of structures to a Perl 6 NativeCall function

一个人想着一个人 提交于 2020-12-08 07:06:21
问题 I'm trying to use NativeCall to interact with some C functions. I have a simple C struct, and a function that wants an array of them. struct foo { int x; char *s; }; struct foo foo_array[3]; foo_array[0].x = 12; foo_array[0].s = "foo"; foo_array[1].x = 27; foo_array[1].s = "bar"; foo_array[2].x = -1; void somefunc(foo_array); I've tried a bunch of ways, but can't seem to get it quite right. class foo is repr('CStruct') { has int32 $.x; has Str $.s }; sub somefunc(CArray[foo]) is native { * }

Passing an array of structures to a Perl 6 NativeCall function

一个人想着一个人 提交于 2020-12-08 07:05:27
问题 I'm trying to use NativeCall to interact with some C functions. I have a simple C struct, and a function that wants an array of them. struct foo { int x; char *s; }; struct foo foo_array[3]; foo_array[0].x = 12; foo_array[0].s = "foo"; foo_array[1].x = 27; foo_array[1].s = "bar"; foo_array[2].x = -1; void somefunc(foo_array); I've tried a bunch of ways, but can't seem to get it quite right. class foo is repr('CStruct') { has int32 $.x; has Str $.s }; sub somefunc(CArray[foo]) is native { * }

Native localtime() segfaults

安稳与你 提交于 2020-02-23 09:07:35
问题 I seem to be doing something wrong in this attempt to expose the localtime functionality in Perl 6: use NativeCall; my class TimeStruct is repr<CStruct> { has int32 $!tm_sec; has int32 $!tm_min; has int32 $!tm_hour; has int32 $!tm_mday; has int32 $!tm_mon; has int32 $!tm_year; has int32 $!tm_wday; has int32 $!tm_yday; has int32 $!tm_isdst; has Str $!tm_zone; has long $!tm_gmtoff; } sub localtime(uint32 $epoch --> TimeStruct) is native {*} dd localtime(time); # segfault Running under perl6

NativeCall loading a library symbol I don't call

旧巷老猫 提交于 2020-01-13 05:19:09
问题 I have two libraries, I want to call routines in the first library, they then call routines in the second library, but crash because those symbols are undefined. Is it possible to say "load these symbols" from library XX even though I don't want to call them? testlib1.c: #include <stdio.h> void sub2(); void sub1() { printf("Called sub1\n"); sub2(); } testlib2.c: #include <stdio.h> void sub2() { printf("Called sub2\n"); } testit.p6: use NativeCall; sub sub1() is native('testlib1') {} sub sub2(

Incorporate C library function into Perl6 with NativeCall

为君一笑 提交于 2019-12-23 19:29:45
问题 I am attempting to use lgamma from C's math.h in Perl6. How can I incorporate this into Perl6? I have tried use NativeCall; sub lgamma(num64 --> num64) is native(Str) {}; say lgamma(3e0); my $x = 3.14; say lgamma($x); This works for the first number (a Str ) but fails for the second, $x , giving the error: This type cannot unbox to a native number: P6opaque, Rat in block <unit> at pvalue.p6 line 8 I want to do this very simply, like in Perl5: use POSIX 'lgamma'; and then lgamma($x) but I don

Passing pointer to pointer in Perl 6 NativeCall

荒凉一梦 提交于 2019-12-23 10:14:21
问题 I'm trying to use NativeCall to interact with some C functions. For one case, I need to pass in pointers that get updated by the function, so it wants a pointer to a pointer, 'void **'. I tried it like this: class Foo { has Pointer $.first; has Pointer $.last; sub somefunc(Pointer is rw, Pointer is rw, Str) is native { * } method myfunc(Str $arg) { somefunc($!first, $!last, $arg); } } It doesn't work. The pointers don't get updated by the function. Since a C array is basically a pointer to a

How to define fixed-length strings in a Perl6 NativeCall struct?

天大地大妈咪最大 提交于 2019-12-23 08:37:36
问题 I have a third-party C library that defines a struct similar to: struct myStruct { int a; int b; char str1[32]; char str2[32]; }; And a function that takes a pointer to this struct and populates it. I need my Perl6 native call to provide that struct, and then read the results. So far I've got the struct defined in Perl6 as: class myStruct is repr('CStruct') { has int32 $.a; has int32 $.b; has Str $.str1; # Option A: This won't work as Perl won't know what length to allocate has CArray[uint8]

How to declare native array of fixed size in Perl 6?

*爱你&永不变心* 提交于 2019-12-23 07:31:09
问题 I'm am trying to declare the following C struct in Perl 6: struct myStruct { int A[2]; //<---NEED to declare this int B; int C; }; My problem is that I don't know how to declare the int A[2]; part using the built in NativeCall api. So for what I have is: class myStruct is repr('CStruct') { has CArray[int32] $.A; has int32 $.B; has int32 $.C; }; However, I know that the has CArray[int32] $.A; part is wrong as it does not declare a part in my struct that takes up ONLY 2 int32 sizes. 回答1: Update

Passing an inlined CArray in a CStruct to a shared library using NativeCall

删除回忆录丶 提交于 2019-12-10 14:53:17
问题 This is a follow-up question to " How to declare native array of fixed size in Perl 6? ". In that question it was discussed how to incorporate an array of a fixed size into a CStruct . In this answer it was suggested to use HAS to inline a CArray in the CStruct . When I tested this idea, I ran into some strange behavior that could not be resolved in the comments section below the question, so I decided to write it up as a new question. Here is is my C test library code: slib.c : #include

Declaring an array inside a Perl 6 NativeCall CStruct

会有一股神秘感。 提交于 2019-12-08 17:24:57
问题 Is there any way to declare an array of objects inside a CStruct? struct my_struct { int foo; int bar; char somestring[80]; }; class My::Struct is repr('CStruct') { has int32 $.foo; has int32 $.bar; ??? } A CArray[uint8] would be a char * pointer, not actually reserving space inside the struct. Instead of My::Struct.new , I could probably make the memory myself (instead of My::Struct.new() , I use a buf8.allocate(xxx) and keep a handle so the GC doesn't reap it, nativecast it to My::Struct),