This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
B::walksymtable: clear cached methods
[perl5.git] / ext / B / t / b.t
CommitLineData
ccc418af
GS
1#!./perl
2
3BEGIN {
74517a3a 4 unshift @INC, 't';
9cd8f857
NC
5 require Config;
6 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7 print "1..0 # Skip -- Perl configured without B module\n";
8 exit 0;
9 }
ccc418af
GS
10}
11
12$| = 1;
13use warnings;
14use strict;
200b5b4b
DD
15BEGIN {
16 eval { require threads; threads->import; }
17}
f9a20969 18use Test::More;
ccc418af 19
c5f0f3aa 20BEGIN { use_ok( 'B' ); }
ccc418af 21
08c6f5ec 22
87a42246
MS
23package Testing::Symtable;
24use vars qw($This @That %wibble $moo %moo);
25my $not_a_sym = 'moo';
ccc418af 26
87a42246
MS
27sub moo { 42 }
28sub car { 23 }
ccc418af 29
f70490b9 30
87a42246
MS
31package Testing::Symtable::Foo;
32sub yarrow { "Hock" }
f70490b9 33
87a42246
MS
34package Testing::Symtable::Bar;
35sub hock { "yarrow" }
9b86dfa2 36
87a42246
MS
37package main;
38use vars qw(%Subs);
39local %Subs = ();
40B::walksymtable(\%Testing::Symtable::, 'find_syms', sub { $_[0] =~ /Foo/ },
41 'Testing::Symtable::');
ccc418af 42
87a42246
MS
43sub B::GV::find_syms {
44 my($symbol) = @_;
de3f1649 45
87a42246 46 $main::Subs{$symbol->STASH->NAME . '::' . $symbol->NAME}++;
cfe9256d 47}
ccc418af 48
87a42246
MS
49my @syms = map { 'Testing::Symtable::'.$_ } qw(This That wibble moo car
50 BEGIN);
51push @syms, "Testing::Symtable::Foo::yarrow";
ccc418af 52
87a42246 53# Make sure we hit all the expected symbols.
c5f0f3aa 54ok( join('', sort @syms) eq join('', sort keys %Subs), 'all symbols found' );
1e1dbab6 55
87a42246 56# Make sure we only hit them each once.
c5f0f3aa
RGS
57ok( (!grep $_ != 1, values %Subs), '...and found once' );
58
f4a37198
FC
59
60# Make sure method caches are not present when walking the sym tab
61@Testing::Method::Caches::Foo::ISA='Testing::Method::Caches::Bar';
62sub Testing::Method::Caches::Bar::foo{}
63Testing::Method::Caches::Foo->foo; # caches the sub in the *foo glob
64
65my $have_cv;
66sub B::GV::method_cache_test { ${shift->CV} and ++$have_cv }
67
68B::walksymtable(\%Testing::Method::Caches::, 'method_cache_test',
69 sub { 1 }, 'Testing::Method::Caches::');
70# $have_cv should only have been incremented for ::Bar::foo
71is $have_cv, 1, 'walksymtable clears cached methods';
72
73
c5f0f3aa
RGS
74# Tests for MAGIC / MOREMAGIC
75ok( B::svref_2object(\$.)->MAGIC->TYPE eq "\0", '$. has \0 magic' );
76{
77 my $e = '';
78 local $SIG{__DIE__} = sub { $e = $_[0] };
79 # Used to dump core, bug #16828
80 eval { B::svref_2object(\$.)->MAGIC->MOREMAGIC->TYPE; };
81 like( $e, qr/Can't call method "TYPE" on an undefined value/,
82 '$. has no more magic' );
83}
01b509b0 84
de64752d
NC
85{
86 my $pie = 'Good';
87 # This needs to be a package variable, as vars in the pad have some flags.
88 my $r = B::svref_2object(\$::data2);
89 is($r->FLAGS(), 0, "uninitialised package variable has flags of 0");
90 is($r->SvTYPE(), 0, "uninitialised package variable has type 0");
91 is($r->POK(), 0, "POK false");
92 is($r->ROK(), 0, "ROK false");
93 is($r->MAGICAL(), 0, "MAGICAL false");
94 $::data2 = $pie;
95 isnt($r->FLAGS(), 0, "initialised package variable has nonzero flags");
96 isnt($r->SvTYPE(), 0, "initialised package variable has nonzero type");
97 isnt($r->POK(), 0, "POK true");
98 is($r->ROK(), 0, "ROK false");
99 is($r->MAGICAL(), 0, "MAGICAL false");
100
101 $::data2 = substr $pie, 0, 1;
102 isnt($r->FLAGS(), 0, "initialised package variable has nonzero flags");
103 isnt($r->SvTYPE(), 0, "initialised package variable has nonzero type");
104 isnt($r->POK(), 0, "POK true");
105 is($r->ROK(), 0, "ROK false");
106 is($r->MAGICAL(), 0, "MAGICAL true");
107
108 $::data2 = \$pie;
109 isnt($r->FLAGS(), 0, "initialised package variable has nonzero flags");
110 isnt($r->SvTYPE(), 0, "initialised package variable has nonzero type");
111 is($r->POK(), 0, "POK false");
112 isnt($r->ROK(), 0, "ROK true");
113 is($r->MAGICAL(), 0, "MAGICAL false");
114
115 is($r->REFCNT(), 1, "Reference count is 1");
116 {
117 my $ref = \$::data2;
118 is($r->REFCNT(), 2, "Second reference");
119 }
120 is($r->REFCNT(), 1, "Reference count is 1");
121
122}
123
5c35adbb
NC
124my $r = qr/foo/;
125my $obj = B::svref_2object($r);
126my $regexp = ($] < 5.011) ? $obj->MAGIC : $obj;
127ok($regexp->precomp() eq 'foo', 'Get string from qr//');
128like($regexp->REGEX(), qr/\d+/, "REGEX() returns numeric value");
1f306347 129like($regexp->compflags, qr/^\d+\z/, "compflags returns numeric value");
4a31a506
FC
130is B::svref_2object(qr/(?{time})/)->qr_anoncv->ROOT->first->name, 'qr',
131 'qr_anoncv';
01b509b0
SP
132my $iv = 1;
133my $iv_ref = B::svref_2object(\$iv);
134is(ref $iv_ref, "B::IV", "Test B:IV return from svref_2object");
135is($iv_ref->REFCNT, 1, "Test B::IV->REFCNT");
136# Flag tests are needed still
137#diag $iv_ref->FLAGS();
138my $iv_ret = $iv_ref->object_2svref();
139is(ref $iv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
140is($$iv_ret, $iv, "Test object_2svref()");
141is($iv_ref->int_value, $iv, "Test int_value()");
142is($iv_ref->IV, $iv, "Test IV()");
143is($iv_ref->IVX(), $iv, "Test IVX()");
144is($iv_ref->UVX(), $iv, "Test UVX()");
f046b1bd
NC
145is(eval { $iv_ref->RV() }, undef, 'Test RV() on IV');
146like($@, qr/argument is not SvROK/, 'Test RV() IV');
147$iv = \"Pie";
148my $val = eval { $iv_ref->RV() };
149is(ref $val, 'B::PV', 'Test RV() on a reference');
150is($val->PV(), 'Pie', 'Value expected');
151is($@, '', "Test RV()");
01b509b0
SP
152
153my $pv = "Foo";
154my $pv_ref = B::svref_2object(\$pv);
155is(ref $pv_ref, "B::PV", "Test B::PV return from svref_2object");
156is($pv_ref->REFCNT, 1, "Test B::PV->REFCNT");
157# Flag tests are needed still
158#diag $pv_ref->FLAGS();
159my $pv_ret = $pv_ref->object_2svref();
160is(ref $pv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
161is($$pv_ret, $pv, "Test object_2svref()");
162is($pv_ref->PV(), $pv, "Test PV()");
f046b1bd
NC
163is(eval { $pv_ref->RV() }, undef, 'Test RV() on PV');
164like($@, qr/argument is not SvROK/, 'Test RV() on PV');
01b509b0 165is($pv_ref->PVX(), $pv, "Test PVX()");
f046b1bd
NC
166$pv = \"Pie";
167$val = eval { $pv_ref->RV() };
168is(ref $val, 'B::PV', 'Test RV() on a reference');
169is($val->PV(), 'Pie', 'Value expected');
170is($@, '', "Test RV()");
01b509b0
SP
171
172my $nv = 1.1;
173my $nv_ref = B::svref_2object(\$nv);
174is(ref $nv_ref, "B::NV", "Test B::NV return from svref_2object");
175is($nv_ref->REFCNT, 1, "Test B::NV->REFCNT");
176# Flag tests are needed still
177#diag $nv_ref->FLAGS();
178my $nv_ret = $nv_ref->object_2svref();
179is(ref $nv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
180is($$nv_ret, $nv, "Test object_2svref()");
181is($nv_ref->NV, $nv, "Test NV()");
182is($nv_ref->NVX(), $nv, "Test NVX()");
f046b1bd
NC
183is(eval { $nv_ref->RV() }, undef, 'Test RV() on NV');
184like($@, qr/Can't locate object method "RV" via package "B::NV"/,
185 'Test RV() on NV');
01b509b0
SP
186
187my $null = undef;
188my $null_ref = B::svref_2object(\$null);
189is(ref $null_ref, "B::NULL", "Test B::NULL return from svref_2object");
190is($null_ref->REFCNT, 1, "Test B::NULL->REFCNT");
191# Flag tests are needed still
192#diag $null_ref->FLAGS();
193my $null_ret = $nv_ref->object_2svref();
194is(ref $null_ret, "SCALAR", "Test object_2svref() return is SCALAR");
195is($$null_ret, $nv, "Test object_2svref()");
196
4df7f6af 197my $RV_class = $] >= 5.011 ? 'B::IV' : 'B::RV';
01b509b0
SP
198my $cv = sub{ 1; };
199my $cv_ref = B::svref_2object(\$cv);
4df7f6af
NC
200is($cv_ref->REFCNT, 1, "Test $RV_class->REFCNT");
201is(ref $cv_ref, "$RV_class",
202 "Test $RV_class return from svref_2object - code");
01b509b0
SP
203my $cv_ret = $cv_ref->object_2svref();
204is(ref $cv_ret, "REF", "Test object_2svref() return is REF");
205is($$cv_ret, $cv, "Test object_2svref()");
206
207my $av = [];
208my $av_ref = B::svref_2object(\$av);
4df7f6af
NC
209is(ref $av_ref, "$RV_class",
210 "Test $RV_class return from svref_2object - array");
01b509b0
SP
211
212my $hv = [];
213my $hv_ref = B::svref_2object(\$hv);
4df7f6af
NC
214is(ref $hv_ref, "$RV_class",
215 "Test $RV_class return from svref_2object - hash");
01b509b0
SP
216
217local *gv = *STDOUT;
218my $gv_ref = B::svref_2object(\*gv);
219is(ref $gv_ref, "B::GV", "Test B::GV return from svref_2object");
220ok(! $gv_ref->is_empty(), "Test is_empty()");
711fbbf0 221ok($gv_ref->isGV_with_GP(), "Test isGV_with_GP()");
01b509b0
SP
222is($gv_ref->NAME(), "gv", "Test NAME()");
223is($gv_ref->SAFENAME(), "gv", "Test SAFENAME()");
224like($gv_ref->FILE(), qr/b\.t$/, "Testing FILE()");
de64752d
NC
225is($gv_ref->SvTYPE(), B::SVt_PVGV, "Test SvTYPE()");
226is($gv_ref->FLAGS() & B::SVTYPEMASK, B::SVt_PVGV, "Test SVTYPEMASK");
2da668d2
SP
227
228# The following return B::SPECIALs.
229is(ref B::sv_yes(), "B::SPECIAL", "B::sv_yes()");
230is(ref B::sv_no(), "B::SPECIAL", "B::sv_no()");
231is(ref B::sv_undef(), "B::SPECIAL", "B::sv_undef()");
a462fa00
DD
232SKIP: {
233 skip('no fork', 1)
234 unless ($Config::Config{d_fork} or $Config::Config{d_pseudofork});
235 my $pid;
bdbabc4e 236 pipe my $r, my $w or die "Can't pipe: $!";;
a462fa00 237 if ($pid = fork) {
bdbabc4e
JK
238 close $w;
239 my $type = <$r>;
240 close $r;
a462fa00 241 waitpid($pid,0);
0d16269d 242 is($type, "B::SPECIAL", "special SV table works after pseudofork");
a462fa00
DD
243 }
244 else {
bdbabc4e
JK
245 close $r;
246 $|++;
247 print $w ref B::svref_2object(\(!!0));
248 close $w;
a462fa00
DD
249 exit;
250 }
251}
2da668d2
SP
252
253# More utility functions
254is(B::ppname(0), "pp_null", "Testing ppname (this might break if opnames.h is changed)");
255is(B::opnumber("null"), 0, "Testing opnumber with opname (null)");
256is(B::opnumber("pp_null"), 0, "Testing opnumber with opname (pp_null)");
b45732d6
NC
257{
258 my $hash = B::hash("wibble");
259 like($hash, qr/\A0x[0-9a-f]+\z/, "Testing B::hash(\"wibble\")");
260 unlike($hash, qr/\A0x0+\z/, "Testing B::hash(\"wibble\")");
261
bb1ca2d4
YO
262 SKIP: {
263 skip "Nulls don't hash to the same bucket regardless of length with this PERL_HASH implementation", 20
264 if B::hash("") ne B::hash("\0" x 19);
265 like(B::hash("\0" x $_), qr/\A0x0+\z/, "Testing B::hash(\"0\" x $_)")
266 for 0..19;
267 }
8c5b7c71
NC
268
269 $hash = eval {B::hash(chr 256)};
270 is($hash, undef, "B::hash() refuses non-octets");
271 like($@, qr/^Wide character in subroutine entry/);
272
273 $hash = B::hash(chr 163);
274 my $str = chr(163) . chr 256;
275 chop $str;
276 is(B::hash($str), $hash, 'B::hash() with chr 128-256 is well-behaved');
b45732d6 277}
f9a20969
NC
278{
279 is(B::cstring(undef), '0', "Testing B::cstring(undef)");
280 is(B::perlstring(undef), '0', "Testing B::perlstring(undef)");
281
282 my @common = map {eval $_, $_}
574fde55
NC
283 '"wibble"', '"\""', '"\'"', '"\\\\"', '"\\n\\r\\t\\b\\a\\f"', '"\000"',
284 '"\000\000"', '"\000Bing\000"', ord 'N' == 78 ? '"\\177"' : ();
f9a20969
NC
285
286 my $oct = sprintf "\\%03o", ord '?';
287 my @tests = (@common, '$_', '"$_"', '@_', '"@_"', '??N', qq{"$oct?N"},
288 ord 'N' == 78 ? (chr 11, '"\v"'): ());
289 while (my ($test, $expect) = splice @tests, 0, 2) {
290 is(B::cstring($test), $expect, "B::cstring($expect)");
291 }
292
293 @tests = (@common, '$_', '"\$_"', '@_', '"\@_"', '??N', '"??N"',
294 chr 256, '"\x{100}"', chr 65536, '"\x{10000}"',
295 ord 'N' == 78 ? (chr 11, '"\013"'): ());
296 while (my ($test, $expect) = splice @tests, 0, 2) {
297 is(B::perlstring($test), $expect, "B::perlstring($expect)");
298 utf8::upgrade $test;
4215ab17 299 $expect =~ s/\\b/sprintf("\\x{%x}", utf8::unicode_to_native(8))/eg;
f9a20969
NC
300 $expect =~ s/\\([0-7]{3})/sprintf "\\x\{%x\}", oct $1/eg;
301 is(B::perlstring($test), $expect, "B::perlstring($expect) (Unicode)");
302 }
303}
01c3a485 304{
574fde55
NC
305 my @tests = ((map {eval(qq{"$_"}), $_} '\\n', '\\r', '\\t',
306 '\\b', '\\a', '\\f', '\\000', '\\\'', '?'), '"', '"',
307 ord 'N' == 78 ? (chr 11, '\v', "\177", '\\177') : ());
01c3a485
NC
308
309 while (my ($test, $expect) = splice @tests, 0, 2) {
310 is(B::cchar($test), "'${expect}'", "B::cchar(qq{$expect})");
311 }
312}
313
2da668d2
SP
314is(B::class(bless {}, "Wibble::Bibble"), "Bibble", "Testing B::class()");
315is(B::cast_I32(3.14), 3, "Testing B::cast_I32()");
5012eebe 316is(B::opnumber("chop"), 38, "Testing opnumber with opname (chop)");
5ce57cc0
JJ
317
318{
319 no warnings 'once';
320 my $sg = B::sub_generation();
e1a479c5 321 *UNIVERSAL::hand_waving = sub { };
5ce57cc0
JJ
322 ok( $sg < B::sub_generation, "sub_generation increments" );
323}
324
3aaeec97 325like( B::amagic_generation, qr/^\d+\z/, "amagic_generation" );
f9a20969 326
fdbd1d64
NC
327is(B::svref_2object(sub {})->ROOT->ppaddr, 'PL_ppaddr[OP_LEAVESUB]',
328 'OP->ppaddr');
329
512ba29b
FC
330# This one crashes from perl 5.8.9 to B 1.24 (perl 5.13.6):
331B::svref_2object(sub{y/\x{100}//})->ROOT->first->first->sibling->sv;
332ok 1, 'B knows that UTF trans is a padop in 5.8.9, not an svop';
333
b852bf25 334{
619dadb5
FC
335 my $o = B::svref_2object(sub{0;0})->ROOT->first->first;
336 # Make sure we are testing what we think we are testing. If these two
337 # fail, tweak the test to find a nulled cop a different way.
338 is $o->name, "null", 'first op of sub{0;0} is a null';
339 is B::ppname($o->targ),'pp_nextstate','first op of sub{0;0} was a cop';
340 # Test its class
341 is B::class($o), "COP", 'nulled cops are of class COP';
342}
343
344{
b852bf25
FR
345 format FOO =
346foo
347.
348 my $f = B::svref_2object(*FOO{FORMAT});
349 isa_ok $f, 'B::FM';
350 can_ok $f, 'LINES';
351}
352
d9cd2aeb
FC
353is B::safename("\cLAST_FH"), "^LAST_FH", 'basic safename test';
354
a60c099b 355my $sub1 = sub {die};
99225839
FC
356{ no warnings 'once'; no strict; *Peel:: = *{"Pe\0e\x{142}::"} }
357my $sub2 = eval 'package Peel; sub {die}';
a60c099b 358my $cop = B::svref_2object($sub1)->ROOT->first->first;
99225839 359my $bobby = B::svref_2object($sub2)->ROOT->first->first;
a60c099b
FC
360is $cop->stash->object_2svref, \%main::, 'COP->stash';
361is $cop->stashpv, 'main', 'COP->stashpv';
82aeefe1
DM
362
363SKIP: {
364 skip "no nulls in packages before 5.17", 1 if $] < 5.017;
365 is $bobby->stashpv, "Pe\0e\x{142}", 'COP->stashpv with utf8 and nulls';
366}
367
368SKIP: {
369 skip "no stashoff", 2 if $] < 5.017 || !$Config::Config{useithreads};
99225839
FC
370 like $cop->stashoff, qr/^[1-9]\d*\z/a, 'COP->stashoff';
371 isnt $cop->stashoff, $bobby->stashoff,
372 'different COP->stashoff for different stashes';
a60c099b
FC
373}
374
429ba3b2
FC
375my $pmop = B::svref_2object(sub{ qr/fit/ })->ROOT->first->first->sibling;
376$regexp = $pmop->pmregexp;
377is B::class($regexp), 'REGEXP', 'B::PMOP::pmregexp returns a regexp';
378is $regexp->precomp, 'fit', 'pmregexp returns the right regexp';
379
71324a3b
DM
380
381# Test $B::overlay
382{
383 my $methods = {
384 BINOP => [ qw(last) ],
385 COP => [ qw(arybase cop_seq file filegv hints hints_hash io
386 label line stash stashpv
387 stashoff warnings) ],
388 LISTOP => [ qw(children) ],
389 LOGOP => [ qw(other) ],
390 LOOP => [ qw(lastop nextop redoop) ],
391 OP => [ qw(desc flags name next opt ppaddr private sibling
392 size spare targ type) ],
393 PADOP => [ qw(gv padix sv) ],
394 PMOP => [ qw(code_list pmflags pmoffset pmreplroot pmreplstart pmstash pmstashpv precomp reflags) ],
395 PVOP => [ qw(pv) ],
396 SVOP => [ qw(gv sv) ],
397 UNOP => [ qw(first) ],
398 };
399
400 my $overlay = {};
401 my $op = B::svref_2object(sub { my $x = 1 })->ROOT;
402
403 for my $class (sort keys %$methods) {
404 for my $meth (@{$methods->{$class}}) {
405 my $full = "B::${class}::$meth";
406 die "Duplicate method '$full'\n"
407 if grep $_ eq $full, @{$overlay->{$meth}};
408 push @{$overlay->{$meth}}, "B::${class}::$meth";
409 }
410 }
411
412 {
413 local $B::overlay; # suppress 'used once' warning
414 local $B::overlay = { $$op => $overlay };
415
416 for my $class (sort keys %$methods) {
417 bless $op, "B::$class"; # naughty
418 for my $meth (@{$methods->{$class}}) {
419 if ($op->can($meth)) {
420 my $list = $op->$meth;
421 ok(defined $list
422 && ref($list) eq "ARRAY"
423 && grep($_ eq "B::${class}::$meth", @$list),
424 "overlay: B::$class $meth");
425 }
426 else {
427 pass("overlay: B::$class $meth (skipped; no method)");
428 }
429 }
430 }
431 }
432 # B::overlay should be disabled again here
433 is($op->name, "leavesub", "overlay: orig name");
434}
435
486b1e7f
TC
436{ # [perl #118525]
437 {
438 sub foo {}
439 my $cv = B::svref_2object(\&foo);
440 ok($cv, "make a B::CV from a non-anon sub reference");
441 isa_ok($cv, "B::CV");
442 my $gv = $cv->GV;
443 ok($gv, "we get a GV from a GV on a normal sub");
444 isa_ok($gv, "B::GV");
445 is($gv->NAME, "foo", "check the GV name");
446 SKIP:
447 { # do we need these version checks?
448 skip "no HEK before 5.18", 1 if $] < 5.018;
449 is($cv->NAME_HEK, undef, "no hek for a global sub");
450 }
451 }
452
453SKIP:
454 {
455 skip "no HEK before 5.18", 4 if $] < 5.018;
456 eval <<'EOS'
457 {
458 use feature 'lexical_subs';
459 no warnings 'experimental::lexical_subs';
460 my sub bar {};
461 my $cv = B::svref_2object(\&bar);
462 ok($cv, "make a B::CV from a lexical sub reference");
463 isa_ok($cv, "B::CV");
486b1e7f
TC
464 my $hek = $cv->NAME_HEK;
465 is($hek, "bar", "check the NAME_HEK");
ae77754a
FC
466 my $gv = $cv->GV;
467 isa_ok($gv, "B::GV", "GV on a lexical sub");
486b1e7f
TC
468 }
469 1;
470EOS
471 or die "lexical_subs test failed to compile: $@";
472 }
473}
474
cc54be84 475{ # [perl #120535]
cc54be84
TC
476 my %h = ( "\x{100}" => 1 );
477 my $b = B::svref_2object(\%h);
478 my ($k, $v) = $b->ARRAY;
479 is($k, "\x{100}", "check utf8 preserved by B::HV::ARRAY");
480}
481
29e61fd9
DM
482# test op_parent
483
484SKIP: {
1fafe688 485 unless ($B::OP::does_parent) {
29e61fd9
DM
486 skip "op_parent only present with -DPERL_OP_PARENT builds", 6;
487 }
488 my $lineseq = B::svref_2object(sub{my $x = 1})->ROOT->first;
489 is ($lineseq->type, B::opnumber('lineseq'),
490 'op_parent: top op is lineseq');
491 my $first = $lineseq->first;
492 my $second = $first->sibling;
493 is(ref $second->sibling, "B::NULL", 'op_parent: second sibling is null');
87b5a8b9
DM
494 is($first->moresib, 1 , 'op_parent: first sibling: moresib');
495 is($second->moresib, 0, 'op_parent: second sibling: !moresib');
29e61fd9
DM
496 is($$lineseq, ${$first->parent}, 'op_parent: first sibling okay');
497 is($$lineseq, ${$second->parent}, 'op_parent: second sibling okay');
498}
499
500
3a23d767
DM
501# make sure ->sv, -gv methods do the right thing on threaded builds
502{
503
504 # for some reason B::walkoptree only likes a sub name, not a code ref
505 my ($gv, $sv);
506 sub gvsv_const {
507 # make the early pad slots something unlike a threaded const or
508 # gvsv
509 my ($dummy1, $dummy2, $dummy3, $dummy4) = qw(foo1 foo2 foo3 foo4);
510 my $self = shift;
511 if ($self->name eq 'gvsv') {
512 $gv = $self->gv;
513 }
514 elsif ($self->name eq 'const') {
515 $sv = $self->sv;
516 }
517 };
518
519 B::walkoptree(B::svref_2object(sub {our $x = 1})->ROOT, "::gvsv_const");
520 ok(defined $gv, "gvsv->gv seen");
521 ok(defined $sv, "const->sv seen");
522 if ($Config::Config{useithreads}) {
523 # should get NULLs
524 is(ref($gv), "B::SPECIAL", "gvsv->gv is special");
525 is(ref($sv), "B::SPECIAL", "const->sv is special");
526 is($$gv, 0, "gvsv->gv special is 0 (NULL)");
527 is($$sv, 0, "const->sv special is 0 (NULL)");
528 }
529 else {
530 is(ref($gv), "B::GV", "gvsv->gv is GV");
531 is(ref($sv), "B::IV", "const->sv is IV");
532 pass();
533 pass();
534 }
535
536}
537
538
1d7705d5
FC
539# Some pad tests
540{
541 my $sub = sub { my main $a; CORE::state @b; our %c };
542 my $padlist = B::svref_2object($sub)->PADLIST;
543 is $padlist->MAX, 1, 'padlist MAX';
544 my @array = $padlist->ARRAY;
545 is @array, 2, 'two items from padlist ARRAY';
546 is ${$padlist->ARRAYelt(0)}, ${$array[0]},
547 'ARRAYelt(0) is first item from ARRAY';
548 is ${$padlist->ARRAYelt(1)}, ${$array[1]},
549 'ARRAYelt(1) is second item from ARRAY';
550 is ${$padlist->NAMES}, ${$array[0]},
551 'NAMES is first item from ARRAY';
552 my @names = $array[0]->ARRAY;
553 cmp_ok @names, ">=", 4, 'at least 4 pad names';
554 is join(" ", map($_->PV//"undef",@names[0..3])), 'undef $a @b %c',
555 'pad name PVs';
556
557 my @closures;
558 for (1,2) { push @closures, sub { sub { @closures } } }
559 my $sub1 = B::svref_2object($closures[0]);
560 my $sub2 = B::svref_2object($closures[1]);
561 is $sub2->PADLIST->id, $sub1->PADLIST->id, 'padlist id';
562 $sub1 = B::svref_2object(my $lr = $closures[0]());
563 $sub2 = B::svref_2object(my $lr2= $closures[1]());
564 is $sub2->PADLIST->outid, $sub1->PADLIST->outid, 'padlist outid';
565}
566
567
f9a20969 568done_testing();