Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support passing floats from P6 to P5
  • Loading branch information
niner committed Sep 17, 2014
1 parent d22f0eb commit 764ec13
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/Inline/Perl5.pm6
Expand Up @@ -100,6 +100,9 @@ sub p5_sv_refcnt_inc(Perl5Interpreter, OpaquePointer)
sub p5_int_to_sv(Perl5Interpreter, Int)
returns OpaquePointer { ... }
native(&p5_int_to_sv);
sub p5_float_to_sv(Perl5Interpreter, num64)
returns OpaquePointer { ... }
native(&p5_float_to_sv);
sub p5_str_to_sv(Perl5Interpreter, Str)
returns OpaquePointer { ... }
native(&p5_str_to_sv);
Expand Down Expand Up @@ -194,6 +197,12 @@ sub p5_terminate()
multi method p6_to_p5(Int:D $value) returns OpaquePointer {
return p5_int_to_sv($!p5, $value);
}
multi method p6_to_p5(Num:D $value) returns OpaquePointer {
return p5_float_to_sv($!p5, $value);
}
multi method p6_to_p5(Rat:D $value) returns OpaquePointer {
return p5_float_to_sv($!p5, $value.Num);
}
multi method p6_to_p5(Str:D $value) returns OpaquePointer {
return p5_str_to_sv($!p5, $value);
}
Expand Down
4 changes: 4 additions & 0 deletions p5helper.c
Expand Up @@ -110,6 +110,10 @@ SV *p5_int_to_sv(PerlInterpreter *my_perl, int value) {
return newSViv(value);
}

SV *p5_float_to_sv(PerlInterpreter *my_perl, double value) {
return newSVnv(value);
}

SV *p5_str_to_sv(PerlInterpreter *my_perl, char* value) {
SV * const sv = newSVpv(value, 0);
SvUTF8_on(sv);
Expand Down
12 changes: 10 additions & 2 deletions t/p6_to_p5.t
Expand Up @@ -4,15 +4,15 @@ use v6;
use Test;
use Inline::Perl5;

plan 7;
plan 9;

my $p5 = Inline::Perl5.new();
$p5.run(q[ sub identity { return $_[1] }; ]);

class Foo {
}

for ('abcö', 24, [1, 2], { a=> 1, b => 2}, Any, Foo.new) -> $obj {
for ('abcö', 24, 2.4.Num, [1, 2], { a => 1, b => 2}, Any, Foo.new) -> $obj {
is_deeply $p5.call('identity', 'main', $obj), $obj, "Can round-trip " ~ $obj.^name;
}

Expand All @@ -27,6 +27,14 @@ $p5.run(q/

ok($p5.call('check_utf8', 'Töst'), 'UTF-8 string recognized in Perl 5');

$p5.run(q/
sub is_two_point_five {
return $_[0] == 2.5;
}
/);

ok($p5.call('is_two_point_five', Num.new(2.5)));

$p5.DESTROY;

# vim: ft=perl6

0 comments on commit 764ec13

Please sign in to comment.