4 require Config; import Config;
5 if ($Config{'extensions'} !~ /\bOpcode\b/ && $Config{'osname'} ne 'VMS') {
14 use Opcode 1.00, qw(opdesc opset opset_to_ops opset_to_hex
15 opmask_add full_opset empty_opset opcodes opmask define_optag);
20 my $TB = Test::Builder->new();
22 # Set up a package namespace of things to be visible to the unsafe code
23 $Root::foo = "visible";
24 our $bar = "invisible";
26 # Stop perl from moaning about identifies which are apparently only used once
30 # create and destroy a couple of automatic Safe compartments first
31 $cpt = new Safe or die;
32 $cpt = new Safe or die;
34 $cpt = new Safe "Root";
36 $cpt->permit(qw(:base_io));
38 $cpt->reval(q{ system("echo not ok 1"); });
39 like($@, qr/^'?system'? trapped by operation mask/);
42 print $foo eq 'visible' ? "ok 2\n" : "not ok 2\n";
43 print $main::foo eq 'visible' ? "ok 3\n" : "not ok 3\n";
44 print defined($bar) ? "not ok 4\n" : "ok 4\n";
45 print defined($::bar) ? "not ok 5\n" : "ok 5\n";
46 print defined($main::bar) ? "not ok 6\n" : "ok 6\n";
52 %bar = (key => "ok 9\n");
53 @baz = (); push(@baz, "o", "10");
55 @glob = qw(not ok 16);
57 sub sayok { print "ok @_\n" }
59 $cpt->share(qw($foo %bar @baz *glob sayok));
60 $cpt->share('$"') unless $Config{use5005threads};
66 sub other_sayok { print "ok @_\n" }
68 print $foo ? $foo : "not ok 8\n";
69 print $bar{key} ? $bar{key} : "not ok 9\n";
70 (@baz) ? print "@baz\n" : print "not ok 10\n";
72 other::other_sayok(12);
74 $bar{new} = "ok 15\n";
79 $TB->current_test(12);
82 is($bar{new}, "ok 15\n");
85 $Root::foo = "not ok 17";
86 @{$cpt->varglob('bar')} = qw(not ok 18);
87 ${$cpt->varglob('foo')} = "ok 17";
89 push(@Root::bar, "18"); # Two steps to prevent "Identifier used only once..."
91 is($Root::foo, 'ok 17');
92 is("@{$cpt->varglob('bar')}", 'ok 18');
99 my @masked = opset_to_ops($m1);
100 is(opset("negate", @masked), $m2);
102 is(eval { $cpt->mask("a bad mask") }, undef);
105 is($cpt->reval("2 + 2"), 4);
107 my $test = $TB->current_test() + 1;
109 my \$todo = \$] < 5.021007 ? ' # TODO' : '';
110 print defined wantarray
111 ? qq'not ok $test\$todo\n'
112 : qq'ok $test\$todo\n'
115 my $t_scalar = $cpt->reval("print wantarray ? 'not ok $test\n' : 'ok $test\n'");
117 my @t_array = $cpt->reval("print wantarray ? 'ok $test\n' : 'not ok $test\n'; (2,3,4)");
118 $TB->current_test($test);
122 is($cpt->reval('@ary=(6,7,8);@ary'), 3);
124 my $t_scalar2 = $cpt->reval('die "foo bar"; 1');
125 is($t_scalar2, undef);
126 like($@, qr/foo bar/);
131 my $nosuch = '/non/existent/file.name';
132 open(NOSUCH, '<', $nosuch);
135 die "Eek! Attempting to open $nosuch failed, but \$! is still 0" unless $!;
140 die "Eek! Didn't expect $nosuch to be there.";