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