From 923cc1bb816922fc7e3d923ecac3b09efc7690cc Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Mon, 18 Oct 2010 20:31:00 +0200 Subject: [PATCH] Convert Fcntl::S_IFMT to XS. This removes the requirement to call XSLoader::load() at BEGIN time, which simplifies the Perl code further. --- ext/Fcntl/Fcntl.pm | 15 ++++----------- ext/Fcntl/Fcntl.xs | 7 +++++++ ext/Fcntl/t/mode.t | 8 +++++++- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/ext/Fcntl/Fcntl.pm b/ext/Fcntl/Fcntl.pm index 1f69019..834d7ec 100644 --- a/ext/Fcntl/Fcntl.pm +++ b/ext/Fcntl/Fcntl.pm @@ -59,10 +59,11 @@ use strict; our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $AUTOLOAD); require Exporter; +require XSLoader; @ISA = qw(Exporter); -BEGIN { - $VERSION = '1.09'; -} +$VERSION = '1.09'; + +XSLoader::load(); # Items to export into callers namespace by default # (move infrequently used names to @EXPORT_OK below) @@ -209,14 +210,6 @@ BEGIN { )], ); -# Force the constants to become inlined -BEGIN { - require XSLoader; - XSLoader::load(); -} - -sub S_IFMT { @_ ? ( $_[0] & _S_IFMT() ) : _S_IFMT() } - sub AUTOLOAD { (my $constname = $AUTOLOAD) =~ s/.*:://; die "&Fcntl::constant not defined" if $constname eq 'constant'; diff --git a/ext/Fcntl/Fcntl.xs b/ext/Fcntl/Fcntl.xs index 2f86366..a66f66e 100644 --- a/ext/Fcntl/Fcntl.xs +++ b/ext/Fcntl/Fcntl.xs @@ -78,6 +78,13 @@ S_IMODE(...) } PUSHu(SvUV(mode) & 07777); +void +S_IFMT(...) + PREINIT: + dXSTARG; + PPCODE: + PUSHu(items ? (SvUV(ST(0)) & S_IFMT) : S_IFMT); + BOOT: { CV *cv; diff --git a/ext/Fcntl/t/mode.t b/ext/Fcntl/t/mode.t index c7fa8d1..4bec202 100644 --- a/ext/Fcntl/t/mode.t +++ b/ext/Fcntl/t/mode.t @@ -18,7 +18,7 @@ if (-c $devnull) { push @tests, ['CHR', $devnull, (stat $devnull)[2]]; } -plan(tests => 6 + 9 * @tests); +plan(tests => 34 + 6 + 9 * @tests); foreach (@tests) { my ($type, $name, $mode) = @$_; @@ -84,3 +84,9 @@ foreach ([S_ISREG => \&S_ISREG], is(scalar @warnings, 1, '1 warning'); like($warnings[0], qr/^Use of uninitialized value/, 'expected warning'); } + +is (S_IFMT(), _S_IFMT(), 'S_IFMT()'); +is (S_IFMT(0), 0, 'S_IFMT(0)'); +for my $shift (0..31) { + is (S_IFMT(1 << $shift), ((1 << $shift) & _S_IFMT()), "S_IFMT(1 << $shift)"); +} -- 2.7.4