Assigning to globals in strict mode

Oliver Hunt oliver at apple.com
Tue Oct 19 12:05:38 PDT 2010


On Oct 19, 2010, at 11:59 AM, Mark S. Miller wrote:

> 
> 
> On Tue, Oct 19, 2010 at 11:49 AM, Oliver Hunt <oliver at apple.com> wrote:
> 
> On Oct 19, 2010, at 10:30 AM, Jason Orendorff wrote:
> 
> > On Mon, Oct 18, 2010 at 12:12 PM, Oliver Hunt <oliver at apple.com> wrote:
> >>>> "use strict"; undeclared = (this.undeclared = 5);
> >>> * The left hand side evaluates to an unresolveable strict Reference
> >>> (§10.2.2.1);
> >>> * the right hand side evaluates to 5;
> >>> * assignment to the Reference throws, because it is still
> >>> unresolveable (see IsUnresolvableReference, §8.7), even though
> >>> undeclared now exists.
> >>
> >> Err when does the exception get thrown?
> >> I just realised the spec does not state explicitly when the error should be
> >> thrown
> >> "When an assignment occurs within strict mode code, its LeftHandSide must
> >> not evaluate to an unresolvable reference. If it does a ReferenceError
> >> exception is thrown upon assignment."
> >> Does 'assignment' in that sentence mean when we reach the assignment
> >> expression, or does it mean at the point of PutValue()?
> >
> > That part is a note, so I think it's just meant to summarize the more
> > explicit parts of the spec. As far as I can tell, the ReferenceError
> > is thrown in PutValue step 5. (The note also mentions some cases where
> > strict assignment triggers a TypeError; that happens in
> > [[DefineOwnProperty]].)
> >
> > -j
> I interpreted this as meaning the exception occurs when we fail to find a reference to write to. eg. immediately following the evaluation of the LHS.
> 
> This also results in
> 
> "use strict"; undeclared = global.undeclared = 5;
> 
> throwing without creating the global property undeclared.
> 
> Minefield nightlies currently throw after evaluating rhs, webkit throws after the lhs is evaluated.
> 
> Given I think both interpretations are valid the spec really should be updated to state the exact moment that an exception is thrown.
> 
> I agree. Which interpretation do you recommend?

I honestly think that throwing after LHS is evaluated and found to be an undefined reference makes the most sense -- it prevents the example i brought up earlier from really being a problem (as you never get to the point where the global is created) and prevents you from causing any side effects prior to the exception being thrown.

OTOH if we thought of this in terms of getters/setters:

o = {get foo (){}, set foo(){ throw "argh!"; }}
with (o)
  foo = 5;

Doesn't throw until the [[Put]] occurs.

--Oliver

> 
>  
> 
> --Oliver
> 
> _______________________________________________
> es5-discuss mailing list
> es5-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es5-discuss
> 
> 
> 
> -- 
>     Cheers,
>     --MarkM

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es5-discuss/attachments/20101019/bd22a1ec/attachment.html>


More information about the es5-discuss mailing list