kjs: variables with/without var [PATCH]

David Faure dfaure at klaralvdalens-datakonsult.se
Thu Jan 23 21:50:23 GMT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thursday 23 January 2003 22:19, Harri Porten wrote:
> On Thu, 23 Jan 2003, David Faure wrote:
> 
> > On Thursday 23 January 2003 21:53, David Faure wrote:
> > > Let's see ... Oh yes, you're right. At least in mozilla, alert(self) shows that
> > > the global object is indeed the frameset, in SomeFunc(), even when called
> > > from the frame.
> > 
> > Flawed reasoning. It simply means that the frameset's globalobject is in
> > the scope chain (and that the calling frame's globalobject is either not
> > in the scope chain, or is after the frameset's one).
> 
> Hmmm. Okay, that's where the self property is found. Examing "this" might
> bring final clarity.

alert(this.name) (with a name var declared in both globalobjects)
shows "frameset" in mozilla and IE.
"this" is resolved directly, without using the scope chain, right?
Hmm, then you're right, they "switch" the global object. Eeek.

> > I believe Maciej is correct then. The right place to put new vars is the last
> > item in the scope chain. Will try that (this bugfix is quite urgent, the 3.1 code
> > has been updated to include this, it seems :(((
> 
> I agree that this is the safest bet for a quick 3.1 fix. In the long run
> we might want to consider if the equality of global object and last item
> is 100% true and then make them synonyms. Ideally by Maciej's second
> suggestion of modifying the getter function. For the sake of clarity vs.
> a mixed usage.

Ok. Hmm, I'm a bit confused about one thing. Theorically, with the fix below
we should still fail the test on "this", since we don't switch the global object, right?
Well strangely enough it works - this.name is 'frameset' with the patch below
applied.

Index: internal.cpp
===================================================================
RCS file: /home/kde/kdelibs/kjs/internal.cpp,v
retrieving revision 1.141
diff -u -p -r1.141 internal.cpp
- --- internal.cpp        16 Jan 2003 23:30:52 -0000      1.141
+++ internal.cpp        23 Jan 2003 21:48:03 -0000
@@ -256,7 +256,23 @@ void Reference2::putValue(ExecState *exe
                   propertyName()).cstring().c_str(), w);
 #endif
   if (bs.type() == NullType)
- -    exec->interpreter()->globalObject().put(exec, propertyName(), w);
+  {
+    // Declare new variable in the right (lexically scoped) global object
+    // which is the last item in the scope chain
+    List chain = exec->context().scopeChain();
+    if ( chain.isEmpty() )
+      fprintf( stderr, "KJS: Reference2::putValue: empty scope chain!\n" );
+    else
+    {
+      ListIterator last = chain.end();
+      --last;
+      Object varObj = Object::dynamicCast( *last );
+      if ( varObj.isValid() )
+        varObj.put(exec, propertyName(), w);
+      else // shouldn't happen
+        fprintf( stderr, "KJS: Reference2::putValue: scope chain contains non-object!\n" );
+    }
+  }
   else
     static_cast<ObjectImp*>(bs.imp())->put(exec, propertyName(), w);
 }

(any shorter way of doing this? Didn't find a List::last() ...)

- -- 
David Faure -- faure at kde.org, dfaure at klaralvdalens-datakonsult.se
Klarälvdalens Datakonsult AB, Platform-independent software solutions
Contributing to: http://www.konqueror.org/, http://www.koffice.org/
KOffice-1.2.1 is available - http://download.kde.org/stable/koffice-1.2.1/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE+MGOf72KcVAmwbhARAtv3AJ46H3cce+9hlMys00ywAGqGGcZNLQCfcL61
aDdnemzM0HDgU/v32m42KNk=
=PYj/
-----END PGP SIGNATURE-----





More information about the kfm-devel mailing list