Commit | Line | Data |
---|---|---|
27c4ce72 TB |
1 | #!perl -w |
2 | ||
3 | $|=1; | |
4 | BEGIN { | |
5 | require Config; import Config; | |
6 | if ($Config{'extensions'} !~ /\bOpcode\b/ && $Config{'osname'} ne 'VMS') { | |
7 | print "1..0\n"; | |
8 | exit 0; | |
9 | } | |
10 | } | |
11 | ||
12 | use strict; | |
13 | use Safe 1.00; | |
7650682f | 14 | use Test::More tests => 10; |
27c4ce72 TB |
15 | |
16 | my $safe = Safe->new('PLPerl'); | |
17 | $safe->permit_only(qw(:default sort)); | |
18 | ||
19 | # eval within an eval: the outer eval is compiled into the sub, the inner is | |
20 | # compiled (by the outer) at runtime and so is subject to runtime opmask | |
21 | my $sub1 = sub { eval " eval '1+1' " }; | |
22 | is $sub1->(), 2; | |
23 | ||
24 | my $sub1w = $safe->wrap_code_ref($sub1); | |
25 | is ref $sub1w, 'CODE'; | |
26 | is eval { $sub1w->() }, undef; | |
27 | like $@, qr/eval .* trapped by operation mask/; | |
28 | ||
29 | is $sub1->(), 2, 'original ref should be unaffected'; | |
30 | ||
31 | # setup args for wrap_code_refs_within including nested data | |
32 | my @args = (42, [[ 0, { sub => $sub1 }, 2 ]], 24); | |
33 | is $args[1][0][1]{sub}, $sub1; | |
34 | ||
35 | $safe->wrap_code_refs_within(@args); | |
36 | my $sub1w2 = $args[1][0][1]{sub}; | |
37 | isnt $sub1w2, $sub1; | |
38 | is eval { $sub1w2->() }, undef; | |
39 | like $@, qr/eval .* trapped by operation mask/; | |
7650682f RGS |
40 | |
41 | # Avoid infinite recursion when looking for coderefs | |
42 | my $r = $safe->reval(<<'END'); | |
43 | %a = (); | |
44 | %b = (a => \%a); | |
45 | $a{b} = \%b; | |
46 | 42; | |
47 | END | |
48 | is($r, 42); |