Variable override

Maciej Stachowiak mjs at apple.com
Sun Apr 17 20:57:59 CEST 2005


On Apr 16, 2005, at 12:25 PM, Harri Porten wrote:

> Hi again,
>
> I just looked at the following code in JavaScriptCore'S
> VarDeclNode::evaluate() function:
>
>     // already declared? - check with getDirect so you can override
>     // built-in properties of the global object with var declarations.
>     if ( variable.imp()->getDirect(ident) )
>         return Value();

I made this change 2 years ago so I don't remember the reasons very  
well, but here's what the corresponding ChangeLog comment says:

         - fixed 3239961 - www.phiffer.com doesn't work; uses "var  
top; top = n;"

Here's the test case:

var top;
top = 3;
alert(top);


In Safari this now properly shows "3" in the alert, it used to show  
"[object Window]".


> I am aware of the difference between hasProperty() (what KJS uses) and
> getDirect(). But I don't quite understand the difference this  
> change makes
> here. After all VarDeclNode::processVarDecls() uses hasProperty()  
> and will
> "put" an Undefined() value into the variable object. In fact I  
> wonder why
> both versions of the libraries do have to make the check twice at all.

It seems wrong for the checks to be mismatched, and a bit weird that  
both exist. I think at the time I made this change I did not  
understand the code very well. I think both checks should be using  
getDirect.

>
> I've tried to come up with a test case demonstrating a difference in
> behavior but I failed. I tried
>
>  var NaN;
>  document.write(NaN);

I would not expect this to show a difference since NaN is added by  
putting a normal property into the global object, rather than being a  
special property like the built-in properties of the window object.

Regards,
Maciej




More information about the Khtml-devel mailing list