super.prop assignment can silently overwrite non-writable properties

Rick Waldron waldron.rick at gmail.com
Mon Apr 20 18:11:57 UTC 2015


On Mon, Apr 20, 2015 at 1:45 PM Allen Wirfs-Brock <allen at wirfs-brock.com>
wrote:

>
> On Apr 20, 2015, at 9:38 AM, Jason Orendorff wrote:
>
> > We're implementing `super` in Firefox, and ran across this surprising
> behavior:
> >
> >    class X {
> >        constructor() {
> >            Object.defineProperty(this, "prop", {
> >                configurable: true,
> >                writable: false,
> >                value: 1});
> >        }
> >        f() {
> >            super.prop = 2;  // overwrites non-writable property!
> >        }
> >    }
> >
> >    var x = new X();
> >    x.f();
> >    assertEq(x.prop, 2);  // passes
> >
> > In the spec, 9.1.9 step 4.d.i. is where `super.prop = 2` ends up, with
> > O=X.prototype.
>
> 4.d.1 doesn't set the property, it just comes up with the property
> descriptor to use, if the `Receiver` does not already have a corresponding
> own property.
>
> 5.c+5.e checks if the corresponding own property actually exists on the
> `Receiver`.  If it already exits then it does a [[DefineOwnProperty]] that
> only specifies the `value` attribute. This should respect the current
> `writable` attribute of the property and hence reject the attempt to change
> the value.
>
> So, sounds like a FF bug to me.  Of course, there might also be a problem
> in the spec. that I'm blind to.
>

V8 has implemented the same behavior that Jason has reported.

If it's determined that this behaviour is undesirable, implementations
could agree to do something like:


14.5.1 Static Semantics: Early Errors

  (extend with...)

  ClassTail : ClassHeritage_opt { ClassBody }

  ClassBody : ClassElementList

  ClassElementList : ClassElement

  ClassElement : MethodDefinition

  ClassElement : static MethodDefinition

    - It is a Syntax Error if ClassHeritage is not present and
HasSuperProperty of MethodDefinition is true.



Static Semantics: HasSuperProperty

MethodDefinition : PropertyName ( StrictFormalParameters ) { FunctionBody }

  1. If StrictFormalParameters Contains SuperProperty is true, return true.
  2. Return FunctionBody Contains SuperProperty.

MethodDefinition : get PropertyName ( ) { FunctionBody }

  1. Return FunctionBody Contains SuperProperty.

MethodDefinition : set PropertyName ( PropertySetParameterList ) {
FunctionBody }

  1. If PropertySetParameterList Contains SuperProperty is true, return
true.
  2. Return FunctionBody Contains SuperProperty.


Which can then be added to ES2016. Note that the above is adapted from
HasDirectSuper.

Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150420/3d9d03dc/attachment-0001.html>


More information about the es-discuss mailing list