3 # test the various call-into-perl-from-C functions
7 push @INC, "::lib:$MacPerl::Architecture:" if $^O eq 'MacOS';
8 require Config; import Config;
9 if ($Config{'extensions'} !~ /\bXS\/APItest\b/) {
10 # Look, I'm using this fully-qualified variable more than once!
11 my $arch = $MacPerl::Architecture;
12 print "1..0 # Skip: XS::APItest was not built\n";
20 # Test::More doesn't have fresh_perl_is() yet
21 # use Test::More tests => 240;
24 require '../../t/test.pl';
29 #########################
35 @_, defined wantarray ? wantarray ? 'x' : 'y' : 'z';
39 no warnings 'misc'; # keep G_KEEPERR from emitting this as a warning
43 my $obj = bless [], 'Foo';
46 return 'bad_self' unless @_ && ref $_[0] && ref($_[0]) eq 'Foo';
51 @_, defined wantarray ? wantarray ? 'x' : 'y' : 'z';
55 no warnings 'misc'; # keep G_KEEPERR from emitting this as a warning
60 # flags args expected description
61 [ G_VOID, [ ], [ qw(z 1) ], '0 args, G_VOID' ],
62 [ G_VOID, [ qw(a p q) ], [ qw(z 1) ], '3 args, G_VOID' ],
63 [ G_SCALAR, [ ], [ qw(y 1) ], '0 args, G_SCALAR' ],
64 [ G_SCALAR, [ qw(a p q) ], [ qw(y 1) ], '3 args, G_SCALAR' ],
65 [ G_ARRAY, [ ], [ qw(x 1) ], '0 args, G_ARRAY' ],
66 [ G_ARRAY, [ qw(a p q) ], [ qw(b p x 3) ], '3 args, G_ARRAY' ],
67 [ G_DISCARD, [ ], [ qw(0) ], '0 args, G_DISCARD' ],
68 [ G_DISCARD, [ qw(a p q) ], [ qw(0) ], '3 args, G_DISCARD' ],
71 my ($flags, $args, $expected, $description) = @$test;
73 ok(eq_array( [ call_sv(\&f, $flags, @$args) ], $expected),
74 "$description call_sv(\\&f)");
76 ok(eq_array( [ call_sv(*f, $flags, @$args) ], $expected),
77 "$description call_sv(*f)");
79 ok(eq_array( [ call_sv('f', $flags, @$args) ], $expected),
80 "$description call_sv('f')");
82 ok(eq_array( [ call_pv('f', $flags, @$args) ], $expected),
83 "$description call_pv('f')");
85 ok(eq_array( [ eval_sv('f(' . join(',',map"'$_'",@$args) . ')', $flags) ],
86 $expected), "$description eval_sv('f(args)')");
88 ok(eq_array( [ call_method('meth', $flags, $obj, @$args) ], $expected),
89 "$description call_method('meth')");
91 my $returnval = ((($flags & G_WANT) == G_ARRAY) || ($flags & G_DISCARD))
93 for my $keep (0, G_KEEPERR) {
94 my $desc = $description . ($keep ? ' G_KEEPERR' : '');
95 my $exp_err = $keep ? "before\n\t(in cleanup) its_dead_jim\n"
98 ok(eq_array( [ call_sv('d', $flags|G_EVAL|$keep, @$args) ],
100 "$desc G_EVAL call_sv('d')");
101 is($@, $exp_err, "$desc G_EVAL call_sv('d') - \$@");
104 ok(eq_array( [ call_pv('d', $flags|G_EVAL|$keep, @$args) ],
106 "$desc G_EVAL call_pv('d')");
107 is($@, $exp_err, "$desc G_EVAL call_pv('d') - \$@");
110 ok(eq_array( [ eval_sv('d()', $flags|$keep) ],
112 "$desc eval_sv('d()')");
113 is($@, $exp_err, "$desc eval_sv('d()') - \$@");
116 ok(eq_array( [ call_method('d', $flags|G_EVAL|$keep, $obj, @$args) ],
118 "$desc G_EVAL call_method('d')");
119 is($@, $exp_err, "$desc G_EVAL call_method('d') - \$@");
122 ok(eq_array( [ sub { call_sv('f', $flags|G_NOARGS, "bad") }->(@$args) ],
123 $expected), "$description G_NOARGS call_sv('f')");
125 ok(eq_array( [ sub { call_pv('f', $flags|G_NOARGS, "bad") }->(@$args) ],
126 $expected), "$description G_NOARGS call_pv('f')");
128 ok(eq_array( [ sub { eval_sv('f(@_)', $flags|G_NOARGS) }->(@$args) ],
129 $expected), "$description G_NOARGS eval_sv('f(@_)')");
131 # XXX call_method(G_NOARGS) isn't tested: I'm assuming
132 # it's not a sensible combination. DAPM.
134 ok(eq_array( [ eval { call_sv('d', $flags, @$args)}, $@ ],
135 [ "its_dead_jim\n" ]), "$description eval { call_sv('d') }");
137 ok(eq_array( [ eval { call_pv('d', $flags, @$args) }, $@ ],
138 [ "its_dead_jim\n" ]), "$description eval { call_pv('d') }");
140 ok(eq_array( [ eval { eval_sv('d', $flags), $@ }, $@ ],
142 "its_dead_jim\n", '' ]),
143 "$description eval { eval_sv('d') }");
145 ok(eq_array( [ eval { call_method('d', $flags, $obj, @$args) }, $@ ],
146 [ "its_dead_jim\n" ]), "$description eval { call_method('d') }");
150 is(eval_pv('f()', 0), 'y', "eval_pv('f()', 0)");
151 is(eval_pv('f(qw(a b c))', 0), 'y', "eval_pv('f(qw(a b c))', 0)");
152 is(eval_pv('d()', 0), undef, "eval_pv('d()', 0)");
153 is($@, "its_dead_jim\n", "eval_pv('d()', 0) - \$@");
154 is(eval { eval_pv('d()', 1) } , undef, "eval { eval_pv('d()', 1) }");
155 is($@, "its_dead_jim\n", "eval { eval_pv('d()', 1) } - \$@");
157 # DAPM 9-Aug-04. A taint test in eval_sv() could die after setting up
158 # a new jump level but before pushing an eval context, leading to
161 fresh_perl_is(<<'EOF', "x=2", { switches => ['-T', '-I../../lib'] }, 'eval_sv() taint');
166 eval { my @a = ($^X . "x" , eval_sv(q(die "inner\n"), 0)) ; };
171 eval { my @a = sort f 2, 1; $x++};