Skip to content
This repository
Browse code

Merge pull request #8 from perlpilot/master

Provide a way to use a different name in your Perl than the native library
  • Loading branch information...
commit 4af0a9aaafb09431cd24e4b194c1d35309f3f740 2 parents 3bf6712 + eb3e3d8
Arne Skjærholt arnsholt authored

Showing 2 changed files with 27 additions and 1 deletion. Show diff stats Hide diff stats

  1. +18 0 README.markdown
  2. +9 1 lib/NativeCall.pm6
18 README.markdown
Source Rendered
@@ -33,6 +33,24 @@ that you can do is just adding to this simple pattern of declaring a Perl 6
33 33 sub, naming it after the symbol you want to call and marking it with the "native"
34 34 trait.
35 35
  36 +## Changing names
  37 +Sometimes you want the name of your Perl subroutine to be different from the name
  38 +used in the library you're loading. Maybe the name is long or has different casing
  39 +or is otherwise cumbersome within the context of the module you are trying to
  40 +create.
  41 +
  42 +Zavolaj provides a "named" trait for you to specify the name of the native
  43 +routine in your library that may be different from your Perl subroutine name.
  44 +
  45 + module Foo;
  46 + use NativeCall;
  47 + our sub Init() is native('libfoo') is named('FOO_INIT') { * }
  48 +
  49 +Inside of "libfoo" there is a routine called "FOO\_INIT" but, since we're
  50 +creating a module called Foo and we'd rather call the routine as Foo::Init,
  51 +we use the "named" trait to specify the name of the symbol in "libfoo"
  52 +and call the subroutine whatever we want ("Init" in this case).
  53 +
36 54 ## Passing and Returning Values
37 55 Normal Perl 6 signatures and the "returns" trait are used in order to convey
38 56 the type of arguments a native function expects and what it returns. Here is
10 lib/NativeCall.pm6
@@ -97,6 +97,10 @@ multi sub map_return_type($type) {
97 97 $type
98 98 }
99 99
  100 +my role Named[Str $name] {
  101 + method native_name() { $name }
  102 +}
  103 +
100 104 # This role is mixed in to any routine that is marked as being a
101 105 # native call.
102 106 my role Native[Routine $r, Str $libname] {
@@ -113,7 +117,7 @@ my role Native[Routine $r, Str $libname] {
113 117 "$libname$*VM<config><load_ext>";
114 118 nqp::buildnativecall(self,
115 119 nqp::unbox_s($realname), # library name
116   - nqp::unbox_s($r.name), # symbol to call
  120 + nqp::unbox_s(self.?native_name || $r.name), # symbol to call
117 121 nqp::unbox_s($conv), # calling convention
118 122 $arg_info,
119 123 return_hash_for($r.signature));
@@ -222,6 +226,10 @@ my class CArray is export is repr('CArray') {
222 226 }
223 227 }
224 228
  229 +multi trait_mod:<is>(Routine $r, :$named!) is export {
  230 + $r does Named[$named];
  231 +}
  232 +
225 233 # Specifies that the routine is actually a native call, into the
226 234 # current executable (platform specific) or into a named library
227 235 multi trait_mod:<is>(Routine $r, :$native!) is export {

0 comments on commit 4af0a9a

Please sign in to comment.
Something went wrong with that request. Please try again.