| 1 | package MOP4Import::Util; | ||
| 2 | use strict; | ||
| 3 | use warnings qw(FATAL all NONFATAL misc); | ||
| 4 | use Carp; | ||
| 5 | use Data::Dumper; | ||
| 6 | use Encode (); | ||
| 7 | |||
| 8 | use Exporter qw/import/; | ||
| 9 | |||
| 10 | use Sub::Util (); | ||
| 11 | |||
| 12 | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||
| 13 | BEGIN { | ||
| 14 | print STDERR "Using (file '" . __FILE__ . "')\n" if DEBUG and DEBUG >= 2 | ||
| 15 | } | ||
| 16 | |||
| 17 | sub globref { | ||
| 18 | my $pack = shift; | ||
| 19 | unless (defined $pack) { | ||
| 20 | Carp::croak "undef is given to globref()"; | ||
| 21 | } | ||
| 22 | my $symname = join("::", $pack, @_); | ||
| 23 | no strict 'refs'; | ||
| 24 | \*{$symname}; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub symtab { | ||
| 28 | *{globref(shift, '')}{HASH} | ||
| 29 | } | ||
| 30 | |||
| 31 | sub maybe_symtab { | ||
| 32 | my ($pack) = @_; | ||
| 33 | my @name = split /::/, $pack | ||
| 34 | or return undef; | ||
| 35 | shift @name if $name[0] eq ""; | ||
| 36 | my $symtab = \%::; | ||
| 37 | foreach my $name (@name) { | ||
| 38 | my $glob = $symtab->{$name . "::"} | ||
| 39 | or return undef; | ||
| 40 | $symtab = *{$glob}{HASH} | ||
| 41 | or return undef; | ||
| 42 | } | ||
| 43 | $symtab; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub safe_globref { | ||
| 47 | my ($pack_or_obj, $name) = @_; | ||
| 48 | unless (defined symtab($pack_or_obj)->{$name}) { | ||
| 49 | my $pack = ref $pack_or_obj || $pack_or_obj; | ||
| 50 | croak "No such symbol '$name' in package $pack"; | ||
| 51 | } | ||
| 52 | globref($pack_or_obj, $name); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub maybe_globref { | ||
| 56 | my ($pack_or_obj, $name) = @_; | ||
| 57 | unless (defined symtab($pack_or_obj)->{$name}) { | ||
| 58 | return undef; | ||
| 59 | } | ||
| 60 | globref($pack_or_obj, $name); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub fields_hash { | ||
| 64 | my $sym = fields_symbol(@_); | ||
| 65 | # XXX: return \%{*$sym}; # If we use this, we get "used only once" warning. | ||
| 66 | ensure_symbol_has_hash($sym); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub maybe_fields_hash { | ||
| 70 | my $sym = maybe_globref($_[0], 'FIELDS') | ||
| 71 | or return undef; | ||
| 72 | *{$sym}{HASH}; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub safe_fields_hash { | ||
| 76 | my $sym = safe_globref($_[0], 'FIELDS'); | ||
| 77 | ensure_symbol_has_hash($sym); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub fields_array { | ||
| 81 | my $sym = fields_symbol(@_); | ||
| 82 | ensure_symbol_has_array($sym); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub fields_symbol { | ||
| 86 | globref(ref $_[0] || $_[0], 'FIELDS'); | ||
| 87 | } | ||
| 88 | |||
| 89 | # | ||
| 90 | # Just lock_keys with fields_hash | ||
| 91 | # | ||
| 92 | sub lock_keys_as { | ||
| 93 | my ($myPack, $typeName, $hash) = @_; | ||
| 94 | require Hash::Util; | ||
| 95 | $hash //= +{}; | ||
| 96 | if (ref $typeName) { | ||
| 97 | Carp::croak "Typename must be a package name: " | ||
| 98 | . terse_dump($typeName); | ||
| 99 | } | ||
| 100 | my $fields = safe_fields_hash($typeName); | ||
| 101 | if (not Hash::Util::hashref_locked($hash)) { | ||
| 102 | Hash::Util::lock_ref_keys($hash, keys %$fields); | ||
| 103 | } else { | ||
| 104 | if (my @unk = grep {not exists $fields->{$_}} | ||
| 105 | Hash::Util::legal_ref_keys($hash)) { | ||
| 106 | Carp::croak "HASH contains illegal keys wrt type $typeName: " | ||
| 107 | . join(" ", sort @unk); | ||
| 108 | } | ||
| 109 | # OK; | ||
| 110 | } | ||
| 111 | $hash; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub isa_array { | ||
| 115 | my $sym = globref($_[0], 'ISA'); | ||
| 116 | ensure_symbol_has_array($sym); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub ensure_symbol_has_array { | ||
| 120 | my ($sym) = @_; | ||
| 121 | unless (*{$sym}{ARRAY}) { | ||
| 122 | *$sym = []; | ||
| 123 | } | ||
| 124 | *{$sym}{ARRAY}; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub ensure_symbol_has_hash { | ||
| 128 | my ($sym) = @_; | ||
| 129 | unless (*{$sym}{HASH}) { | ||
| 130 | *$sym = {}; | ||
| 131 | } | ||
| 132 | *{$sym}{HASH}; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub define_constant { | ||
| 136 | my ($name_or_glob, $value) = @_; | ||
| 137 | my $glob = ref $name_or_glob ? $name_or_glob : globref($name_or_glob); | ||
| 138 | *$glob = my $const_sub = sub () { $value }; | ||
| 139 | $const_sub; | ||
| 140 | } | ||
| 141 | |||
| 142 | # MOP4Import::Util::extract_fields_as(BASE_CLASS => $obj) | ||
| 143 | # => returns name, value pairs found in BASE_CLASS and defined in $obj. | ||
| 144 | # Note: this only extracts fields starting with [a-z]. | ||
| 145 | sub extract_fields_as ($$) { | ||
| 146 | my ($asPack, $obj) = @_; | ||
| 147 | my $fields = fields_hash($asPack); | ||
| 148 | map { | ||
| 149 | if (/^[a-z]/ and defined $obj->{$_}) { | ||
| 150 | ($_ => $obj->{$_}) | ||
| 151 | } else { | ||
| 152 | () | ||
| 153 | } | ||
| 154 | } keys %$fields | ||
| 155 | } | ||
| 156 | |||
| 157 | # | ||
| 158 | # For (shallow) copy constructor. | ||
| 159 | # | ||
| 160 | sub shallow_copy { | ||
| 161 | if (ref $_[0] eq 'HASH') { | ||
| 162 | +{%{$_[0]}}; | ||
| 163 | } elsif (ref $_[0] eq 'ARRAY') { | ||
| 164 | +[@{$_[0]}]; | ||
| 165 | } elsif (ref $_[0] eq 'Regexp' | ||
| 166 | or not ref $_[0]) { | ||
| 167 | my $copy = $_[0]; | ||
| 168 | } elsif ($_[1]) { | ||
| 169 | # Pass thru unknown refs if 2nd arg is true. | ||
| 170 | $_[0]; | ||
| 171 | } else { | ||
| 172 | croak "Unsupported data type for shallow_copy: " . ref $_[0]; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | # | ||
| 177 | # Expand given item as list. | ||
| 178 | # | ||
| 179 | sub lexpand { | ||
| 180 | if (not defined $_[0]) { | ||
| 181 | return | ||
| 182 | } elsif (ref $_[0] eq 'ARRAY') { | ||
| 183 | @{$_[0]} | ||
| 184 | } else { | ||
| 185 | $_[0] | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | sub terse_dump { | ||
| 190 | join ", ", map { | ||
| 191 | Data::Dumper->new([$_])->Terse(1)->Indent(0)->Dump; | ||
| 192 | } @_; | ||
| 193 | } | ||
| 194 | |||
| 195 | # | ||
| 196 | # This may be useful to parse/take subcommand option/hash. | ||
| 197 | # | ||
| 198 | sub take_hash_opts_maybe { | ||
| 199 | my ($pack, $list, $result, $alias) = @_; | ||
| 200 | |||
| 201 | if (@$list and ref $list->[0] eq 'HASH') { | ||
| 202 | # If first element of $list is HASH, take it. | ||
| 203 | |||
| 204 | shift @$list; | ||
| 205 | } else { | ||
| 206 | # Otherwise, take --posix_style=options. | ||
| 207 | |||
| 208 | $pack->parse_opts($list, $result, $alias); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | sub take_locked_opts_of { | ||
| 213 | my ($myPack, $typeName, $list, $alias, $sink) = @_; | ||
| 214 | $myPack->lock_keys_as($typeName, scalar take_hash_opts_maybe( | ||
| 215 | $myPack, | ||
| 216 | $list, | ||
| 217 | $sink, | ||
| 218 | $alias, | ||
| 219 | )); | ||
| 220 | } | ||
| 221 | |||
| 222 | # | ||
| 223 | # posix_style long option. | ||
| 224 | # | ||
| 225 | sub parse_opts { | ||
| 226 | my ($pack, $list, $result, $alias, $converter, %opts) = @_; | ||
| 227 | my $wantarray = wantarray; | ||
| 228 | unless (defined $result) { | ||
| 229 | $result = $wantarray ? [] : {}; | ||
| 230 | } | ||
| 231 | my $preserve_hyphen = delete $opts{preserve_hyphen} // do { | ||
| 232 | my $sub = $pack->can("parse_opts__preserve_hyphen"); | ||
| 233 | $sub && $sub->($pack); | ||
| 234 | }; | ||
| 235 | if (keys %opts) { | ||
| 236 | Carp::croak("Unknown option for parse_opts(): ".join(", ", keys %opts)); | ||
| 237 | } | ||
| 238 | while (@$list and defined $list->[0] and my ($n, $v) = $list->[0] | ||
| 239 | =~ m{^--$ | ^(?:--? ([\w:\-\.]+) (?: =(.*))?)$}xs) { | ||
| 240 | shift @$list; | ||
| 241 | last unless defined $n; | ||
| 242 | $n =~ s/-/_/g unless $preserve_hyphen; | ||
| 243 | $n = $alias->{$n} if $alias and $alias->{$n}; | ||
| 244 | $v = 1 unless defined $v; | ||
| 245 | if (ref $result eq 'HASH') { | ||
| 246 | $result->{$n} = $converter ? $converter->($v) : $v; | ||
| 247 | } else { | ||
| 248 | push @$result, $n, $converter ? $converter->($v) : $v; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | if ($converter) { | ||
| 252 | $_ = $converter->($_) for @$list; | ||
| 253 | } | ||
| 254 | $wantarray && ref $result ne 'HASH' ? @$result : $result; | ||
| 255 | } | ||
| 256 | |||
| 257 | # | ||
| 258 | # posix_style long option with JSON support. | ||
| 259 | # | ||
| 260 | sub parse_json_opts { | ||
| 261 | my ($pack, $list, $result, $alias) = @_; | ||
| 262 | require JSON::MaybeXS; | ||
| 263 | parse_opts($pack, $list, $result, $alias, sub { | ||
| 264 | if (not defined $_[0]) { | ||
| 265 | undef | ||
| 266 | } elsif ($_[0] =~ /^(?:\[.*?\]|\{.*?\})\z/s) { | ||
| 267 | # Arguments might be already decoded. | ||
| 268 | my $copy = $_[0]; | ||
| 269 | Encode::_utf8_off($copy) if Encode::is_utf8($copy); | ||
| 270 | JSON::MaybeXS::JSON()->new->utf8->relaxed->decode($copy); | ||
| 271 | } elsif (not Encode::is_utf8($_[0]) and $_[0] =~ /\P{ASCII}/) { | ||
| 272 | Encode::decode(utf8 => $_[0]); | ||
| 273 | } else { | ||
| 274 | $_[0]; | ||
| 275 | } | ||
| 276 | }); | ||
| 277 | } | ||
| 278 | |||
| 279 | # | ||
| 280 | # make style KEY=VALUE list | ||
| 281 | # | ||
| 282 | sub parse_pairlist { | ||
| 283 | my ($pack, $aref, $do_box) = @_; | ||
| 284 | my @res; | ||
| 285 | while (@$aref and defined $aref->[0] | ||
| 286 | and $aref->[0] =~ /^([\w:\-\.]+)=(.*)/) { | ||
| 287 | my $item = shift @$aref; | ||
| 288 | push @res, $do_box ? [$1, $2] : ($1, $2); | ||
| 289 | } | ||
| 290 | @res; | ||
| 291 | } | ||
| 292 | |||
| 293 | sub function_names { | ||
| 294 | my (%opts) = @_; | ||
| 295 | my $packname = delete $opts{from} // caller; | ||
| 296 | my $pattern = delete $opts{matching} || qr{^[A-Za-z]\w+$}; | ||
| 297 | my $except = delete $opts{except} // qr{^import$}; | ||
| 298 | my $grep = delete $opts{grep}; | ||
| 299 | if (keys %opts) { | ||
| 300 | croak "Unknown arguments: ".join(", ", keys %opts); | ||
| 301 | } | ||
| 302 | my $symtab = *{globref($packname, '')}{HASH}; | ||
| 303 | my @result; | ||
| 304 | foreach (sort keys %$symtab) { | ||
| 305 | my $item = $symtab->{$_}; | ||
| 306 | defined (my $code = ref $item eq 'CODE' ? $item : | ||
| 307 | ref \$item eq 'GLOB' ? *{$item}{CODE} : undef) | ||
| 308 | or next; | ||
| 309 | my $realName = Sub::Util::subname($code); | ||
| 310 | my ($stash) = $realName =~ m/^(.+)::(.*?)$/; | ||
| 311 | next unless $stash eq $packname; | ||
| 312 | next unless $_ =~ $pattern; | ||
| 313 | next if $except and $_ =~ $except; | ||
| 314 | next if $grep and not $grep->($realName, $code); | ||
| 315 | push @result, $_; | ||
| 316 | } | ||
| 317 | @result; | ||
| 318 | } | ||
| 319 | |||
| 320 | #======================================== | ||
| 321 | |||
| 322 | sub has_method_attr { | ||
| 323 | grep {$_ eq 'method'} attributes::get($_[0]) | ||
| 324 | } | ||
| 325 | |||
| 326 | use MOP4Import::FieldSpec qw(FieldSpec); | ||
| 327 | |||
| 328 | sub list_validator { | ||
| 329 | my ($typeName) = @_; | ||
| 330 | my $fields = maybe_fields_hash($typeName) | ||
| 331 | or Carp::croak "Can't find type spec for $typeName"; | ||
| 332 | my $names = fields_array($typeName); | ||
| 333 | my @names = $names ? @$names : sort keys %$fields; | ||
| 334 | map { | ||
| 335 | my FieldSpec $spec = $fields->{$_}; | ||
| 336 | if (not UNIVERSAL::isa($spec, FieldSpec)) { | ||
| 337 | () | ||
| 338 | } | ||
| 339 | elsif ($spec->{validator}) { | ||
| 340 | ($_ => $spec->{validator}) | ||
| 341 | } | ||
| 342 | elsif ($spec->{isa}) { | ||
| 343 | ($_ => +{isa => $spec->{isa}, (exists $spec->{default} ? (default => $spec->{default}) : ())}) | ||
| 344 | } | ||
| 345 | else { | ||
| 346 | () | ||
| 347 | } | ||
| 348 | } @names; | ||
| 349 | } | ||
| 350 | |||
| 351 | #======================================== | ||
| 352 | |||
| 353 | sub m4i_log_start { | ||
| 354 | my $m4i_meta = caller; | ||
| 355 | my $m4i_dest = caller(1); | ||
| 356 | print STDERR "\n", "START of $m4i_meta->import() for $m4i_dest.\n"; | ||
| 357 | } | ||
| 358 | |||
| 359 | sub m4i_log_end { | ||
| 360 | my ($m4i_dest) = @_; | ||
| 361 | my $m4i_meta = caller; | ||
| 362 | $m4i_dest //= caller(1); | ||
| 363 | print STDERR "END of $m4i_meta->import() for $m4i_dest.\n\n"; | ||
| 364 | } | ||
| 365 | |||
| 366 | our @EXPORT = qw/globref | ||
| 367 | safe_globref | ||
| 368 | fields_hash fields_symbol lexpand terse_dump | ||
| 369 | fields_array | ||
| 370 | m4i_log_start | ||
| 371 | m4i_log_end | ||
| 372 | /; | ||
| 373 | our @EXPORT_OK = function_names(from => __PACKAGE__ | ||
| 374 | , except => qr/^(import|c\w*)$/ | ||
| 375 | ); | ||
| 376 | |||
| 377 | 1; |