my @res;
my $has_triggers;
+ my $strict_constructor = $metaclass->__strict_constructor;
+
+
+ if($strict_constructor){
+ push @res, 'my $used = 0;';
+ }
for my $index (0 .. @$attrs - 1) {
my $code = '';
$code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
}
- $code .= "\n} else {\n";
+ if ($strict_constructor){
+ $code .= '$used++;' . "\n";
+ }
+
+ $code .= "\n} else {\n"; # $value exists
}
if ($attr->has_default || $attr->has_builder) {
push @res, $code;
}
+ if($strict_constructor){
+ push @res, q{if($used < keys %{$args})}
+ . q{{ Mouse::Meta::Method::Constructor::_report_unknown_args($metaclass, \@attrs, $instance, $args) }};
+ }
+
if($metaclass->is_anon_class){
push @res, q{$instance->{__METACLASS__} = $metaclass;};
}
if($has_triggers){
unshift @res, q{my @triggers;};
- push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
+ push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
}
return join "\n", @res;
return join "\n", @code;
}
+sub _report_unknown_args {
+ my($metaclass, $attrs, $instance, $args) = @_;
+
+ my @unknowns;
+ my %init_args;
+ foreach my $attr(@{$attrs}){
+ my $init_arg = $attr->init_arg;
+ if(defined $init_arg){
+ $init_args{$init_arg}++;
+ }
+ }
+
+ while(my $key = each %{$args}){
+ if(!exists $init_args{$key}){
+ push @unknowns, $key;
+ }
+ }
+
+ $metaclass->throw_error( sprintf
+ "Unknown attribute passed to the constructor of %s: %s",
+ ref($instance), join ', ', @unknowns
+ );
+}
+
1;
__END__
MOUSEf_XC_IS_IMMUTABLE = 0x0001,
MOUSEf_XC_IS_ANON = 0x0002,
MOUSEf_XC_HAS_BUILDARGS = 0x0004,
+ MOUSEf_XC_CONSTRUCTOR_IS_STRICT
+ = 0x0008,
MOUSEf_XC_mask = 0xFFFF /* not used */
};
flags |= MOUSEf_XC_HAS_BUILDARGS;
}
+ if(predicate_calls(metaclass, "__strict_constructor")){
+ flags |= MOUSEf_XC_CONSTRUCTOR_IS_STRICT;
+ }
+
av_store(xc, MOUSE_XC_FLAGS, newSVuv(flags));
av_store(xc, MOUSE_XC_ATTRALL, (SV*)attrall);
av_store(xc, MOUSE_XC_BUILDALL, (SV*)buildall);
}
static void
+mouse_report_unknown_args(pTHX_ SV* const meta, AV* const attrs, HV* const args) {
+ HV* const attr_map = newHV_mortal();
+ SV* const unknown = newSVpvs_flags("", SVs_TEMP);
+ I32 const len = AvFILLp(attrs) + 1;
+ I32 i;
+ HE* he;
+
+ for(i = 0; i < len; i++){
+ SV* const attr = MOUSE_av_at(attrs, i);
+ AV* const xa = mouse_get_xa(aTHX_ attr);
+ SV* const init_arg = MOUSE_xa_init_arg(xa);
+ if(SvOK(init_arg)){
+ (void)hv_store_ent(attr_map, init_arg, &PL_sv_undef, 0U);
+ }
+ }
+
+ hv_iterinit(args);
+ while((he = hv_iternext(args))){
+ SV* const key = hv_iterkeysv(he);
+ if(!hv_exists_ent(attr_map, key, 0U)){
+ sv_catpvf(unknown, "%"SVf", ", key);
+ }
+ }
+
+ if(SvCUR(unknown) > 0){
+ SvCUR(unknown) -= 2; /* chop "," */
+ }
+ else{
+ sv_setpvs(unknown, "(unknown)");
+ }
+
+ mouse_throw_error(meta, NULL,
+ "Unknown attribute passed to the constructor of %"SVf": %"SVf,
+ mcall0(meta, mouse_name), unknown);
+}
+
+
+
+static void
mouse_class_initialize_object(pTHX_ SV* const meta, SV* const object, HV* const args, bool const ignore_triggers) {
AV* const xc = mouse_get_xc(aTHX_ meta);
AV* const attrs = MOUSE_xc_attrall(xc);
I32 len = AvFILLp(attrs) + 1;
I32 i;
AV* triggers_queue = NULL;
+ I32 used = 0;
assert(meta || object);
assert(args);
triggers_queue = newAV_mortal();
}
+ /* for each attribute */
for(i = 0; i < len; i++){
SV* const attr = MOUSE_av_at(attrs, i);
AV* const xa = mouse_get_xa(aTHX_ attr);
av_push(triggers_queue, (SV*)pair);
}
+ used++;
}
else { /* no init arg */
if(flags & (MOUSEf_ATTR_HAS_DEFAULT | MOUSEf_ATTR_HAS_BUILDER)){
mouse_throw_error(attr, NULL, "Attribute (%"SVf") is required", slot);
}
}
- } /* for each attributes */
+ } /* for each attribute */
+
+ if(MOUSE_xc_flags(xc) & MOUSEf_XC_CONSTRUCTOR_IS_STRICT && used < HvUSEDKEYS(args)){
+ mouse_report_unknown_args(aTHX_ meta, attrs, args);
+ }
if(triggers_queue){
len = AvFILLp(triggers_queue) + 1;
INSTALL_SIMPLE_READER(Class, roles);
INSTALL_SIMPLE_PREDICATE_WITH_KEY(Class, is_anon_class, anon_serial_id);
INSTALL_SIMPLE_READER(Class, is_immutable);
+ INSTALL_SIMPLE_READER_WITH_KEY(Class, __strict_constructor, strict_constructor);
INSTALL_CLASS_HOLDER(Class, method_metaclass, "Mouse::Meta::Method");
INSTALL_CLASS_HOLDER(Class, attribute_metaclass, "Mouse::Meta::Attribute");