Hello list !

So, I'm trying to make a MongoDB driver for Perl6.
I was advised at #perl6to use NativeCall ( https://github.com/jnthn/zavolaj).

I did this, by copying the MySQL driver in examples :

use NativeCall;
sub mongo_connect( OpaquePointer $client, OpaquePointer $options) returns
OpaquePointer is native('libmongoc') { ... }
my OpaquePointer $connection;
my $answer = mongo_connect($connection,pir::null__P());

The function I'm trying to call is here :
https://github.com/mongodb/mongo-c-driver/blob/master/src/mongo.c
Here is the result :

art...@aquarelle /t/zavolaj> perl6 examples/mongodb.p6
Null PMC access in isa_pmc()
  in '&infix:<=>' at line 1
  in main program body at line 15:examples/mongodb.p6
fish: Tâche 1, 'perl6 examples/mongodb.p6 ' terminée par le signal SIGSEGV
(Erreur de frontière d'adresse)

The good news is : watching the MongoDB log files *show a connection* : woot
!
The bad news is the error.
The SIGSEGV error I get at the end of the script whatever I do, so I'm not
worying about it for now.
So the problem is the Null PMC access in isa_pmc() error.

I don't find how to get rid of it, any ideas ?

So a workaround I found was to instead of null__P() ( eg. not specifying
options and get the defaults ), pass a mongo_connection_options C struct.
In the code it is defined like this :

typedef struct mongo_connection_options {
    char host[255];
    int port;
} mongo_connection_options;

But I can't define/instantiate it in my Perl6 code, so I need to do it in a
C function, then call it from perl, and then feed the struct to the
mongo_connect function ( as writing this I see I could do a C function that
creates the struct then instead of returning it calls directly mongo_connect
with it, I'll look into that later ).
So I end up with the following code ( C ) :

mongo_connection_options * make_mongo_connection_options( char host[255],
int port ){
        mongo_connection_options *opts =
malloc(sizeof(mongo_connection_options));
        strcpy( opts->host , host );
        opts->port = port;
        return opts;
}

And the following code ( Perl6 ) :

use NativeCall;
sub mongo_connect( OpaquePointer $client, OpaquePointer $options) returns
OpaquePointer is native('libmongoc') { ... }
sub make_mongo_connection_options( Str $host, Int $port ) returns
OpaquePointer is native('libmongoc') { ... }
my OpaquePointer $connection;
my $options = make_mongo_connection_options('localhost', 27017);
my $answer = mongo_connect($connection, $options);


So now I don't get the Null PMC access in isa_pmc() error ( still the SIGSGV
at the end ), but I also don't get a connection in the MongoDB logs, so it's
not working.

I don't know where the problem is, and I don't have lots of tools ( or
knowledge, I was hoping not to have to write C for this ) to debug it.

Anyone has ideas ?

A clue I may have is this : doing $options.WHAT says : Method 'WHAT' not
found for invocant of class *'UnManagedStruct'** *, so it is not a
OpaquePointer , which it is what I guess it should be.

Thanks a lot in advance for any help you can give !!

-- 
Courage (et Bonne humeur|and Good humor)\.

Reply via email to