This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove extraneous semicolons from Deparse output
[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;
f9a20969 15use Test::More;
ccc418af 16
c5f0f3aa 17BEGIN { use_ok( 'B' ); }
ccc418af 18
08c6f5ec 19
87a42246
MS
20package Testing::Symtable;
21use vars qw($This @That %wibble $moo %moo);
22my $not_a_sym = 'moo';
ccc418af 23
87a42246
MS
24sub moo { 42 }
25sub car { 23 }
ccc418af 26
f70490b9 27
87a42246
MS
28package Testing::Symtable::Foo;
29sub yarrow { "Hock" }
f70490b9 30
87a42246
MS
31package Testing::Symtable::Bar;
32sub hock { "yarrow" }
9b86dfa2 33
87a42246
MS
34package main;
35use vars qw(%Subs);
36local %Subs = ();
37B::walksymtable(\%Testing::Symtable::, 'find_syms', sub { $_[0] =~ /Foo/ },
38 'Testing::Symtable::');
ccc418af 39
87a42246
MS
40sub B::GV::find_syms {
41 my($symbol) = @_;
de3f1649 42
87a42246 43 $main::Subs{$symbol->STASH->NAME . '::' . $symbol->NAME}++;
cfe9256d 44}
ccc418af 45
87a42246
MS
46my @syms = map { 'Testing::Symtable::'.$_ } qw(This That wibble moo car
47 BEGIN);
48push @syms, "Testing::Symtable::Foo::yarrow";
ccc418af 49
87a42246 50# Make sure we hit all the expected symbols.
c5f0f3aa 51ok( join('', sort @syms) eq join('', sort keys %Subs), 'all symbols found' );
1e1dbab6 52
87a42246 53# Make sure we only hit them each once.
c5f0f3aa
RGS
54ok( (!grep $_ != 1, values %Subs), '...and found once' );
55
56# Tests for MAGIC / MOREMAGIC
57ok( B::svref_2object(\$.)->MAGIC->TYPE eq "\0", '$. has \0 magic' );
58{
59 my $e = '';
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' );
65}
01b509b0 66
de64752d
NC
67{
68 my $pie = 'Good';
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");
76 $::data2 = $pie;
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");
82
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");
89
90 $::data2 = \$pie;
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");
96
97 is($r->REFCNT(), 1, "Reference count is 1");
98 {
99 my $ref = \$::data2;
100 is($r->REFCNT(), 2, "Second reference");
101 }
102 is($r->REFCNT(), 1, "Reference count is 1");
103
104}
105
5c35adbb
NC
106my $r = qr/foo/;
107my $obj = B::svref_2object($r);
108my $regexp = ($] < 5.011) ? $obj->MAGIC : $obj;
109ok($regexp->precomp() eq 'foo', 'Get string from qr//');
110like($regexp->REGEX(), qr/\d+/, "REGEX() returns numeric value");
01b509b0
SP
111my $iv = 1;
112my $iv_ref = B::svref_2object(\$iv);
113is(ref $iv_ref, "B::IV", "Test B:IV return from svref_2object");
114is($iv_ref->REFCNT, 1, "Test B::IV->REFCNT");
115# Flag tests are needed still
116#diag $iv_ref->FLAGS();
117my $iv_ret = $iv_ref->object_2svref();
118is(ref $iv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
119is($$iv_ret, $iv, "Test object_2svref()");
120is($iv_ref->int_value, $iv, "Test int_value()");
121is($iv_ref->IV, $iv, "Test IV()");
122is($iv_ref->IVX(), $iv, "Test IVX()");
123is($iv_ref->UVX(), $iv, "Test UVX()");
f046b1bd
NC
124is(eval { $iv_ref->RV() }, undef, 'Test RV() on IV');
125like($@, qr/argument is not SvROK/, 'Test RV() IV');
126$iv = \"Pie";
127my $val = eval { $iv_ref->RV() };
128is(ref $val, 'B::PV', 'Test RV() on a reference');
129is($val->PV(), 'Pie', 'Value expected');
130is($@, '', "Test RV()");
01b509b0
SP
131
132my $pv = "Foo";
133my $pv_ref = B::svref_2object(\$pv);
134is(ref $pv_ref, "B::PV", "Test B::PV return from svref_2object");
135is($pv_ref->REFCNT, 1, "Test B::PV->REFCNT");
136# Flag tests are needed still
137#diag $pv_ref->FLAGS();
138my $pv_ret = $pv_ref->object_2svref();
139is(ref $pv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
140is($$pv_ret, $pv, "Test object_2svref()");
141is($pv_ref->PV(), $pv, "Test PV()");
f046b1bd
NC
142is(eval { $pv_ref->RV() }, undef, 'Test RV() on PV');
143like($@, qr/argument is not SvROK/, 'Test RV() on PV');
01b509b0 144is($pv_ref->PVX(), $pv, "Test PVX()");
f046b1bd
NC
145$pv = \"Pie";
146$val = eval { $pv_ref->RV() };
147is(ref $val, 'B::PV', 'Test RV() on a reference');
148is($val->PV(), 'Pie', 'Value expected');
149is($@, '', "Test RV()");
01b509b0
SP
150
151my $nv = 1.1;
152my $nv_ref = B::svref_2object(\$nv);
153is(ref $nv_ref, "B::NV", "Test B::NV return from svref_2object");
154is($nv_ref->REFCNT, 1, "Test B::NV->REFCNT");
155# Flag tests are needed still
156#diag $nv_ref->FLAGS();
157my $nv_ret = $nv_ref->object_2svref();
158is(ref $nv_ret, "SCALAR", "Test object_2svref() return is SCALAR");
159is($$nv_ret, $nv, "Test object_2svref()");
160is($nv_ref->NV, $nv, "Test NV()");
161is($nv_ref->NVX(), $nv, "Test NVX()");
f046b1bd
NC
162is(eval { $nv_ref->RV() }, undef, 'Test RV() on NV');
163like($@, qr/Can't locate object method "RV" via package "B::NV"/,
164 'Test RV() on NV');
01b509b0
SP
165
166my $null = undef;
167my $null_ref = B::svref_2object(\$null);
168is(ref $null_ref, "B::NULL", "Test B::NULL return from svref_2object");
169is($null_ref->REFCNT, 1, "Test B::NULL->REFCNT");
170# Flag tests are needed still
171#diag $null_ref->FLAGS();
172my $null_ret = $nv_ref->object_2svref();
173is(ref $null_ret, "SCALAR", "Test object_2svref() return is SCALAR");
174is($$null_ret, $nv, "Test object_2svref()");
175
4df7f6af 176my $RV_class = $] >= 5.011 ? 'B::IV' : 'B::RV';
01b509b0
SP
177my $cv = sub{ 1; };
178my $cv_ref = B::svref_2object(\$cv);
4df7f6af
NC
179is($cv_ref->REFCNT, 1, "Test $RV_class->REFCNT");
180is(ref $cv_ref, "$RV_class",
181 "Test $RV_class return from svref_2object - code");
01b509b0
SP
182my $cv_ret = $cv_ref->object_2svref();
183is(ref $cv_ret, "REF", "Test object_2svref() return is REF");
184is($$cv_ret, $cv, "Test object_2svref()");
185
186my $av = [];
187my $av_ref = B::svref_2object(\$av);
4df7f6af
NC
188is(ref $av_ref, "$RV_class",
189 "Test $RV_class return from svref_2object - array");
01b509b0
SP
190
191my $hv = [];
192my $hv_ref = B::svref_2object(\$hv);
4df7f6af
NC
193is(ref $hv_ref, "$RV_class",
194 "Test $RV_class return from svref_2object - hash");
01b509b0
SP
195
196local *gv = *STDOUT;
197my $gv_ref = B::svref_2object(\*gv);
198is(ref $gv_ref, "B::GV", "Test B::GV return from svref_2object");
199ok(! $gv_ref->is_empty(), "Test is_empty()");
711fbbf0 200ok($gv_ref->isGV_with_GP(), "Test isGV_with_GP()");
01b509b0
SP
201is($gv_ref->NAME(), "gv", "Test NAME()");
202is($gv_ref->SAFENAME(), "gv", "Test SAFENAME()");
203like($gv_ref->FILE(), qr/b\.t$/, "Testing FILE()");
de64752d
NC
204is($gv_ref->SvTYPE(), B::SVt_PVGV, "Test SvTYPE()");
205is($gv_ref->FLAGS() & B::SVTYPEMASK, B::SVt_PVGV, "Test SVTYPEMASK");
bb1efdce
FC
206is($gv_ref->GPFLAGS & B::GPf_ALIASED_SV, 0, 'GPFLAGS are unset');
207{
208 local *gv = \my $x;
209 is($gv_ref->GPFLAGS & B::GPf_ALIASED_SV, B::GPf_ALIASED_SV,
210 'GPFLAGS gets GPf_ALIASED_SV set');
211}
2da668d2
SP
212
213# The following return B::SPECIALs.
214is(ref B::sv_yes(), "B::SPECIAL", "B::sv_yes()");
215is(ref B::sv_no(), "B::SPECIAL", "B::sv_no()");
216is(ref B::sv_undef(), "B::SPECIAL", "B::sv_undef()");
217
218# More utility functions
219is(B::ppname(0), "pp_null", "Testing ppname (this might break if opnames.h is changed)");
220is(B::opnumber("null"), 0, "Testing opnumber with opname (null)");
221is(B::opnumber("pp_null"), 0, "Testing opnumber with opname (pp_null)");
b45732d6
NC
222{
223 my $hash = B::hash("wibble");
224 like($hash, qr/\A0x[0-9a-f]+\z/, "Testing B::hash(\"wibble\")");
225 unlike($hash, qr/\A0x0+\z/, "Testing B::hash(\"wibble\")");
226
bb1ca2d4
YO
227 SKIP: {
228 skip "Nulls don't hash to the same bucket regardless of length with this PERL_HASH implementation", 20
229 if B::hash("") ne B::hash("\0" x 19);
230 like(B::hash("\0" x $_), qr/\A0x0+\z/, "Testing B::hash(\"0\" x $_)")
231 for 0..19;
232 }
8c5b7c71
NC
233
234 $hash = eval {B::hash(chr 256)};
235 is($hash, undef, "B::hash() refuses non-octets");
236 like($@, qr/^Wide character in subroutine entry/);
237
238 $hash = B::hash(chr 163);
239 my $str = chr(163) . chr 256;
240 chop $str;
241 is(B::hash($str), $hash, 'B::hash() with chr 128-256 is well-behaved');
b45732d6 242}
f9a20969
NC
243{
244 is(B::cstring(undef), '0', "Testing B::cstring(undef)");
245 is(B::perlstring(undef), '0', "Testing B::perlstring(undef)");
246
247 my @common = map {eval $_, $_}
574fde55
NC
248 '"wibble"', '"\""', '"\'"', '"\\\\"', '"\\n\\r\\t\\b\\a\\f"', '"\000"',
249 '"\000\000"', '"\000Bing\000"', ord 'N' == 78 ? '"\\177"' : ();
f9a20969
NC
250
251 my $oct = sprintf "\\%03o", ord '?';
252 my @tests = (@common, '$_', '"$_"', '@_', '"@_"', '??N', qq{"$oct?N"},
253 ord 'N' == 78 ? (chr 11, '"\v"'): ());
254 while (my ($test, $expect) = splice @tests, 0, 2) {
255 is(B::cstring($test), $expect, "B::cstring($expect)");
256 }
257
258 @tests = (@common, '$_', '"\$_"', '@_', '"\@_"', '??N', '"??N"',
259 chr 256, '"\x{100}"', chr 65536, '"\x{10000}"',
260 ord 'N' == 78 ? (chr 11, '"\013"'): ());
261 while (my ($test, $expect) = splice @tests, 0, 2) {
262 is(B::perlstring($test), $expect, "B::perlstring($expect)");
263 utf8::upgrade $test;
264 $expect =~ s/\\b/\\x\{8\}/g;
265 $expect =~ s/\\([0-7]{3})/sprintf "\\x\{%x\}", oct $1/eg;
266 is(B::perlstring($test), $expect, "B::perlstring($expect) (Unicode)");
267 }
268}
01c3a485 269{
574fde55
NC
270 my @tests = ((map {eval(qq{"$_"}), $_} '\\n', '\\r', '\\t',
271 '\\b', '\\a', '\\f', '\\000', '\\\'', '?'), '"', '"',
272 ord 'N' == 78 ? (chr 11, '\v', "\177", '\\177') : ());
01c3a485
NC
273
274 while (my ($test, $expect) = splice @tests, 0, 2) {
275 is(B::cchar($test), "'${expect}'", "B::cchar(qq{$expect})");
276 }
277}
278
2da668d2
SP
279is(B::class(bless {}, "Wibble::Bibble"), "Bibble", "Testing B::class()");
280is(B::cast_I32(3.14), 3, "Testing B::cast_I32()");
32b17be1
DM
281is(B::opnumber("chop"), $] >= 5.015 ? 39 : 38,
282 "Testing opnumber with opname (chop)");
5ce57cc0
JJ
283
284{
285 no warnings 'once';
286 my $sg = B::sub_generation();
e1a479c5 287 *UNIVERSAL::hand_waving = sub { };
5ce57cc0
JJ
288 ok( $sg < B::sub_generation, "sub_generation increments" );
289}
290
3aaeec97 291like( B::amagic_generation, qr/^\d+\z/, "amagic_generation" );
f9a20969 292
fdbd1d64
NC
293is(B::svref_2object(sub {})->ROOT->ppaddr, 'PL_ppaddr[OP_LEAVESUB]',
294 'OP->ppaddr');
295
512ba29b
FC
296# This one crashes from perl 5.8.9 to B 1.24 (perl 5.13.6):
297B::svref_2object(sub{y/\x{100}//})->ROOT->first->first->sibling->sv;
298ok 1, 'B knows that UTF trans is a padop in 5.8.9, not an svop';
299
b852bf25
FR
300{
301 format FOO =
302foo
303.
304 my $f = B::svref_2object(*FOO{FORMAT});
305 isa_ok $f, 'B::FM';
306 can_ok $f, 'LINES';
307}
308
d9cd2aeb
FC
309is B::safename("\cLAST_FH"), "^LAST_FH", 'basic safename test';
310
a60c099b 311my $sub1 = sub {die};
99225839
FC
312{ no warnings 'once'; no strict; *Peel:: = *{"Pe\0e\x{142}::"} }
313my $sub2 = eval 'package Peel; sub {die}';
a60c099b 314my $cop = B::svref_2object($sub1)->ROOT->first->first;
99225839 315my $bobby = B::svref_2object($sub2)->ROOT->first->first;
a60c099b
FC
316is $cop->stash->object_2svref, \%main::, 'COP->stash';
317is $cop->stashpv, 'main', 'COP->stashpv';
82aeefe1
DM
318
319SKIP: {
320 skip "no nulls in packages before 5.17", 1 if $] < 5.017;
321 is $bobby->stashpv, "Pe\0e\x{142}", 'COP->stashpv with utf8 and nulls';
322}
323
324SKIP: {
325 skip "no stashoff", 2 if $] < 5.017 || !$Config::Config{useithreads};
99225839
FC
326 like $cop->stashoff, qr/^[1-9]\d*\z/a, 'COP->stashoff';
327 isnt $cop->stashoff, $bobby->stashoff,
328 'different COP->stashoff for different stashes';
a60c099b
FC
329}
330
71324a3b
DM
331
332# Test $B::overlay
333{
334 my $methods = {
335 BINOP => [ qw(last) ],
336 COP => [ qw(arybase cop_seq file filegv hints hints_hash io
337 label line stash stashpv
338 stashoff warnings) ],
339 LISTOP => [ qw(children) ],
340 LOGOP => [ qw(other) ],
341 LOOP => [ qw(lastop nextop redoop) ],
342 OP => [ qw(desc flags name next opt ppaddr private sibling
343 size spare targ type) ],
344 PADOP => [ qw(gv padix sv) ],
345 PMOP => [ qw(code_list pmflags pmoffset pmreplroot pmreplstart pmstash pmstashpv precomp reflags) ],
346 PVOP => [ qw(pv) ],
347 SVOP => [ qw(gv sv) ],
348 UNOP => [ qw(first) ],
349 };
350
351 my $overlay = {};
352 my $op = B::svref_2object(sub { my $x = 1 })->ROOT;
353
354 for my $class (sort keys %$methods) {
355 for my $meth (@{$methods->{$class}}) {
356 my $full = "B::${class}::$meth";
357 die "Duplicate method '$full'\n"
358 if grep $_ eq $full, @{$overlay->{$meth}};
359 push @{$overlay->{$meth}}, "B::${class}::$meth";
360 }
361 }
362
363 {
364 local $B::overlay; # suppress 'used once' warning
365 local $B::overlay = { $$op => $overlay };
366
367 for my $class (sort keys %$methods) {
368 bless $op, "B::$class"; # naughty
369 for my $meth (@{$methods->{$class}}) {
370 if ($op->can($meth)) {
371 my $list = $op->$meth;
372 ok(defined $list
373 && ref($list) eq "ARRAY"
374 && grep($_ eq "B::${class}::$meth", @$list),
375 "overlay: B::$class $meth");
376 }
377 else {
378 pass("overlay: B::$class $meth (skipped; no method)");
379 }
380 }
381 }
382 }
383 # B::overlay should be disabled again here
384 is($op->name, "leavesub", "overlay: orig name");
385}
386
486b1e7f
TC
387{ # [perl #118525]
388 {
389 sub foo {}
390 my $cv = B::svref_2object(\&foo);
391 ok($cv, "make a B::CV from a non-anon sub reference");
392 isa_ok($cv, "B::CV");
393 my $gv = $cv->GV;
394 ok($gv, "we get a GV from a GV on a normal sub");
395 isa_ok($gv, "B::GV");
396 is($gv->NAME, "foo", "check the GV name");
397 SKIP:
398 { # do we need these version checks?
399 skip "no HEK before 5.18", 1 if $] < 5.018;
400 is($cv->NAME_HEK, undef, "no hek for a global sub");
401 }
402 }
403
404SKIP:
405 {
406 skip "no HEK before 5.18", 4 if $] < 5.018;
407 eval <<'EOS'
408 {
409 use feature 'lexical_subs';
410 no warnings 'experimental::lexical_subs';
411 my sub bar {};
412 my $cv = B::svref_2object(\&bar);
413 ok($cv, "make a B::CV from a lexical sub reference");
414 isa_ok($cv, "B::CV");
486b1e7f
TC
415 my $hek = $cv->NAME_HEK;
416 is($hek, "bar", "check the NAME_HEK");
ae77754a
FC
417 my $gv = $cv->GV;
418 isa_ok($gv, "B::GV", "GV on a lexical sub");
486b1e7f
TC
419 }
420 1;
421EOS
422 or die "lexical_subs test failed to compile: $@";
423 }
424}
425
cc54be84 426{ # [perl #120535]
cc54be84
TC
427 my %h = ( "\x{100}" => 1 );
428 my $b = B::svref_2object(\%h);
429 my ($k, $v) = $b->ARRAY;
430 is($k, "\x{100}", "check utf8 preserved by B::HV::ARRAY");
431}
432
29e61fd9
DM
433# test op_parent
434
435SKIP: {
436 unless ($Config::Config{ccflags} =~ /PERL_OP_PARENT/) {
437 skip "op_parent only present with -DPERL_OP_PARENT builds", 6;
438 }
439 my $lineseq = B::svref_2object(sub{my $x = 1})->ROOT->first;
440 is ($lineseq->type, B::opnumber('lineseq'),
441 'op_parent: top op is lineseq');
442 my $first = $lineseq->first;
443 my $second = $first->sibling;
444 is(ref $second->sibling, "B::NULL", 'op_parent: second sibling is null');
445 is($first->lastsib, 0 , 'op_parent: first sibling: !lastsib');
446 is($second->lastsib, 1, 'op_parent: second sibling: lastsib');
447 is($$lineseq, ${$first->parent}, 'op_parent: first sibling okay');
448 is($$lineseq, ${$second->parent}, 'op_parent: second sibling okay');
449}
450
451
3a23d767
DM
452# make sure ->sv, -gv methods do the right thing on threaded builds
453{
454
455 # for some reason B::walkoptree only likes a sub name, not a code ref
456 my ($gv, $sv);
457 sub gvsv_const {
458 # make the early pad slots something unlike a threaded const or
459 # gvsv
460 my ($dummy1, $dummy2, $dummy3, $dummy4) = qw(foo1 foo2 foo3 foo4);
461 my $self = shift;
462 if ($self->name eq 'gvsv') {
463 $gv = $self->gv;
464 }
465 elsif ($self->name eq 'const') {
466 $sv = $self->sv;
467 }
468 };
469
470 B::walkoptree(B::svref_2object(sub {our $x = 1})->ROOT, "::gvsv_const");
471 ok(defined $gv, "gvsv->gv seen");
472 ok(defined $sv, "const->sv seen");
473 if ($Config::Config{useithreads}) {
474 # should get NULLs
475 is(ref($gv), "B::SPECIAL", "gvsv->gv is special");
476 is(ref($sv), "B::SPECIAL", "const->sv is special");
477 is($$gv, 0, "gvsv->gv special is 0 (NULL)");
478 is($$sv, 0, "const->sv special is 0 (NULL)");
479 }
480 else {
481 is(ref($gv), "B::GV", "gvsv->gv is GV");
482 is(ref($sv), "B::IV", "const->sv is IV");
483 pass();
484 pass();
485 }
486
487}
488
489
f9a20969 490done_testing();