| 1 | package Moo::HandleMoose::_TypeMap; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package | ||
| 6 | Moo::HandleMoose; | ||
| 7 | our %TYPE_MAP; | ||
| 8 | |||
| 9 | package Moo::HandleMoose::_TypeMap; | ||
| 10 | |||
| 11 | use Scalar::Util (); | ||
| 12 | use Config (); | ||
| 13 | |||
| 14 | BEGIN { | ||
| 15 | *_OVERLOAD_ON_REF = "$]" < 5.010000 ? sub(){1} : sub(){0}; | ||
| 16 | } | ||
| 17 | |||
| 18 | our %WEAK_TYPES; | ||
| 19 | |||
| 20 | sub _str_to_ref { | ||
| 21 | my $in = shift; | ||
| 22 | return $in | ||
| 23 | if ref $in; | ||
| 24 | |||
| 25 | if ($in =~ /(?:^|=)([A-Z]+)\(0x([0-9a-zA-Z]+)\)$/) { | ||
| 26 | my $type = $1; | ||
| 27 | my $id = do { no warnings 'portable'; hex "$2" }; | ||
| 28 | require B; | ||
| 29 | my $sv = bless \$id, 'B::SV'; | ||
| 30 | my $ref = eval { $sv->object_2svref }; | ||
| 31 | |||
| 32 | if (!defined $ref or Scalar::Util::reftype($ref) ne $type) { | ||
| 33 | die <<'END_ERROR'; | ||
| 34 | Moo initialization encountered types defined in a parent thread - ensure that | ||
| 35 | Moo is require()d before any further thread spawns following a type definition. | ||
| 36 | END_ERROR | ||
| 37 | } | ||
| 38 | |||
| 39 | # on older perls where overloading magic is attached to the ref rather | ||
| 40 | # than the ref target, reblessing will pick up the magic | ||
| 41 | if (_OVERLOAD_ON_REF and my $class = Scalar::Util::blessed($ref)) { | ||
| 42 | bless $ref, $class; | ||
| 43 | } | ||
| 44 | |||
| 45 | return $ref; | ||
| 46 | } | ||
| 47 | return $in; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub TIEHASH { bless {}, $_[0] } | ||
| 51 | |||
| 52 | sub STORE { | ||
| 53 | my ($self, $key, $value) = @_; | ||
| 54 | my $type = _str_to_ref($key); | ||
| 55 | $key = "$type"; | ||
| 56 | $WEAK_TYPES{$key} = $type; | ||
| 57 | Scalar::Util::weaken($WEAK_TYPES{$key}) | ||
| 58 | if ref $type; | ||
| 59 | $self->{$key} = $value; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub FETCH { $_[0]->{$_[1]} } | ||
| 63 | sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} } | ||
| 64 | sub NEXTKEY { each %{$_[0]} } | ||
| 65 | sub EXISTS { exists $_[0]->{$_[1]} } | ||
| 66 | sub DELETE { delete $_[0]->{$_[1]} } | ||
| 67 | sub CLEAR { %{$_[0]} = () } | ||
| 68 | sub SCALAR { scalar %{$_[0]} } | ||
| 69 | |||
| 70 | sub CLONE { | ||
| 71 | my @types = map { | ||
| 72 | defined $WEAK_TYPES{$_} ? ($WEAK_TYPES{$_} => $TYPE_MAP{$_}) : () | ||
| 73 | } keys %TYPE_MAP; | ||
| 74 | %WEAK_TYPES = (); | ||
| 75 | %TYPE_MAP = @types; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub DESTROY { | ||
| 79 | my %types = %{$_[0]}; | ||
| 80 | untie %TYPE_MAP; | ||
| 81 | %TYPE_MAP = %types; | ||
| 82 | } | ||
| 83 | |||
| 84 | if ($Config::Config{useithreads}) { | ||
| 85 | my @types = %TYPE_MAP; | ||
| 86 | tie %TYPE_MAP, __PACKAGE__; | ||
| 87 | %TYPE_MAP = @types; | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; |