Questions tagged [nativecall]

Raku mechanism for calling code from installed C libraries. The following is a complete example that uses installed C libraries on Windows to open up a message box: use NativeCall; sub MessageBoxA(int32, Str, Str, int32) returns int32 is native('user32') { * } MessageBoxA(0, "We have NativeCall", "ohai", 64);

Raku mechanism for calling code from C libraries. The following examples are from https://docs.perl6.org/language/nativecall.

Function without arguments:

use NativeCall;
sub some_argless_function() is native('something') { * }
some_argless_function();

The first line imports various traits and types. The next line looks like a relatively ordinary Perl 6 sub declaration—with a twist. We use the "native" trait in order to specify that the sub is actually defined in a native library. The platform-specific extension (e.g., '.so' or '.dll'), as well as any customary prefixes (e.g., 'lib') will be added for you.

The first time you call "some_argless_function", the "libsomething" will be loaded and the "some_argless_function" will be located in it. A call will then be made. Subsequent calls will be faster, since the symbol handle is retained.

Function that takes and returns typed arguments:

use NativeCall;
sub add(int32, int32) returns int32 is native("calculator") { * }

Example on Windows:

use NativeCall;

sub MessageBoxA(int32, Str, Str, int32) returns int32 is native('user32') { * }

MessageBoxA(0, "We have NativeCall", "ohai", 64);
61 questions
12
votes
0 answers

Is there any way to list the functions and data structures available from the native interface in Raku?

Mainly with the objective of creating sensible examples, I'd like to know which data structures and functions are available for use in Raku's native interface, as in class a-class is repr('CStruct') { has int $.whatever; } class another-class is…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
12
votes
1 answer

How to handle varargs with NativeCall

I'm writing bindings for Editline; one of its functions, history, does the bulk of the work for this part of the library, but has several possible signatures: :(Pointer[Internal], Pointer[Event], int32 --> int32) :(Pointer[Internal], Pointer[Event],…
Kaiepi
  • 3,230
  • 7
  • 27
11
votes
3 answers

Using NativeCall to call the C fn `erf` gets more precise output than `erf` in C

I have written a Raku script to call erf function in C standard library: use NativeCall; sub erf(num64) returns num64 is native { * }; say [0.5,1,2,3,4,-0.9].map: {erf($_.Num)}; The output of this script (0.5204998778130465 0.8427007929497149…
Suman Khanal
  • 3,079
  • 1
  • 17
  • 29
11
votes
1 answer

Performant math operations on large Perl6 CArrays?

I have some large CArrays returned by a native sub that I need to perform basic element-wise math operations on. The CArrays are usually on the order of 10^6 elements. I have found that calling .list on them to them treat them as normal Perl6 types…
ryn1x
  • 1,052
  • 2
  • 8
  • 19
10
votes
1 answer

Multi-dispatch to candidate with native parameters not working

I am trying to use multi in my Raku code that wraps a function in C. Here I am calling lchoose function in shared libray Rmath. This is my code which works: use NativeCall; constant RMATH = "./Rmath"; # shared library sub lchoose(num64, num64)…
Suman Khanal
  • 3,079
  • 1
  • 17
  • 29
10
votes
1 answer

How to pass a CArray[ of-struct] from Raku to C?

How do I make this work? Update: After searching Github, which includes the Raku spec-test, and here, I haven't found any examples of passing a CArray[of-structs]. Here there is a post by Christoph from 2017 which gives a "work…
rir
  • 101
  • 3
10
votes
2 answers

Handle C typedef on different platform using NativeCall

Is there a convenient way handle C typedef which may have different value on various platform ? For example #if defined(_WIN32) #define foo_t int32_t #elif defined(_WIN64) #define foo_t int64_t #else #define foo_t long #endif void…
araraloren
  • 185
  • 7
10
votes
1 answer

Rakudo Memory/Garbage collecting techniques

I understand that this question verges into implementation specific domains, but at this point, Rakudo/MoarVM specific answers would help me too. I am working on some NativeCall modules, and wondering how to debug memory leaks. Some memory is…
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24
9
votes
4 answers

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

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…
annoying_squid
  • 513
  • 5
  • 10
9
votes
1 answer

Native localtime() segfaults

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 { has int32 $!tm_sec; has int32 $!tm_min; has int32 $!tm_hour; has int32…
Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
9
votes
1 answer

In Perl 6, how can I convert from raw bytes to floating point using the NativeCall interface?

From this conversation in the Perl 6 IRC channel and a question posted by Martin Barth, I'm trying to reproduce this C code using the Perl6 NativeCall interface, which is used with that purpose. This is what I have tried: use NativeCall; my uint32…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
9
votes
1 answer

Creating a new file descriptor from stdout with fcntl fails in files

I have a simple test file that looks like this: use v6.c; use NativeCall; sub fcntl(int32, int32 --> int32) is native { * } sub close(int32 --> int32) is native { * } my $fd := fcntl($*OUT.native-descriptor, 0); say $fd; close($fd); The file…
Kaiepi
  • 3,230
  • 7
  • 27
9
votes
1 answer

Array of structs as an attribute of a Perl 6 NativeCall struct

I'm trying to encapsulate a C struct, one member of which is an array of pointers to structs, and I'm having problems figuring out how to do it. Let's say the C code looks like this: struct foo { unsigned char a; }; struct bar { struct foo…
Fernando Santagata
  • 1,487
  • 1
  • 8
  • 15
8
votes
1 answer

Bit fields in NativeCall

I am trying to create Perl6 bindings for Cgraph, and one of the structs has bit fields set for some of its attributes with values under 8. How should I represent that in my module? I have tried defining a custom type using the is nativesize(x)…
tmtvl
  • 83
  • 1
  • 5
8
votes
1 answer

triple pointer native call on perl 6

I try to wrap sd-bus with perl6, but have problem with a function call triple pointer. from sd-bus.h int sd_bus_list_names(sd_bus *bus, char ***acquired, char ***activatable); /* free the results */ try with native call sub…
1
2 3 4 5