6 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7 print "1..0 # Skip -- Perl configured without B module\n";
17 BEGIN { use_ok( 'B' ); }
20 package Testing::Symtable;
21 use vars qw($This @That %wibble $moo %moo);
22 my $not_a_sym = 'moo';
28 package Testing::Symtable::Foo;
31 package Testing::Symtable::Bar;
37 B::walksymtable(\%Testing::Symtable::, 'find_syms', sub { $_[0] =~ /Foo/ },
38 'Testing::Symtable::');
40 sub B::GV::find_syms {
43 $main::Subs{$symbol->STASH->NAME . '::' . $symbol->NAME}++;
46 my @syms = map { 'Testing::Symtable::'.$_ } qw(This That wibble moo car
48 push @syms, "Testing::Symtable::Foo::yarrow";
50 # Make sure we hit all the expected symbols.
51 ok( join('', sort @syms) eq join('', sort keys %Subs), 'all symbols found' );
53 # Make sure we only hit them each once.
54 ok( (!grep $_ != 1, values %Subs), '...and found once' );
56 # Tests for MAGIC / MOREMAGIC
57 ok( B::svref_2object(\$.)->MAGIC->TYPE eq "\0", '$. has \0 magic' );
60 local $SIG{__DIE__} = sub { $e = $_[0] };
61 # Used to dump core, bug #16828
62 eval { B::svref_2object(\$.)->MAGIC->MOREMAGIC->TYPE; };
63 like( $e, qr/Can't call method "TYPE" on an undefined value/,
64 '$. has no more magic' );
69 # This needs to be a package variable, as vars in the pad have some flags.
70 my $r = B::svref_2object(\$::data2);
71 is($r->FLAGS(), 0, "uninitialised package variable has flags of 0");
72 is($r->SvTYPE(), 0, "uninitialised package variable has type 0");
73 is($r->POK(), 0, "POK false");
74 is($r->ROK(), 0, "ROK false");
75 is($r->MAGICAL(), 0, "MAGICAL false");
77 isnt($r->FLAGS(), 0, "initialised package variable has nonzero flags");
78 isnt($r->SvTYPE(), 0, "initialised package variable has nonzero type");
79 isnt($r->POK(), 0, "POK true");
80 is($r->ROK(), 0, "ROK false");
81 is($r->MAGICAL(), 0, "MAGICAL false");
83 $::data2 = substr $pie, 0, 1;
84 isnt($r->FLAGS(), 0, "initialised package variable has nonzero flags");
85 isnt($r->SvTYPE(), 0, "initialised package variable has nonzero type");
86 isnt($r->POK(), 0, "POK true");
87 is($r->ROK(), 0, "ROK false");
88 is($r->MAGICAL(), 0, "MAGICAL true");
91 isnt($r->FLAGS(), 0, "initialised package variable has nonzero flags");
92 isnt($r->SvTYPE(), 0, "initialised package variable has nonzero type");
93 is($r->POK(), 0, "POK false");
94 isnt($r->ROK(), 0, "ROK true");
95 is($r->MAGICAL(), 0, "MAGICAL false");
97 is($r->REFCNT(), 1, "Reference count is 1");
100 is($r->REFCNT(), 2, "Second reference");
102 is($r->REFCNT(), 1, "Reference count is 1");
107 my $obj = B::svref_2object($r);
108 my $regexp = ($] < 5.011) ? $obj->MAGIC : $obj;
109 ok($regexp->precomp() eq 'foo', 'Get string from qr//');
110 like($regexp->REGEX(), qr/\d+/, "REGEX() returns numeric value");
112 my $iv_ref = B::svref_2object(\$iv);
113 is(ref $iv_ref, "B::IV", "Test B:IV return from svref_2object");
114 is($iv_ref->REFCNT, 1, "Test B::IV->REFCNT");
115 # Flag tests are needed still
116 #diag $iv_ref->FLAGS();
117 my $iv_ret = $iv_ref->object_2svref();
118 is(ref $iv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
119 is($$iv_ret, $iv, "Test object_2svref()");
120 is($iv_ref->int_value, $iv, "Test int_value()");
121 is($iv_ref->IV, $iv, "Test IV()");
122 is($iv_ref->IVX(), $iv, "Test IVX()");
123 is($iv_ref->UVX(), $iv, "Test UVX()");
124 is(eval { $iv_ref->RV() }, undef, 'Test RV() on IV');
125 like($@, qr/argument is not SvROK/, 'Test RV() IV');
127 my $val = eval { $iv_ref->RV() };
128 is(ref $val, 'B::PV', 'Test RV() on a reference');
129 is($val->PV(), 'Pie', 'Value expected');
130 is($@, '', "Test RV()");
133 my $pv_ref = B::svref_2object(\$pv);
134 is(ref $pv_ref, "B::PV", "Test B::PV return from svref_2object");
135 is($pv_ref->REFCNT, 1, "Test B::PV->REFCNT");
136 # Flag tests are needed still
137 #diag $pv_ref->FLAGS();
138 my $pv_ret = $pv_ref->object_2svref();
139 is(ref $pv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
140 is($$pv_ret, $pv, "Test object_2svref()");
141 is($pv_ref->PV(), $pv, "Test PV()");
142 is(eval { $pv_ref->RV() }, undef, 'Test RV() on PV');
143 like($@, qr/argument is not SvROK/, 'Test RV() on PV');
144 is($pv_ref->PVX(), $pv, "Test PVX()");
146 $val = eval { $pv_ref->RV() };
147 is(ref $val, 'B::PV', 'Test RV() on a reference');
148 is($val->PV(), 'Pie', 'Value expected');
149 is($@, '', "Test RV()");
152 my $nv_ref = B::svref_2object(\$nv);
153 is(ref $nv_ref, "B::NV", "Test B::NV return from svref_2object");
154 is($nv_ref->REFCNT, 1, "Test B::NV->REFCNT");
155 # Flag tests are needed still
156 #diag $nv_ref->FLAGS();
157 my $nv_ret = $nv_ref->object_2svref();
158 is(ref $nv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
159 is($$nv_ret, $nv, "Test object_2svref()");
160 is($nv_ref->NV, $nv, "Test NV()");
161 is($nv_ref->NVX(), $nv, "Test NVX()");
162 is(eval { $nv_ref->RV() }, undef, 'Test RV() on NV');
163 like($@, qr/Can't locate object method "RV" via package "B::NV"/,
167 my $null_ref = B::svref_2object(\$null);
168 is(ref $null_ref, "B::NULL", "Test B::NULL return from svref_2object");
169 is($null_ref->REFCNT, 1, "Test B::NULL->REFCNT");
170 # Flag tests are needed still
171 #diag $null_ref->FLAGS();
172 my $null_ret = $nv_ref->object_2svref();
173 is(ref $null_ret, "SCALAR", "Test object_2svref() return is SCALAR");
174 is($$null_ret, $nv, "Test object_2svref()");
176 my $RV_class = $] >= 5.011 ? 'B::IV' : 'B::RV';
178 my $cv_ref = B::svref_2object(\$cv);
179 is($cv_ref->REFCNT, 1, "Test $RV_class->REFCNT");
180 is(ref $cv_ref, "$RV_class",
181 "Test $RV_class return from svref_2object - code");
182 my $cv_ret = $cv_ref->object_2svref();
183 is(ref $cv_ret, "REF", "Test object_2svref() return is REF");
184 is($$cv_ret, $cv, "Test object_2svref()");
187 my $av_ref = B::svref_2object(\$av);
188 is(ref $av_ref, "$RV_class",
189 "Test $RV_class return from svref_2object - array");
192 my $hv_ref = B::svref_2object(\$hv);
193 is(ref $hv_ref, "$RV_class",
194 "Test $RV_class return from svref_2object - hash");
197 my $gv_ref = B::svref_2object(\*gv);
198 is(ref $gv_ref, "B::GV", "Test B::GV return from svref_2object");
199 ok(! $gv_ref->is_empty(), "Test is_empty()");
200 ok($gv_ref->isGV_with_GP(), "Test isGV_with_GP()");
201 is($gv_ref->NAME(), "gv", "Test NAME()");
202 is($gv_ref->SAFENAME(), "gv", "Test SAFENAME()");
203 like($gv_ref->FILE(), qr/b\.t$/, "Testing FILE()");
204 is($gv_ref->SvTYPE(), B::SVt_PVGV, "Test SvTYPE()");
205 is($gv_ref->FLAGS() & B::SVTYPEMASK, B::SVt_PVGV, "Test SVTYPEMASK");
207 # The following return B::SPECIALs.
208 is(ref B::sv_yes(), "B::SPECIAL", "B::sv_yes()");
209 is(ref B::sv_no(), "B::SPECIAL", "B::sv_no()");
210 is(ref B::sv_undef(), "B::SPECIAL", "B::sv_undef()");
212 # More utility functions
213 is(B::ppname(0), "pp_null", "Testing ppname (this might break if opnames.h is changed)");
214 is(B::opnumber("null"), 0, "Testing opnumber with opname (null)");
215 is(B::opnumber("pp_null"), 0, "Testing opnumber with opname (pp_null)");
217 my $hash = B::hash("wibble");
218 like($hash, qr/\A0x[0-9a-f]+\z/, "Testing B::hash(\"wibble\")");
219 unlike($hash, qr/\A0x0+\z/, "Testing B::hash(\"wibble\")");
222 skip "Nulls don't hash to the same bucket regardless of length with this PERL_HASH implementation", 20
223 if B::hash("") ne B::hash("\0" x 19);
224 like(B::hash("\0" x $_), qr/\A0x0+\z/, "Testing B::hash(\"0\" x $_)")
228 $hash = eval {B::hash(chr 256)};
229 is($hash, undef, "B::hash() refuses non-octets");
230 like($@, qr/^Wide character in subroutine entry/);
232 $hash = B::hash(chr 163);
233 my $str = chr(163) . chr 256;
235 is(B::hash($str), $hash, 'B::hash() with chr 128-256 is well-behaved');
238 is(B::cstring(undef), '0', "Testing B::cstring(undef)");
239 is(B::perlstring(undef), '0', "Testing B::perlstring(undef)");
241 my @common = map {eval $_, $_}
242 '"wibble"', '"\""', '"\'"', '"\\\\"', '"\\n\\r\\t\\b\\a\\f"', '"\000"',
243 '"\000\000"', '"\000Bing\000"', ord 'N' == 78 ? '"\\177"' : ();
245 my $oct = sprintf "\\%03o", ord '?';
246 my @tests = (@common, '$_', '"$_"', '@_', '"@_"', '??N', qq{"$oct?N"},
247 ord 'N' == 78 ? (chr 11, '"\v"'): ());
248 while (my ($test, $expect) = splice @tests, 0, 2) {
249 is(B::cstring($test), $expect, "B::cstring($expect)");
252 @tests = (@common, '$_', '"\$_"', '@_', '"\@_"', '??N', '"??N"',
253 chr 256, '"\x{100}"', chr 65536, '"\x{10000}"',
254 ord 'N' == 78 ? (chr 11, '"\013"'): ());
255 while (my ($test, $expect) = splice @tests, 0, 2) {
256 is(B::perlstring($test), $expect, "B::perlstring($expect)");
258 $expect =~ s/\\b/\\x\{8\}/g;
259 $expect =~ s/\\([0-7]{3})/sprintf "\\x\{%x\}", oct $1/eg;
260 is(B::perlstring($test), $expect, "B::perlstring($expect) (Unicode)");
264 my @tests = ((map {eval(qq{"$_"}), $_} '\\n', '\\r', '\\t',
265 '\\b', '\\a', '\\f', '\\000', '\\\'', '?'), '"', '"',
266 ord 'N' == 78 ? (chr 11, '\v', "\177", '\\177') : ());
268 while (my ($test, $expect) = splice @tests, 0, 2) {
269 is(B::cchar($test), "'${expect}'", "B::cchar(qq{$expect})");
273 is(B::class(bless {}, "Wibble::Bibble"), "Bibble", "Testing B::class()");
274 is(B::cast_I32(3.14), 3, "Testing B::cast_I32()");
275 is(B::opnumber("chop"), $] >= 5.015 ? 39 : 38,
276 "Testing opnumber with opname (chop)");
280 my $sg = B::sub_generation();
281 *UNIVERSAL::hand_waving = sub { };
282 ok( $sg < B::sub_generation, "sub_generation increments" );
285 like( B::amagic_generation, qr/^\d+\z/, "amagic_generation" );
287 is(B::svref_2object(sub {})->ROOT->ppaddr, 'PL_ppaddr[OP_LEAVESUB]',
290 # This one crashes from perl 5.8.9 to B 1.24 (perl 5.13.6):
291 B::svref_2object(sub{y/\x{100}//})->ROOT->first->first->sibling->sv;
292 ok 1, 'B knows that UTF trans is a padop in 5.8.9, not an svop';
298 my $f = B::svref_2object(*FOO{FORMAT});
303 is B::safename("\cLAST_FH"), "^LAST_FH", 'basic safename test';
305 my $sub1 = sub {die};
306 { no warnings 'once'; no strict; *Peel:: = *{"Pe\0e\x{142}::"} }
307 my $sub2 = eval 'package Peel; sub {die}';
308 my $cop = B::svref_2object($sub1)->ROOT->first->first;
309 my $bobby = B::svref_2object($sub2)->ROOT->first->first;
310 is $cop->stash->object_2svref, \%main::, 'COP->stash';
311 is $cop->stashpv, 'main', 'COP->stashpv';
314 skip "no nulls in packages before 5.17", 1 if $] < 5.017;
315 is $bobby->stashpv, "Pe\0e\x{142}", 'COP->stashpv with utf8 and nulls';
319 skip "no stashoff", 2 if $] < 5.017 || !$Config::Config{useithreads};
320 like $cop->stashoff, qr/^[1-9]\d*\z/a, 'COP->stashoff';
321 isnt $cop->stashoff, $bobby->stashoff,
322 'different COP->stashoff for different stashes';
329 BINOP => [ qw(last) ],
330 COP => [ qw(arybase cop_seq file filegv hints hints_hash io
331 label line stash stashpv
332 stashoff warnings) ],
333 LISTOP => [ qw(children) ],
334 LOGOP => [ qw(other) ],
335 LOOP => [ qw(lastop nextop redoop) ],
336 OP => [ qw(desc flags name next opt ppaddr private sibling
337 size spare targ type) ],
338 PADOP => [ qw(gv padix sv) ],
339 PMOP => [ qw(code_list pmflags pmoffset pmreplroot pmreplstart pmstash pmstashpv precomp reflags) ],
341 SVOP => [ qw(gv sv) ],
342 UNOP => [ qw(first) ],
346 my $op = B::svref_2object(sub { my $x = 1 })->ROOT;
348 for my $class (sort keys %$methods) {
349 for my $meth (@{$methods->{$class}}) {
350 my $full = "B::${class}::$meth";
351 die "Duplicate method '$full'\n"
352 if grep $_ eq $full, @{$overlay->{$meth}};
353 push @{$overlay->{$meth}}, "B::${class}::$meth";
358 local $B::overlay; # suppress 'used once' warning
359 local $B::overlay = { $$op => $overlay };
361 for my $class (sort keys %$methods) {
362 bless $op, "B::$class"; # naughty
363 for my $meth (@{$methods->{$class}}) {
364 if ($op->can($meth)) {
365 my $list = $op->$meth;
367 && ref($list) eq "ARRAY"
368 && grep($_ eq "B::${class}::$meth", @$list),
369 "overlay: B::$class $meth");
372 pass("overlay: B::$class $meth (skipped; no method)");
377 # B::overlay should be disabled again here
378 is($op->name, "leavesub", "overlay: orig name");
384 my $cv = B::svref_2object(\&foo);
385 ok($cv, "make a B::CV from a non-anon sub reference");
386 isa_ok($cv, "B::CV");
388 ok($gv, "we get a GV from a GV on a normal sub");
389 isa_ok($gv, "B::GV");
390 is($gv->NAME, "foo", "check the GV name");
392 { # do we need these version checks?
393 skip "no HEK before 5.18", 1 if $] < 5.018;
394 is($cv->NAME_HEK, undef, "no hek for a global sub");
400 skip "no HEK before 5.18", 4 if $] < 5.018;
403 use feature 'lexical_subs';
404 no warnings 'experimental::lexical_subs';
406 my $cv = B::svref_2object(\&bar);
407 ok($cv, "make a B::CV from a lexical sub reference");
408 isa_ok($cv, "B::CV");
409 my $hek = $cv->NAME_HEK;
410 is($hek, "bar", "check the NAME_HEK");
412 isa_ok($gv, "B::GV", "GV on a lexical sub");
416 or die "lexical_subs test failed to compile: $@";
421 my %h = ( "\x{100}" => 1 );
422 my $b = B::svref_2object(\%h);
423 my ($k, $v) = $b->ARRAY;
424 is($k, "\x{100}", "check utf8 preserved by B::HV::ARRAY");
430 unless ($Config::Config{ccflags} =~ /PERL_OP_PARENT/) {
431 skip "op_parent only present with -DPERL_OP_PARENT builds", 6;
433 my $lineseq = B::svref_2object(sub{my $x = 1})->ROOT->first;
434 is ($lineseq->type, B::opnumber('lineseq'),
435 'op_parent: top op is lineseq');
436 my $first = $lineseq->first;
437 my $second = $first->sibling;
438 is(ref $second->sibling, "B::NULL", 'op_parent: second sibling is null');
439 is($first->lastsib, 0 , 'op_parent: first sibling: !lastsib');
440 is($second->lastsib, 1, 'op_parent: second sibling: lastsib');
441 is($$lineseq, ${$first->parent}, 'op_parent: first sibling okay');
442 is($$lineseq, ${$second->parent}, 'op_parent: second sibling okay');