From 5540212cfc8805f869ce566c94c406f5cb18224b Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Wed, 26 Dec 2018 15:32:13 +0100 Subject: [PATCH] Turn a lot of stuff into constants So we don't have to do this at each startup --- src/core/Compiler.pm6 | 2 +- src/core/Date.pm6 | 3 +- src/core/DateTime.pm6 | 3 +- src/core/Dateish.pm6 | 4 +-- src/core/Encoding/Builtin.pm6 | 3 +- src/core/Exception.pm6 | 68 +++++++++++++++++------------------ src/core/IO/Spec.pm6 | 34 ++++++++++-------- src/core/PseudoStash.pm6 | 2 +- src/core/Rakudo/Internals.pm6 | 8 ++--- src/core/ShapedNArray.pm6 | 2 +- 10 files changed, 68 insertions(+), 61 deletions(-) diff --git a/src/core/Compiler.pm6 b/src/core/Compiler.pm6 index a889227c153..b1a4a5ef9c6 100644 --- a/src/core/Compiler.pm6 +++ b/src/core/Compiler.pm6 @@ -3,7 +3,7 @@ class Compiler does Systemic { has Str $.release; has Str $!build-date; has Str $.codename; - BEGIN my $id = nqp::sha1( + my constant $id = nqp::sha1( $*W.handle.Str ~ nqp::atkey(nqp::getcurhllsym('$COMPILER_CONFIG'), 'source-digest') ); diff --git a/src/core/Date.pm6 b/src/core/Date.pm6 index 7d446a33e00..05292bf6024 100644 --- a/src/core/Date.pm6 +++ b/src/core/Date.pm6 @@ -2,7 +2,7 @@ my class Date does Dateish { method !formatter() { sprintf '%s-%02d-%02d',self!year-Str,$!month,$!day } - my $valid-units := nqp::hash( + my constant $valid-units = nqp::hash( 'day', 1, 'days', 1, 'week', 7, @@ -12,6 +12,7 @@ my class Date does Dateish { 'year', 0, 'years', 0, ); + method !VALID-UNIT($unit) { nqp::existskey($valid-units,$unit) ?? $unit diff --git a/src/core/DateTime.pm6 b/src/core/DateTime.pm6 index 5511dcbbe13..61224e74981 100644 --- a/src/core/DateTime.pm6 +++ b/src/core/DateTime.pm6 @@ -24,7 +24,7 @@ my class DateTime does Dateish { ($!timezone.abs/60%60).floor) } - my $valid-units := nqp::hash( + my constant $valid-units = nqp::hash( 'second', 0, 'seconds', 0, 'minute', 0, @@ -40,6 +40,7 @@ my class DateTime does Dateish { 'year', 1, 'years', 1, ); + method !VALID-UNIT($unit) { nqp::existskey($valid-units,$unit) ?? $unit diff --git a/src/core/Dateish.pm6 b/src/core/Dateish.pm6 index 91d0804ea18..a3af27faf42 100644 --- a/src/core/Dateish.pm6 +++ b/src/core/Dateish.pm6 @@ -11,7 +11,7 @@ my role Dateish { sub IS-LEAP-YEAR($y) { $y %% 4 and not $y %% 100 or $y %% 400 } method is-leap-year(Dateish:D:) { IS-LEAP-YEAR($!year) } - my $days-in-month := nqp::list_i( + my constant $days-in-month = nqp::list_i( 0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); # This method is used by Date and DateTime: @@ -97,7 +97,7 @@ my role Dateish { ($!day - 1) div 7 + 1 } - my $days-at-start-of-month := nqp::list_i( + my constant $days-at-start-of-month = nqp::list_i( 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 ); method day-of-year() { diff --git a/src/core/Encoding/Builtin.pm6 b/src/core/Encoding/Builtin.pm6 index 9ba026e3192..dad0f051d40 100644 --- a/src/core/Encoding/Builtin.pm6 +++ b/src/core/Encoding/Builtin.pm6 @@ -33,7 +33,8 @@ class Encoding::Builtin does Encoding { !! $encoder } - my $enc_type := nqp::hash('utf8',utf8,'utf16',utf16,'utf32',utf32); + my constant $enc_type = nqp::hash('utf8',utf8,'utf16',utf16,'utf32',utf32); + method !buf-type() { nqp::ifnull(nqp::atkey($enc_type, $!name), blob8) } diff --git a/src/core/Exception.pm6 b/src/core/Exception.pm6 index 14afd831cc9..eda910e762c 100644 --- a/src/core/Exception.pm6 +++ b/src/core/Exception.pm6 @@ -1569,43 +1569,43 @@ my class X::Syntax::ConditionalOperator::SecondPartInvalid does X::Syntax { my class X::Syntax::Perl5Var does X::Syntax { has $.name; has $.identifier-name; - BEGIN my %m = - '$"' => '.join() method', - '$$' => '$*PID', - '$;' => 'real multidimensional hashes', - '$&' => '$<>', - '$`' => '$/.prematch', - '$\'' => '$/.postmatch', - '$,' => '.join() method', - '$.' => "the .kv method on e.g. .lines", - '$/' => "the filehandle's .nl-in attribute", - '$\\' => "the filehandle's .nl-out attribute", - '$|' => "the filehandle's .out-buffer attribute", - '$?' => '$! for handling child errors also', - '$@' => '$!', - '$]' => '$*PERL.version or $*PERL.compiler.version', - - '$^C' => 'COMPILING namespace', - '$^H' => '$?FOO variables', - '$^N' => '$/[*-1]', - '$^O' => 'VM.osname', - '$^R' => 'an explicit result variable', - '$^S' => 'context function', - '$^T' => '$*INIT-INSTANT', - '$^V' => '$*PERL.version or $*PERL.compiler.version', - '$^X' => '$*EXECUTABLE-NAME', - - '@-' => '.from method', - '@+' => '.to method', - - '%-' => '.from method', - '%+' => '.to method', - '%^H' => '$?FOO variables', - ; + my constant $m = nqp::hash( + '$"', '.join() method', + '$$', '$*PID', + '$;', 'real multidimensional hashes', + '$&', '$<>', + '$`', '$/.prematch', + '$\'', '$/.postmatch', + '$,', '.join() method', + '$.', "the .kv method on e.g. .lines", + '$/', "the filehandle's .nl-in attribute", + '$\\', "the filehandle's .nl-out attribute", + '$|', "the filehandle's .out-buffer attribute", + '$?', '$! for handling child errors also', + '$@', '$!', + '$]', '$*PERL.version or $*PERL.compiler.version', + + '$^C', 'COMPILING namespace', + '$^H', '$?FOO variables', + '$^N', '$/[*-1]', + '$^O', 'VM.osname', + '$^R', 'an explicit result variable', + '$^S', 'context function', + '$^T', '$*INIT-INSTANT', + '$^V', '$*PERL.version or $*PERL.compiler.version', + '$^X', '$*EXECUTABLE-NAME', + + '@-', '.from method', + '@+', '.to method', + + '%-', '.from method', + '%+', '.to method', + '%^H', '$?FOO variables', + ); method message() { my $name = $!name; my $v = $name ~~ m/ <[ $ @ % & ]> [ \^ <[ A..Z ]> | \W ] /; - my $sugg = %m{~$v}; + my $sugg = nqp::atkey($m,~$v); if $name eq '$#' { # Currently only `$#` var has this identifier business handling. # Should generalize the logic if we get more of stuff like this. diff --git a/src/core/IO/Spec.pm6 b/src/core/IO/Spec.pm6 index 9ace593f522..a95e2392d6f 100644 --- a/src/core/IO/Spec.pm6 +++ b/src/core/IO/Spec.pm6 @@ -2,22 +2,26 @@ my class VM { ... } my class IO::Spec { - BEGIN my %module = # only list the non-Unix ones in lowercase - 'mswin32' => 'Win32', - 'os2' => 'Win32', - 'dos' => 'Win32', - 'symbian' => 'Win32', - 'netware' => 'Win32', - 'win32' => 'Win32', - 'cygwin' => 'Cygwin', - 'qnx' => 'QNX', - 'nto' => 'QNX', - # »=>» 'Mac', - # 'VMS' => 'VMS' - ; + my constant $module = nqp::hash( # only list the non-Unix ones in lowercase + 'mswin32', 'Win32', + 'os2', 'Win32', + 'dos', 'Win32', + 'symbian', 'Win32', + 'netware', 'Win32', + 'win32', 'Win32', + 'cygwin', 'Cygwin', + 'qnx', 'QNX', + 'nto', 'QNX', + # »=>» 'Mac', + # 'VMS' => 'VMS' + ); - method select(IO::Spec:U: $token?) { - IO::Spec::{%module{ lc($token // VM.osname) } // 'Unix'}; + proto method select(|) {*} + multi method select(IO::Spec:U:) { + IO::Spec::{nqp::ifnull(nqp::atkey($module,VM.osname),'Unix')}; + } + multi method select(IO::Spec:U: $token) { + IO::Spec::{nqp::ifnull(nqp::atkey($module,$token.lc),'Unix')}; } } diff --git a/src/core/PseudoStash.pm6 b/src/core/PseudoStash.pm6 index fd5142f0abc..2e219713031 100644 --- a/src/core/PseudoStash.pm6 +++ b/src/core/PseudoStash.pm6 @@ -22,7 +22,7 @@ my class PseudoStash is Map { method WHICH() { self.Mu::WHICH } - my $pseudoers := nqp::hash( + my constant $pseudoers = nqp::hash( 'MY', sub ($cur) { my $stash := nqp::clone($cur); nqp::bindattr_i($stash, PseudoStash, '$!mode', PRECISE_SCOPE); diff --git a/src/core/Rakudo/Internals.pm6 b/src/core/Rakudo/Internals.pm6 index 1b2b9d26b3a..553fd48063e 100644 --- a/src/core/Rakudo/Internals.pm6 +++ b/src/core/Rakudo/Internals.pm6 @@ -830,10 +830,10 @@ my class Rakudo::Internals { # http://tf.nist.gov/pubs/bulletin/leapsecond.htm # http://hpiers.obspm.fr/eop-pc/earthor/utc/TAI-UTC_tab.html - my int $initial-offset = 10; + my int constant $initial-offset = 10; # TAI - UTC at the Unix epoch (1970-01-01T00:00:00Z). - my $dates := nqp::list_s( + my constant $dates = nqp::list_s( #BEGIN leap-second-dates '1972-06-30', '1972-12-31', @@ -873,7 +873,7 @@ my class Rakudo::Internals { # %leap-seconds{$d} seconds behind TAI. # Ambiguous POSIX times. - my $posixes := nqp::list_i( + my constant $posixes = nqp::list_i( #BEGIN leap-second-posix 78796800, 94694400, @@ -904,7 +904,7 @@ my class Rakudo::Internals { 1483228800, #END leap-second-posix ); - my int $elems = nqp::elems($dates); + my int constant $elems = nqp::elems($dates); method is-leap-second-date(\date) { nqp::hllbool( diff --git a/src/core/ShapedNArray.pm6 b/src/core/ShapedNArray.pm6 index 5b13b17868b..82c67e6f472 100644 --- a/src/core/ShapedNArray.pm6 +++ b/src/core/ShapedNArray.pm6 @@ -1,6 +1,6 @@ # this is actually part of the Array class - my \dim2role := + my constant \dim2role = nqp::list(ShapedArray,Shaped1Array,Shaped2Array,Shaped3Array); sub set-shape(\base, \shape) is raw {