xref: /openbsd-src/gnu/usr.bin/perl/ext/XS-APItest/t/clone-with-stack.t (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#!perl
2
3use strict;
4use warnings;
5
6require "../../t/test.pl";
7
8use 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 psuedo-fork and exiting the parent.
14
15use Config;
16if (not $Config{'useithreads'}) {
17    skip_all("clone_with_stack requires threads");
18}
19
20plan(5);
21
22fresh_perl_is( <<'----', <<'====', undef, "minimal clone_with_stack" );
23use XS::APItest;
24clone_with_stack();
25print "ok\n";
26----
27ok
28====
29
30fresh_perl_is( <<'----', <<'====', undef, "inside a subroutine" );
31use XS::APItest;
32sub f {
33    clone_with_stack();
34}
35f();
36print "ok\n";
37----
38ok
39====
40
41{
42    local our $TODO = "clone_with_stack inside a begin block";
43    fresh_perl_is( <<'----', <<'====', undef, "inside a BEGIN block" );
44use XS::APItest;
45BEGIN {
46    clone_with_stack();
47}
48print "ok\n";
49----
50ok
51====
52
53}
54
55{
56    fresh_perl_is( <<'----', <<'====', undef, "clone stack" );
57use XS::APItest;
58sub f {
59    clone_with_stack();
60    0..4;
61}
62print 'X-', 'Y-', join(':', f()), "-Z\n";
63----
64X-Y-0:1:2:3:4-Z
65====
66
67}
68
69{
70    fresh_perl_is( <<'----', <<'====', undef, "with localised stuff" );
71use XS::APItest;
72$s = "outer";
73$a[0] = "anterior";
74$h{k} = "hale";
75{
76    local $s = "inner";
77    local $a[0] = 'posterior';
78    local $h{k} = "halt";
79    clone_with_stack();
80}
81print "scl: $s\n";
82print "ary: $a[0]\n";
83print "hsh: $h{k}\n";
84----
85scl: outer
86ary: anterior
87hsh: hale
88====
89
90}
91