| 1 |
#!perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
require "../../t/test.pl"; |
| 7 |
|
| 8 |
use XS::APItest; |
| 9 |
|
| 10 |
# clone_with_stack creates a clone of the perl interpreter including |
| 11 |
# the stack, then destroys the original interpreter and runs the |
| 12 |
# remaining code using the new one. |
| 13 |
# This is like doing a pseudo-fork and exiting the parent. |
| 14 |
|
| 15 |
use Config; |
| 16 |
if (not $Config{'useithreads'}) { |
| 17 |
skip_all("clone_with_stack requires threads"); |
| 18 |
} |
| 19 |
|
| 20 |
plan(8); |
| 21 |
|
| 22 |
fresh_perl_is( <<'----', <<'====', undef, "minimal clone_with_stack" ); |
| 23 |
use XS::APItest; |
| 24 |
clone_with_stack(); |
| 25 |
print "ok\n"; |
| 26 |
---- |
| 27 |
ok |
| 28 |
==== |
| 29 |
|
| 30 |
fresh_perl_is( <<'----', <<'====', undef, "inside a subroutine" ); |
| 31 |
use XS::APItest; |
| 32 |
sub f { |
| 33 |
clone_with_stack(); |
| 34 |
} |
| 35 |
f(); |
| 36 |
print "ok\n"; |
| 37 |
---- |
| 38 |
ok |
| 39 |
==== |
| 40 |
|
| 41 |
{ |
| 42 |
local our $TODO = "clone_with_stack inside a begin block"; |
| 43 |
fresh_perl_is( <<'----', <<'====', undef, "inside a BEGIN block" ); |
| 44 |
use XS::APItest; |
| 45 |
BEGIN { |
| 46 |
clone_with_stack(); |
| 47 |
} |
| 48 |
print "ok\n"; |
| 49 |
---- |
| 50 |
ok |
| 51 |
==== |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
{ |
| 56 |
fresh_perl_is( <<'----', <<'====', undef, "clone stack" ); |
| 57 |
use XS::APItest; |
| 58 |
sub f { |
| 59 |
clone_with_stack(); |
| 60 |
0..4; |
| 61 |
} |
| 62 |
print 'X-', 'Y-', join(':', f()), "-Z\n"; |
| 63 |
---- |
| 64 |
X-Y-0:1:2:3:4-Z |
| 65 |
==== |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
{ |
| 70 |
fresh_perl_is( <<'----', <<'====', undef, "with a lexical sub" ); |
| 71 |
use XS::APItest; |
| 72 |
use experimental lexical_subs=>; |
| 73 |
my sub f { print "42\n" } |
| 74 |
clone_with_stack(); |
| 75 |
f(); |
| 76 |
---- |
| 77 |
42 |
| 78 |
==== |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
{ |
| 83 |
fresh_perl_is( <<'----', <<'====', undef, "with localised stuff" ); |
| 84 |
use XS::APItest; |
| 85 |
$s = "outer"; |
| 86 |
$a[0] = "anterior"; |
| 87 |
$h{k} = "hale"; |
| 88 |
{ |
| 89 |
local $s = "inner"; |
| 90 |
local $a[0] = 'posterior'; |
| 91 |
local $h{k} = "halt"; |
| 92 |
clone_with_stack(); |
| 93 |
} |
| 94 |
print "scl: $s\n"; |
| 95 |
print "ary: $a[0]\n"; |
| 96 |
print "hsh: $h{k}\n"; |
| 97 |
---- |
| 98 |
scl: outer |
| 99 |
ary: anterior |
| 100 |
hsh: hale |
| 101 |
==== |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
{ |
| 106 |
fresh_perl_is( <<'----', <<'====', undef, "inside a loop inside a fn" ); |
| 107 |
use XS::APItest; |
| 108 |
my $a = 'aa'; |
| 109 |
sub f { |
| 110 |
my $b = 'bb'; |
| 111 |
my @c; |
| 112 |
my $d = 'dd'; |
| 113 |
for my $d (0..4) { |
| 114 |
clone_with_stack() if $d == 2; |
| 115 |
push @c, $d; |
| 116 |
} |
| 117 |
return @c, $d; |
| 118 |
} |
| 119 |
print "X-$a-", join(':', f()), "-Z\n"; |
| 120 |
---- |
| 121 |
X-aa-0:1:2:3:4:dd-Z |
| 122 |
==== |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
{ |
| 127 |
fresh_perl_is( <<'----', <<'====', undef, "inside fn inside a loop inside a fn" ); |
| 128 |
use XS::APItest; |
| 129 |
my $a = 'aa'; |
| 130 |
|
| 131 |
sub g { |
| 132 |
my $e = 'ee'; |
| 133 |
my $f = 'ff'; |
| 134 |
clone_with_stack(); |
| 135 |
} |
| 136 |
|
| 137 |
sub f { |
| 138 |
my $b = 'bb'; |
| 139 |
my @c; |
| 140 |
my $d = 'dd'; |
| 141 |
for my $d (0..4) { |
| 142 |
g() if $d == 2; |
| 143 |
push @c, $d; |
| 144 |
} |
| 145 |
return @c, $d; |
| 146 |
} |
| 147 |
print "X-$a-", join(':', f()), "-Z\n"; |
| 148 |
---- |
| 149 |
X-aa-0:1:2:3:4:dd-Z |
| 150 |
==== |
| 151 |
|
| 152 |
} |