| 1 | # -*- coding: utf-8 -*- | ||
| 2 | package MOP4Import::Declare; | ||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings qw(FATAL all NONFATAL misc); | ||
| 6 | our $VERSION = '0.072_001'; BEGIN {$VERSION = '0.072_001'}; | ||
| 7 | use Carp; | ||
| 8 | use mro qw/c3/; | ||
| 9 | |||
| 10 | use constant DEBUG => $ENV{DEBUG_MOP4IMPORT}; | ||
| 11 | BEGIN { | ||
| 12 | print STDERR "\nUsing ".__PACKAGE__. " = $VERSION (file '" | ||
| 13 | . __FILE__ . "')\n" | ||
| 14 | if DEBUG; | ||
| 15 | } | ||
| 16 | |||
| 17 | use Sub::Util (); | ||
| 18 | |||
| 19 | use MOP4Import::Opts | ||
| 20 | qw/ | ||
| 21 | Opts | ||
| 22 | m4i_args | ||
| 23 | m4i_opts | ||
| 24 | /; | ||
| 25 | use MOP4Import::Util; | ||
| 26 | use MOP4Import::FieldSpec; | ||
| 27 | use MOP4Import::NamedCodeAttributes | ||
| 28 | qw(MODIFY_CODE_ATTRIBUTES | ||
| 29 | FETCH_CODE_ATTRIBUTES | ||
| 30 | /^m4i_CODE_ATTR_/ | ||
| 31 | declare_code_attributes | ||
| 32 | ); | ||
| 33 | |||
| 34 | #======================================== | ||
| 35 | |||
| 36 | our %FIELDS; | ||
| 37 | |||
| 38 | sub import :MetaOnly { | ||
| 39 | my ($myPack, @decls) = @_; | ||
| 40 | |||
| 41 | m4i_log_start() if DEBUG; | ||
| 42 | |||
| 43 | my Opts $opts = m4i_opts([caller]); | ||
| 44 | |||
| 45 | @decls = $myPack->default_exports($opts) unless @decls; | ||
| 46 | |||
| 47 | $myPack->dispatch_declare($opts, $myPack->always_exports($opts), @decls); | ||
| 48 | |||
| 49 | my $tasks; | ||
| 50 | if ($tasks = $opts->{delayed_tasks} and @$tasks) { | ||
| 51 | print STDERR " Calling delayed tasks for $opts->{destpkg}\n" if DEBUG; | ||
| 52 | $_->($opts) for @$tasks; | ||
| 53 | } | ||
| 54 | |||
| 55 | m4i_log_end($opts->{callpack}) if DEBUG; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub m4i_file_line_of :method :MetaOnly { | ||
| 59 | (my $myPack, my Opts $opts) = @_; | ||
| 60 | " at $opts->{filename} line $opts->{line}"; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub m4i_stash :method :MetaOnly { | ||
| 64 | (my $myPack, my Opts $opts) = @_; | ||
| 65 | $opts->{stash}{$myPack} //= +{}; | ||
| 66 | } | ||
| 67 | |||
| 68 | # | ||
| 69 | # `default_exports` is a hook to list exported symbols | ||
| 70 | # when the module is used without arguments, like "use Foo". | ||
| 71 | # Default implementation get them from `@EXPORT`. | ||
| 72 | # | ||
| 73 | sub default_exports { | ||
| 74 | (my $myPack, my Opts $opts) = @_; | ||
| 75 | my $symtab = MOP4Import::Util::symtab($myPack); | ||
| 76 | my $sym = $symtab->{EXPORT}; | ||
| 77 | if ($sym and my $export = *{$sym}{ARRAY}) { | ||
| 78 | print STDERR "HAS \@EXPORT: $myPack" if DEBUG; | ||
| 79 | @$export | ||
| 80 | } else { | ||
| 81 | print STDERR "NO \@EXPORT: $myPack" if DEBUG; | ||
| 82 | (); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | sub declare_default_exports { | ||
| 87 | (my $myPack, my Opts $opts, my (@decls)) = m4i_args(@_); | ||
| 88 | |||
| 89 | my $export = MOP4Import::Util::ensure_symbol_has_array( | ||
| 90 | globref($opts->{objpkg}, 'EXPORT') | ||
| 91 | ); | ||
| 92 | |||
| 93 | push @$export, @decls; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub always_exports { | ||
| 97 | (my $myPack, my Opts $opts) = @_; | ||
| 98 | qw(-strict); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub dispatch_declare :MetaOnly { | ||
| 102 | (my $myPack, my Opts $opts, my (@decls)) = m4i_args(@_); | ||
| 103 | |||
| 104 | print STDERR "$myPack->dispatch_declare(" | ||
| 105 | .terse_dump($opts, @decls).")\n" if DEBUG; | ||
| 106 | |||
| 107 | foreach my $declSpec (@decls) { | ||
| 108 | |||
| 109 | croak "Undefined pragma!" unless defined $declSpec; | ||
| 110 | |||
| 111 | if (not ref $declSpec | ||
| 112 | or ref $declSpec eq 'Regexp') { | ||
| 113 | |||
| 114 | $myPack->dispatch_import($opts, $declSpec); | ||
| 115 | |||
| 116 | } elsif (ref $declSpec eq 'ARRAY') { | ||
| 117 | |||
| 118 | $myPack->dispatch_declare_pragma($opts, @$declSpec); | ||
| 119 | |||
| 120 | } elsif (ref $declSpec eq 'CODE') { | ||
| 121 | |||
| 122 | $declSpec->($myPack, $opts); | ||
| 123 | |||
| 124 | } else { | ||
| 125 | croak "Invalid pragma: ".terse_dump($declSpec); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | our %SIGIL_MAP = qw(* GLOB | ||
| 131 | $ SCALAR | ||
| 132 | % HASH | ||
| 133 | @ ARRAY | ||
| 134 | & CODE); | ||
| 135 | |||
| 136 | sub dispatch_import :MetaOnly { | ||
| 137 | (my $myPack, my Opts $opts, my ($declSpec)) = m4i_args(@_); | ||
| 138 | |||
| 139 | my ($name, $exported); | ||
| 140 | |||
| 141 | if (not ref $declSpec and $declSpec =~ /^-([A-Za-z]\w*)$/) { | ||
| 142 | |||
| 143 | return $myPack->dispatch_declare_pragma($opts, $1); | ||
| 144 | |||
| 145 | } else { | ||
| 146 | |||
| 147 | $myPack->dispatch_import_no_pragma($opts, $declSpec); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | sub dispatch_import_no_pragma :MetaOnly { | ||
| 152 | (my $myPack, my Opts $opts, my (@declSpec)) = m4i_args(@_); | ||
| 153 | foreach my $declSpec (@declSpec) { | ||
| 154 | |||
| 155 | if (ref $declSpec eq 'Regexp') { | ||
| 156 | $myPack->import_by_regexp($opts, $declSpec); | ||
| 157 | } | ||
| 158 | elsif ($declSpec =~ /^([\*\$\%\@\&])?([A-Za-z]\w*)$/) { | ||
| 159 | if ($1) { | ||
| 160 | my $kind = $SIGIL_MAP{$1}; | ||
| 161 | $myPack->can("import_$kind") | ||
| 162 | ->($myPack, $opts, $1, $kind, $2); | ||
| 163 | } else { | ||
| 164 | $myPack->import_NAME($opts => $2); | ||
| 165 | } | ||
| 166 | } else { | ||
| 167 | croak "Invalid import spec: $declSpec"; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | sub import_by_regexp :MetaOnly { | ||
| 173 | (my $myPack, my Opts $opts, my ($pattern)) = m4i_args(@_); | ||
| 174 | |||
| 175 | my $symtab = MOP4Import::Util::symtab($myPack); | ||
| 176 | foreach my $name (keys %$symtab) { | ||
| 177 | next unless $name =~ $pattern; | ||
| 178 | *{globref($opts->{destpkg}, $name)} = $symtab->{$name}; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | sub import_NAME :MetaOnly { | ||
| 183 | (my $myPack, my Opts $opts, my ($name)) = m4i_args(@_); | ||
| 184 | |||
| 185 | my $exported = safe_globref($myPack, $name); | ||
| 186 | |||
| 187 | print STDERR " Declaring $name in $opts->{destpkg} as " | ||
| 188 | .terse_dump($exported)."\n" if DEBUG; | ||
| 189 | |||
| 190 | *{globref($opts->{destpkg}, $name)} = $exported; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub import_GLOB :MetaOnly { | ||
| 194 | (my $myPack, my Opts $opts, my ($sigil, $kind, $name)) = m4i_args(@_); | ||
| 195 | |||
| 196 | my $exported = safe_globref($myPack, $name); | ||
| 197 | |||
| 198 | print STDERR " Declaring $name in $opts->{destpkg} as " | ||
| 199 | .terse_dump($exported)."\n" if DEBUG; | ||
| 200 | |||
| 201 | *{globref($opts->{destpkg}, $name)} = $exported; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub import_SIGIL :MetaOnly { | ||
| 205 | (my $myPack, my Opts $opts, my ($sigil, $kind, $name)) = m4i_args(@_); | ||
| 206 | |||
| 207 | my $exported = *{safe_globref($myPack, $name)}{$kind}; | ||
| 208 | |||
| 209 | print STDERR " Declaring $sigil$opts->{destpkg}::$name" | ||
| 210 | . ", import from $sigil${myPack}::$name" | ||
| 211 | . " (=".terse_dump($exported).")\n" if DEBUG; | ||
| 212 | |||
| 213 | *{globref($opts->{destpkg}, $name)} = $exported; | ||
| 214 | } | ||
| 215 | |||
| 216 | *import_SCALAR = *import_SIGIL; *import_SCALAR = *import_SIGIL; | ||
| 217 | *import_ARRAY = *import_SIGIL; *import_ARRAY = *import_SIGIL; | ||
| 218 | *import_HASH = *import_SIGIL; *import_HASH = *import_SIGIL; | ||
| 219 | *import_CODE = *import_SIGIL; *import_CODE = *import_SIGIL; | ||
| 220 | |||
| 221 | sub dispatch_declare_pragma :MetaOnly { | ||
| 222 | (my $myPack, my Opts $opts, my ($pragma, @args)) = m4i_args(@_); | ||
| 223 | if ($pragma =~ /^[A-Za-z]/ | ||
| 224 | and my $sub = $myPack->can("declare_$pragma")) { | ||
| 225 | $sub->($myPack, $opts, @args); | ||
| 226 | } else { | ||
| 227 | croak "No such pragma: \`use $myPack\ [".terse_dump($pragma)."]`" | ||
| 228 | . $myPack->m4i_file_line_of($opts); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | #======================================== | ||
| 233 | |||
| 234 | # You may want to override these pragrams. | ||
| 235 | sub declare_default_pragma :MetaOnly { | ||
| 236 | (my $myPack, my Opts $opts) = m4i_args(@_); | ||
| 237 | $myPack->declare_c3($opts); | ||
| 238 | } | ||
| 239 | |||
| 240 | sub declare_strict :MetaOnly { | ||
| 241 | (my $myPack, my Opts $opts) = m4i_args(@_); | ||
| 242 | $_->import for qw(strict warnings); # I prefer fatalized warnings, but... | ||
| 243 | } | ||
| 244 | |||
| 245 | # Not enabled by default. | ||
| 246 | sub declare_fatal :MetaOnly { | ||
| 247 | (my $myPack, my Opts $opts) = m4i_args(@_); | ||
| 248 | warnings->import(qw(FATAL all NONFATAL misc)); | ||
| 249 | } | ||
| 250 | |||
| 251 | sub declare_c3 :MetaOnly { | ||
| 252 | (my $myPack, my Opts $opts) = m4i_args(@_); | ||
| 253 | mro::set_mro($opts->{destpkg}, 'c3'); | ||
| 254 | } | ||
| 255 | |||
| 256 | #======================================== | ||
| 257 | |||
| 258 | # | ||
| 259 | # Just for readability | ||
| 260 | # | ||
| 261 | # use XXX [import => qw/X Y Z/]; | ||
| 262 | # | ||
| 263 | sub declare_import :MetaOnly { | ||
| 264 | (my $myPack, my Opts $opts, my (@import)) = m4i_args(@_); | ||
| 265 | |||
| 266 | $myPack->dispatch_import_no_pragma($opts, @import); | ||
| 267 | } | ||
| 268 | |||
| 269 | #======================================== | ||
| 270 | |||
| 271 | sub declare_fileless_base :MetaOnly { | ||
| 272 | (my $myPack, my Opts $opts, my (@base)) = m4i_args(@_); | ||
| 273 | |||
| 274 | $myPack->declare___add_isa($opts->{objpkg}, @base); | ||
| 275 | |||
| 276 | $myPack->declare_fields($opts); | ||
| 277 | } | ||
| 278 | |||
| 279 | *declare_base = *declare_parent; *declare_base = *declare_parent; | ||
| 280 | |||
| 281 | sub declare_parent :MetaOnly { | ||
| 282 | (my $myPack, my Opts $opts, my (@base)) = m4i_args(@_); | ||
| 283 | |||
| 284 | foreach my $fn (@base) { | ||
| 285 | (my $cp = $fn) =~ s{::|\'}{/}g; | ||
| 286 | require "$cp.pm"; | ||
| 287 | } | ||
| 288 | |||
| 289 | $myPack->declare_fileless_base($opts, @base); | ||
| 290 | } | ||
| 291 | |||
| 292 | sub declare_as_base :MetaOnly { | ||
| 293 | (my $myPack, my Opts $opts, my (@fields)) = m4i_args(@_); | ||
| 294 | |||
| 295 | print STDERR "Class $opts->{objpkg} inherits $myPack\n" | ||
| 296 | if DEBUG; | ||
| 297 | |||
| 298 | $myPack->declare_default_pragma($opts); # strict, mro c3... | ||
| 299 | |||
| 300 | $myPack->declare___add_isa($opts->{objpkg}, $myPack); | ||
| 301 | |||
| 302 | $myPack->declare_fields($opts, @fields); | ||
| 303 | |||
| 304 | $myPack->declare_private_constant($opts, MY => $opts->{objpkg}, or_ignore => 1); | ||
| 305 | } | ||
| 306 | |||
| 307 | sub declare___add_isa :MetaOnly { | ||
| 308 | my ($myPack, $objpkg, @parents) = @_; | ||
| 309 | |||
| 310 | print STDERR "Class $objpkg extends ".terse_dump(@parents)."\n" | ||
| 311 | if DEBUG; | ||
| 312 | |||
| 313 | my $isa = MOP4Import::Util::isa_array($objpkg); | ||
| 314 | |||
| 315 | my $using_c3 = mro::get_mro($objpkg) eq 'c3'; | ||
| 316 | |||
| 317 | if (DEBUG) { | ||
| 318 | print STDERR " $objpkg (MRO=",mro::get_mro($objpkg),") ISA " | ||
| 319 | , terse_dump(mro::get_linear_isa($objpkg)), "\n"; | ||
| 320 | print STDERR " Adding $_ (MRO=",mro::get_mro($_),") ISA " | ||
| 321 | , terse_dump(mro::get_linear_isa($_)) | ||
| 322 | , "\n" for @parents; | ||
| 323 | } | ||
| 324 | |||
| 325 | my @new = grep { | ||
| 326 | my $parent = $_; | ||
| 327 | $parent ne $objpkg | ||
| 328 | and not grep {$parent eq $_} @$isa; | ||
| 329 | } @parents; | ||
| 330 | |||
| 331 | if ($using_c3) { | ||
| 332 | local $@; | ||
| 333 | foreach my $parent (@new) { | ||
| 334 | my $cur = mro::get_linear_isa($objpkg); | ||
| 335 | my $adding = mro::get_linear_isa($parent); | ||
| 336 | eval { | ||
| 337 | unshift @$isa, $parent; | ||
| 338 | # if ($] < 5.014) { | ||
| 339 | # mro::method_changed_in($objpkg); | ||
| 340 | # mro::get_linear_isa($objpkg); | ||
| 341 | # } | ||
| 342 | }; | ||
| 343 | if ($@) { | ||
| 344 | croak "Can't add base '$parent' to '$objpkg' (\n" | ||
| 345 | . " $objpkg ISA ".terse_dump($cur).")\n" | ||
| 346 | . " Adding $parent ISA ".terse_dump($adding) | ||
| 347 | ."\n) because of this error: " . $@; | ||
| 348 | } | ||
| 349 | } | ||
| 350 | } else { | ||
| 351 | push @$isa, @new; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | # I'm afraid this 'as' pragma could invite ambiguous interpretation. | ||
| 356 | # But in following case, I can't find any other pragma name. | ||
| 357 | # | ||
| 358 | # use XXX [as => 'YYY'] | ||
| 359 | # | ||
| 360 | # So, let's define `declare_as` as an alias of `declare_naming`. | ||
| 361 | # | ||
| 362 | *declare_as = *declare_naming; *declare_as = *declare_naming; | ||
| 363 | |||
| 364 | sub declare_naming :MetaOnly { | ||
| 365 | (my $myPack, my Opts $opts, my ($name)) = m4i_args(@_); | ||
| 366 | |||
| 367 | unless (defined $name and $name ne '') { | ||
| 368 | croak "Usage: use ${myPack} [naming => NAME]"; | ||
| 369 | } | ||
| 370 | |||
| 371 | $myPack->declare_constant($opts, $name => $myPack); | ||
| 372 | } | ||
| 373 | |||
| 374 | sub declare_inc :MetaOnly { | ||
| 375 | (my $myPack, my Opts $opts, my ($pkg)) = m4i_args(@_); | ||
| 376 | $pkg //= $opts->{objpkg}; | ||
| 377 | $pkg =~ s{::}{/}g; | ||
| 378 | $INC{$pkg . '.pm'} = 1; | ||
| 379 | } | ||
| 380 | |||
| 381 | sub declare_constant :MetaOnly { | ||
| 382 | (my $myPack, my Opts $opts, my ($name, $value, %opts)) = m4i_args(@_); | ||
| 383 | |||
| 384 | $myPack->declare_private_constant($opts, $name, $value, %opts); | ||
| 385 | |||
| 386 | my $export = MOP4Import::Util::ensure_symbol_has_array( | ||
| 387 | globref($opts->{objpkg}, 'EXPORT') | ||
| 388 | ); | ||
| 389 | push @$export, $name; | ||
| 390 | print STDERR " constant $name is added to default exports of " | ||
| 391 | . $opts->{objpkg}. "\n" if DEBUG; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub declare_private_constant :MetaOnly { | ||
| 395 | (my $myPack, my Opts $opts, my ($name, $value, %opts)) = m4i_args(@_); | ||
| 396 | |||
| 397 | my $or_ignore = delete $opts{or_ignore}; | ||
| 398 | if (keys %opts) { | ||
| 399 | Carp::croak("Unknown options: ". join ", ", sort keys(%opts)); | ||
| 400 | } | ||
| 401 | |||
| 402 | my $my_sym = globref($opts->{objpkg}, $name); | ||
| 403 | if (*{$my_sym}{CODE}) { | ||
| 404 | return if $or_ignore; | ||
| 405 | croak "constant $opts->{objpkg}::$name is already defined"; | ||
| 406 | } | ||
| 407 | |||
| 408 | MOP4Import::Util::define_constant($my_sym, $value); | ||
| 409 | } | ||
| 410 | |||
| 411 | sub declare_fields :MetaOnly { | ||
| 412 | (my $myPack, my Opts $opts, my (@fields)) = m4i_args(@_); | ||
| 413 | |||
| 414 | my $extended = fields_hash($opts->{objpkg}); | ||
| 415 | my $fields_array = fields_array($opts->{objpkg}); | ||
| 416 | |||
| 417 | my $isFirst = not $myPack->JSON_TYPE_HANDLER->lookup_json_type($opts->{objpkg}); | ||
| 418 | |||
| 419 | # Import all fields from super class | ||
| 420 | foreach my $super_class (@{*{globref($opts->{objpkg}, 'ISA')}{ARRAY}}) { | ||
| 421 | my $super = fields_hash($super_class); | ||
| 422 | next unless $super; | ||
| 423 | |||
| 424 | |||
| 425 | my $super_names = fields_array($super_class); | ||
| 426 | my @names = @$super_names ? @$super_names : keys %$super; | ||
| 427 | foreach my $name (@names) { | ||
| 428 | next if defined $extended->{$name}; | ||
| 429 | print STDERR " Field $opts->{objpkg}.$name is inherited " | ||
| 430 | . "from $super_class.\n" if DEBUG; | ||
| 431 | my $anyFieldSpec = $extended->{$name} = do { | ||
| 432 | my $origin = $super->{$name}; | ||
| 433 | # XXX: mark origin in this clone? | ||
| 434 | ref $origin ? $origin->clone : $origin; | ||
| 435 | }; | ||
| 436 | push @$fields_array, $name; | ||
| 437 | } | ||
| 438 | |||
| 439 | $myPack->JSON_TYPE_HANDLER->inherit_json_type($opts->{objpkg}, $super_class) if $isFirst; | ||
| 440 | |||
| 441 | } | ||
| 442 | |||
| 443 | { | ||
| 444 | my %dup; | ||
| 445 | foreach my $spec (@fields) { | ||
| 446 | my ($name) = ref $spec ? @$spec : $spec; | ||
| 447 | if ($dup{$name}++) { | ||
| 448 | croak "Duplicate field decl! $name"; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | } | ||
| 452 | |||
| 453 | |||
| 454 | my $field_class = do { | ||
| 455 | # XXX: objpkg? destpkg? | ||
| 456 | if (my $sub = $opts->{destpkg}->can("FieldSpec")) { | ||
| 457 | $sub->(); | ||
| 458 | } else { | ||
| 459 | $myPack->FieldSpec; | ||
| 460 | } | ||
| 461 | }; | ||
| 462 | |||
| 463 | print STDERR " FieldSpec is: $field_class\n" if DEBUG; | ||
| 464 | |||
| 465 | $myPack->JSON_TYPE_HANDLER->declare_json_type_record($opts->{objpkg}); | ||
| 466 | |||
| 467 | $myPack->declare___field($opts, $field_class, ref $_ ? @$_ : $_) for @fields; | ||
| 468 | |||
| 469 | $opts->{objpkg}; # XXX: | ||
| 470 | } | ||
| 471 | |||
| 472 | sub declare___field :MetaOnly { | ||
| 473 | (my $myPack, my Opts $opts, my ($field_class, $name, @rest)) = m4i_args(@_); | ||
| 474 | print STDERR " Declaring field $opts->{objpkg}.$name " if DEBUG; | ||
| 475 | my $extended = fields_hash($opts->{objpkg}); | ||
| 476 | my $fields_array = fields_array($opts->{objpkg}); | ||
| 477 | |||
| 478 | my $spec = fields_hash($field_class); | ||
| 479 | my (@early, @delayed); | ||
| 480 | while (my ($k, $v) = splice @rest, 0, 2) { | ||
| 481 | unless (defined $k) { | ||
| 482 | croak "Undefined field spec key for $opts->{objpkg}.$name in $opts->{callpack}"; | ||
| 483 | } | ||
| 484 | if ($k =~ /^[A-Za-z]/) { | ||
| 485 | if (my $sub = $myPack->can("declare___field_with_$k")) { | ||
| 486 | push @delayed, [$sub, $k, $v]; | ||
| 487 | next; | ||
| 488 | } elsif (exists $spec->{$k}) { | ||
| 489 | push @early, $k, $v; | ||
| 490 | next; | ||
| 491 | } | ||
| 492 | } | ||
| 493 | croak "Unknown option for $opts->{objpkg}.$name in $opts->{callpack}: " | ||
| 494 | . ".$k"; | ||
| 495 | } | ||
| 496 | |||
| 497 | my FieldSpec $fs = $extended->{$name} | ||
| 498 | = $field_class->new(@early, name => $name); | ||
| 499 | $fs->{package} = $opts->{objpkg}; | ||
| 500 | print STDERR " with $myPack $field_class => ", terse_dump($fs), "\n" | ||
| 501 | if DEBUG; | ||
| 502 | push @$fields_array, $name; | ||
| 503 | |||
| 504 | # Create accessor for all public fields. | ||
| 505 | if ($name =~ /^[a-z]/i and not $fs->{no_getter}) { | ||
| 506 | if (my $entry = MOP4Import::Util::symtab($opts->{objpkg})->{$name}) { | ||
| 507 | if (ref $entry eq 'CODE' | ||
| 508 | or ref \$entry eq 'GLOB' and *{$entry}{CODE}) { | ||
| 509 | croak "Accessor $opts->{objpkg}::$name is redefined!\nIf you really want to define the accessor by hand, please specify fields spec like: [$name => no_getter => 1, ...]."; | ||
| 510 | } | ||
| 511 | } | ||
| 512 | |||
| 513 | *{globref($opts->{objpkg}, $name)} | ||
| 514 | = Sub::Util::set_subname(join("::", $opts->{objpkg}, $name) | ||
| 515 | , $myPack->declare___accessor($opts, $name)); | ||
| 516 | } | ||
| 517 | |||
| 518 | foreach my $delayed (@delayed) { | ||
| 519 | my ($sub, $k, $v) = @$delayed; | ||
| 520 | $sub->($myPack, $opts, $fs, $k, $v); | ||
| 521 | } | ||
| 522 | |||
| 523 | if ($fs->{name} !~ /^_/) { | ||
| 524 | $myPack->JSON_TYPE_HANDLER->register_json_type_of_field( | ||
| 525 | $opts, $opts->{objpkg}, $fs->{name}, | ||
| 526 | $fs->{json_type} || $opts->{default_json_type} || 'string' | ||
| 527 | ); | ||
| 528 | } | ||
| 529 | |||
| 530 | $fs; | ||
| 531 | } | ||
| 532 | |||
| 533 | sub declare___accessor :MetaOnly { | ||
| 534 | (my $myPack, my Opts $opts, my ($name)) = m4i_args(@_); | ||
| 535 | return sub :method { $_[0]->{$name} }; | ||
| 536 | } | ||
| 537 | |||
| 538 | sub declare___field_with_default :MetaOnly { | ||
| 539 | (my $myPack, my Opts $opts, my FieldSpec $fs, my ($k, $v)) = m4i_args(@_); | ||
| 540 | |||
| 541 | $fs->{$k} = $v; | ||
| 542 | |||
| 543 | if (ref $v eq 'CODE') { | ||
| 544 | *{globref($opts->{objpkg}, "default_$fs->{name}")} = $v; | ||
| 545 | } else { | ||
| 546 | $myPack->declare_private_constant($opts, "default_$fs->{name}", $v); | ||
| 547 | } | ||
| 548 | } | ||
| 549 | |||
| 550 | sub JSON_TYPE_HANDLER { | ||
| 551 | require MOP4Import::Util::JSON_TYPE; | ||
| 552 | 'MOP4Import::Util::JSON_TYPE'; | ||
| 553 | } | ||
| 554 | |||
| 555 | sub declare_alias :MetaOnly { | ||
| 556 | (my $myPack, my Opts $opts, my ($name, $alias)) = m4i_args(@_); | ||
| 557 | print STDERR " Declaring alias $name in $opts->{destpkg} as $alias\n" if DEBUG; | ||
| 558 | my $sym = globref($opts->{destpkg}, $name); | ||
| 559 | if (*{$sym}{CODE}) { | ||
| 560 | croak "Subroutine (alias) $opts->{destpkg}::$name redefined"; | ||
| 561 | } | ||
| 562 | *$sym = sub () {$alias}; | ||
| 563 | } | ||
| 564 | |||
| 565 | # Set(override) default value for inherited fields | ||
| 566 | # | ||
| 567 | # [defaults => | ||
| 568 | # fieldName => defaultValue, ... | ||
| 569 | # ] | ||
| 570 | # | ||
| 571 | sub declare_defaults :MetaOnly { | ||
| 572 | (my $myPack, my Opts $opts, my (@kvlist)) = m4i_args(@_); | ||
| 573 | |||
| 574 | my $fields = fields_hash($opts->{objpkg}); | ||
| 575 | |||
| 576 | while (my ($k, $v) = splice @kvlist, 0, 2) { | ||
| 577 | my FieldSpec $fs = $fields->{$k} | ||
| 578 | or Carp::croak "No such field: $k"; | ||
| 579 | $fs->{default} = $v; | ||
| 580 | my $fn = "default_$k"; | ||
| 581 | $myPack->declare_private_constant($opts, $fn, $v); | ||
| 582 | } | ||
| 583 | } | ||
| 584 | *declare_override_defaults = *declare_defaults; | ||
| 585 | *declare_override_defaults = *declare_defaults; | ||
| 586 | |||
| 587 | sub declare_map_methods :MetaOnly { | ||
| 588 | (my $myPack, my Opts $opts, my (@pairs)) = m4i_args(@_); | ||
| 589 | |||
| 590 | foreach my $pair (@pairs) { | ||
| 591 | my ($from, $to) = @$pair; | ||
| 592 | my $sub = $opts->{objpkg}->can($to) | ||
| 593 | or croak "Can't find method $to in (parent of) $opts->{objpkg}"; | ||
| 594 | *{globref($opts->{objpkg}, $from)} = $sub; | ||
| 595 | } | ||
| 596 | } | ||
| 597 | |||
| 598 | sub declare_carp_not :MetaOnly { | ||
| 599 | (my $myPack, my Opts $opts, my (@carp_not)) = m4i_args(@_); | ||
| 600 | |||
| 601 | unless (@carp_not) { | ||
| 602 | push @carp_not, $myPack; | ||
| 603 | } | ||
| 604 | |||
| 605 | my $name = 'CARP_NOT'; | ||
| 606 | |||
| 607 | print STDERR "Declaring \@$opts->{objpkg}.$name = ".terse_dump(@carp_not) | ||
| 608 | if DEBUG; | ||
| 609 | |||
| 610 | *{globref($opts->{objpkg}, $name)} = \@carp_not; | ||
| 611 | } | ||
| 612 | |||
| 613 | { | ||
| 614 | # | ||
| 615 | # Below does equiv of `our @CARP_NOT = qw/ MOP4Import::Util /;` | ||
| 616 | # | ||
| 617 | __PACKAGE__->declare_carp_not(MOP4Import::Opts::m4i_fake_opts(__PACKAGE__), | ||
| 618 | qw/ | ||
| 619 | MOP4Import::Util | ||
| 620 | / | ||
| 621 | ); | ||
| 622 | } | ||
| 623 | |||
| 624 | 1; | ||
| 625 | |||
| 626 |