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
8
votes
1 answer

How to correctly use CPointer and CStruct in NativeCall interface

I am trying to get this example of NativeCall running: use NativeCall; class p_timespec is repr('CPointer') { has uint32 $.tv_sec; has long $.tv_nanosecs; } sub clock_gettime(uint32 $clock-id, p_timespec $tspec --> uint32) is native(Str) {…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
7
votes
2 answers

How to use CStruct

I'm trying to return struct from shared library written in C. This is simple code, for testing of returning structure and simple int32, libstruct.c, compiled by gcc -shared -Wl,-soname,libstruct.so.1 -o libstruct.so.1 libstruct.c: #include…
fingolfin
  • 591
  • 9
7
votes
1 answer

Does nativecast() create a new container?

I'm writing an interface to a C library. A C function allocates some memory, reads a value, and returns a void * pointer to that buffer, to be subsequently freed. I wish to be sure that when I assign the output of a call to nativecast(Str, $data) to…
Fernando Santagata
  • 1,487
  • 1
  • 8
  • 15
7
votes
1 answer

"Too many positionals passed" in NativeCall sub binding

I have this defined: use NativeCall; unit module kazmath; class mat4 is repr('CStruct') { HAS num32 @.mat[16] is CArray; } sub kmMat4Fill( mat4 $mat, num32 @filler ) returns mat4 is native('kazmath') …
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
7
votes
1 answer

How do I bundle the C source in a Raku distribution that uses NativeCall?

I'm writing Raku bindings to a C library, but rather than require the users of the Raku code to install the C library on their own, I'd like to bundle the C code as part of the distribution (it's not such a widely distributed library). I've read…
jja
  • 2,058
  • 14
  • 27
7
votes
1 answer

Declaring "native" types in Perl 6

In the natives.pm6 module, many native types are declared this way: my native int is repr('P6int') is Int { } You can apparently use it in the same way, my native smallish is repr('P6int') is Int is nativesize(8) { }; say smallish.^mro; # OUTPUT:…
jjmerelo
  • 22,578
  • 8
  • 40
  • 86
7
votes
1 answer

Strange message declaring a Pointer[void] in a NativeCall perl6 module

These snippets of code may seem odd, it's because I started with my original code and cut off pieces until I arrived to the minimal set of instructions that reproduced the error. So bear with the apparent uselessness. There are two perl6 modules,…
Fernando Santagata
  • 1,487
  • 1
  • 8
  • 15
7
votes
2 answers

How to pass user data to a callback function

I'm working on a NativeCall interface; there's a C function which accepts a callback, defined as: typedef void (* ExifContentForeachEntryFunc) (ExifEntry *, void *user_data); void exif_content_foreach_entry (ExifContent *content,…
Fernando Santagata
  • 1,487
  • 1
  • 8
  • 15
7
votes
2 answers

Declaring an array inside a Perl 6 NativeCall CStruct

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]…
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24
7
votes
3 answers

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

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…
Digicrat
  • 581
  • 5
  • 13
7
votes
2 answers

How can I pass a Perl 6 object through a Nativecall callback?

I'm working with the NativeCall interface. The library is going to call my callback function a bunch of times. That works fine. I can just declare my callback with the right signature, pass it in as &callback and the library calls the sub…
Curt Tilmes
  • 3,035
  • 1
  • 12
  • 24
7
votes
1 answer

Native calling interface: how to translate "wchar_t"?

I would like to use the ncurses int addwstr(const wchar_t *wstr); function in Perl6. How could I get a Perl 6 signatures which conveys const wchar_t *wstr of addwstr? use v6; use NativeCall; constant LIB = 'libncursesw.so.5'; sub addwstr( ? )…
sid_com
  • 24,137
  • 26
  • 96
  • 187
7
votes
1 answer

NativeCall Struct which contains Pointer

I have the following Struct : typedef struct _info{ DWORD myInfo; BYTE *pInfo; LPWSTR ExtData; } Info; I represented this struct using NativeCall thus: class Info is repr('CStruct') { has int32 $.myInfo; has Pointer[int8] $.pInfo ;…
smith
  • 3,232
  • 26
  • 55
6
votes
0 answers

Nativecall Segfaults Getting Tuple from Rust

Per the Rust FFI Omnibus the following should work. This is a rust cdylib lib.rs named "foo" made with cargo build... use std::convert::From; // Rust FFI Omnibus: Tuples // http://jakegoulding.com/rust-ffi-omnibus/tuples/ // A Rust function that…
librasteve
  • 6,832
  • 8
  • 30
6
votes
1 answer

Raku how to pass a pointer to a Buf to a native call for writing

I'm trying to wrap the read function from unistd.h, but can't get it to work. Here's what I have: (in file: read.raku) use NativeCall; # ssize_t read(int fd, void *buf, size_t count); sub c_read(int32 $fd, Pointer $buf is rw, size_t $count -->…
cowbaymoo
  • 1,202
  • 5
  • 14