KJS: reverseList()

Harri Porten porten@trolltech.com
Sun, 2 Feb 2003 04:35:14 +0100 (CET)


Hi !

I see some reverseList() functions in JavaScriptCore's parse nodes. I
assume they had to be introduced them when enhancing those nodes to be
evaluated in a simple for-loop rather than by slower recursive
evaluate() calls.

Here's an alternative solution I would like to propose. It has been
implemented for object literals and avoids a post-creation reversion
by a slighty modified construction.

The grammar rules, actions and node evaluation stay the same. Only the
constructors of PropertyValueNode and ObjectLiteralNode are changed to
(mis-)use the "list" pointer of the list tail.

class PropertyValueNode : public Node {
  public:
    // list is circular during construction, cut in ObjectLiteralNode ctor
    PropertyValueNode(PropertyNode *n, Node *a)
      : name(n), assign(a), list(this) { }
    PropertyValueNode(PropertyNode *n, Node *a, PropertyValueNode *l)
      : name(n), assign(a), list(l) { l->list = this; }
    // ...
};

class ObjectLiteralNode : public Node {
  public:
    // empty literal
    ObjectLiteralNode() : list(0) { }
    // l points to last list element, get and detach pointer to first one
    ObjectLiteralNode(PropertyValueNode *l) : list(l->list) { l->list = 0; 
}
    // ...
  };

Upon request I can provide some ASCII art for illustration :)

Harri.