Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix Perl5 classes unable to override Mu's methods
Even Mu's methods trump fallbacks, so we have to explicitly pass them through
to the marshalled Perl 5 object or package, same as we do with Any's methods.
As Mu's methods are integral to Perl 6's workings, we cannot just wrapp all
of them.

Instead, only pass through <note print put say split> as those should be safe.
  • Loading branch information
niner committed Aug 5, 2016
1 parent e7b7332 commit 3acaa28
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/Inline/Perl5.pm6
Expand Up @@ -17,6 +17,8 @@ my $default_perl5;

my constant $p5helper = %?RESOURCES<libraries/p5helper>.Str;

my constant @pass_through_methods = |Any.^methods>>.name.grep(/^\w+$/), |<note print put say split>;

class Perl5Object { ... }
class Perl5Callable { ... }

Expand Down Expand Up @@ -772,7 +774,7 @@ role Perl5Package[Inline::Perl5 $p5, Str $module] {
!! $p5.invoke($module, $name, |@args.list, |%kwargs);
}

for Any.^methods>>.name.list, <say> -> $name {
for @pass_through_methods -> $name {
next if $?CLASS.^declares_method($name);
my $method = method (|args) {
return self.defined
Expand Down Expand Up @@ -1032,7 +1034,8 @@ BEGIN {
}
}
);
for Any.^methods>>.name -> $name {
for @pass_through_methods -> $name {
next if Perl5Object.^declares_method($name);
Perl5Object.^add_method(
$name,
method (|args) {
Expand Down
8 changes: 6 additions & 2 deletions t/inherit.t
Expand Up @@ -4,11 +4,11 @@ use v6;
use Test;

BEGIN {
plan 10; # adjust the skip as well!
plan 14; # adjust the skip as well!

EVAL 'use lib qw(t/lib);', :lang<Perl5>;
unless EVAL 'eval { require Moose; 1};', :lang<Perl5> {
skip('Perl 5 Moose module not available', 10);
skip('Perl 5 Moose module not available', 14);
exit;
}
}
Expand All @@ -32,6 +32,10 @@ is(P6Bar.new(foo => 'custom').foo, 'custom');
is(P6Bar.new.call_end, 'end', 'Any methods not interfering with inheritance');
is(P6Bar.new.call_list, 'list', 'Any methods not interfering with inheritance');
is(P6Bar.new.say, 'say', 'say method not interfering with inheritance');
is(P6Bar.new.print, 'print', 'print method not interfering with inheritance');
is(P6Bar.new.note, 'note', 'print method not interfering with inheritance');
is(P6Bar.new.put, 'put', 'print method not interfering with inheritance');
is(P6Bar.new.split, 'split', 'print method not interfering with inheritance');

class Baz is Foo {
method bar {
Expand Down
16 changes: 16 additions & 0 deletions t/lib/Foo.pm
Expand Up @@ -52,4 +52,20 @@ sub say {
return 'say';
}

sub print {
return 'print';
}

sub note {
return 'note';
}

sub put {
return 'put';
}

sub split {
return 'split';
}

1;
35 changes: 35 additions & 0 deletions t/lib/Shadow.pm
@@ -0,0 +1,35 @@
package Shadow;

sub new {
return bless {};
}

sub list {
return 'list';
}

sub end {
return 'end';
}

sub say {
return 'say';
}

sub print {
return 'print';
}

sub note {
return 'note';
}

sub put {
return 'put';
}

sub split {
return 'split';
}

1;
22 changes: 22 additions & 0 deletions t/shadow.t
@@ -0,0 +1,22 @@
#!/usr/bin/env perl6

use v6;
use Test;

use lib:from<Perl5> 't/lib';
use Shadow:from<Perl5>;

my constant @methods = <list end say print note put split>;

plan @methods.elems * 2;

for @methods -> $name {
is(Shadow."$name"(), $name);
}

my $shadow = Shadow.new;
for @methods -> $name {
is($shadow."$name"(), $name);
}

# vim: ft=perl6

0 comments on commit 3acaa28

Please sign in to comment.