[Laszlo-dev] SWF9: Need for with(this)

Donald Anderson dda at ddanderson.com
Fri Dec 7 10:27:37 PST 2007


On Dec 7, 2007, at 12:36 PM, P T Withington wrote:

> Maybe we just need to think a bit more about how we are going to  
> convert the user application into something compilable.
>
> I guess we decided that the user application needs to be a class?   
> The question is, whether the globals in the old app should be  
> instance variables, or, should they be class variables?  If  
> LzTestManager were a class variable, then it would be 'in scope'  
> without needing `with(this)`.
>
I just tried
   static var LzTestManager
and it is not seen.   I need to qualify it ---
    DefaultApplication.LzTestManager.failAssertion(errmsg);

> But, I think the real problem here is just that LzTestManager needs  
> to be class-ified, rather than trying to simulate the old Object  
> way of doing things.  In which case the reference to LzTestManager  
> would just need to be replaced with `this`, or, removed altogether  
> if you rely on AS3's implicit with this inside methods

there is an implicit this inside methods, but this is a closure, so  
explicit qualification appears to be needed.
In java for this situation you have access to anything declared  
final, but final cannot be used on vars in AS3.

Are you suggesting .lzx code changes?

> (which reminds me:  the compiler should _not_ insert it's own `with 
> (this)` when generating for AS3, and makes me wonder if we should  
> simulate the same in our script class methods in addition to LZX  
> methods).
>
> On 2007-12-07, at 12:25 EST, Donald Anderson wrote:
>
>> I'm trying to get lztest-node compiled in SWF9, and I see a  
>> compiler error
>> coming from the included lztestmanager.lzx:
>>
>> LzTestManager.assertTrue = function(condition, assertion) {
>>    if (!condition)  {
>>        var errmsg = "FAIL: assertTrue('" + condition + "') failed"
>>                    + (assertion ? ': ' + assertion : '');
>> ==>        LzTestManager.failAssertion(errmsg);
>>    }
>> }
>>
>> On the indicated line, LzTestManager shows up as undefined.
>> I've declared LzTestManager as an instance variable of the
>> application class that was created, so the LzTestManager.assertTrue
>> reference is not a problem.  The indicated one is a problem  
>> because the
>> associated block for a function expression does not automatically
>> inherit the surrounding scope.
>>
>> What is needed is a with(this), like:
>>
>> LzTestManager.assertTrue = function(condition, assertion) {
>>  with (this) {
>>    if (!condition)  {
>>        var errmsg = "FAIL: assertTrue('" + condition + "') failed"
>>                    + (assertion ? ': ' + assertion : '');
>>        LzTestManager.failAssertion(errmsg);
>>    }
>>  }
>> }
>>
>> I could do this when we generate code for function expression blocks.
>> One concern is that this may be done in a static method or block
>> (where there is no 'this').  I think we can detect if we're in a  
>> static
>> context, so I'm not too worried about that.  The other approach is
>> adding with(this) to existing code -- is that out of the question?
>> I know there are many of these constructions in our source tree,
>> and I'm guessing it would affect user's code, you all know that  
>> better than I.
>>
>> - Don
>>
>> --
>>
>> Don Anderson
>> Java/C/C++, Berkeley DB, systems consultant
>>
>> Voice:  617-547-7881
>> Email:  dda at ddanderson.com
>> WWW:    http://www.ddanderson.com
>>
>


--

Don Anderson
Java/C/C++, Berkeley DB, systems consultant

Voice:  617-547-7881
Email:  dda at ddanderson.com
WWW:    http://www.ddanderson.com



More information about the Laszlo-dev mailing list