Changeset 15497 in webkit for trunk/JavaScriptCore/API/minidom.js


Ignore:
Timestamp:
Jul 17, 2006, 9:33:46 PM (19 years ago)
Author:
ggaren
Message:

Reviewed by Maciej.


  • Added automatic prototype creation for classes.


A class stores a weak reference to a prototype, which is cleared when
the prototype is garbage collected, to avoid a reference cycle.


We now have an attributes field in JSClassDefinition, that currently is
used only to override automatic prototype creation when you want to manage your
own prototypes, but can be extended in the future for other nefarious purposes.


Similarly, we have JSObjectMake and JSObjectMakeWithPrototype, the latter
allowing you to manage your own prototypes.


JSObjectMakeConstructor is more interesting now, able to make a constructor
on your behalf if you just give it a class.


  • Removed bogus old code from minidom.js.


  • Tweaked the headerdocs.


  • Added more GC testing, which caught some leaks, and tested more funny edge cases in lookup, which caught a lookup bug. Removed some testing we used to do with MyObject because it was redundant with the new, cool stuff.


While fixing the lookup bug I retracted this change:


"If a static setProperty callback returns 'false', to indicate that the
property was not set, we no longer forward the set request up the class
chain, because that's almost certainly not what the programmer expected."

Returning false when setting a static property is a little silly, but you can see
it being useful when shadowing a base class's static properties, and, regardless
of usefullness, this is the defined behavior of the setProperty callback.


  • Plus a little ASCII art, for the kids.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/minidom.js

    r15484 r15497  
    2424 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2525 */
    26 
    27 // For browser-based testing
    28 if (typeof window != 'undefined') {
    29     /*
    30     The methods and constructors below approximate what that the native host should provide
    31 
    32     print(string);
    33 
    34     Node
    35         |-- TextNode
    36         |-- Element
    37             |-- RootElement
    38             |-- HeroElement
    39             |-- VillainElement
    40             |-- NameElement
    41             |-- WeaponElement
    42         |-- Document
    43     */
    44      
    45     function print(string, indentLevel)
    46     {
    47         document.getElementById('pre').appendChild(document.createTextNode(string));
    48     }
    49    
    50     Node = function()
    51     {
    52         this.__defineGetter__("childNodes", function() {
    53             if (!this._childNodes)
    54                 this._childNodes = new Array();
    55             return this._childNodes;
    56         });
    57         this.__defineGetter__("firstChild", function () {
    58             return this.childNodes[0];
    59         });
    60     }
    61 
    62     Node.prototype.nodeType = "Node";
    63 
    64     Node.prototype.appendChild = function(child) {
    65         this.childNodes.push(child);
    66     }
    67    
    68     Node.prototype.serialize = function(numSpaces) {
    69         function printSpaces(n)
    70         {
    71             for (var i = 0; i < n; i++) // >
    72             print(" ");
    73         }
    74 
    75         printSpaces(numSpaces);
    76         print('<' + this.nodeType + '>' + '\n');
    77        
    78         var childNodesLength = this.childNodes.length;
    79         for (var i = 0; i < childNodesLength; i++) //>
    80             this.childNodes[i].serialize(numSpaces + 4);
    81        
    82         printSpaces(numSpaces);
    83         print('</' + this.nodeType + '>\n');
    84     }
    85 
    86     TextNode = function(text)
    87     {
    88         this.text = text;
    89     }
    90 
    91     TextNode.prototype = new Node();
    92    
    93     TextNode.prototype.nodeType = "Text";
    94    
    95     TextNode.prototype.serialize = function(numSpaces) {
    96         for (var i = 0; i < numSpaces; i++) // >
    97             print(" ");
    98         print(this.text + '\n');
    99     }
    100 
    101     Element = function()
    102     {
    103     }
    104    
    105     Element.prototype = new Node();
    106    
    107     Element.prototype.nodeType = "Element";
    108 
    109     RootElement = function()
    110     {
    111     }
    112    
    113     RootElement.prototype = new Element();
    114 
    115     RootElement.prototype.nodeType = "Root";
    116    
    117     HeroElement = function()
    118     {
    119     }
    120    
    121     HeroElement.prototype = new Element();
    122 
    123     HeroElement.prototype.nodeType = "Hero";
    124 
    125     VillainElement = function()
    126     {
    127     }
    128    
    129     VillainElement.prototype = new Element();
    130 
    131     VillainElement.prototype.nodeType = "Villain";
    132 
    133     NameElement = function()
    134     {
    135     }
    136    
    137     NameElement.prototype = new Element();
    138 
    139     NameElement.prototype.nodeType = "Name";
    140 
    141     WeaponElement = function()
    142     {
    143     }
    144    
    145     WeaponElement.prototype = new Element();
    146 
    147     WeaponElement.prototype.nodeType = "Weapon";
    148 
    149     Document = function()
    150     {
    151     }
    152 
    153     Document.prototype = new Node();
    154 
    155     Document.prototype.serialize = function() {
    156         this.firstChild.serialize(0);
    157     }
    158    
    159     Document.prototype.createElement = function(elementType) {
    160         return eval('new ' + elementType + 'Element()');
    161     }
    162 
    163     Document.prototype.createTextNode = function(text) {
    164         return new TextNode(text);
    165     }
    166 }
    16726
    16827function shouldBe(a, b)
     
    248107   
    249108    print(Node);
    250 
    251     /*
    252     var element, name, weapon;
    253    
    254     var document = new Document();
    255     document.appendChild(document.createElement('Root'));
    256 
    257     // Tank Girl
    258     element = document.createElement('Hero');
    259 
    260     name = document.createElement('Name');
    261     name.appendChild(document.createTextNode('Tank Girl'));
    262     element.appendChild(name);
    263    
    264     weapon = document.createElement('Weapon');
    265     weapon.appendChild(document.createTextNode('Tank'));
    266     element.appendChild(weapon);
    267    
    268     weapon = document.createElement('Weapon');
    269     weapon.appendChild(document.createTextNode('Attitude'));
    270     element.appendChild(weapon);
    271    
    272     weapon = document.createElement('Weapon');
    273     weapon.appendChild(document.createTextNode('Army of genetically engineered super-kangaroos'));
    274     element.appendChild(weapon);
    275    
    276     document.firstChild.appendChild(element);
    277 
    278 
    279     // Skeletor
    280     element = document.createElement('Villain');
    281 
    282     name = document.createElement('Name');
    283     name.appendChild(document.createTextNode('Skeletor'));
    284     element.appendChild(name);
    285    
    286     weapon = document.createElement('Weapon');
    287     weapon.appendChild(document.createTextNode('Havok Staff'));
    288     element.appendChild(weapon);
    289    
    290     weapon = document.createElement('Weapon');
    291     weapon.appendChild(document.createTextNode('Motley crew of henchmen'));
    292     element.appendChild(weapon);
    293    
    294     document.firstChild.appendChild(element);
    295 
    296     // Serialize
    297     document.serialize();
    298     */
    299109}
    300110
Note: See TracChangeset for help on using the changeset viewer.