Skip to content

Commit

Permalink
Streamline NativeCall (part 2/N)
Browse files Browse the repository at this point in the history
- finding the right name mangler
- abstract checking into a helper sub for convenience
  • Loading branch information
lizmat committed May 1, 2024
1 parent 40c7b77 commit 3041066
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions lib/NativeCall.rakumod
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
use nqp;
use QAST:from<NQP>;

#- C compiler specfic ----------------------------------------------------------
use NativeCall::Compiler::GNU;
use NativeCall::Compiler::MSVC;

my constant $cpp-name-manglers = nqp::list(
&NativeCall::Compiler::MSVC::mangle_cpp_symbol,
&NativeCall::Compiler::GNU::mangle_cpp_symbol,
);
my int $manglers = nqp::elems($cpp-name-manglers);

#- re-export constants ---------------------------------------------------------
use NativeCall::Types;

Expand Down Expand Up @@ -252,26 +260,24 @@ multi guess_library_name(Str $libname, $apiversion='') is export(:TEST) {
!! ''
}

my %lib;
my @cpp-name-mangler =
&NativeCall::Compiler::MSVC::mangle_cpp_symbol,
&NativeCall::Compiler::GNU::mangle_cpp_symbol,
;

sub guess-name-mangler(Routine $r, $name, Str $libname) {
if $r.package.REPR eq 'CPPStruct' {
my $sym = $r.?native_symbol // ($r.package.^name ~ '::' ~ $name);
for @cpp-name-mangler -> &mangler {
return &mangler if try cglobal($libname, mangler($r, $sym), Pointer)

my sub mangler-for($sym) {
my int $i;
while $i < $manglers {
my &mangler := nqp::atpos($cpp-name-manglers, $i);
(try cglobal($libname, mangler($r, $sym), Pointer))
?? (return &mangler)
!! ++$i;
}
die "Don't know how to mangle symbol '$sym' for library '$libname'"
die "Don't know how to mangle symbol '$sym' for library '$libname'";
}

if $r.package.REPR eq 'CPPStruct' {
mangler-for $r.?native_symbol // ($r.package.^name ~ '::' ~ $name)
}
elsif $r.?native_call_mangled {
my $sym = $r.?native_symbol // $name;
for @cpp-name-mangler -> &mangler {
return &mangler if try cglobal($libname, mangler($r, $sym), Pointer)
}
die "Don't know how to mangle symbol '$sym' for library '$libname'"
mangler-for $r.?native_symbol // $name
}
}

Expand All @@ -288,6 +294,8 @@ my $use-dispatcher := BEGIN {
!! False
};

my %lib;

# This role is mixed in to any routine that is marked as being a
# native call.
our role Native[Routine $r, $libname where Str|Callable|List|IO::Path|Distribution::Resource] {
Expand Down

0 comments on commit 3041066

Please sign in to comment.