Rename virtual getConstructData to getConstructDataVirtual
[WebKit-https.git] / Source / WebCore / bindings / scripts / CodeGeneratorJS.pm
1 #
2 # Copyright (C) 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 # Copyright (C) 2006 Anders Carlsson <andersca@mac.com>
4 # Copyright (C) 2006, 2007 Samuel Weinig <sam@webkit.org>
5 # Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org>
6 # Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
7 # Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au>
8 # Copyright (C) Research In Motion Limited 2010. All rights reserved.
9 # Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
10 # Copyright (C) 2011 Patrick Gansterer <paroga@webkit.org>
11 #
12 # This library is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU Library General Public
14 # License as published by the Free Software Foundation; either
15 # version 2 of the License, or (at your option) any later version.
16 #
17 # This library is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 # Library General Public License for more details.
21 #
22 # You should have received a copy of the GNU Library General Public License
23 # along with this library; see the file COPYING.LIB.  If not, write to
24 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 # Boston, MA 02110-1301, USA.
26
27 package CodeGeneratorJS;
28
29 use strict;
30
31 my ($codeGenerator, $IMPL, $HEADER, $DEPS);
32
33 my $module = "";
34 my $outputDir = "";
35 my $writeDependencies = 0;
36
37 my @headerContentHeader = ();
38 my @headerContent = ();
39 my %headerIncludes = ();
40 my %headerTrailingIncludes = ();
41
42 my @implContentHeader = ();
43 my @implContent = ();
44 my %implIncludes = ();
45 my @depsContent = ();
46 my $numCachedAttributes = 0;
47 my $currentCachedAttribute = 0;
48
49 # Default .h template
50 my $headerTemplate = << "EOF";
51 /*
52     This file is part of the WebKit open source project.
53     This file has been generated by generate-bindings.pl. DO NOT MODIFY!
54
55     This library is free software; you can redistribute it and/or
56     modify it under the terms of the GNU Library General Public
57     License as published by the Free Software Foundation; either
58     version 2 of the License, or (at your option) any later version.
59
60     This library is distributed in the hope that it will be useful,
61     but WITHOUT ANY WARRANTY; without even the implied warranty of
62     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
63     Library General Public License for more details.
64
65     You should have received a copy of the GNU Library General Public License
66     along with this library; see the file COPYING.LIB.  If not, write to
67     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
68     Boston, MA 02110-1301, USA.
69 */
70 EOF
71
72 # Default constructor
73 sub new
74 {
75     my $object = shift;
76     my $reference = { };
77
78     $codeGenerator = shift;
79     $outputDir = shift;
80     shift; # $outputHeadersDir
81     shift; # $useLayerOnTop
82     shift; # $preprocessor
83     $writeDependencies = shift;
84
85     bless($reference, $object);
86     return $reference;
87 }
88
89 sub finish
90 {
91     my $object = shift;
92
93     # Commit changes!
94     $object->WriteData();
95 }
96
97 sub leftShift($$) {
98     my ($value, $distance) = @_;
99     return (($value << $distance) & 0xFFFFFFFF);
100 }
101
102 # Params: 'domClass' struct
103 sub GenerateInterface
104 {
105     my $object = shift;
106     my $dataNode = shift;
107     my $defines = shift;
108
109     $codeGenerator->LinkOverloadedFunctions($dataNode);
110
111     # Start actual generation
112     if ($dataNode->extendedAttributes->{"Callback"}) {
113         $object->GenerateCallbackHeader($dataNode);
114         $object->GenerateCallbackImplementation($dataNode);
115     } else {
116         $object->GenerateHeader($dataNode);
117         $object->GenerateImplementation($dataNode);
118     }
119
120     my $name = $dataNode->name;
121
122     # Open files for writing
123     my $headerFileName = "$outputDir/JS$name.h";
124     my $implFileName = "$outputDir/JS$name.cpp";
125     my $depsFileName = "$outputDir/JS$name.dep";
126
127     # Remove old dependency file.
128     unlink($depsFileName);
129
130     open($IMPL, ">$implFileName") || die "Couldn't open file $implFileName";
131     open($HEADER, ">$headerFileName") || die "Couldn't open file $headerFileName";
132     if (@depsContent) {
133         open($DEPS, ">$depsFileName") || die "Couldn't open file $depsFileName";
134     }
135 }
136
137 sub GenerateAttributeEventListenerCall
138 {
139     my $className = shift;
140     my $implSetterFunctionName = shift;
141     my $windowEventListener = shift;
142
143     my $wrapperObject = $windowEventListener ? "globalObject" : "thisObject";
144     my @GenerateEventListenerImpl = ();
145
146     if ($className eq "JSSVGElementInstance") {
147         # SVGElementInstances have to create JSEventListeners with the wrapper equal to the correspondingElement
148         $wrapperObject = "asObject(correspondingElementWrapper)";
149
150         push(@GenerateEventListenerImpl, <<END);
151     JSValue correspondingElementWrapper = toJS(exec, castedThis->globalObject(), imp->correspondingElement());
152     if (correspondingElementWrapper.isObject())
153 END
154
155         # Add leading whitespace to format the imp->set... line correctly
156         push(@GenerateEventListenerImpl, "    ");
157     }
158
159     push(@GenerateEventListenerImpl, "    imp->set$implSetterFunctionName(createJSAttributeEventListener(exec, value, $wrapperObject));\n");
160     return @GenerateEventListenerImpl;
161 }
162
163 sub GenerateEventListenerCall
164 {
165     my $className = shift;
166     my $functionName = shift;
167     my $passRefPtrHandling = ($functionName eq "add") ? "" : ".get()";
168
169     $implIncludes{"JSEventListener.h"} = 1;
170
171     my @GenerateEventListenerImpl = ();
172     my $wrapperObject = "castedThis";
173     if ($className eq "JSSVGElementInstance") {
174         # SVGElementInstances have to create JSEventListeners with the wrapper equal to the correspondingElement
175         $wrapperObject = "asObject(correspondingElementWrapper)";
176
177         push(@GenerateEventListenerImpl, <<END);
178     JSValue correspondingElementWrapper = toJS(exec, castedThis->globalObject(), imp->correspondingElement());
179     if (!correspondingElementWrapper.isObject())
180         return JSValue::encode(jsUndefined());
181 END
182     }
183
184     push(@GenerateEventListenerImpl, <<END);
185     JSValue listener = exec->argument(1);
186     if (!listener.isObject())
187         return JSValue::encode(jsUndefined());
188     imp->${functionName}EventListener(ustringToAtomicString(exec->argument(0).toString(exec)), JSEventListener::create(asObject(listener), $wrapperObject, false, currentWorld(exec))$passRefPtrHandling, exec->argument(2).toBoolean(exec));
189     return JSValue::encode(jsUndefined());
190 END
191     return @GenerateEventListenerImpl;
192 }
193
194 # Params: 'idlDocument' struct
195 sub GenerateModule
196 {
197     my $object = shift;
198     my $dataNode = shift;
199
200     $module = $dataNode->module;
201 }
202
203 sub GetParentClassName
204 {
205     my $dataNode = shift;
206
207     return $dataNode->extendedAttributes->{"LegacyParent"} if $dataNode->extendedAttributes->{"LegacyParent"};
208     return "JSDOMWrapper" if (@{$dataNode->parents} eq 0);
209     return "JS" . $codeGenerator->StripModule($dataNode->parents(0));
210 }
211
212 sub GetVisibleClassName
213 {
214     my $className = shift;
215
216     return "DOMException" if $className eq "DOMCoreException";
217     return "FormData" if $className eq "DOMFormData";
218     return "MimeType" if $className eq "DOMMimeType";
219     return "MimeTypeArray" if $className eq "DOMMimeTypeArray";
220     return "Plugin" if $className eq "DOMPlugin";
221     return "PluginArray" if $className eq "DOMPluginArray";
222     
223     return $className;
224 }
225
226 sub GetCallbackClassName
227 {
228     my $className = shift;
229
230     return "JSCustomVoidCallback" if $className eq "VoidCallback";
231     return "JS$className";
232 }
233
234 sub IndexGetterReturnsStrings
235 {
236     my $type = shift;
237
238     return 1 if $type eq "CSSStyleDeclaration" or $type eq "MediaList" or $type eq "DOMStringList" or $type eq "DOMTokenList" or $type eq "DOMSettableTokenList";
239     return 0;
240 }
241
242 sub AddIncludesForTypeInImpl
243 {
244     my $type = $codeGenerator->StripModule(shift);
245     my $isCallback = @_ ? shift : 0;
246     
247     AddIncludesForType($type, $isCallback, \%implIncludes);
248     
249     # additional includes (things needed to compile the bindings but not the header)
250     if ($type eq "CanvasRenderingContext2D") {
251         $implIncludes{"CanvasGradient.h"} = 1;
252         $implIncludes{"CanvasPattern.h"} = 1;
253         $implIncludes{"CanvasStyle.h"} = 1;
254     }
255
256     if ($type eq "CanvasGradient" or $type eq "XPathNSResolver" or $type eq "MessagePort") {
257         $implIncludes{"PlatformString.h"} = 1;
258     }
259
260     if ($type eq "Document") {
261         $implIncludes{"NodeFilter.h"} = 1;
262     }
263
264     if ($type eq "MediaQueryListListener") {
265         $implIncludes{"MediaQueryListListener.h"} = 1;
266     }
267 }
268
269 sub AddIncludesForTypeInHeader
270 {
271     my $type = $codeGenerator->StripModule(shift);
272     my $isCallback = @_ ? shift : 0;
273     
274     AddIncludesForType($type, $isCallback, \%headerIncludes);
275 }
276
277 sub AddIncludesForType
278 {
279     my $type = shift;
280     my $isCallback = shift;
281     my $includesRef = shift;
282
283     # When we're finished with the one-file-per-class
284     # reorganization, we won't need these special cases.
285     if ($codeGenerator->IsPrimitiveType($type) or $codeGenerator->AvoidInclusionOfType($type)
286         or $type eq "DOMString" or $type eq "DOMObject" or $type eq "Array" or $type eq "DOMTimeStamp") {
287     } elsif ($type =~ /SVGPathSeg/) {
288         my $joinedName = $type;
289         $joinedName =~ s/Abs|Rel//;
290         $includesRef->{"${joinedName}.h"} = 1;
291     } elsif ($type eq "XPathNSResolver") {
292         $includesRef->{"JSXPathNSResolver.h"} = 1;
293         $includesRef->{"JSCustomXPathNSResolver.h"} = 1;
294     } elsif ($isCallback) {
295         $includesRef->{"JS${type}.h"} = 1;
296     } else {
297         # default, include the same named file
298         $includesRef->{"${type}.h"} = 1;
299     }
300 }
301
302 # FIXME: This method will go away once all SVG animated properties are converted to the new scheme.
303 sub AddIncludesForSVGAnimatedType
304 {
305     my $type = shift;
306     $type =~ s/SVGAnimated//;
307
308     if ($type eq "Point" or $type eq "Rect") {
309         $implIncludes{"Float$type.h"} = 1;
310     } elsif ($type eq "String") {
311         $implIncludes{"PlatformString.h"} = 1;
312     }
313 }
314
315 sub AddToImplIncludes
316 {
317     my $header = shift;
318     my $conditional = shift;
319
320     if (not $conditional) {
321         $implIncludes{$header} = 1;
322     } elsif (not exists($implIncludes{$header})) {
323         $implIncludes{$header} = $conditional;
324     } else {
325         my $oldValue = $implIncludes{$header};
326         if ($oldValue ne 1) {
327             my %newValue = ();
328             $newValue{$conditional} = 1;
329             foreach my $condition (split(/\|/, $oldValue)) {
330                 $newValue{$condition} = 1;
331             }
332             $implIncludes{$header} = join("|", sort keys %newValue);
333         }
334     }
335 }
336
337 sub IsScriptProfileType
338 {
339     my $type = shift;
340     return 1 if ($type eq "ScriptProfileNode");
341     return 0;
342 }
343
344 sub AddTypedefForScriptProfileType
345 {
346     my $type = shift;
347     (my $jscType = $type) =~ s/Script//;
348
349     push(@headerContent, "typedef JSC::$jscType $type;\n\n");
350 }
351
352 sub AddClassForwardIfNeeded
353 {
354     my $implClassName = shift;
355
356     # SVGAnimatedLength/Number/etc. are typedefs to SVGAnimatedTemplate, so don't use class forwards for them!
357     unless ($codeGenerator->IsSVGAnimatedType($implClassName) or IsScriptProfileType($implClassName)) {
358         push(@headerContent, "class $implClassName;\n\n");
359     # ScriptProfile and ScriptProfileNode are typedefs to JSC::Profile and JSC::ProfileNode.
360     } elsif (IsScriptProfileType($implClassName)) {
361         $headerIncludes{"<profiler/ProfileNode.h>"} = 1;
362         AddTypedefForScriptProfileType($implClassName);
363     }
364 }
365
366 sub HashValueForClassAndName
367 {
368     my $class = shift;
369     my $name = shift;
370
371     # SVG Filter enums live in WebCore namespace (platform/graphics/)
372     if ($class =~ /^SVGFE*/ or $class =~ /^SVGComponentTransferFunctionElement$/) {
373         return "WebCore::$name";
374     }
375
376     return "${class}::$name";
377 }
378
379 sub hashTableAccessor
380 {
381     my $noStaticTables = shift;
382     my $className = shift;
383     if ($noStaticTables) {
384         return "get${className}Table(exec)";
385     } else {
386         return "&${className}Table";
387     }
388 }
389
390 sub prototypeHashTableAccessor
391 {
392     my $noStaticTables = shift;
393     my $className = shift;
394     if ($noStaticTables) {
395         return "get${className}PrototypeTable(exec)";
396     } else {
397         return "&${className}PrototypeTable";
398     }
399 }
400
401 sub GenerateConditionalStringFromAttributeValue
402 {
403     my $conditional = shift;
404     if ($conditional =~ /&/) {
405         return "ENABLE(" . join(") && ENABLE(", split(/&/, $conditional)) . ")";
406     } elsif ($conditional =~ /\|/) {
407         return "ENABLE(" . join(") || ENABLE(", split(/\|/, $conditional)) . ")";
408     } else {
409         return "ENABLE(" . $conditional . ")";
410     }
411 }
412
413 sub GenerateConditionalString
414 {
415     my $node = shift;
416     my $conditional = $node->extendedAttributes->{"Conditional"};
417     if ($conditional) {
418         return GenerateConditionalStringFromAttributeValue($conditional);
419     } else {
420         return "";
421     }
422 }
423
424 sub GenerateGetOwnPropertySlotBody
425 {
426     my ($dataNode, $interfaceName, $className, $implClassName, $hasAttributes, $inlined) = @_;
427
428     my $namespaceMaybe = ($inlined ? "JSC::" : "");
429
430     my @getOwnPropertySlotImpl = ();
431
432     if ($interfaceName eq "NamedNodeMap" or $interfaceName eq "HTMLCollection" or $interfaceName eq "HTMLAllCollection") {
433         push(@getOwnPropertySlotImpl, "    ${namespaceMaybe}JSValue proto = prototype();\n");
434         push(@getOwnPropertySlotImpl, "    if (proto.isObject() && static_cast<${namespaceMaybe}JSObject*>(asObject(proto))->hasProperty(exec, propertyName))\n");
435         push(@getOwnPropertySlotImpl, "        return false;\n\n");
436     }
437
438     my $manualLookupGetterGeneration = sub {
439         my $requiresManualLookup = $dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasNameGetter"};
440         if ($requiresManualLookup) {
441             push(@getOwnPropertySlotImpl, "    const ${namespaceMaybe}HashEntry* entry = ${className}Table.entry(exec, propertyName);\n");
442             push(@getOwnPropertySlotImpl, "    if (entry) {\n");
443             push(@getOwnPropertySlotImpl, "        slot.setCustom(this, entry->propertyGetter());\n");
444             push(@getOwnPropertySlotImpl, "        return true;\n");
445             push(@getOwnPropertySlotImpl, "    }\n");
446         }
447     };
448
449     if (!$dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
450         &$manualLookupGetterGeneration();
451     }
452
453     if ($dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
454         push(@getOwnPropertySlotImpl, "    bool ok;\n");
455         push(@getOwnPropertySlotImpl, "    unsigned index = propertyName.toUInt32(ok);\n");
456
457         # If the item function returns a string then we let the ConvertNullStringTo handle the cases
458         # where the index is out of range.
459         if (IndexGetterReturnsStrings($implClassName)) {
460             push(@getOwnPropertySlotImpl, "    if (ok) {\n");
461         } else {
462             push(@getOwnPropertySlotImpl, "    if (ok && index < static_cast<$implClassName*>(impl())->length()) {\n");
463         }
464         if ($dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
465             push(@getOwnPropertySlotImpl, "        slot.setValue(getByIndex(exec, index));\n");
466         } else {
467             push(@getOwnPropertySlotImpl, "        slot.setCustomIndex(this, index, indexGetter);\n");
468         }
469         push(@getOwnPropertySlotImpl, "        return true;\n");
470         push(@getOwnPropertySlotImpl, "    }\n");
471     }
472
473     if ($dataNode->extendedAttributes->{"HasNameGetter"} || $dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
474         push(@getOwnPropertySlotImpl, "    if (canGetItemsForName(exec, static_cast<$implClassName*>(impl()), propertyName)) {\n");
475         push(@getOwnPropertySlotImpl, "        slot.setCustom(this, nameGetter);\n");
476         push(@getOwnPropertySlotImpl, "        return true;\n");
477         push(@getOwnPropertySlotImpl, "    }\n");
478         if ($inlined) {
479             $headerIncludes{"wtf/text/AtomicString.h"} = 1;
480         } else {
481             $implIncludes{"wtf/text/AtomicString.h"} = 1;
482         }
483     }
484
485     if ($dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
486         &$manualLookupGetterGeneration();
487     }
488
489     if ($dataNode->extendedAttributes->{"DelegatingGetOwnPropertySlot"}) {
490         push(@getOwnPropertySlotImpl, "    if (getOwnPropertySlotDelegate(exec, propertyName, slot))\n");
491         push(@getOwnPropertySlotImpl, "        return true;\n");
492     }
493
494     if ($hasAttributes) {
495         if ($inlined) {
496             die "Cannot inline if NoStaticTables is set." if ($dataNode->extendedAttributes->{"NoStaticTables"});
497             push(@getOwnPropertySlotImpl, "    return ${namespaceMaybe}getStaticValueSlot<$className, Base>(exec, s_info.staticPropHashTable, this, propertyName, slot);\n");
498         } else {
499             push(@getOwnPropertySlotImpl, "    return ${namespaceMaybe}getStaticValueSlot<$className, Base>(exec, " . hashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, slot);\n");
500         }
501     } else {
502         push(@getOwnPropertySlotImpl, "    return Base::getOwnPropertySlot(exec, propertyName, slot);\n");
503     }
504
505     return @getOwnPropertySlotImpl;
506 }
507
508 sub GenerateGetOwnPropertyDescriptorBody
509 {
510     my ($dataNode, $interfaceName, $className, $implClassName, $hasAttributes, $inlined) = @_;
511     
512     my $namespaceMaybe = ($inlined ? "JSC::" : "");
513     
514     my @getOwnPropertyDescriptorImpl = ();
515     if ($dataNode->extendedAttributes->{"CheckDomainSecurity"}) {
516         if ($interfaceName eq "DOMWindow") {
517             push(@implContent, "    if (!static_cast<$className*>(thisObject)->allowsAccessFrom(exec))\n");
518         } else {
519             push(@implContent, "    if (!allowsAccessFromFrame(exec, static_cast<$className*>(thisObject)->impl()->frame()))\n");
520         }
521         push(@implContent, "        return false;\n");
522     }
523     
524     if ($interfaceName eq "NamedNodeMap" or $interfaceName eq "HTMLCollection" or $interfaceName eq "HTMLAllCollection") {
525         push(@getOwnPropertyDescriptorImpl, "    ${namespaceMaybe}JSValue proto = prototype();\n");
526         push(@getOwnPropertyDescriptorImpl, "    if (proto.isObject() && static_cast<${namespaceMaybe}JSObject*>(asObject(proto))->hasProperty(exec, propertyName))\n");
527         push(@getOwnPropertyDescriptorImpl, "        return false;\n\n");
528     }
529     
530     my $manualLookupGetterGeneration = sub {
531         my $requiresManualLookup = $dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasNameGetter"};
532         if ($requiresManualLookup) {
533             push(@getOwnPropertyDescriptorImpl, "    const ${namespaceMaybe}HashEntry* entry = ${className}Table.entry(exec, propertyName);\n");
534             push(@getOwnPropertyDescriptorImpl, "    if (entry) {\n");
535             push(@getOwnPropertyDescriptorImpl, "        PropertySlot slot;\n");
536             push(@getOwnPropertyDescriptorImpl, "        slot.setCustom(this, entry->propertyGetter());\n");
537             push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes());\n");
538             push(@getOwnPropertyDescriptorImpl, "        return true;\n");
539             push(@getOwnPropertyDescriptorImpl, "    }\n");
540         }
541     };
542     
543     if (!$dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
544         &$manualLookupGetterGeneration();
545     }
546     
547     if ($dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
548         push(@getOwnPropertyDescriptorImpl, "    bool ok;\n");
549         push(@getOwnPropertyDescriptorImpl, "    unsigned index = propertyName.toUInt32(ok);\n");
550         push(@getOwnPropertyDescriptorImpl, "    if (ok && index < static_cast<$implClassName*>(impl())->length()) {\n");
551         if ($dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
552             # Assume that if there's a setter, the index will be writable
553             if ($dataNode->extendedAttributes->{"HasIndexSetter"} || $dataNode->extendedAttributes->{"HasCustomIndexSetter"}) {
554                 push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(getByIndex(exec, index), ${namespaceMaybe}DontDelete);\n");
555             } else {
556                 push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(getByIndex(exec, index), ${namespaceMaybe}DontDelete | ${namespaceMaybe}ReadOnly);\n");
557             }
558         } else {
559             push(@getOwnPropertyDescriptorImpl, "        ${namespaceMaybe}PropertySlot slot;\n");
560             push(@getOwnPropertyDescriptorImpl, "        slot.setCustomIndex(this, index, indexGetter);\n");
561             # Assume that if there's a setter, the index will be writable
562             if ($dataNode->extendedAttributes->{"HasIndexSetter"} || $dataNode->extendedAttributes->{"HasCustomIndexSetter"}) {
563                 push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(slot.getValue(exec, propertyName), ${namespaceMaybe}DontDelete);\n");
564             } else {
565                 push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(slot.getValue(exec, propertyName), ${namespaceMaybe}DontDelete | ${namespaceMaybe}ReadOnly);\n");
566             }
567         }
568         push(@getOwnPropertyDescriptorImpl, "        return true;\n");
569         push(@getOwnPropertyDescriptorImpl, "    }\n");
570     }
571     
572     if ($dataNode->extendedAttributes->{"HasNameGetter"} || $dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
573         push(@getOwnPropertyDescriptorImpl, "    if (canGetItemsForName(exec, static_cast<$implClassName*>(impl()), propertyName)) {\n");
574         push(@getOwnPropertyDescriptorImpl, "        ${namespaceMaybe}PropertySlot slot;\n");
575         push(@getOwnPropertyDescriptorImpl, "        slot.setCustom(this, nameGetter);\n");
576         push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(slot.getValue(exec, propertyName), ReadOnly | DontDelete | DontEnum);\n");
577         push(@getOwnPropertyDescriptorImpl, "        return true;\n");
578         push(@getOwnPropertyDescriptorImpl, "    }\n");
579         if ($inlined) {
580             $headerIncludes{"wtf/text/AtomicString.h"} = 1;
581         } else {
582             $implIncludes{"wtf/text/AtomicString.h"} = 1;
583         }
584     }
585     
586     if ($dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
587         &$manualLookupGetterGeneration();
588     }
589     
590     if ($dataNode->extendedAttributes->{"DelegatingGetOwnPropertySlot"}) {
591         push(@getOwnPropertyDescriptorImpl, "    if (getOwnPropertyDescriptorDelegate(exec, propertyName, descriptor))\n");
592         push(@getOwnPropertyDescriptorImpl, "        return true;\n");
593     }
594     
595     if ($hasAttributes) {
596         if ($inlined) {
597             die "Cannot inline if NoStaticTables is set." if ($dataNode->extendedAttributes->{"NoStaticTables"});
598             push(@getOwnPropertyDescriptorImpl, "    return ${namespaceMaybe}getStaticValueDescriptor<$className, Base>(exec, s_info.staticPropHashTable, this, propertyName, descriptor);\n");
599         } else {
600             push(@getOwnPropertyDescriptorImpl, "    return ${namespaceMaybe}getStaticValueDescriptor<$className, Base>(exec, " . hashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, descriptor);\n");
601         }
602     } else {
603         push(@getOwnPropertyDescriptorImpl, "    return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor);\n");
604     }
605     
606     return @getOwnPropertyDescriptorImpl;
607 }
608
609 sub GenerateHeaderContentHeader
610 {
611     my $dataNode = shift;
612     my $className = "JS" . $dataNode->name;
613
614     my @headerContentHeader = split("\r", $headerTemplate);
615
616     # - Add header protection
617     push(@headerContentHeader, "\n#ifndef $className" . "_h");
618     push(@headerContentHeader, "\n#define $className" . "_h\n\n");
619
620     my $conditionalString = GenerateConditionalString($dataNode);
621     push(@headerContentHeader, "#if ${conditionalString}\n\n") if $conditionalString;
622     return @headerContentHeader;
623 }
624
625 sub GenerateImplementationContentHeader
626 {
627     my $dataNode = shift;
628     my $className = "JS" . $dataNode->name;
629
630     my @implContentHeader = split("\r", $headerTemplate);
631
632     push(@implContentHeader, "\n#include \"config.h\"\n");
633     my $conditionalString = GenerateConditionalString($dataNode);
634     push(@implContentHeader, "\n#if ${conditionalString}\n\n") if $conditionalString;
635     push(@implContentHeader, "#include \"$className.h\"\n\n");
636     return @implContentHeader;
637 }
638
639 my %usesToJSNewlyCreated = (
640     "CDATASection" => 1,
641     "Element" => 1,
642     "Node" => 1,
643     "Text" => 1,
644     "Touch" => 1,
645     "TouchList" => 1
646 );
647
648 sub GenerateHeader
649 {
650     my $object = shift;
651     my $dataNode = shift;
652
653     my $interfaceName = $dataNode->name;
654     my $className = "JS$interfaceName";
655     my $implClassName = $interfaceName;
656     my @ancestorInterfaceNames = ();
657     my %structureFlags = ();
658
659     # We only support multiple parents with SVG (for now).
660     if (@{$dataNode->parents} > 1) {
661         die "A class can't have more than one parent" unless $interfaceName =~ /SVG/;
662         $codeGenerator->AddMethodsConstantsAndAttributesFromParentClasses($dataNode, \@ancestorInterfaceNames);
663     }
664
665     my $hasLegacyParent = $dataNode->extendedAttributes->{"LegacyParent"};
666     my $hasRealParent = @{$dataNode->parents} > 0;
667     my $hasParent = $hasLegacyParent || $hasRealParent;
668     my $parentClassName = GetParentClassName($dataNode);
669     my $eventTarget = $dataNode->extendedAttributes->{"EventTarget"};
670     my $needsMarkChildren = $dataNode->extendedAttributes->{"CustomMarkFunction"} || $dataNode->extendedAttributes->{"EventTarget"};
671     
672     # - Add default header template and header protection
673     push(@headerContentHeader, GenerateHeaderContentHeader($dataNode));
674
675     if ($hasParent) {
676         $headerIncludes{"$parentClassName.h"} = 1;
677     } else {
678         $headerIncludes{"JSDOMBinding.h"} = 1;
679         $headerIncludes{"<runtime/JSGlobalObject.h>"} = 1;
680         $headerIncludes{"<runtime/ObjectPrototype.h>"} = 1;
681     }
682
683     if ($dataNode->extendedAttributes->{"CustomCall"}) {
684         $headerIncludes{"<runtime/CallData.h>"} = 1;
685     }
686
687     if ($dataNode->extendedAttributes->{"InlineGetOwnPropertySlot"}) {
688         $headerIncludes{"<runtime/Lookup.h>"} = 1;
689         $headerIncludes{"<wtf/AlwaysInline.h>"} = 1;
690     }
691
692     if ($hasParent && $dataNode->extendedAttributes->{"GenerateNativeConverter"}) {
693         $headerIncludes{"$implClassName.h"} = 1;
694     }
695     
696     $headerIncludes{"<runtime/JSObject.h>"} = 1;
697     $headerIncludes{"SVGElement.h"} = 1 if $className =~ /^JSSVG/;
698
699     my $implType = $implClassName;
700     my ($svgPropertyType, $svgListPropertyType, $svgNativeType) = GetSVGPropertyTypes($implType);
701     $implType = $svgNativeType if $svgNativeType;
702
703     my $svgPropertyOrListPropertyType;
704     $svgPropertyOrListPropertyType = $svgPropertyType if $svgPropertyType;
705     $svgPropertyOrListPropertyType = $svgListPropertyType if $svgListPropertyType;
706
707     my $numConstants = @{$dataNode->constants};
708     my $numAttributes = @{$dataNode->attributes};
709     my $numFunctions = @{$dataNode->functions};
710
711     push(@headerContent, "\nnamespace WebCore {\n\n");
712
713     if ($codeGenerator->IsSVGAnimatedType($implClassName)) {
714         $headerIncludes{"$implClassName.h"} = 1;
715     } else {
716         # Implementation class forward declaration
717         if ($interfaceName eq "DOMWindow" || $dataNode->extendedAttributes->{"IsWorkerContext"}) {
718             AddClassForwardIfNeeded($implClassName) unless $svgPropertyOrListPropertyType;
719         }
720     }
721
722     AddClassForwardIfNeeded("JSDOMWindowShell") if $interfaceName eq "DOMWindow";
723
724     # Class declaration
725     push(@headerContent, "class $className : public $parentClassName {\n");
726
727     # Static create methods
728     push(@headerContent, "public:\n");
729     push(@headerContent, "    typedef $parentClassName Base;\n");
730     if ($interfaceName eq "DOMWindow") {
731         push(@headerContent, "    static $className* create(JSC::JSGlobalData& globalData, JSC::Structure* structure, PassRefPtr<$implType> impl, JSDOMWindowShell* windowShell)\n");
732         push(@headerContent, "    {\n");
733         push(@headerContent, "        $className* ptr = new (JSC::allocateCell<$className>(globalData.heap)) ${className}(globalData, structure, impl, windowShell);\n");
734         push(@headerContent, "        ptr->finishCreation(globalData, windowShell);\n");
735         push(@headerContent, "        return ptr;\n");
736         push(@headerContent, "    }\n\n");
737     } elsif ($dataNode->extendedAttributes->{"IsWorkerContext"}) {
738         push(@headerContent, "    static $className* create(JSC::JSGlobalData& globalData, JSC::Structure* structure, PassRefPtr<$implType> impl)\n");
739         push(@headerContent, "    {\n");
740         push(@headerContent, "        $className* ptr = new (JSC::allocateCell<$className>(globalData.heap)) ${className}(globalData, structure, impl);\n");
741         push(@headerContent, "        ptr->finishCreation(globalData);\n");
742         push(@headerContent, "        return ptr;\n");
743         push(@headerContent, "    }\n\n");
744     } else {
745         AddIncludesForTypeInHeader($implType) unless $svgPropertyOrListPropertyType;
746         push(@headerContent, "    static $className* create(JSC::Structure* structure, JSDOMGlobalObject* globalObject, PassRefPtr<$implType> impl)\n");
747         push(@headerContent, "    {\n");
748         push(@headerContent, "        $className* ptr = new (JSC::allocateCell<$className>(globalObject->globalData().heap)) $className(structure, globalObject, impl);\n");
749         push(@headerContent, "        ptr->finishCreation(globalObject->globalData());\n");
750         push(@headerContent, "        return ptr;\n");
751         push(@headerContent, "    }\n\n");
752     }
753
754     # Prototype
755     push(@headerContent, "    static JSC::JSObject* createPrototype(JSC::ExecState*, JSC::JSGlobalObject*);\n") unless ($dataNode->extendedAttributes->{"ExtendsDOMGlobalObject"});
756
757     $headerTrailingIncludes{"${className}Custom.h"} = 1 if $dataNode->extendedAttributes->{"CustomHeader"};
758
759     $implIncludes{"${className}Custom.h"} = 1 if !$dataNode->extendedAttributes->{"CustomHeader"} && ($dataNode->extendedAttributes->{"CustomPutFunction"} || $dataNode->extendedAttributes->{"DelegatingPutFunction"});
760
761     my $hasGetter = $numAttributes > 0 
762                  || !$dataNode->extendedAttributes->{"OmitConstructor"}
763                  || $dataNode->extendedAttributes->{"HasIndexGetter"}
764                  || $dataNode->extendedAttributes->{"HasCustomIndexGetter"}
765                  || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}
766                  || $dataNode->extendedAttributes->{"CustomGetOwnPropertySlot"}
767                  || $dataNode->extendedAttributes->{"DelegatingGetOwnPropertySlot"}
768                  || $dataNode->extendedAttributes->{"HasNameGetter"}
769                  || $dataNode->extendedAttributes->{"HasOverridingNameGetter"};
770
771     # Getters
772     if ($hasGetter) {
773         push(@headerContent, "    virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&);\n");
774         push(@headerContent, "    virtual bool getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor&);\n");
775         push(@headerContent, "    virtual bool getOwnPropertySlot(JSC::ExecState*, unsigned propertyName, JSC::PropertySlot&);\n") if ($dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) && !$dataNode->extendedAttributes->{"HasOverridingNameGetter"};
776         push(@headerContent, "    bool getOwnPropertySlotDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::PropertySlot&);\n") if $dataNode->extendedAttributes->{"DelegatingGetOwnPropertySlot"};
777         push(@headerContent, "    bool getOwnPropertyDescriptorDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::PropertyDescriptor&);\n") if $dataNode->extendedAttributes->{"DelegatingGetOwnPropertySlot"};
778         $structureFlags{"JSC::OverridesGetOwnPropertySlot"} = 1;
779     }
780
781     # Check if we have any writable properties
782     my $hasReadWriteProperties = 0;
783     foreach (@{$dataNode->attributes}) {
784         if ($_->type !~ /^readonly\ attribute$/) {
785             $hasReadWriteProperties = 1;
786         }
787     }
788
789     my $hasSetter = $hasReadWriteProperties
790                  || $dataNode->extendedAttributes->{"CustomPutFunction"}
791                  || $dataNode->extendedAttributes->{"DelegatingPutFunction"}
792                  || $dataNode->extendedAttributes->{"HasCustomIndexSetter"};
793
794     # Getters
795     if ($hasSetter) {
796         push(@headerContent, "    virtual void put(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&);\n");
797         push(@headerContent, "    static void put(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&);\n");
798         push(@headerContent, "    virtual void put(JSC::ExecState*, unsigned propertyName, JSC::JSValue);\n") if $dataNode->extendedAttributes->{"HasCustomIndexSetter"};
799         push(@headerContent, "    static void put(JSC::JSCell*, JSC::ExecState*, unsigned propertyName, JSC::JSValue);\n") if $dataNode->extendedAttributes->{"HasCustomIndexSetter"};
800         push(@headerContent, "    bool putDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::JSValue, JSC::PutPropertySlot&);\n") if $dataNode->extendedAttributes->{"DelegatingPutFunction"};
801     }
802
803     # Class info
804     push(@headerContent, "    static const JSC::ClassInfo s_info;\n\n");
805
806     # Structure ID
807     if ($interfaceName eq "DOMWindow") {
808         $structureFlags{"JSC::ImplementsHasInstance"} = 1;
809     }
810     push(@headerContent,
811         "    static JSC::Structure* createStructure(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)\n" .
812         "    {\n" .
813         "        return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), &s_info);\n" .
814         "    }\n\n");
815
816     # Custom pushEventHandlerScope function
817     push(@headerContent, "    virtual JSC::ScopeChainNode* pushEventHandlerScope(JSC::ExecState*, JSC::ScopeChainNode*) const;\n\n") if $dataNode->extendedAttributes->{"CustomPushEventHandlerScope"};
818
819     # Custom call functions
820     push(@headerContent, "    static JSC::CallType getCallData(JSC::JSCell*, JSC::CallData&);\n\n") if $dataNode->extendedAttributes->{"CustomCall"};
821
822     # Custom deleteProperty function
823     push(@headerContent, "    virtual bool deleteProperty(JSC::ExecState*, const JSC::Identifier&);\n") if $dataNode->extendedAttributes->{"CustomDeleteProperty"};
824
825     # Custom getPropertyNames function exists on DOMWindow
826     if ($interfaceName eq "DOMWindow") {
827         push(@headerContent, "    virtual void getPropertyNames(JSC::ExecState*, JSC::PropertyNameArray&, JSC::EnumerationMode mode = JSC::ExcludeDontEnumProperties);\n");
828         $structureFlags{"JSC::OverridesGetPropertyNames"} = 1;
829     }
830
831     # Custom defineProperty function exists on DOMWindow
832     push(@headerContent, "    virtual bool defineOwnProperty(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor&, bool shouldThrow);\n") if $interfaceName eq "DOMWindow";
833
834     # Custom getOwnPropertyNames function
835     if ($dataNode->extendedAttributes->{"CustomGetPropertyNames"} || $dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
836         push(@headerContent, "    virtual void getOwnPropertyNames(JSC::ExecState*, JSC::PropertyNameArray&, JSC::EnumerationMode mode = JSC::ExcludeDontEnumProperties);\n");
837         $structureFlags{"JSC::OverridesGetPropertyNames"} = 1;       
838     }
839
840     # Custom defineGetter function
841     push(@headerContent, "    virtual void defineGetter(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSObject* getterFunction, unsigned attributes);\n") if $dataNode->extendedAttributes->{"CustomDefineGetter"};
842
843     # Custom defineSetter function
844     push(@headerContent, "    virtual void defineSetter(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSObject* setterFunction, unsigned attributes);\n") if $dataNode->extendedAttributes->{"CustomDefineSetter"};
845
846     # Custom lookupGetter function
847     push(@headerContent, "    virtual JSC::JSValue lookupGetter(JSC::ExecState*, const JSC::Identifier& propertyName);\n") if $dataNode->extendedAttributes->{"CustomLookupGetter"};
848
849     # Custom lookupSetter function
850     push(@headerContent, "    virtual JSC::JSValue lookupSetter(JSC::ExecState*, const JSC::Identifier& propertyName);\n") if $dataNode->extendedAttributes->{"CustomLookupSetter"};
851
852     # Override toBoolean to return false for objects that want to 'MasqueradesAsUndefined'.
853     if ($dataNode->extendedAttributes->{"MasqueradesAsUndefined"}) {
854         push(@headerContent, "    virtual bool toBoolean(JSC::ExecState*) const { return false; };\n");
855         $structureFlags{"JSC::MasqueradesAsUndefined"} = 1;
856     }
857
858     # Constructor object getter
859     push(@headerContent, "    static JSC::JSValue getConstructor(JSC::ExecState*, JSC::JSGlobalObject*);\n") if !$dataNode->extendedAttributes->{"OmitConstructor"};
860
861     my $numCustomFunctions = 0;
862     my $numCustomAttributes = 0;
863
864     # Attribute and function enums
865     if ($numAttributes > 0) {
866         foreach (@{$dataNode->attributes}) {
867             my $attribute = $_;
868             $numCustomAttributes++ if $attribute->signature->extendedAttributes->{"Custom"} || $attribute->signature->extendedAttributes->{"JSCCustom"};
869             $numCustomAttributes++ if $attribute->signature->extendedAttributes->{"CustomGetter"} || $attribute->signature->extendedAttributes->{"JSCCustomGetter"};
870             $numCustomAttributes++ if $attribute->signature->extendedAttributes->{"CustomSetter"} || $attribute->signature->extendedAttributes->{"JSCCustomSetter"};
871             if ($attribute->signature->extendedAttributes->{"CachedAttribute"}) {
872                 push(@headerContent, "    JSC::WriteBarrier<JSC::Unknown> m_" . $attribute->signature->name . ";\n");
873                 $numCachedAttributes++;
874                 $needsMarkChildren = 1;
875             }
876         }
877     }
878
879     # visit function
880     if ($needsMarkChildren) {
881         push(@headerContent, "    static void visitChildren(JSCell*, JSC::SlotVisitor&);\n\n");
882         $structureFlags{"JSC::OverridesVisitChildren"} = 1;
883     }
884
885     if ($numCustomAttributes > 0) {
886         push(@headerContent, "\n    // Custom attributes\n");
887
888         foreach my $attribute (@{$dataNode->attributes}) {
889             if ($attribute->signature->extendedAttributes->{"Custom"} || $attribute->signature->extendedAttributes->{"JSCCustom"}) {
890                 push(@headerContent, "    JSC::JSValue " . $codeGenerator->WK_lcfirst($attribute->signature->name) . "(JSC::ExecState*) const;\n");
891                 if ($attribute->type !~ /^readonly/) {
892                     push(@headerContent, "    void set" . $codeGenerator->WK_ucfirst($attribute->signature->name) . "(JSC::ExecState*, JSC::JSValue);\n");
893                 }
894             } elsif ($attribute->signature->extendedAttributes->{"CustomGetter"} || $attribute->signature->extendedAttributes->{"JSCCustomGetter"}) {
895                 push(@headerContent, "    JSC::JSValue " . $codeGenerator->WK_lcfirst($attribute->signature->name) . "(JSC::ExecState*) const;\n");
896             } elsif ($attribute->signature->extendedAttributes->{"CustomSetter"} || $attribute->signature->extendedAttributes->{"JSCCustomSetter"}) {
897                 if ($attribute->type !~ /^readonly/) {
898                     push(@headerContent, "    void set" . $codeGenerator->WK_ucfirst($attribute->signature->name) . "(JSC::ExecState*, JSC::JSValue);\n");
899                 }
900             }
901         }
902     }
903
904     foreach my $function (@{$dataNode->functions}) {
905         $numCustomFunctions++ if $function->signature->extendedAttributes->{"Custom"} || $function->signature->extendedAttributes->{"JSCCustom"};
906     }
907
908     if ($numCustomFunctions > 0) {
909         push(@headerContent, "\n    // Custom functions\n");
910         foreach my $function (@{$dataNode->functions}) {
911             if ($function->signature->extendedAttributes->{"Custom"} || $function->signature->extendedAttributes->{"JSCCustom"}) {
912                 my $functionImplementationName = $function->signature->extendedAttributes->{"ImplementationFunction"} || $codeGenerator->WK_lcfirst($function->signature->name);
913                 push(@headerContent, "    JSC::JSValue " . $functionImplementationName . "(JSC::ExecState*);\n");
914             }
915         }
916     }
917
918     if (!$hasParent) {
919         push(@headerContent, "    $implType* impl() const { return m_impl.get(); }\n\n");
920         push(@headerContent, "private:\n");
921         push(@headerContent, "    RefPtr<$implType> m_impl;\n");
922     } elsif ($dataNode->extendedAttributes->{"GenerateNativeConverter"}) {
923         push(@headerContent, "    $implClassName* impl() const\n");
924         push(@headerContent, "    {\n");
925         push(@headerContent, "        return static_cast<$implClassName*>(Base::impl());\n");
926         push(@headerContent, "    }\n");
927     }
928
929     push(@headerContent, "protected:\n");
930     # Constructor
931     if ($interfaceName eq "DOMWindow") {
932         push(@headerContent, "    $className(JSC::JSGlobalData&, JSC::Structure*, PassRefPtr<$implType>, JSDOMWindowShell*);\n");
933     } elsif ($dataNode->extendedAttributes->{"IsWorkerContext"}) {
934         push(@headerContent, "    $className(JSC::JSGlobalData&, JSC::Structure*, PassRefPtr<$implType>);\n");
935     } else {
936         push(@headerContent, "    $className(JSC::Structure*, JSDOMGlobalObject*, PassRefPtr<$implType>);\n");
937         push(@headerContent, "    void finishCreation(JSC::JSGlobalData&);\n");
938     }
939
940     # structure flags
941     push(@headerContent, "    static const unsigned StructureFlags = ");
942     foreach my $structureFlag (keys %structureFlags) {
943         push(@headerContent, $structureFlag . " | ");
944     }
945     push(@headerContent, "Base::StructureFlags;\n");
946
947     # Index getter
948     if ($dataNode->extendedAttributes->{"HasIndexGetter"}) {
949         push(@headerContent, "    static JSC::JSValue indexGetter(JSC::ExecState*, JSC::JSValue, unsigned);\n");
950     }
951     if ($dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
952         push(@headerContent, "    JSC::JSValue getByIndex(JSC::ExecState*, unsigned index);\n");
953         
954     }
955     
956     # Index setter
957     if ($dataNode->extendedAttributes->{"HasCustomIndexSetter"}) {
958         push(@headerContent, "    void indexSetter(JSC::ExecState*, unsigned index, JSC::JSValue);\n");
959     }
960     # Name getter
961     if ($dataNode->extendedAttributes->{"HasNameGetter"} || $dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
962         push(@headerContent, "private:\n");
963         push(@headerContent, "    static bool canGetItemsForName(JSC::ExecState*, $implClassName*, const JSC::Identifier&);\n");
964         push(@headerContent, "    static JSC::JSValue nameGetter(JSC::ExecState*, JSC::JSValue, const JSC::Identifier&);\n");
965     }
966
967     push(@headerContent, "};\n\n");
968
969     if ($dataNode->extendedAttributes->{"InlineGetOwnPropertySlot"} && !$dataNode->extendedAttributes->{"CustomGetOwnPropertySlot"}) {
970         push(@headerContent, "ALWAYS_INLINE bool ${className}::getOwnPropertySlot(JSC::ExecState* exec, const JSC::Identifier& propertyName, JSC::PropertySlot& slot)\n");
971         push(@headerContent, "{\n");
972         push(@headerContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
973         push(@headerContent, GenerateGetOwnPropertySlotBody($dataNode, $interfaceName, $className, $implClassName, $numAttributes > 0, 1));
974         push(@headerContent, "}\n\n");
975         push(@headerContent, "ALWAYS_INLINE bool ${className}::getOwnPropertyDescriptor(JSC::ExecState* exec, const JSC::Identifier& propertyName, JSC::PropertyDescriptor& descriptor)\n");
976         push(@headerContent, "{\n");
977         push(@headerContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
978         push(@headerContent, GenerateGetOwnPropertyDescriptorBody($dataNode, $interfaceName, $className, $implClassName, $numAttributes > 0, 1));
979         push(@headerContent, "}\n\n");
980     }
981
982     if ($dataNode->extendedAttributes->{"GenerateIsReachable"} || 
983         $dataNode->extendedAttributes->{"CustomIsReachable"} || 
984         $dataNode->extendedAttributes->{"CustomFinalize"} ||
985         $dataNode->extendedAttributes->{"ActiveDOMObject"}) {
986         push(@headerContent, "class JS${implType}Owner : public JSC::WeakHandleOwner {\n");
987         push(@headerContent, "    virtual bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::SlotVisitor&);\n");
988         push(@headerContent, "    virtual void finalize(JSC::Handle<JSC::Unknown>, void* context);\n");
989         push(@headerContent, "};\n");
990         push(@headerContent, "\n");
991         push(@headerContent, "inline JSC::WeakHandleOwner* wrapperOwner(DOMWrapperWorld*, $implType*)\n");
992         push(@headerContent, "{\n");
993         push(@headerContent, "    DEFINE_STATIC_LOCAL(JS${implType}Owner, js${implType}Owner, ());\n");
994         push(@headerContent, "    return &js${implType}Owner;\n");
995         push(@headerContent, "}\n");
996         push(@headerContent, "\n");
997         push(@headerContent, "inline void* wrapperContext(DOMWrapperWorld* world, $implType*)\n");
998         push(@headerContent, "{\n");
999         push(@headerContent, "    return world;\n");
1000         push(@headerContent, "}\n");
1001         push(@headerContent, "\n");
1002     }
1003
1004     if (!$hasParent || $dataNode->extendedAttributes->{"GenerateToJS"} || $dataNode->extendedAttributes->{"CustomToJS"}) {
1005         push(@headerContent, "JSC::JSValue toJS(JSC::ExecState*, JSDOMGlobalObject*, $implType*);\n");
1006     }
1007     if (!$hasParent || $dataNode->extendedAttributes->{"GenerateNativeConverter"}) {
1008         if ($interfaceName eq "NodeFilter") {
1009             push(@headerContent, "PassRefPtr<NodeFilter> toNodeFilter(JSC::JSGlobalData&, JSC::JSValue);\n");
1010         } else {
1011             push(@headerContent, "$implType* to${interfaceName}(JSC::JSValue);\n");
1012         }
1013     }
1014     if ($usesToJSNewlyCreated{$interfaceName}) {
1015         push(@headerContent, "JSC::JSValue toJSNewlyCreated(JSC::ExecState*, JSDOMGlobalObject*, $interfaceName*);\n");
1016     }
1017     
1018     push(@headerContent, "\n");
1019
1020     # Add prototype declaration.
1021     %structureFlags = ();
1022     push(@headerContent, "class ${className}Prototype : public JSC::JSNonFinalObject {\n");
1023     push(@headerContent, "public:\n");
1024     push(@headerContent, "    typedef JSC::JSNonFinalObject Base;\n");
1025     if ($interfaceName ne "DOMWindow" && !$dataNode->extendedAttributes->{"IsWorkerContext"}) {
1026         push(@headerContent, "    static JSC::JSObject* self(JSC::ExecState*, JSC::JSGlobalObject*);\n");
1027     }
1028
1029     push(@headerContent, "    static ${className}Prototype* create(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::Structure* structure)\n");
1030     push(@headerContent, "    {\n");
1031     push(@headerContent, "        ${className}Prototype* ptr = new (JSC::allocateCell<${className}Prototype>(globalData.heap)) ${className}Prototype(globalData, globalObject, structure);\n");
1032     push(@headerContent, "        ptr->finishCreation(globalData);\n");
1033     push(@headerContent, "        return ptr;\n");
1034     push(@headerContent, "    }\n\n");
1035
1036     push(@headerContent, "    static const JSC::ClassInfo s_info;\n");
1037     if ($numFunctions > 0 || $numConstants > 0 || $dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"}) {
1038         push(@headerContent, "    virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier&, JSC::PropertySlot&);\n");
1039         push(@headerContent, "    virtual bool getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier&, JSC::PropertyDescriptor&);\n");
1040         push(@headerContent, "    bool getOwnPropertySlotDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::PropertySlot&);\n") if $dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"};
1041         push(@headerContent, "    bool getOwnPropertyDescriptorDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::PropertyDescriptor&);\n") if $dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"};
1042         $structureFlags{"JSC::OverridesGetOwnPropertySlot"} = 1;
1043     }
1044     if ($dataNode->extendedAttributes->{"CustomMarkFunction"} or $needsMarkChildren) {
1045         $structureFlags{"JSC::OverridesVisitChildren"} = 1;
1046     }
1047     push(@headerContent,
1048         "    static JSC::Structure* createStructure(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)\n" .
1049         "    {\n" .
1050         "        return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), &s_info);\n" .
1051         "    }\n");
1052     if ($dataNode->extendedAttributes->{"DelegatingPrototypePutFunction"}) {
1053         push(@headerContent, "    virtual void put(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&);\n");
1054         push(@headerContent, "    static void put(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&);\n");
1055         push(@headerContent, "    bool putDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::JSValue, JSC::PutPropertySlot&);\n");
1056     }
1057
1058     # Custom defineGetter function
1059     push(@headerContent, "    virtual void defineGetter(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSObject* getterFunction, unsigned attributes);\n") if $dataNode->extendedAttributes->{"CustomPrototypeDefineGetter"};
1060
1061     push(@headerContent, "\nprivate:\n");
1062     push(@headerContent, "    ${className}Prototype(JSC::JSGlobalData& globalData, JSC::JSGlobalObject*, JSC::Structure* structure) : JSC::JSNonFinalObject(globalData, structure) { }\n");
1063
1064     # structure flags
1065     push(@headerContent, "protected:\n");
1066     push(@headerContent, "    static const unsigned StructureFlags = ");
1067     foreach my $structureFlag (keys %structureFlags) {
1068         push(@headerContent, $structureFlag . " | ");
1069     }
1070     push(@headerContent, "Base::StructureFlags;\n");
1071
1072     push(@headerContent, "};\n\n");
1073
1074     # Conditionally emit the constructor object's declaration
1075     if ($dataNode->extendedAttributes->{"JSCustomConstructor"} || $dataNode->extendedAttributes->{"CustomConstructor"}) {
1076         GenerateConstructorDeclaration(\@headerContent, $className, $dataNode);
1077     }
1078
1079
1080     if ($numFunctions > 0) {
1081         push(@headerContent,"// Functions\n\n");
1082         foreach my $function (@{$dataNode->functions}) {
1083             next if $function->{overloadIndex} && $function->{overloadIndex} > 1;
1084             my $functionName = $codeGenerator->WK_lcfirst($className) . "PrototypeFunction" . $codeGenerator->WK_ucfirst($function->signature->name);
1085             push(@headerContent, "JSC::EncodedJSValue JSC_HOST_CALL ${functionName}(JSC::ExecState*);\n");
1086         }
1087     }
1088
1089     if ($numAttributes > 0 || !$dataNode->extendedAttributes->{"OmitConstructor"}) {
1090         push(@headerContent,"// Attributes\n\n");
1091         foreach my $attribute (@{$dataNode->attributes}) {
1092             my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($attribute->signature->name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1093             push(@headerContent, "JSC::JSValue ${getter}(JSC::ExecState*, JSC::JSValue, const JSC::Identifier&);\n");
1094             unless ($attribute->type =~ /readonly/) {
1095                 my $setter = "setJS" . $interfaceName . $codeGenerator->WK_ucfirst($attribute->signature->name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1096                 push(@headerContent, "void ${setter}(JSC::ExecState*, JSC::JSObject*, JSC::JSValue);\n");
1097             }
1098         }
1099         
1100         if (!$dataNode->extendedAttributes->{"OmitConstructor"}) {
1101             my $getter = "js" . $interfaceName . "Constructor";
1102             push(@headerContent, "JSC::JSValue ${getter}(JSC::ExecState*, JSC::JSValue, const JSC::Identifier&);\n");
1103         }
1104
1105         if ($dataNode->extendedAttributes->{"ReplaceableConstructor"}) {
1106             my $constructorFunctionName = "setJS" . $interfaceName . "Constructor";
1107             push(@headerContent, "void ${constructorFunctionName}(JSC::ExecState*, JSC::JSObject*, JSC::JSValue);\n");
1108         }
1109     }
1110
1111     if ($numConstants > 0) {
1112         push(@headerContent,"// Constants\n\n");
1113         foreach my $constant (@{$dataNode->constants}) {
1114             my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($constant->name);
1115             push(@headerContent, "JSC::JSValue ${getter}(JSC::ExecState*, JSC::JSValue, const JSC::Identifier&);\n");
1116         }
1117     }
1118
1119     my $conditionalString = GenerateConditionalString($dataNode);
1120     push(@headerContent, "\n} // namespace WebCore\n\n");
1121     push(@headerContent, "#endif // ${conditionalString}\n\n") if $conditionalString;
1122     push(@headerContent, "#endif\n");
1123
1124     # - Generate dependencies.
1125     if ($writeDependencies && @ancestorInterfaceNames) {
1126         push(@depsContent, "$className.h : ", join(" ", map { "$_.idl" } @ancestorInterfaceNames), "\n");
1127         push(@depsContent, map { "$_.idl :\n" } @ancestorInterfaceNames); 
1128     }
1129 }
1130
1131 sub GenerateAttributesHashTable($$)
1132 {
1133     my ($object, $dataNode) = @_;
1134
1135     # FIXME: These should be functions on $dataNode.
1136     my $interfaceName = $dataNode->name;
1137     my $className = "JS$interfaceName";
1138     
1139     # - Add all attributes in a hashtable definition
1140     my $numAttributes = @{$dataNode->attributes};
1141     $numAttributes++ if !$dataNode->extendedAttributes->{"OmitConstructor"};
1142
1143     return 0  if !$numAttributes;
1144
1145     my $hashSize = $numAttributes;
1146     my $hashName = $className . "Table";
1147
1148     my @hashKeys = ();
1149     my @hashSpecials = ();
1150     my @hashValue1 = ();
1151     my @hashValue2 = ();
1152     my %conditionals = ();
1153
1154     my @entries = ();
1155
1156     foreach my $attribute (@{$dataNode->attributes}) {
1157         my $name = $attribute->signature->name;
1158         push(@hashKeys, $name);
1159
1160         my @specials = ();
1161         push(@specials, "DontDelete") unless $attribute->signature->extendedAttributes->{"Deletable"};
1162         push(@specials, "DontEnum") if $attribute->signature->extendedAttributes->{"DontEnum"};
1163         push(@specials, "ReadOnly") if $attribute->type =~ /readonly/;
1164         my $special = (@specials > 0) ? join(" | ", @specials) : "0";
1165         push(@hashSpecials, $special);
1166
1167         my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($attribute->signature->name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1168         push(@hashValue1, $getter);
1169
1170         if ($attribute->type =~ /readonly/) {
1171             push(@hashValue2, "0");
1172         } else {
1173             my $setter = "setJS" . $interfaceName . $codeGenerator->WK_ucfirst($attribute->signature->name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1174             push(@hashValue2, $setter);
1175         }
1176
1177         my $conditional = $attribute->signature->extendedAttributes->{"Conditional"};
1178         if ($conditional) {
1179             $conditionals{$name} = $conditional;
1180         }
1181     }
1182
1183     if (!$dataNode->extendedAttributes->{"OmitConstructor"}) {
1184         push(@hashKeys, "constructor");
1185         my $getter = "js" . $interfaceName . "Constructor";
1186         push(@hashValue1, $getter);
1187         if ($dataNode->extendedAttributes->{"ReplaceableConstructor"}) {
1188             my $setter = "setJS" . $interfaceName . "Constructor";
1189             push(@hashValue2, $setter);
1190             push(@hashSpecials, "DontEnum | DontDelete");
1191         } else {            
1192             push(@hashValue2, "0");
1193             push(@hashSpecials, "DontEnum | ReadOnly");
1194         }
1195     }
1196
1197     $object->GenerateHashTable($hashName, $hashSize,
1198                                \@hashKeys, \@hashSpecials,
1199                                \@hashValue1, \@hashValue2,
1200                                \%conditionals);
1201     return $numAttributes;
1202 }
1203
1204 sub GenerateParametersCheckExpression
1205 {
1206     my $numParameters = shift;
1207     my $function = shift;
1208
1209     my @andExpression = ();
1210     push(@andExpression, "argsCount == $numParameters");
1211     my $parameterIndex = 0;
1212     my %usedArguments = ();
1213     foreach my $parameter (@{$function->parameters}) {
1214         last if $parameterIndex >= $numParameters;
1215         my $value = "arg$parameterIndex";
1216         my $type = $codeGenerator->StripModule($parameter->type);
1217
1218         # Only DOMString or wrapper types are checked.
1219         # For DOMString, Null, Undefined and any Object are accepted too, as
1220         # these are acceptable values for a DOMString argument (any Object can
1221         # be converted to a string via .toString).
1222         if ($codeGenerator->IsStringType($type)) {
1223             push(@andExpression, "(${value}.isUndefinedOrNull() || ${value}.isString() || ${value}.isObject())");
1224             $usedArguments{$parameterIndex} = 1;
1225         } elsif ($parameter->extendedAttributes->{"Callback"}) {
1226             # For Callbacks only checks if the value is null or object.
1227             push(@andExpression, "(${value}.isNull() || ${value}.isObject())");
1228             $usedArguments{$parameterIndex} = 1;
1229         } elsif (!IsNativeType($type)) {
1230             push(@andExpression, "(${value}.isNull() || (${value}.isObject() && asObject(${value})->inherits(&JS${type}::s_info)))");
1231             $usedArguments{$parameterIndex} = 1;
1232         }
1233         $parameterIndex++;
1234     }
1235     my $res = join(" && ", @andExpression);
1236     $res = "($res)" if @andExpression > 1;
1237     return ($res, keys %usedArguments);
1238 }
1239
1240 sub GenerateFunctionParametersCheck
1241 {
1242     my $function = shift;
1243
1244     my @orExpression = ();
1245     my $numParameters = 0;
1246     my @neededArguments = ();
1247
1248     foreach my $parameter (@{$function->parameters}) {
1249         if ($parameter->extendedAttributes->{"Optional"}) {
1250             my ($expression, @usedArguments) = GenerateParametersCheckExpression($numParameters, $function);
1251             push(@orExpression, $expression);
1252             push(@neededArguments, @usedArguments);
1253         }
1254         $numParameters++;
1255     }
1256     my ($expression, @usedArguments) = GenerateParametersCheckExpression($numParameters, $function);
1257     push(@orExpression, $expression);
1258     push(@neededArguments, @usedArguments);
1259     return (join(" || ", @orExpression), @neededArguments);
1260 }
1261
1262 sub GenerateOverloadedPrototypeFunction
1263 {
1264     my $function = shift;
1265     my $dataNode = shift;
1266     my $implClassName = shift;
1267
1268     # Generate code for choosing the correct overload to call. Overloads are
1269     # chosen based on the total number of arguments passed and the type of
1270     # values passed in non-primitive argument slots. When more than a single
1271     # overload is applicable, precedence is given according to the order of
1272     # declaration in the IDL.
1273
1274     my $functionName = "js${implClassName}PrototypeFunction" . $codeGenerator->WK_ucfirst($function->signature->name);
1275
1276     push(@implContent, "EncodedJSValue JSC_HOST_CALL ${functionName}(ExecState* exec)\n");
1277     push(@implContent, <<END);
1278 {
1279     size_t argsCount = exec->argumentCount();
1280 END
1281
1282     my %fetchedArguments = ();
1283
1284     foreach my $overload (@{$function->{overloads}}) {
1285         my ($parametersCheck, @neededArguments) = GenerateFunctionParametersCheck($overload);
1286
1287         foreach my $parameterIndex (@neededArguments) {
1288             next if exists $fetchedArguments{$parameterIndex};
1289             push(@implContent, "    JSValue arg$parameterIndex(exec->argument($parameterIndex));\n");
1290             $fetchedArguments{$parameterIndex} = 1;
1291         }
1292
1293         push(@implContent, "    if ($parametersCheck)\n");
1294         push(@implContent, "        return ${functionName}$overload->{overloadIndex}(exec);\n");
1295     }
1296     push(@implContent, <<END);
1297     return throwVMTypeError(exec);
1298 }
1299
1300 END
1301 }
1302
1303 sub GenerateImplementation
1304 {
1305     my ($object, $dataNode) = @_;
1306
1307     my $interfaceName = $dataNode->name;
1308     my $className = "JS$interfaceName";
1309     my $implClassName = $interfaceName;
1310
1311     my $hasLegacyParent = $dataNode->extendedAttributes->{"LegacyParent"};
1312     my $hasRealParent = @{$dataNode->parents} > 0;
1313     my $hasParent = $hasLegacyParent || $hasRealParent;
1314     my $parentClassName = GetParentClassName($dataNode);
1315     my $visibleClassName = GetVisibleClassName($interfaceName);
1316     my $eventTarget = $dataNode->extendedAttributes->{"EventTarget"};
1317     my $needsMarkChildren = $dataNode->extendedAttributes->{"CustomMarkFunction"} || $dataNode->extendedAttributes->{"EventTarget"};
1318
1319     # - Add default header template
1320     push(@implContentHeader, GenerateImplementationContentHeader($dataNode));
1321
1322     AddIncludesForSVGAnimatedType($interfaceName) if $className =~ /^JSSVGAnimated/;
1323
1324     $implIncludes{"<wtf/GetPtr.h>"} = 1;
1325     $implIncludes{"<runtime/PropertyNameArray.h>"} = 1 if $dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"};
1326
1327     AddIncludesForTypeInImpl($interfaceName);
1328
1329     @implContent = ();
1330
1331     push(@implContent, "\nusing namespace JSC;\n\n");
1332     push(@implContent, "namespace WebCore {\n\n");
1333
1334     push(@implContent, "ASSERT_CLASS_FITS_IN_CELL($className);\n\n");
1335
1336     my $numAttributes = GenerateAttributesHashTable($object, $dataNode);
1337
1338     my $numConstants = @{$dataNode->constants};
1339     my $numFunctions = @{$dataNode->functions};
1340
1341     # - Add all constants
1342     if (!$dataNode->extendedAttributes->{"OmitConstructor"}) {
1343         my $hashSize = $numConstants;
1344         my $hashName = $className . "ConstructorTable";
1345
1346         my @hashKeys = ();
1347         my @hashValue1 = ();
1348         my @hashValue2 = ();
1349         my @hashSpecials = ();
1350
1351         # FIXME: we should not need a function for every constant.
1352         foreach my $constant (@{$dataNode->constants}) {
1353             push(@hashKeys, $constant->name);
1354             my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($constant->name);
1355             push(@hashValue1, $getter);
1356             push(@hashValue2, "0");
1357             push(@hashSpecials, "DontDelete | ReadOnly");
1358         }
1359
1360         $object->GenerateHashTable($hashName, $hashSize,
1361                                    \@hashKeys, \@hashSpecials,
1362                                    \@hashValue1, \@hashValue2);
1363
1364         push(@implContent, $codeGenerator->GenerateCompileTimeCheckForEnumsIfNeeded($dataNode));
1365
1366         my $protoClassName = "${className}Prototype";
1367
1368         GenerateConstructorDeclaration(\@implContent, $className, $dataNode) unless ($dataNode->extendedAttributes->{"JSCustomConstructor"} || $dataNode->extendedAttributes->{"CustomConstructor"});
1369         GenerateConstructorDefinition(\@implContent, $className, $protoClassName, $interfaceName, $visibleClassName, $dataNode);
1370     }
1371
1372     # - Add functions and constants to a hashtable definition
1373     my $hashSize = $numFunctions + $numConstants;
1374     my $hashName = $className . "PrototypeTable";
1375
1376     my @hashKeys = ();
1377     my @hashValue1 = ();
1378     my @hashValue2 = ();
1379     my @hashSpecials = ();
1380     my %conditionals = ();
1381
1382     # FIXME: we should not need a function for every constant.
1383     foreach my $constant (@{$dataNode->constants}) {
1384         push(@hashKeys, $constant->name);
1385         my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($constant->name);
1386         push(@hashValue1, $getter);
1387         push(@hashValue2, "0");
1388         push(@hashSpecials, "DontDelete | ReadOnly");
1389     }
1390
1391     foreach my $function (@{$dataNode->functions}) {
1392         next if $function->{overloadIndex} && $function->{overloadIndex} > 1;
1393         my $name = $function->signature->name;
1394         push(@hashKeys, $name);
1395
1396         my $value = $codeGenerator->WK_lcfirst($className) . "PrototypeFunction" . $codeGenerator->WK_ucfirst($name);
1397         push(@hashValue1, $value);
1398
1399         my $numParameters = @{$function->parameters};
1400         push(@hashValue2, $numParameters);
1401
1402         my @specials = ();
1403         push(@specials, "DontDelete") unless $function->signature->extendedAttributes->{"Deletable"};
1404         push(@specials, "DontEnum") if $function->signature->extendedAttributes->{"DontEnum"};
1405         push(@specials, "Function");
1406         my $special = (@specials > 0) ? join(" | ", @specials) : "0";
1407         push(@hashSpecials, $special);
1408
1409         my $conditional = $function->signature->extendedAttributes->{"Conditional"};
1410         if ($conditional) {
1411             $conditionals{$name} = $conditional;
1412         }
1413     }
1414
1415     $object->GenerateHashTable($hashName, $hashSize,
1416                                \@hashKeys, \@hashSpecials,
1417                                \@hashValue1, \@hashValue2,
1418                                \%conditionals);
1419
1420     if ($dataNode->extendedAttributes->{"NoStaticTables"}) {
1421         push(@implContent, "static const HashTable* get${className}PrototypeTable(ExecState* exec)\n");
1422         push(@implContent, "{\n");
1423         push(@implContent, "    return getHashTableForGlobalData(exec->globalData(), &${className}PrototypeTable);\n");
1424         push(@implContent, "}\n\n");
1425         push(@implContent, "const ClassInfo ${className}Prototype::s_info = { \"${visibleClassName}Prototype\", &JSC::JSNonFinalObject::s_info, 0, get${className}PrototypeTable, CREATE_METHOD_TABLE(${className}Prototype) };\n\n");
1426     } else {
1427         push(@implContent, "const ClassInfo ${className}Prototype::s_info = { \"${visibleClassName}Prototype\", &JSC::JSNonFinalObject::s_info, &${className}PrototypeTable, 0, CREATE_METHOD_TABLE(${className}Prototype) };\n\n");
1428     }
1429     if ($interfaceName ne "DOMWindow" && !$dataNode->extendedAttributes->{"IsWorkerContext"}) {
1430         push(@implContent, "JSObject* ${className}Prototype::self(ExecState* exec, JSGlobalObject* globalObject)\n");
1431         push(@implContent, "{\n");
1432         push(@implContent, "    return getDOMPrototype<${className}>(exec, globalObject);\n");
1433         push(@implContent, "}\n\n");
1434     }
1435     if ($numConstants > 0 || $numFunctions > 0 || $dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"}) {
1436         push(@implContent, "bool ${className}Prototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\n");
1437         push(@implContent, "{\n");
1438
1439         if ($dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"}) {
1440             push(@implContent, "    if (getOwnPropertySlotDelegate(exec, propertyName, slot))\n");
1441             push(@implContent, "        return true;\n");
1442         }
1443
1444         if ($numConstants eq 0 && $numFunctions eq 0) {
1445             push(@implContent, "    return Base::getOwnPropertySlot(exec, propertyName, slot);\n");        
1446         } elsif ($numConstants eq 0) {
1447             push(@implContent, "    return getStaticFunctionSlot<JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, slot);\n");
1448         } elsif ($numFunctions eq 0) {
1449             push(@implContent, "    return getStaticValueSlot<${className}Prototype, JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, slot);\n");
1450         } else {
1451             push(@implContent, "    return getStaticPropertySlot<${className}Prototype, JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, slot);\n");
1452         }
1453         push(@implContent, "}\n\n");
1454
1455         push(@implContent, "bool ${className}Prototype::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)\n");
1456         push(@implContent, "{\n");
1457         
1458         if ($dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"}) {
1459             push(@implContent, "    if (getOwnPropertyDescriptorDelegate(exec, propertyName, descriptor))\n");
1460             push(@implContent, "        return true;\n");
1461         }
1462         
1463         if ($numConstants eq 0 && $numFunctions eq 0) {
1464             push(@implContent, "    return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor);\n");        
1465         } elsif ($numConstants eq 0) {
1466             push(@implContent, "    return getStaticFunctionDescriptor<JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, descriptor);\n");
1467         } elsif ($numFunctions eq 0) {
1468             push(@implContent, "    return getStaticValueDescriptor<${className}Prototype, JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, descriptor);\n");
1469         } else {
1470             push(@implContent, "    return getStaticPropertyDescriptor<${className}Prototype, JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, descriptor);\n");
1471         }
1472         push(@implContent, "}\n\n");
1473     }
1474
1475     if ($dataNode->extendedAttributes->{"DelegatingPrototypePutFunction"}) {
1476         push(@implContent, "void ${className}Prototype::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)\n");
1477         push(@implContent, "{\n");
1478         push(@implContent, "    put(this, exec, propertyName, value, slot);\n");
1479         push(@implContent, "}\n\n");
1480
1481         push(@implContent, "void ${className}Prototype::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)\n");
1482         push(@implContent, "{\n");
1483         push(@implContent, "    ${className}Prototype* thisObject = static_cast<${className}Prototype*>(cell);\n");
1484         push(@implContent, "    if (thisObject->putDelegate(exec, propertyName, value, slot))\n");
1485         push(@implContent, "        return;\n");
1486         push(@implContent, "    Base::put(thisObject, exec, propertyName, value, slot);\n");
1487         push(@implContent, "}\n\n");
1488     }
1489
1490     # - Initialize static ClassInfo object
1491     if ($numAttributes > 0 && $dataNode->extendedAttributes->{"NoStaticTables"}) {
1492         push(@implContent, "static const HashTable* get${className}Table(ExecState* exec)\n");
1493         push(@implContent, "{\n");
1494         push(@implContent, "    return getHashTableForGlobalData(exec->globalData(), &${className}Table);\n");
1495         push(@implContent, "}\n\n");
1496     }
1497
1498     push(@implContent, "const ClassInfo $className" . "::s_info = { \"${visibleClassName}\", &" . $parentClassName . "::s_info, ");
1499
1500     if ($numAttributes > 0 && !$dataNode->extendedAttributes->{"NoStaticTables"}) {
1501         push(@implContent, "&${className}Table");
1502     } else {
1503         push(@implContent, "0");
1504     }
1505     if ($numAttributes > 0 && $dataNode->extendedAttributes->{"NoStaticTables"}) {
1506         push(@implContent, ", get${className}Table ");
1507     } else {
1508         push(@implContent, ", 0 ");
1509     }
1510     push(@implContent, ", CREATE_METHOD_TABLE($className) };\n\n");
1511
1512     my $implType = $implClassName;
1513     my ($svgPropertyType, $svgListPropertyType, $svgNativeType) = GetSVGPropertyTypes($implType);
1514     $implType = $svgNativeType if $svgNativeType;
1515
1516     my $svgPropertyOrListPropertyType;
1517     $svgPropertyOrListPropertyType = $svgPropertyType if $svgPropertyType;
1518     $svgPropertyOrListPropertyType = $svgListPropertyType if $svgListPropertyType;
1519
1520     # Constructor
1521     if ($interfaceName eq "DOMWindow") {
1522         AddIncludesForTypeInImpl("JSDOMWindowShell");
1523         push(@implContent, "${className}::$className(JSGlobalData& globalData, Structure* structure, PassRefPtr<$implType> impl, JSDOMWindowShell* shell)\n");
1524         push(@implContent, "    : $parentClassName(globalData, structure, impl, shell)\n");
1525         push(@implContent, "{\n");
1526         push(@implContent, "}\n\n");
1527     } elsif ($dataNode->extendedAttributes->{"IsWorkerContext"}) {
1528         AddIncludesForTypeInImpl($interfaceName);
1529         push(@implContent, "${className}::$className(JSGlobalData& globalData, Structure* structure, PassRefPtr<$implType> impl)\n");
1530         push(@implContent, "    : $parentClassName(globalData, structure, impl)\n");
1531         push(@implContent, "{\n");
1532         push(@implContent, "}\n\n");
1533     } else {
1534         push(@implContent, "${className}::$className(Structure* structure, JSDOMGlobalObject* globalObject, PassRefPtr<$implType> impl)\n");
1535         if ($hasParent) {
1536             push(@implContent, "    : $parentClassName(structure, globalObject, impl)\n");
1537         } else {
1538             push(@implContent, "    : $parentClassName(structure, globalObject)\n");
1539             push(@implContent, "    , m_impl(impl)\n");
1540         }
1541         push(@implContent, "{\n");
1542         push(@implContent, "}\n\n");
1543
1544         push(@implContent, "void ${className}::finishCreation(JSGlobalData& globalData)\n");
1545         push(@implContent, "{\n");
1546         push(@implContent, "    Base::finishCreation(globalData);\n");
1547         push(@implContent, "    ASSERT(inherits(&s_info));\n");
1548         push(@implContent, "}\n\n");
1549     }
1550
1551     if (!$dataNode->extendedAttributes->{"ExtendsDOMGlobalObject"}) {
1552         push(@implContent, "JSObject* ${className}::createPrototype(ExecState* exec, JSGlobalObject* globalObject)\n");
1553         push(@implContent, "{\n");
1554         if ($hasParent && $parentClassName ne "JSC::DOMNodeFilter") {
1555             push(@implContent, "    return ${className}Prototype::create(exec->globalData(), globalObject, ${className}Prototype::createStructure(exec->globalData(), globalObject, ${parentClassName}Prototype::self(exec, globalObject)));\n");
1556         } else {
1557             push(@implContent, "    return ${className}Prototype::create(exec->globalData(), globalObject, ${className}Prototype::createStructure(globalObject->globalData(), globalObject, globalObject->objectPrototype()));\n");
1558         }
1559         push(@implContent, "}\n\n");
1560     }
1561
1562     my $hasGetter = $numAttributes > 0 
1563                  || !$dataNode->extendedAttributes->{"OmitConstructor"} 
1564                  || $dataNode->extendedAttributes->{"HasIndexGetter"}
1565                  || $dataNode->extendedAttributes->{"HasCustomIndexGetter"}
1566                  || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}
1567                  || $dataNode->extendedAttributes->{"DelegatingGetOwnPropertySlot"}
1568                  || $dataNode->extendedAttributes->{"CustomGetOwnPropertySlot"}
1569                  || $dataNode->extendedAttributes->{"HasNameGetter"}
1570                  || $dataNode->extendedAttributes->{"HasOverridingNameGetter"};
1571
1572     # Attributes
1573     if ($hasGetter) {
1574         if (!$dataNode->extendedAttributes->{"InlineGetOwnPropertySlot"} && !$dataNode->extendedAttributes->{"CustomGetOwnPropertySlot"}) {
1575             push(@implContent, "bool ${className}::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\n");
1576             push(@implContent, "{\n");
1577             push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
1578             push(@implContent, GenerateGetOwnPropertySlotBody($dataNode, $interfaceName, $className, $implClassName, $numAttributes > 0, 0));
1579             push(@implContent, "}\n\n");
1580             push(@implContent, "bool ${className}::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)\n");
1581             push(@implContent, "{\n");
1582             push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
1583             push(@implContent, GenerateGetOwnPropertyDescriptorBody($dataNode, $interfaceName, $className, $implClassName, $numAttributes > 0, 0));
1584             push(@implContent, "}\n\n");
1585         }
1586
1587         if (($dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) 
1588                 && !$dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
1589             push(@implContent, "bool ${className}::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)\n");
1590             push(@implContent, "{\n");
1591             push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
1592             push(@implContent, "    if (propertyName < static_cast<$implClassName*>(impl())->length()) {\n");
1593             if ($dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
1594                 push(@implContent, "        slot.setValue(getByIndex(exec, propertyName));\n");
1595             } else {
1596                 push(@implContent, "        slot.setCustomIndex(this, propertyName, indexGetter);\n");
1597             }
1598             push(@implContent, "        return true;\n");
1599             push(@implContent, "    }\n");
1600             push(@implContent, "    return getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);\n");
1601             push(@implContent, "}\n\n");
1602         }
1603         
1604         if ($numAttributes > 0) {
1605             foreach my $attribute (@{$dataNode->attributes}) {
1606                 my $name = $attribute->signature->name;
1607                 my $type = $codeGenerator->StripModule($attribute->signature->type);
1608                 my $getFunctionName = "js" . $interfaceName .  $codeGenerator->WK_ucfirst($attribute->signature->name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1609                 my $implGetterFunctionName = $codeGenerator->WK_lcfirst($name);
1610
1611                 my $attributeConditionalString = GenerateConditionalString($attribute->signature);
1612                 push(@implContent, "#if ${attributeConditionalString}\n") if $attributeConditionalString;
1613
1614                 push(@implContent, "JSValue ${getFunctionName}(ExecState* exec, JSValue slotBase, const Identifier&)\n");
1615                 push(@implContent, "{\n");
1616                 push(@implContent, "    ${className}* castedThis = static_cast<$className*>(asObject(slotBase));\n");
1617
1618                 if ($attribute->signature->extendedAttributes->{"CachedAttribute"}) {
1619                     $needsMarkChildren = 1;
1620                 }
1621
1622                 if ($dataNode->extendedAttributes->{"CheckDomainSecurity"} && 
1623                         !$attribute->signature->extendedAttributes->{"DoNotCheckDomainSecurity"} &&
1624                         !$attribute->signature->extendedAttributes->{"DoNotCheckDomainSecurityOnGet"}) {
1625                     push(@implContent, "    if (!castedThis->allowsAccessFrom(exec))\n");
1626                     push(@implContent, "        return jsUndefined();\n");
1627                 }
1628
1629                 if ($attribute->signature->extendedAttributes->{"Custom"} || $attribute->signature->extendedAttributes->{"JSCCustom"} || $attribute->signature->extendedAttributes->{"CustomGetter"} || $attribute->signature->extendedAttributes->{"JSCCustomGetter"}) {
1630                     push(@implContent, "    return castedThis->$implGetterFunctionName(exec);\n");
1631                 } elsif ($attribute->signature->extendedAttributes->{"CheckNodeSecurity"}) {
1632                     $implIncludes{"JSDOMBinding.h"} = 1;
1633                     push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1634                     push(@implContent, "    return checkNodeSecurity(exec, imp->$implGetterFunctionName()) ? " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp->$implGetterFunctionName()", "castedThis") . " : jsUndefined();\n");
1635                 } elsif ($attribute->signature->extendedAttributes->{"CheckFrameSecurity"}) {
1636                     $implIncludes{"Document.h"} = 1;
1637                     $implIncludes{"JSDOMBinding.h"} = 1;
1638                     push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1639                     push(@implContent, "    return checkNodeSecurity(exec, imp->contentDocument()) ? " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp->$implGetterFunctionName()", "castedThis") . " : jsUndefined();\n");
1640                 } elsif ($type eq "EventListener") {
1641                     $implIncludes{"EventListener.h"} = 1;
1642                     push(@implContent, "    UNUSED_PARAM(exec);\n");
1643                     push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1644                     push(@implContent, "    if (EventListener* listener = imp->$implGetterFunctionName()) {\n");
1645                     push(@implContent, "        if (const JSEventListener* jsListener = JSEventListener::cast(listener)) {\n");
1646                     if ($implClassName eq "Document" || $implClassName eq "WorkerContext" || $implClassName eq "SharedWorkerContext" || $implClassName eq "DedicatedWorkerContext") {
1647                         push(@implContent, "            if (JSObject* jsFunction = jsListener->jsFunction(imp))\n");
1648                     } else {
1649                         push(@implContent, "            if (JSObject* jsFunction = jsListener->jsFunction(imp->scriptExecutionContext()))\n");
1650                     }
1651                     push(@implContent, "                return jsFunction;\n");
1652                     push(@implContent, "        }\n");
1653                     push(@implContent, "    }\n");
1654                     push(@implContent, "    return jsNull();\n");
1655                 } elsif ($attribute->signature->type =~ /Constructor$/) {
1656                     my $constructorType = $codeGenerator->StripModule($attribute->signature->type);
1657                     $constructorType =~ s/Constructor$//;
1658                     # Constructor attribute is only used by DOMWindow.idl, so it's correct to pass castedThis as the global object
1659                     # Once JSDOMWrappers have a back-pointer to the globalObject we can pass castedThis->globalObject()
1660                     push(@implContent, "    return JS" . $constructorType . "::getConstructor(exec, castedThis);\n");
1661                 } elsif (!@{$attribute->getterExceptions}) {
1662                     push(@implContent, "    UNUSED_PARAM(exec);\n");
1663                     my $cacheIndex = 0;
1664                     if ($attribute->signature->extendedAttributes->{"CachedAttribute"}) {
1665                         $cacheIndex = $currentCachedAttribute;
1666                         $currentCachedAttribute++;
1667                         push(@implContent, "    if (JSValue cachedValue = m_" . $attribute->signature->name . ".get())\n");
1668                         push(@implContent, "        return cachedValue;\n");
1669                     }
1670
1671                     if ($svgListPropertyType) {
1672                         push(@implContent, "    JSValue result =  " . NativeToJSValue($attribute->signature, 0, $implClassName, "castedThis->impl()->$implGetterFunctionName()", "castedThis") . ";\n");
1673                     } elsif ($svgPropertyOrListPropertyType) {
1674                         push(@implContent, "    $svgPropertyOrListPropertyType& imp = castedThis->impl()->propertyReference();\n");
1675                         if ($svgPropertyOrListPropertyType eq "float") { # Special case for JSSVGNumber
1676                             push(@implContent, "    JSValue result =  " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp", "castedThis") . ";\n");
1677                         } else {
1678                             push(@implContent, "    JSValue result =  " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp.$implGetterFunctionName()", "castedThis") . ";\n");
1679                         }
1680                     } else {
1681                         my $getterExpression = "imp->" . $codeGenerator->GetterExpressionPrefix(\%implIncludes, $interfaceName, $attribute) . ")";
1682                         my $jsType = NativeToJSValue($attribute->signature, 0, $implClassName, $getterExpression, "castedThis");
1683                         push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1684                         if ($codeGenerator->IsSVGAnimatedType($type)) {
1685                             push(@implContent, "    RefPtr<$type> obj = $jsType;\n");
1686                             push(@implContent, "    JSValue result =  toJS(exec, castedThis->globalObject(), obj.get());\n");
1687                         } else {
1688                             push(@implContent, "    JSValue result = $jsType;\n");
1689                         }
1690                     }
1691                     
1692                     push(@implContent, "    m_" . $attribute->signature->name . ".set(exec->globalData(), this, result);\n") if ($attribute->signature->extendedAttributes->{"CachedAttribute"});
1693                     push(@implContent, "    return result;\n");
1694
1695                 } else {
1696                     push(@implContent, "    ExceptionCode ec = 0;\n");                    
1697                     if ($svgPropertyOrListPropertyType) {
1698                         push(@implContent, "    $svgPropertyOrListPropertyType imp(*castedThis->impl());\n");
1699                         push(@implContent, "    JSC::JSValue result = " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp.$implGetterFunctionName(ec)", "castedThis") . ";\n");
1700                     } else {
1701                         push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1702                         push(@implContent, "    JSC::JSValue result = " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp->$implGetterFunctionName(ec)", "castedThis") . ";\n");
1703                     }
1704
1705                     push(@implContent, "    setDOMException(exec, ec);\n");
1706                     push(@implContent, "    return result;\n");
1707                 }
1708
1709                 push(@implContent, "}\n\n");
1710
1711                 push(@implContent, "#endif\n") if $attributeConditionalString;
1712
1713                 push(@implContent, "\n");
1714             }
1715
1716             if (!$dataNode->extendedAttributes->{"OmitConstructor"}) {
1717                 my $constructorFunctionName = "js" . $interfaceName . "Constructor";
1718
1719                 push(@implContent, "JSValue ${constructorFunctionName}(ExecState* exec, JSValue slotBase, const Identifier&)\n");
1720                 push(@implContent, "{\n");
1721                 push(@implContent, "    ${className}* domObject = static_cast<$className*>(asObject(slotBase));\n");
1722
1723                 if ($dataNode->extendedAttributes->{"CheckDomainSecurity"}) {
1724                     push(@implContent, "    if (!domObject->allowsAccessFrom(exec))\n");
1725                     push(@implContent, "        return jsUndefined();\n");
1726                 }
1727
1728                 push(@implContent, "    return ${className}::getConstructor(exec, domObject->globalObject());\n");
1729                 push(@implContent, "}\n\n");
1730             }
1731         }
1732
1733         # Check if we have any writable attributes
1734         my $hasReadWriteProperties = 0;
1735         foreach my $attribute (@{$dataNode->attributes}) {
1736             $hasReadWriteProperties = 1 if $attribute->type !~ /^readonly/;
1737         }
1738
1739         my $hasSetter = $hasReadWriteProperties
1740                      || $dataNode->extendedAttributes->{"DelegatingPutFunction"}
1741                      || $dataNode->extendedAttributes->{"HasCustomIndexSetter"};
1742
1743         if ($hasSetter) {
1744             if (!$dataNode->extendedAttributes->{"CustomPutFunction"}) {
1745                 push(@implContent, "void ${className}::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)\n");
1746                 push(@implContent, "{\n");
1747                 push(@implContent, "    put(this, exec, propertyName, value, slot);\n");
1748                 push(@implContent, "}\n\n");
1749
1750                 push(@implContent, "void ${className}::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)\n");
1751                 push(@implContent, "{\n");
1752                 push(@implContent, "    ${className}* thisObject = static_cast<${className}*>(cell);\n");
1753                 push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\n");
1754                 if ($dataNode->extendedAttributes->{"HasCustomIndexSetter"}) {
1755                     push(@implContent, "    bool ok;\n");
1756                     push(@implContent, "    unsigned index = propertyName.toUInt32(ok);\n");
1757                     push(@implContent, "    if (ok) {\n");
1758                     push(@implContent, "        thisObject->indexSetter(exec, index, value);\n");
1759                     push(@implContent, "        return;\n");
1760                     push(@implContent, "    }\n");
1761                 }
1762                 if ($dataNode->extendedAttributes->{"DelegatingPutFunction"}) {
1763                     push(@implContent, "    if (thisObject->putDelegate(exec, propertyName, value, slot))\n");
1764                     push(@implContent, "        return;\n");
1765                 }
1766
1767                 if ($hasReadWriteProperties) {
1768                     push(@implContent, "    lookupPut<$className, Base>(exec, propertyName, value, " . hashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", thisObject, slot);\n");
1769                 } else {
1770                     push(@implContent, "    Base::put(thisObject, exec, propertyName, value, slot);\n");
1771                 }
1772                 push(@implContent, "}\n\n");
1773             }
1774
1775             if ($dataNode->extendedAttributes->{"HasCustomIndexSetter"}) {
1776                 push(@implContent, "void ${className}::put(ExecState* exec, unsigned propertyName, JSValue value)\n");
1777                 push(@implContent, "{\n");
1778                 push(@implContent, "    put(this, exec, propertyName, value);\n");
1779                 push(@implContent, "}\n\n");
1780                 push(@implContent, "void ${className}::put(JSCell* cell, ExecState* exec, unsigned propertyName, JSValue value)\n");
1781                 push(@implContent, "{\n");
1782                 push(@implContent, "    ${className}* thisObject = static_cast<${className}*>(cell);\n");
1783                 push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\n");
1784                 push(@implContent, "    thisObject->indexSetter(exec, propertyName, value);\n");
1785                 push(@implContent, "    return;\n");
1786                 push(@implContent, "}\n\n");
1787             }
1788
1789             if ($hasReadWriteProperties) {
1790                 foreach my $attribute (@{$dataNode->attributes}) {
1791                     if ($attribute->type !~ /^readonly/) {
1792                         my $name = $attribute->signature->name;
1793                         my $type = $codeGenerator->StripModule($attribute->signature->type);
1794                         my $putFunctionName = "setJS" . $interfaceName .  $codeGenerator->WK_ucfirst($name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1795                         my $implSetterFunctionName = $codeGenerator->WK_ucfirst($name);
1796
1797                         my $attributeConditionalString = GenerateConditionalString($attribute->signature);
1798                         push(@implContent, "#if ${attributeConditionalString}\n") if $attributeConditionalString;
1799
1800                         push(@implContent, "void ${putFunctionName}(ExecState* exec, JSObject* thisObject, JSValue value)\n");
1801                         push(@implContent, "{\n");
1802
1803                         if ($dataNode->extendedAttributes->{"CheckDomainSecurity"} && !$attribute->signature->extendedAttributes->{"DoNotCheckDomainSecurity"}) {
1804                             if ($interfaceName eq "DOMWindow") {
1805                                 push(@implContent, "    if (!static_cast<$className*>(thisObject)->allowsAccessFrom(exec))\n");
1806                             } else {
1807                                 push(@implContent, "    if (!allowsAccessFromFrame(exec, static_cast<$className*>(thisObject)->impl()->frame()))\n");
1808                             }
1809                             push(@implContent, "        return;\n");
1810                         }
1811
1812                         if ($attribute->signature->extendedAttributes->{"Custom"} || $attribute->signature->extendedAttributes->{"JSCCustom"} || $attribute->signature->extendedAttributes->{"CustomSetter"} || $attribute->signature->extendedAttributes->{"JSCCustomSetter"}) {
1813                             push(@implContent, "    static_cast<$className*>(thisObject)->set$implSetterFunctionName(exec, value);\n");
1814                         } elsif ($type eq "EventListener") {
1815                             $implIncludes{"JSEventListener.h"} = 1;
1816                             push(@implContent, "    UNUSED_PARAM(exec);\n");
1817                             push(@implContent, "    ${className}* castedThis = static_cast<${className}*>(thisObject);\n");
1818                             my $windowEventListener = $attribute->signature->extendedAttributes->{"WindowEventListener"};
1819                             if ($windowEventListener) {
1820                                 push(@implContent, "    JSDOMGlobalObject* globalObject = castedThis->globalObject();\n");
1821                             }
1822                             push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1823                             if ((($interfaceName eq "DOMWindow") or ($interfaceName eq "WorkerContext")) and $name eq "onerror") {
1824                                 $implIncludes{"JSErrorHandler.h"} = 1;
1825                                 push(@implContent, "    imp->set$implSetterFunctionName(createJSErrorHandler(exec, value, thisObject));\n");
1826                             } else {
1827                                 push(@implContent, GenerateAttributeEventListenerCall($className, $implSetterFunctionName, $windowEventListener));
1828                             }
1829                         } elsif ($attribute->signature->type =~ /Constructor$/) {
1830                             my $constructorType = $attribute->signature->type;
1831                             $constructorType =~ s/Constructor$//;
1832                             if ($constructorType ne "DOMObject") {
1833                                 AddToImplIncludes("JS" . $constructorType . ".h", $attribute->signature->extendedAttributes->{"Conditional"});
1834                             }
1835                             push(@implContent, "    // Shadowing a built-in constructor\n");
1836                             if ($interfaceName eq "DOMWindow" && $className eq "JSblah") {
1837                                 # FIXME: This branch never executes and should be removed.
1838                                 push(@implContent, "    static_cast<$className*>(thisObject)->putDirect(exec->globalData(), exec->propertyNames().constructor, value);\n");
1839                             } else {
1840                                 push(@implContent, "    static_cast<$className*>(thisObject)->putDirect(exec->globalData(), Identifier(exec, \"$name\"), value);\n");
1841                             }
1842                         } elsif ($attribute->signature->extendedAttributes->{"Replaceable"}) {
1843                             push(@implContent, "    // Shadowing a built-in object\n");
1844                             push(@implContent, "    static_cast<$className*>(thisObject)->putDirect(exec->globalData(), Identifier(exec, \"$name\"), value);\n");
1845                         } else {
1846                             push(@implContent, "    $className* castedThis = static_cast<$className*>(thisObject);\n");
1847                             push(@implContent, "    $implType* imp = static_cast<$implType*>(castedThis->impl());\n");
1848                             push(@implContent, "    ExceptionCode ec = 0;\n") if @{$attribute->setterExceptions};
1849
1850                             # If the "StrictTypeChecking" extended attribute is present, and the attribute's type is an
1851                             # interface type, then if the incoming value does not implement that interface, a TypeError
1852                             # is thrown rather than silently passing NULL to the C++ code.
1853                             # Per the Web IDL and ECMAScript specifications, incoming values can always be converted to
1854                             # both strings and numbers, so do not throw TypeError if the attribute is of these types.
1855                             if ($attribute->signature->extendedAttributes->{"StrictTypeChecking"}) {
1856                                 $implIncludes{"<runtime/Error.h>"} = 1;
1857
1858                                 my $argType = $attribute->signature->type;
1859                                 if (!IsNativeType($argType)) {
1860                                     push(@implContent, "    if (!value.isUndefinedOrNull() && !value.inherits(&JS${argType}::s_info)) {\n");
1861                                     push(@implContent, "        throwVMTypeError(exec);\n");
1862                                     push(@implContent, "        return;\n");
1863                                     push(@implContent, "    };\n");
1864                                 }
1865                             }
1866
1867                             my $nativeValue = JSValueToNative($attribute->signature, "value");
1868                             if ($svgPropertyOrListPropertyType) {
1869                                 if ($svgPropertyType) {
1870                                     push(@implContent, "    if (imp->role() == AnimValRole) {\n");
1871                                     push(@implContent, "        setDOMException(exec, NO_MODIFICATION_ALLOWED_ERR);\n");
1872                                     push(@implContent, "        return;\n");
1873                                     push(@implContent, "    }\n");
1874                                 }
1875                                 push(@implContent, "    $svgPropertyOrListPropertyType& podImp = imp->propertyReference();\n");
1876                                 if ($svgPropertyOrListPropertyType eq "float") { # Special case for JSSVGNumber
1877                                     push(@implContent, "    podImp = $nativeValue;\n");
1878                                 } else {
1879                                     push(@implContent, "    podImp.set$implSetterFunctionName($nativeValue");
1880                                     push(@implContent, ", ec") if @{$attribute->setterExceptions};
1881                                     push(@implContent, ");\n");
1882                                     push(@implContent, "    setDOMException(exec, ec);\n") if @{$attribute->setterExceptions};
1883                                 }
1884                                 if ($svgPropertyType) {
1885                                     if (@{$attribute->setterExceptions}) {
1886                                         push(@implContent, "    if (!ec)\n"); 
1887                                         push(@implContent, "        imp->commitChange();\n");
1888                                     } else {
1889                                         push(@implContent, "    imp->commitChange();\n");
1890                                     }
1891                                 }
1892                             } else {
1893                                 my $setterExpressionPrefix = $codeGenerator->SetterExpressionPrefix(\%implIncludes, $interfaceName, $attribute);
1894                                 push(@implContent, "    imp->$setterExpressionPrefix$nativeValue");
1895                                 push(@implContent, ", ec") if @{$attribute->setterExceptions};
1896                                 push(@implContent, ");\n");
1897                                 push(@implContent, "    setDOMException(exec, ec);\n") if @{$attribute->setterExceptions};
1898                             }
1899                         }
1900                         
1901                         push(@implContent, "}\n\n");
1902
1903                         push(@implContent, "#endif\n") if $attributeConditionalString;
1904
1905                         push(@implContent, "\n");
1906                     }
1907                 }
1908             }
1909             
1910             if ($dataNode->extendedAttributes->{"ReplaceableConstructor"}) {
1911                 my $constructorFunctionName = "setJS" . $interfaceName . "Constructor";
1912                 
1913                 push(@implContent, "void ${constructorFunctionName}(ExecState* exec, JSObject* thisObject, JSValue value)\n");
1914                 push(@implContent, "{\n");
1915                 if ($dataNode->extendedAttributes->{"CheckDomainSecurity"}) {
1916                     if ($interfaceName eq "DOMWindow") {
1917                         push(@implContent, "    if (!static_cast<$className*>(thisObject)->allowsAccessFrom(exec))\n");
1918                     } else {
1919                         push(@implContent, "    if (!allowsAccessFromFrame(exec, static_cast<$className*>(thisObject)->impl()->frame()))\n");
1920                     }
1921                     push(@implContent, "        return;\n");
1922                 }
1923
1924                 push(@implContent, "    // Shadowing a built-in constructor\n");
1925
1926                 if ($interfaceName eq "DOMWindow") {
1927                     push(@implContent, "    static_cast<$className*>(thisObject)->putDirect(exec->globalData(), exec->propertyNames().constructor, value);\n");
1928                 } else {
1929                     die "No way to handle interface with ReplaceableConstructor extended attribute: $interfaceName";
1930                 }
1931                 push(@implContent, "}\n\n");
1932             }        
1933         }
1934     }
1935
1936     if (($dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) && !$dataNode->extendedAttributes->{"CustomGetPropertyNames"}) {
1937         push(@implContent, "void ${className}::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)\n");
1938         push(@implContent, "{\n");
1939         push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
1940         if ($dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
1941             push(@implContent, "    for (unsigned i = 0; i < static_cast<${implClassName}*>(impl())->length(); ++i)\n");
1942             push(@implContent, "        propertyNames.add(Identifier::from(exec, i));\n");
1943         }
1944         push(@implContent, "     Base::getOwnPropertyNames(exec, propertyNames, mode);\n");
1945         push(@implContent, "}\n\n");
1946     }
1947
1948     if (!$dataNode->extendedAttributes->{"OmitConstructor"}) {
1949         push(@implContent, "JSValue ${className}::getConstructor(ExecState* exec, JSGlobalObject* globalObject)\n{\n");
1950         push(@implContent, "    return getDOMConstructor<${className}Constructor>(exec, static_cast<JSDOMGlobalObject*>(globalObject));\n");
1951         push(@implContent, "}\n\n");
1952     }
1953
1954     # Functions
1955     if ($numFunctions > 0) {
1956         foreach my $function (@{$dataNode->functions}) {
1957             AddIncludesForTypeInImpl($function->signature->type);
1958
1959             my $functionName = $codeGenerator->WK_lcfirst($className) . "PrototypeFunction" . $codeGenerator->WK_ucfirst($function->signature->name);
1960
1961             if ($function->{overloads} && @{$function->{overloads}} > 1) {
1962                 # Append a number to an overloaded method's name to make it unique:
1963                 $functionName = $functionName . $function->{overloadIndex};
1964                 # Make this function static to avoid compiler warnings, since we
1965                 # don't generate a prototype for it in the header.
1966                 push(@implContent, "static ");
1967             }
1968             
1969             my $functionImplementationName = $function->signature->extendedAttributes->{"ImplementationFunction"} || $codeGenerator->WK_lcfirst($function->signature->name);
1970
1971             my $conditional = $function->signature->extendedAttributes->{"Conditional"};
1972             if ($conditional) {
1973                 my $conditionalString = GenerateConditionalStringFromAttributeValue($conditional);
1974                 push(@implContent, "#if ${conditionalString}\n");
1975             }
1976
1977             push(@implContent, "EncodedJSValue JSC_HOST_CALL ${functionName}(ExecState* exec)\n");
1978             push(@implContent, "{\n");
1979
1980             $implIncludes{"<runtime/Error.h>"} = 1;
1981
1982             if ($interfaceName eq "DOMWindow") {
1983                 push(@implContent, "    $className* castedThis = toJSDOMWindow(exec->hostThisValue().toThisObject(exec));\n");
1984                 push(@implContent, "    if (!castedThis)\n");
1985                 push(@implContent, "        return throwVMTypeError(exec);\n");
1986             } elsif ($dataNode->extendedAttributes->{"IsWorkerContext"}) {
1987                 push(@implContent, "    $className* castedThis = to${className}(exec->hostThisValue().toThisObject(exec));\n");
1988                 push(@implContent, "    if (!castedThis)\n");
1989                 push(@implContent, "        return throwVMTypeError(exec);\n");
1990             } else {
1991                 push(@implContent, "    JSValue thisValue = exec->hostThisValue();\n");
1992                 push(@implContent, "    if (!thisValue.inherits(&${className}::s_info))\n");
1993                 push(@implContent, "        return throwVMTypeError(exec);\n");
1994                 push(@implContent, "    $className* castedThis = static_cast<$className*>(asObject(thisValue));\n");
1995             }
1996             push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(castedThis, &${className}::s_info);\n");
1997
1998             if ($dataNode->extendedAttributes->{"CheckDomainSecurity"} && 
1999                 !$function->signature->extendedAttributes->{"DoNotCheckDomainSecurity"}) {
2000                 push(@implContent, "    if (!castedThis->allowsAccessFrom(exec))\n");
2001                 push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2002             }
2003
2004             if ($function->signature->extendedAttributes->{"Custom"} || $function->signature->extendedAttributes->{"JSCCustom"}) {
2005                 push(@implContent, "    return JSValue::encode(castedThis->" . $functionImplementationName . "(exec));\n");
2006             } else {
2007                 push(@implContent, "    $implType* imp = static_cast<$implType*>(castedThis->impl());\n");
2008                 if ($svgPropertyType) {
2009                     push(@implContent, "    if (imp->role() == AnimValRole) {\n");
2010                     push(@implContent, "        setDOMException(exec, NO_MODIFICATION_ALLOWED_ERR);\n");
2011                     push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2012                     push(@implContent, "    }\n");
2013                     push(@implContent, "    $svgPropertyType& podImp = imp->propertyReference();\n");
2014                 }
2015
2016                 my $numParameters = @{$function->parameters};
2017
2018                 my $requiresAllArguments;
2019                 my $requiresAllArgumentsDefault = "";
2020                 if (!$dataNode->extendedAttributes->{"LegacyDefaultOptionalArguments"}) {
2021                     $requiresAllArgumentsDefault = "Raise";
2022                 }
2023                 $requiresAllArguments = $function->signature->extendedAttributes->{"RequiresAllArguments"} || $requiresAllArgumentsDefault;
2024                 if ($requiresAllArguments) {
2025                     my $numMandatoryParams = @{$function->parameters};
2026                     foreach my $param (reverse(@{$function->parameters})) {
2027                         if ($param->extendedAttributes->{"Optional"}) {
2028                             $numMandatoryParams--;
2029                         } else {
2030                             last;
2031                         }
2032                     }
2033                     if ($numMandatoryParams > 0)
2034                     {
2035                         push(@implContent, "    if (exec->argumentCount() < $numMandatoryParams)\n");
2036                         if ($requiresAllArguments eq "Raise") {
2037                             push(@implContent, "        return throwVMError(exec, createTypeError(exec, \"Not enough arguments\"));\n");
2038                         } else {
2039                             push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2040                         }
2041                     }
2042                 }
2043
2044                 if (@{$function->raisesExceptions}) {
2045                     push(@implContent, "    ExceptionCode ec = 0;\n");
2046                 }
2047
2048                 if ($function->signature->extendedAttributes->{"SVGCheckSecurityDocument"}) {
2049                     push(@implContent, "    if (!checkNodeSecurity(exec, imp->getSVGDocument(" . (@{$function->raisesExceptions} ? "ec" : "") .")))\n");
2050                     push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2051                     $implIncludes{"JSDOMBinding.h"} = 1;
2052                 }
2053
2054                 if ($function->signature->name eq "addEventListener") {
2055                     push(@implContent, GenerateEventListenerCall($className, "add"));
2056                 } elsif ($function->signature->name eq "removeEventListener") {
2057                     push(@implContent, GenerateEventListenerCall($className, "remove"));
2058                 } else {
2059                     my $argsIndex = 0;
2060                     my $paramIndex = 0;
2061                     my $functionString = (($svgPropertyOrListPropertyType and !$svgListPropertyType) ? "podImp." : "imp->") . $functionImplementationName . "(";
2062                     my $hasOptionalArguments = 0;
2063
2064                     if ($function->signature->extendedAttributes->{"CustomArgumentHandling"}) {
2065                         push(@implContent, "    RefPtr<ScriptArguments> scriptArguments(createScriptArguments(exec, $numParameters));\n");
2066                         push(@implContent, "    size_t maxStackSize = imp->shouldCaptureFullStackTrace() ? ScriptCallStack::maxCallStackSizeToCapture : 1;\n");
2067                         push(@implContent, "    RefPtr<ScriptCallStack> callStack(createScriptCallStack(exec, maxStackSize));\n");
2068                         $implIncludes{"ScriptArguments.h"} = 1;
2069                         $implIncludes{"ScriptCallStack.h"} = 1;
2070                         $implIncludes{"ScriptCallStackFactory.h"} = 1;
2071                     }
2072
2073                     my $callWith = $function->signature->extendedAttributes->{"CallWith"};
2074                     if ($callWith) {
2075                         my $callWithArg = "COMPILE_ASSERT(false)";
2076                         if ($callWith eq "DynamicFrame") {
2077                             push(@implContent, "    Frame* dynamicFrame = toDynamicFrame(exec);\n");
2078                             push(@implContent, "    if (!dynamicFrame)\n");
2079                             push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2080                             $callWithArg = "dynamicFrame";
2081                         } elsif ($callWith eq "ScriptState") {
2082                             $callWithArg = "exec";
2083                         } elsif ($callWith eq "ScriptExecutionContext") {
2084                             push(@implContent, "    ScriptExecutionContext* scriptContext = static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject())->scriptExecutionContext();\n");
2085                             push(@implContent, "    if (!scriptContext)\n");
2086                             push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2087                             $callWithArg = "scriptContext"; 
2088                         }
2089                         $functionString .= ", " if $paramIndex;
2090                         $functionString .= $callWithArg;
2091                         $paramIndex++;
2092                     }
2093
2094                     $implIncludes{"ExceptionCode.h"} = 1;
2095                     $implIncludes{"JSDOMBinding.h"} = 1;
2096                     foreach my $parameter (@{$function->parameters}) {
2097                         # Optional callbacks should be treated differently, because they always have a default value (0),
2098                         # and we can reduce the number of overloaded functions that take a different number of parameters.
2099                         # Optional arguments with default values [Optional=CallWithDefaultValue] should not generate an early call.
2100                         my $optional = $parameter->extendedAttributes->{"Optional"};        
2101                         if ($optional && $optional ne "CallWithDefaultValue" && !$parameter->extendedAttributes->{"Callback"}) {
2102                             # Generate early call if there are enough parameters.
2103                             if (!$hasOptionalArguments) {
2104                                 push(@implContent, "\n    size_t argsCount = exec->argumentCount();\n");
2105                                 $hasOptionalArguments = 1;
2106                             }
2107                             push(@implContent, "    if (argsCount <= $argsIndex) {\n");
2108                             GenerateImplementationFunctionCall($function, $functionString, $paramIndex, "    " x 2, $svgPropertyType, $implClassName);
2109                             push(@implContent, "    }\n\n");
2110                         }
2111
2112                         my $name = $parameter->name;
2113                         my $argType = $codeGenerator->StripModule($parameter->type);
2114
2115                         if ($argType eq "XPathNSResolver") {
2116                             push(@implContent, "    RefPtr<XPathNSResolver> customResolver;\n");
2117                             push(@implContent, "    XPathNSResolver* resolver = toXPathNSResolver(exec->argument($argsIndex));\n");
2118                             push(@implContent, "    if (!resolver) {\n");
2119                             push(@implContent, "        customResolver = JSCustomXPathNSResolver::create(exec, exec->argument($argsIndex));\n");
2120                             push(@implContent, "        if (exec->hadException())\n");
2121                             push(@implContent, "            return JSValue::encode(jsUndefined());\n");
2122                             push(@implContent, "        resolver = customResolver.get();\n");
2123                             push(@implContent, "    }\n");
2124                         } elsif ($parameter->extendedAttributes->{"Callback"}) {
2125                             my $callbackClassName = GetCallbackClassName($argType);
2126                             $implIncludes{"$callbackClassName.h"} = 1;
2127                             if ($parameter->extendedAttributes->{"Optional"}) {
2128                                 push(@implContent, "    RefPtr<$argType> $name;\n");
2129                                 push(@implContent, "    if (exec->argumentCount() > $argsIndex && !exec->argument($argsIndex).isUndefinedOrNull()) {\n");
2130                                 push(@implContent, "        if (!exec->argument($argsIndex).isObject()) {\n");
2131                                 push(@implContent, "            setDOMException(exec, TYPE_MISMATCH_ERR);\n");
2132                                 push(@implContent, "            return JSValue::encode(jsUndefined());\n");
2133                                 push(@implContent, "        }\n");
2134                                 push(@implContent, "        $name = ${callbackClassName}::create(asObject(exec->argument($argsIndex)), castedThis->globalObject());\n");
2135                                 push(@implContent, "    }\n");
2136                             } else {
2137                                 push(@implContent, "    if (exec->argumentCount() <= $argsIndex || !exec->argument($argsIndex).isObject()) {\n");
2138                                 push(@implContent, "        setDOMException(exec, TYPE_MISMATCH_ERR);\n");
2139                                 push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2140                                 push(@implContent, "    }\n");
2141                                 push(@implContent, "    RefPtr<$argType> $name = ${callbackClassName}::create(asObject(exec->argument($argsIndex)), castedThis->globalObject());\n");
2142                             }
2143                         } else {
2144                             # If the "StrictTypeChecking" extended attribute is present, and the argument's type is an
2145                             # interface type, then if the incoming value does not implement that interface, a TypeError
2146                             # is thrown rather than silently passing NULL to the C++ code.
2147                             # Per the Web IDL and ECMAScript semantics, incoming values can always be converted to both
2148                             # strings and numbers, so do not throw TypeError if the argument is of these types.
2149                             if ($function->signature->extendedAttributes->{"StrictTypeChecking"}) {
2150                                 $implIncludes{"<runtime/Error.h>"} = 1;
2151
2152                                 my $argValue = "exec->argument($argsIndex)";
2153                                 if (!IsNativeType($argType)) {
2154                                     push(@implContent, "    if (exec->argumentCount() > $argsIndex && !${argValue}.isUndefinedOrNull() && !${argValue}.inherits(&JS${argType}::s_info))\n");
2155                                     push(@implContent, "        return throwVMTypeError(exec);\n");
2156                                 }
2157                             }
2158
2159                             push(@implContent, "    " . GetNativeTypeFromSignature($parameter) . " $name(" . JSValueToNative($parameter, "exec->argument($argsIndex)") . ");\n");
2160
2161                             # If a parameter is "an index" and it's negative it should throw an INDEX_SIZE_ERR exception.
2162                             # But this needs to be done in the bindings, because the type is unsigned and the fact that it
2163                             # was negative will be lost by the time we're inside the DOM.
2164                             if ($parameter->extendedAttributes->{"IsIndex"}) {
2165                                 push(@implContent, "    if ($name < 0) {\n");
2166                                 push(@implContent, "        setDOMException(exec, INDEX_SIZE_ERR);\n");
2167                                 push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2168                                 push(@implContent, "    }\n");
2169                             }
2170
2171                             # Check if the type conversion succeeded.
2172                             push(@implContent, "    if (exec->hadException())\n");
2173                             push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2174
2175                             if ($codeGenerator->IsSVGTypeNeedingTearOff($argType) and not $implClassName =~ /List$/) {
2176                                 push(@implContent, "    if (!$name) {\n");
2177                                 push(@implContent, "        setDOMException(exec, TYPE_MISMATCH_ERR);\n");
2178                                 push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2179                                 push(@implContent, "    }\n");
2180                             }
2181                         }
2182
2183                         $functionString .= ", " if $paramIndex;
2184
2185                         if ($argType eq "NodeFilter") {
2186                             $functionString .= "$name.get()";
2187                         } elsif ($codeGenerator->IsSVGTypeNeedingTearOff($argType) and not $implClassName =~ /List$/) {
2188                             $functionString .= "$name->propertyReference()";
2189                         } else {
2190                             $functionString .= $name;
2191                         }
2192                         $argsIndex++;
2193                         $paramIndex++;
2194                     }
2195
2196                     if ($function->signature->extendedAttributes->{"NeedsUserGestureCheck"}) {
2197                         $functionString .= ", " if $paramIndex;
2198                         $functionString .= "ScriptController::processingUserGesture()";
2199                         $paramIndex++;
2200                         $implIncludes{"ScriptController.h"} = 1;
2201                     }
2202
2203                     push(@implContent, "\n");
2204                     GenerateImplementationFunctionCall($function, $functionString, $paramIndex, "    ", $svgPropertyType, $implClassName);
2205                 }
2206             }
2207
2208             push(@implContent, "}\n\n");
2209
2210             if ($function->{overloads} && @{$function->{overloads}} > 1 && $function->{overloadIndex} == @{$function->{overloads}}) {
2211                 # Generate a function dispatching call to the rest of the overloads.
2212                 GenerateOverloadedPrototypeFunction($function, $dataNode, $implClassName);
2213             }
2214
2215             if ($conditional) {
2216                 push(@implContent, "#endif\n\n");
2217             }
2218         }
2219         
2220         if ($needsMarkChildren && !$dataNode->extendedAttributes->{"CustomMarkFunction"}) {
2221             push(@implContent, "void ${className}::visitChildren(JSCell* cell, SlotVisitor& visitor)\n");
2222             push(@implContent, "{\n");
2223             push(@implContent, "    ${className}* thisObject = static_cast<${className}*>(cell);\n");
2224             push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\n");
2225             push(@implContent, "    COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);\n");
2226             push(@implContent, "    ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());\n");
2227             push(@implContent, "    Base::visitChildren(thisObject, visitor);\n");
2228             if ($dataNode->extendedAttributes->{"EventTarget"}) {
2229                 push(@implContent, "    thisObject->impl()->visitJSEventListeners(visitor);\n");
2230             }
2231             if ($numCachedAttributes > 0) {
2232                 foreach (@{$dataNode->attributes}) {
2233                     my $attribute = $_;
2234                     if ($attribute->signature->extendedAttributes->{"CachedAttribute"}) {
2235                         push(@implContent, "    if (thisObject->m_" . $attribute->signature->name . ")\n");
2236                         push(@implContent, "        visitor.append(&thisObject->m_" . $attribute->signature->name . ");\n");
2237                     }
2238                 }
2239             }
2240             push(@implContent, "}\n\n");
2241         }
2242         die "Can't generate binding for class with cached attribute and custom mark." if (($numCachedAttributes > 0) and ($dataNode->extendedAttributes->{"CustomMarkFunction"}));
2243     }
2244
2245     if ($numConstants > 0) {
2246         push(@implContent, "// Constant getters\n\n");
2247
2248         foreach my $constant (@{$dataNode->constants}) {
2249             my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($constant->name);
2250
2251             # FIXME: this casts into int to match our previous behavior which turned 0xFFFFFFFF in -1 for NodeFilter.SHOW_ALL
2252             push(@implContent, "JSValue ${getter}(ExecState* exec, JSValue, const Identifier&)\n");
2253             push(@implContent, "{\n");
2254             if ($constant->type eq "DOMString") {
2255                 push(@implContent, "    return jsStringOrNull(exec, String(" . $constant->value . "));\n");
2256             } else {
2257                 push(@implContent, "    UNUSED_PARAM(exec);\n");
2258                 push(@implContent, "    return jsNumber(static_cast<int>(" . $constant->value . "));\n");
2259             }
2260             push(@implContent, "}\n\n");
2261         }
2262     }
2263
2264     if ($dataNode->extendedAttributes->{"HasIndexGetter"}) {
2265         push(@implContent, "\nJSValue ${className}::indexGetter(ExecState* exec, JSValue slotBase, unsigned index)\n");
2266         push(@implContent, "{\n");
2267         push(@implContent, "    ${className}* thisObj = static_cast<$className*>(asObject(slotBase));\n");
2268         push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(thisObj, &s_info);\n");
2269         if (IndexGetterReturnsStrings($implClassName)) {
2270             $implIncludes{"KURL.h"} = 1;
2271             push(@implContent, "    return jsStringOrNull(exec, thisObj->impl()->item(index));\n");
2272         } else {
2273             push(@implContent, "    return toJS(exec, thisObj->globalObject(), static_cast<$implClassName*>(thisObj->impl())->item(index));\n");
2274         }
2275         push(@implContent, "}\n\n");
2276         if ($interfaceName eq "HTMLCollection" or $interfaceName eq "HTMLAllCollection") {
2277             $implIncludes{"JSNode.h"} = 1;
2278             $implIncludes{"Node.h"} = 1;
2279         }
2280     }
2281     
2282     if ($dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
2283         push(@implContent, "\nJSValue ${className}::getByIndex(ExecState*, unsigned index)\n");
2284         push(@implContent, "{\n");
2285         push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
2286         push(@implContent, "    double result = static_cast<$implClassName*>(impl())->item(index);\n");
2287         # jsNumber conversion doesn't suppress signalling NaNs, so enforce that here.
2288         push(@implContent, "    if (isnan(result))\n");
2289         push(@implContent, "        return jsNaN();\n");
2290         push(@implContent, "    return JSValue(result);\n");
2291         push(@implContent, "}\n\n");
2292         if ($interfaceName eq "HTMLCollection" or $interfaceName eq "HTMLAllCollection") {
2293             $implIncludes{"JSNode.h"} = 1;
2294             $implIncludes{"Node.h"} = 1;
2295         }
2296     }
2297
2298     if ($dataNode->extendedAttributes->{"GenerateIsReachable"} || $dataNode->extendedAttributes->{"ActiveDOMObject"}) {
2299         push(@implContent, "static inline bool isObservable(JS${implType}* js${implType})\n");
2300         push(@implContent, "{\n");
2301         push(@implContent, "    if (js${implType}->hasCustomProperties())\n");
2302         push(@implContent, "        return true;\n");
2303         if ($eventTarget) {
2304             push(@implContent, "    if (js${implType}->impl()->hasEventListeners())\n");
2305             push(@implContent, "        return true;\n");
2306         }
2307         push(@implContent, "    return false;\n");
2308         push(@implContent, "}\n\n");
2309
2310         push(@implContent, "bool JS${implType}Owner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)\n");
2311         push(@implContent, "{\n");
2312         push(@implContent, "    JS${implType}* js${implType} = static_cast<JS${implType}*>(handle.get().asCell());\n");
2313         # All ActiveDOMObjects implement hasPendingActivity(), but not all of them
2314         # increment their C++ reference counts when hasPendingActivity() becomes
2315         # true. As a result, ActiveDOMObjects can be prematurely destroyed before
2316         # their pending activities complete. To wallpaper over this bug, JavaScript
2317         # wrappers unconditionally keep ActiveDOMObjects with pending activity alive.
2318         # FIXME: Fix this lifetime issue in the DOM, and move this hasPendingActivity
2319         # check below the isObservable check.
2320         if ($dataNode->extendedAttributes->{"ActiveDOMObject"}) {
2321             push(@implContent, "    if (js${implType}->impl()->hasPendingActivity())\n");
2322             push(@implContent, "        return true;\n");
2323         }
2324         push(@implContent, "    if (!isObservable(js${implType}))\n");
2325         push(@implContent, "        return false;\n");
2326         if ($dataNode->extendedAttributes->{"GenerateIsReachable"}) {
2327             my $rootString;
2328             if ($dataNode->extendedAttributes->{"GenerateIsReachable"} eq "Impl") {
2329                 $rootString  = "    ${implType}* root = js${implType}->impl();\n";
2330             } elsif ($dataNode->extendedAttributes->{"GenerateIsReachable"} eq "ImplContext") {
2331                 $rootString  = "    WebGLRenderingContext* root = js${implType}->impl()->context();\n";
2332             } elsif ($dataNode->extendedAttributes->{"GenerateIsReachable"} eq "ImplFrame") {
2333                 $rootString  = "    Frame* root = js${implType}->impl()->frame();\n";
2334                 $rootString .= "    if (!root)\n";
2335                 $rootString .= "        return false;\n";
2336             } elsif ($dataNode->extendedAttributes->{"GenerateIsReachable"} eq "ImplDocument") {
2337                 $rootString  = "    Document* root = js${implType}->impl()->document();\n";
2338                 $rootString .= "    if (!root)\n";
2339                 $rootString .= "        return false;\n";
2340             } elsif ($dataNode->extendedAttributes->{"GenerateIsReachable"} eq "ImplElementRoot") {
2341                 $rootString  = "    Element* element = js${implType}->impl()->element();\n";
2342                 $rootString .= "    if (!element)\n";
2343                 $rootString .= "        return false;\n";
2344                 $rootString .= "    void* root = WebCore::root(element);\n";
2345             } elsif ($interfaceName eq "CanvasRenderingContext") {
2346                 $rootString  = "    void* root = WebCore::root(js${implType}->impl()->canvas());\n";
2347             } elsif ($interfaceName eq "HTMLCollection") {
2348                 $rootString  = "    void* root = WebCore::root(js${implType}->impl()->base());\n";
2349             } else {
2350                 $rootString  = "    void* root = WebCore::root(js${implType}->impl());\n";
2351             }
2352
2353             push(@implContent, $rootString);
2354             push(@implContent, "    return visitor.containsOpaqueRoot(root);\n");
2355         } else {
2356             push(@implContent, "    UNUSED_PARAM(visitor);\n");
2357             push(@implContent, "    return false;\n");
2358         }
2359         push(@implContent, "}\n\n");
2360     }
2361
2362     if (!$dataNode->extendedAttributes->{"CustomFinalize"} &&
2363         ($dataNode->extendedAttributes->{"GenerateIsReachable"} || 
2364          $dataNode->extendedAttributes->{"CustomIsReachable"} ||
2365          $dataNode->extendedAttributes->{"ActiveDOMObject"})) {
2366         push(@implContent, "void JS${implType}Owner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)\n");
2367         push(@implContent, "{\n");
2368         push(@implContent, "    JS${implType}* js${implType} = static_cast<JS${implType}*>(handle.get().asCell());\n");
2369         push(@implContent, "    DOMWrapperWorld* world = static_cast<DOMWrapperWorld*>(context);\n");
2370         push(@implContent, "    uncacheWrapper(world, js${implType}->impl(), js${implType});\n");
2371         push(@implContent, "}\n\n");
2372     }
2373
2374     if ((!$hasParent or $dataNode->extendedAttributes->{"GenerateToJS"}) and !$dataNode->extendedAttributes->{"CustomToJS"}) {
2375         push(@implContent, "JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, $implType* impl)\n");
2376         push(@implContent, "{\n");
2377         if ($svgPropertyType) {
2378             push(@implContent, "    return wrap<$className, $implType>(exec, globalObject, impl);\n");
2379         } else {
2380             push(@implContent, "    return wrap<$className>(exec, globalObject, impl);\n");
2381         }
2382         push(@implContent, "}\n\n");
2383     }
2384
2385     if ((!$hasParent or $dataNode->extendedAttributes->{"GenerateNativeConverter"}) and !$dataNode->extendedAttributes->{"CustomNativeConverter"}) {
2386         push(@implContent, "$implType* to${interfaceName}(JSC::JSValue value)\n");
2387         push(@implContent, "{\n");
2388         push(@implContent, "    return value.inherits(&${className}::s_info) ? static_cast<$className*>(asObject(value))->impl() : 0");
2389         push(@implContent, ";\n}\n");
2390     }
2391
2392     push(@implContent, "\n}\n");
2393
2394     my $conditionalString = GenerateConditionalString($dataNode);
2395     push(@implContent, "\n#endif // ${conditionalString}\n") if $conditionalString;
2396 }
2397
2398 sub GenerateCallbackHeader
2399 {
2400     my $object = shift;
2401     my $dataNode = shift;
2402
2403     my $interfaceName = $dataNode->name;
2404     my $className = "JS$interfaceName";
2405
2406     # - Add default header template and header protection
2407     push(@headerContentHeader, GenerateHeaderContentHeader($dataNode));
2408
2409     $headerIncludes{"ActiveDOMCallback.h"} = 1;
2410     $headerIncludes{"$interfaceName.h"} = 1;
2411     $headerIncludes{"JSCallbackData.h"} = 1;
2412     $headerIncludes{"<wtf/Forward.h>"} = 1;
2413
2414     push(@headerContent, "\nnamespace WebCore {\n\n");
2415     push(@headerContent, "class $className : public $interfaceName, public ActiveDOMCallback {\n");
2416     push(@headerContent, "public:\n");
2417
2418     # The static create() method.
2419     push(@headerContent, "    static PassRefPtr<$className> create(JSC::JSObject* callback, JSDOMGlobalObject* globalObject)\n");
2420     push(@headerContent, "    {\n");
2421     push(@headerContent, "        return adoptRef(new $className(callback, globalObject));\n");
2422     push(@headerContent, "    }\n\n");
2423
2424     # Destructor
2425     push(@headerContent, "    virtual ~$className();\n");
2426
2427     # Functions
2428     my $numFunctions = @{$dataNode->functions};
2429     if ($numFunctions > 0) {
2430         push(@headerContent, "\n    // Functions\n");
2431         foreach my $function (@{$dataNode->functions}) {
2432             my @params = @{$function->parameters};
2433             if (!$function->signature->extendedAttributes->{"Custom"} &&
2434                 !(GetNativeType($function->signature->type) eq "bool")) {
2435                 push(@headerContent, "    COMPILE_ASSERT(false)");
2436             }
2437
2438             push(@headerContent, "    virtual " . GetNativeType($function->signature->type) . " " . $function->signature->name . "(");
2439
2440             my @args = ();
2441             foreach my $param (@params) {
2442                 push(@args, GetNativeType($param->type) . " " . $param->name);
2443             }
2444             push(@headerContent, join(", ", @args));
2445
2446             push(@headerContent, ");\n");
2447         }
2448     }
2449
2450     push(@headerContent, "\nprivate:\n");
2451
2452     # Constructor
2453     push(@headerContent, "    $className(JSC::JSObject* callback, JSDOMGlobalObject*);\n\n");
2454
2455     # Private members
2456     push(@headerContent, "    JSCallbackData* m_data;\n");
2457     push(@headerContent, "};\n\n");
2458
2459     push(@headerContent, "} // namespace WebCore\n\n");
2460     my $conditionalString = GenerateConditionalString($dataNode);
2461     push(@headerContent, "#endif // ${conditionalString}\n\n") if $conditionalString;
2462     push(@headerContent, "#endif\n");
2463 }
2464
2465 sub GenerateCallbackImplementation
2466 {
2467     my ($object, $dataNode) = @_;
2468
2469     my $interfaceName = $dataNode->name;
2470     my $className = "JS$interfaceName";
2471
2472     # - Add default header template
2473     push(@implContentHeader, GenerateImplementationContentHeader($dataNode));
2474
2475     $implIncludes{"ScriptExecutionContext.h"} = 1;
2476     $implIncludes{"<runtime/JSLock.h>"} = 1;
2477
2478     @implContent = ();
2479
2480     push(@implContent, "\nusing namespace JSC;\n\n");
2481     push(@implContent, "namespace WebCore {\n\n");
2482
2483     # Constructor
2484     push(@implContent, "${className}::${className}(JSObject* callback, JSDOMGlobalObject* globalObject)\n");
2485     push(@implContent, "    : ActiveDOMCallback(globalObject->scriptExecutionContext())\n");
2486     push(@implContent, "    , m_data(new JSCallbackData(callback, globalObject))\n");
2487     push(@implContent, "{\n");
2488     push(@implContent, "}\n\n");
2489
2490     # Destructor
2491     push(@implContent, "${className}::~${className}()\n");
2492     push(@implContent, "{\n");
2493     push(@implContent, "    ScriptExecutionContext* context = scriptExecutionContext();\n");
2494     push(@implContent, "    // When the context is destroyed, all tasks with a reference to a callback\n");
2495     push(@implContent, "    // should be deleted. So if the context is 0, we are on the context thread.\n");
2496     push(@implContent, "    if (!context || context->isContextThread())\n");
2497     push(@implContent, "        delete m_data;\n");
2498     push(@implContent, "    else\n");
2499     push(@implContent, "        context->postTask(DeleteCallbackDataTask::create(m_data));\n");
2500     push(@implContent, "#ifndef NDEBUG\n");
2501     push(@implContent, "    m_data = 0;\n");
2502     push(@implContent, "#endif\n");
2503     push(@implContent, "}\n");
2504
2505     # Functions
2506     my $numFunctions = @{$dataNode->functions};
2507     if ($numFunctions > 0) {
2508         push(@implContent, "\n// Functions\n");
2509         foreach my $function (@{$dataNode->functions}) {
2510             my @params = @{$function->parameters};
2511             if ($function->signature->extendedAttributes->{"Custom"} ||
2512                 !(GetNativeType($function->signature->type) eq "bool")) {
2513                 next;
2514             }
2515
2516             AddIncludesForTypeInImpl($function->signature->type);
2517             push(@implContent, "\n" . GetNativeType($function->signature->type) . " ${className}::" . $function->signature->name . "(");
2518
2519             my @args = ();
2520             foreach my $param (@params) {
2521                 AddIncludesForTypeInImpl($param->type, 1);
2522                 push(@args, GetNativeType($param->type) . " " . $param->name);
2523             }
2524             push(@implContent, join(", ", @args));
2525             push(@implContent, ")\n");
2526
2527             push(@implContent, "{\n");
2528             push(@implContent, "    if (!canInvokeCallback())\n");
2529             push(@implContent, "        return true;\n\n");
2530             push(@implContent, "    RefPtr<$className> protect(this);\n\n");
2531             push(@implContent, "    JSLock lock(SilenceAssertionsOnly);\n\n");
2532             push(@implContent, "    ExecState* exec = m_data->globalObject()->globalExec();\n");
2533             push(@implContent, "    MarkedArgumentBuffer args;\n");
2534
2535             foreach my $param (@params) {
2536                 my $paramName = $param->name;
2537                 if ($param->type eq "DOMString") {
2538                     push(@implContent, "    args.append(jsString(exec, ${paramName}));\n");
2539                 } else {
2540                     push(@implContent, "    args.append(toJS(exec, m_data->globalObject(), ${paramName}));\n");
2541                 }
2542             }
2543
2544             push(@implContent, "\n    bool raisedException = false;\n");
2545             push(@implContent, "    m_data->invokeCallback(args, &raisedException);\n");
2546             push(@implContent, "    return !raisedException;\n");
2547             push(@implContent, "}\n");
2548         }
2549     }
2550
2551     push(@implContent, "\n}\n");
2552     my $conditionalString = GenerateConditionalString($dataNode);
2553     push(@implContent, "\n#endif // ${conditionalString}\n") if $conditionalString;
2554 }
2555
2556 sub GenerateImplementationFunctionCall()
2557 {
2558     my $function = shift;
2559     my $functionString = shift;
2560     my $paramIndex = shift;
2561     my $indent = shift;
2562     my $svgPropertyType = shift;
2563     my $implClassName = shift;
2564
2565     if ($function->signature->extendedAttributes->{"CustomArgumentHandling"}) {
2566         $functionString .= ", " if $paramIndex;
2567         $paramIndex += 2;
2568         $functionString .= "scriptArguments, callStack";
2569     }
2570
2571     if (@{$function->raisesExceptions}) {
2572         $functionString .= ", " if $paramIndex;
2573         $functionString .= "ec";
2574     }
2575     $functionString .= ")";
2576
2577     if ($function->signature->type eq "void") {
2578         push(@implContent, $indent . "$functionString;\n");
2579         push(@implContent, $indent . "setDOMException(exec, ec);\n") if @{$function->raisesExceptions};
2580
2581         if ($svgPropertyType) {
2582             if (@{$function->raisesExceptions}) {
2583                 push(@implContent, $indent . "if (!ec)\n"); 
2584                 push(@implContent, $indent . "    imp->commitChange();\n");
2585             } else {
2586                 push(@implContent, $indent . "imp->commitChange();\n");
2587             }
2588         }
2589
2590         push(@implContent, $indent . "return JSValue::encode(jsUndefined());\n");
2591     } else {
2592         push(@implContent, "\n" . $indent . "JSC::JSValue result = " . NativeToJSValue($function->signature, 1, $implClassName, $functionString, "castedThis") . ";\n");
2593         push(@implContent, $indent . "setDOMException(exec, ec);\n") if @{$function->raisesExceptions};
2594
2595         my $callWith = $function->signature->extendedAttributes->{"CallWith"};
2596         if ($callWith and $callWith eq "ScriptState") {
2597             push(@implContent, $indent . "if (exec->hadException())\n");
2598             push(@implContent, $indent . "    return JSValue::encode(jsUndefined());\n");
2599         }
2600
2601         push(@implContent, $indent . "return JSValue::encode(result);\n");
2602     }
2603 }
2604
2605 sub GetNativeTypeFromSignature
2606 {
2607     my $signature = shift;
2608     my $type = $codeGenerator->StripModule($signature->type);
2609
2610     if ($type eq "unsigned long" and $signature->extendedAttributes->{"IsIndex"}) {
2611         # Special-case index arguments because we need to check that they aren't < 0.
2612         return "int";
2613     }
2614
2615     return GetNativeType($type);
2616 }
2617
2618 my %nativeType = (
2619     "CompareHow" => "Range::CompareHow",
2620     "DOMString" => "const String&",
2621     "DOMObject" => "ScriptValue",
2622     "NodeFilter" => "RefPtr<NodeFilter>",
2623     "SerializedScriptValue" => "RefPtr<SerializedScriptValue>",
2624     "IDBKey" => "RefPtr<IDBKey>",
2625     "boolean" => "bool",
2626     "double" => "double",
2627     "float" => "float",
2628     "short" => "short",
2629     "long" => "int",
2630     "unsigned long" => "unsigned",
2631     "unsigned short" => "unsigned short",
2632     "long long" => "long long",
2633     "unsigned long long" => "unsigned long long",
2634     "MediaQueryListListener" => "RefPtr<MediaQueryListListener>",
2635     "DOMTimeStamp" => "DOMTimeStamp"
2636 );
2637
2638 sub GetNativeType
2639 {
2640     my $type = shift;
2641
2642     my $svgNativeType = $codeGenerator->GetSVGTypeNeedingTearOff($type);
2643     return "${svgNativeType}*" if $svgNativeType;
2644     return $nativeType{$type} if exists $nativeType{$type};
2645
2646     # For all other types, the native type is a pointer with same type name as the IDL type.
2647     return "${type}*";
2648 }
2649
2650 sub GetSVGPropertyTypes
2651 {
2652     my $implType = shift;
2653
2654     my $svgPropertyType;
2655     my $svgListPropertyType;
2656     my $svgNativeType;
2657
2658     return ($svgPropertyType, $svgListPropertyType, $svgNativeType) if not $implType =~ /SVG/;
2659     
2660     $svgNativeType = $codeGenerator->GetSVGTypeNeedingTearOff($implType);
2661     return ($svgPropertyType, $svgListPropertyType, $svgNativeType) if not $svgNativeType;
2662
2663     # Append space to avoid compilation errors when using  PassRefPtr<$svgNativeType>
2664     $svgNativeType = "$svgNativeType ";
2665
2666     my $svgWrappedNativeType = $codeGenerator->GetSVGWrappedTypeNeedingTearOff($implType);
2667     if ($svgNativeType =~ /SVGPropertyTearOff/) {
2668         $svgPropertyType = $svgWrappedNativeType;
2669         $headerIncludes{"$svgWrappedNativeType.h"} = 1;
2670         $headerIncludes{"SVGAnimatedPropertyTearOff.h"} = 1;
2671     } elsif ($svgNativeType =~ /SVGListPropertyTearOff/ or $svgNativeType =~ /SVGStaticListPropertyTearOff/) {
2672         $svgListPropertyType = $svgWrappedNativeType;
2673         $headerIncludes{"$svgWrappedNativeType.h"} = 1;
2674         $headerIncludes{"SVGAnimatedListPropertyTearOff.h"} = 1;
2675     } elsif ($svgNativeType =~ /SVGTransformListPropertyTearOff/) {
2676         $svgListPropertyType = $svgWrappedNativeType;
2677         $headerIncludes{"$svgWrappedNativeType.h"} = 1;
2678         $headerIncludes{"SVGAnimatedListPropertyTearOff.h"} = 1;
2679         $headerIncludes{"SVGTransformListPropertyTearOff.h"} = 1;
2680     } elsif ($svgNativeType =~ /SVGPathSegListPropertyTearOff/) {
2681         $svgListPropertyType = $svgWrappedNativeType;
2682         $headerIncludes{"$svgWrappedNativeType.h"} = 1;
2683         $headerIncludes{"SVGAnimatedListPropertyTearOff.h"} = 1;
2684         $headerIncludes{"SVGPathSegListPropertyTearOff.h"} = 1;
2685     }
2686
2687     return ($svgPropertyType, $svgListPropertyType, $svgNativeType);
2688 }
2689
2690 sub IsNativeType
2691 {
2692     my $type = shift;
2693     return exists $nativeType{$type};
2694 }
2695
2696 sub JSValueToNative
2697 {
2698     my $signature = shift;
2699     my $value = shift;
2700
2701     my $conditional = $signature->extendedAttributes->{"Conditional"};
2702     my $type = $codeGenerator->StripModule($signature->type);
2703
2704     return "$value.toBoolean(exec)" if $type eq "boolean";
2705     return "$value.toNumber(exec)" if $type eq "double";
2706     return "$value.toFloat(exec)" if $type eq "float";
2707     return "$value.toInt32(exec)" if $type eq "long" or $type eq "short";
2708     return "$value.toUInt32(exec)" if $type eq "unsigned long" or $type eq "unsigned short";
2709     return "static_cast<$type>($value.toInteger(exec))" if $type eq "long long" or $type eq "unsigned long long";
2710
2711     return "valueToDate(exec, $value)" if $type eq "Date";
2712     return "static_cast<Range::CompareHow>($value.toInt32(exec))" if $type eq "CompareHow";
2713
2714     if ($type eq "DOMString") {
2715         return "valueToStringWithNullCheck(exec, $value)" if $signature->extendedAttributes->{"ConvertNullToNullString"} || $signature->extendedAttributes->{"Reflect"};
2716         return "valueToStringWithUndefinedOrNullCheck(exec, $value)" if $signature->extendedAttributes->{"ConvertUndefinedOrNullToNullString"};
2717         return "ustringToString($value.toString(exec))";
2718     }
2719
2720     if ($type eq "DOMObject") {
2721         return "exec->globalData(), $value";
2722     }
2723
2724     if ($type eq "NodeFilter") {
2725         AddToImplIncludes("JS$type.h", $conditional);
2726         return "to$type(exec->globalData(), $value)";
2727     }
2728
2729     if ($type eq "MediaQueryListListener") {
2730         AddToImplIncludes("MediaQueryListListener.h", $conditional);
2731         return "MediaQueryListListener::create(ScriptValue(exec->globalData(), " . $value ."))";
2732     }
2733
2734     if ($type eq "SerializedScriptValue" or $type eq "any") {
2735         AddToImplIncludes("SerializedScriptValue.h", $conditional);
2736         return "SerializedScriptValue::create(exec, $value)";
2737     }
2738
2739     if ($type eq "IDBKey") {
2740         AddToImplIncludes("IDBBindingUtilities.h", $conditional);
2741         AddToImplIncludes("IDBKey.h", $conditional);
2742         return "createIDBKeyFromValue(exec, $value)";
2743     }
2744
2745     AddToImplIncludes("HTMLOptionElement.h", $conditional) if $type eq "HTMLOptionElement";
2746     AddToImplIncludes("JSCustomVoidCallback.h", $conditional) if $type eq "VoidCallback";
2747     AddToImplIncludes("Event.h", $conditional) if $type eq "Event";
2748
2749     # Default, assume autogenerated type conversion routines
2750     AddToImplIncludes("JS$type.h", $conditional);
2751     return "to$type($value)";
2752 }
2753
2754 sub NativeToJSValue
2755 {
2756     my $signature = shift;
2757     my $inFunctionCall = shift;
2758     my $implClassName = shift;
2759     my $value = shift;
2760     my $thisValue = shift;
2761
2762     my $conditional = $signature->extendedAttributes->{"Conditional"};
2763     my $type = $codeGenerator->StripModule($signature->type);
2764
2765     return "jsBoolean($value)" if $type eq "boolean";
2766
2767     # Need to check Date type before IsPrimitiveType().
2768     if ($type eq "Date") {
2769         return "jsDateOrNull(exec, $value)";
2770     }
2771
2772     if ($signature->extendedAttributes->{"Reflect"} and ($type eq "unsigned long" or $type eq "unsigned short")) {
2773         $value =~ s/getUnsignedIntegralAttribute/getIntegralAttribute/g;
2774         return "jsNumber(std::max(0, " . $value . "))";
2775     }
2776
2777     if ($codeGenerator->IsPrimitiveType($type) or $type eq "DOMTimeStamp") {
2778         return "jsNumber($value)";
2779     }
2780
2781     if ($codeGenerator->IsStringType($type)) {
2782         AddToImplIncludes("KURL.h", $conditional);
2783         my $conv = $signature->extendedAttributes->{"ConvertNullStringTo"};
2784         if (defined $conv) {
2785             return "jsStringOrNull(exec, $value)" if $conv eq "Null";
2786             return "jsStringOrUndefined(exec, $value)" if $conv eq "Undefined";
2787             return "jsStringOrFalse(exec, $value)" if $conv eq "False";
2788
2789             die "Unknown value for ConvertNullStringTo extended attribute";
2790         }
2791         $conv = $signature->extendedAttributes->{"ConvertScriptString"};
2792         return "jsOwnedStringOrNull(exec, $value)" if $conv;
2793         AddToImplIncludes("<runtime/JSString.h>", $conditional);
2794         return "jsString(exec, $value)";
2795     }
2796     
2797     my $globalObject = "$thisValue->globalObject()";
2798
2799     if ($type eq "CSSStyleDeclaration") {
2800         AddToImplIncludes("CSSMutableStyleDeclaration.h", $conditional);
2801     }
2802
2803     if ($type eq "NodeList") {
2804         AddToImplIncludes("NameNodeList.h", $conditional);
2805     }
2806
2807     if ($type eq "DOMObject") {
2808         if ($implClassName eq "Document") {
2809             AddToImplIncludes("JSCanvasRenderingContext2D.h", $conditional);
2810         } else {
2811             return "($value.hasNoValue() ? jsNull() : $value.jsValue())";
2812         }
2813     } elsif ($type =~ /SVGPathSeg/) {
2814         AddToImplIncludes("JS$type.h", $conditional);
2815         my $joinedName = $type;
2816         $joinedName =~ s/Abs|Rel//;
2817         AddToImplIncludes("$joinedName.h", $conditional);
2818     } elsif ($type eq "SerializedScriptValue" or $type eq "any") {
2819         AddToImplIncludes("SerializedScriptValue.h", $conditional);
2820         return "$value ? $value->deserialize(exec, castedThis->globalObject()) : jsNull()";
2821     } else {
2822         # Default, include header with same name.
2823         AddToImplIncludes("JS$type.h", $conditional);
2824         AddToImplIncludes("$type.h", $conditional) if not $codeGenerator->AvoidInclusionOfType($type);
2825     }
2826
2827     return $value if $codeGenerator->IsSVGAnimatedType($type);
2828
2829     if ($signature->extendedAttributes->{"ReturnsNew"}) {        
2830         return "toJSNewlyCreated(exec, $globalObject, WTF::getPtr($value))";
2831     }
2832
2833     if ($codeGenerator->IsSVGAnimatedType($implClassName)) {
2834         # Convert from abstract SVGProperty to real type, so the right toJS() method can be invoked.
2835         $value = "static_cast<" . GetNativeType($type) . ">($value)";
2836     } elsif ($codeGenerator->IsSVGTypeNeedingTearOff($type) and not $implClassName =~ /List$/) {
2837         my $tearOffType = $codeGenerator->GetSVGTypeNeedingTearOff($type);
2838         if ($codeGenerator->IsSVGTypeWithWritablePropertiesNeedingTearOff($type) and $inFunctionCall eq 0 and not defined $signature->extendedAttributes->{"Immutable"}) {
2839             my $getter = $value;
2840             $getter =~ s/imp\.//;
2841             $getter =~ s/imp->//;
2842             $getter =~ s/\(\)//;
2843             my $updateMethod = "&${implClassName}::update" . $codeGenerator->WK_ucfirst($getter);
2844
2845             my $selfIsTearOffType = $codeGenerator->IsSVGTypeNeedingTearOff($implClassName);
2846             if ($selfIsTearOffType) {
2847                 AddToImplIncludes("SVGStaticPropertyWithParentTearOff.h", $conditional);
2848                 $tearOffType =~ s/SVGPropertyTearOff</SVGStaticPropertyWithParentTearOff<$implClassName, /;
2849
2850                 if ($value =~ /matrix/ and $implClassName eq "SVGTransform") {
2851                     # SVGTransform offers a matrix() method for internal usage that returns an AffineTransform
2852                     # and a svgMatrix() method returning a SVGMatrix, used for the bindings.
2853                     $value =~ s/matrix/svgMatrix/;
2854                 }
2855
2856                 $value = "${tearOffType}::create(castedThis->impl(), $value, $updateMethod)";
2857             } else {
2858                 AddToImplIncludes("SVGStaticPropertyTearOff.h", $conditional);
2859                 $tearOffType =~ s/SVGPropertyTearOff</SVGStaticPropertyTearOff<$implClassName, /;
2860                 $value = "${tearOffType}::create(imp, $value, $updateMethod)";
2861             }
2862         } elsif ($tearOffType =~ /SVGStaticListPropertyTearOff/) {
2863             $value = "${tearOffType}::create(imp, $value)";
2864         } elsif (not $tearOffType =~ /SVG(Point|PathSeg)List/) {
2865             $value = "${tearOffType}::create($value)";
2866         }
2867     }
2868
2869     return "toJS(exec, $globalObject, WTF::getPtr($value))";
2870 }
2871
2872 sub ceilingToPowerOf2
2873 {
2874     my ($size) = @_;
2875
2876     my $powerOf2 = 1;
2877     while ($size > $powerOf2) {
2878         $powerOf2 <<= 1;
2879     }
2880
2881     return $powerOf2;
2882 }
2883
2884 # Internal Helper
2885 sub GenerateHashTable
2886 {
2887     my $object = shift;
2888
2889     my $name = shift;
2890     my $size = shift;
2891     my $keys = shift;
2892     my $specials = shift;
2893     my $value1 = shift;
2894     my $value2 = shift;
2895     my $conditionals = shift;
2896
2897     # Generate size data for compact' size hash table
2898
2899     my @table = ();
2900     my @links = ();
2901
2902     my $compactSize = ceilingToPowerOf2($size * 2);
2903
2904     my $maxDepth = 0;
2905     my $collisions = 0;
2906     my $numEntries = $compactSize;
2907
2908     my $i = 0;
2909     foreach (@{$keys}) {
2910         my $depth = 0;
2911         my $h = $object->GenerateHashValue($_) % $numEntries;
2912
2913         while (defined($table[$h])) {
2914             if (defined($links[$h])) {
2915                 $h = $links[$h];
2916                 $depth++;
2917             } else {
2918                 $collisions++;
2919                 $links[$h] = $compactSize;
2920                 $h = $compactSize;
2921                 $compactSize++;
2922             }
2923         }
2924
2925         $table[$h] = $i;
2926
2927         $i++;
2928         $maxDepth = $depth if ($depth > $maxDepth);
2929     }
2930
2931     # Start outputing the hashtables
2932     my $nameEntries = "${name}Values";
2933     $nameEntries =~ s/:/_/g;
2934
2935     if (($name =~ /Prototype/) or ($name =~ /Constructor/)) {
2936         my $type = $name;
2937         my $implClass;
2938
2939         if ($name =~ /Prototype/) {
2940             $type =~ s/Prototype.*//;
2941             $implClass = $type; $implClass =~ s/Wrapper$//;
2942             push(@implContent, "/* Hash table for prototype */\n");
2943         } else {
2944             $type =~ s/Constructor.*//;
2945             $implClass = $type; $implClass =~ s/Constructor$//;
2946             push(@implContent, "/* Hash table for constructor */\n");
2947         }
2948     } else {
2949         push(@implContent, "/* Hash table */\n");
2950     }
2951
2952     # Dump the hash table
2953     push(@implContent, "#if ENABLE(JIT)\n");
2954     push(@implContent, "#define THUNK_GENERATOR(generator) , generator\n");
2955     push(@implContent, "#else\n");
2956     push(@implContent, "#define THUNK_GENERATOR(generator)\n");
2957     push(@implContent, "#endif\n");
2958     push(@implContent, "#if ENABLE(DFG_JIT)\n");
2959     push(@implContent, "#define INTRINSIC(intrinsic) , intrinsic\n");
2960     push(@implContent, "#else\n");
2961     push(@implContent, "#define INTRINSIC(intrinsic)\n");
2962     push(@implContent, "#endif\n");
2963     push(@implContent, "\nstatic const HashTableValue $nameEntries\[\] =\n\{\n");
2964     $i = 0;
2965     foreach my $key (@{$keys}) {
2966         my $conditional;
2967         my $targetType;
2968
2969         if ($conditionals) {
2970             $conditional = $conditionals->{$key};
2971         }
2972         if ($conditional) {
2973             my $conditionalString = GenerateConditionalStringFromAttributeValue($conditional);
2974             push(@implContent, "#if ${conditionalString}\n");
2975         }
2976         
2977         if ("@$specials[$i]" =~ m/Function/) {
2978             $targetType = "static_cast<NativeFunction>";
2979         } else {
2980             $targetType = "static_cast<PropertySlot::GetValueFunc>";
2981         }
2982         push(@implContent, "    { \"$key\", @$specials[$i], (intptr_t)" . $targetType . "(@$value1[$i]), (intptr_t)@$value2[$i] THUNK_GENERATOR(0) INTRINSIC(DFG::NoIntrinsic) },\n");
2983         if ($conditional) {
2984             push(@implContent, "#endif\n");
2985         }
2986         ++$i;
2987     }
2988     push(@implContent, "    { 0, 0, 0, 0 THUNK_GENERATOR(0) INTRINSIC(DFG::NoIntrinsic) }\n");
2989     push(@implContent, "};\n\n");
2990     push(@implContent, "#undef THUNK_GENERATOR\n");
2991     my $compactSizeMask = $numEntries - 1;
2992     push(@implContent, "static JSC_CONST_HASHTABLE HashTable $name = { $compactSize, $compactSizeMask, $nameEntries, 0 };\n");
2993 }
2994
2995 # Internal helper
2996 sub GenerateHashValue
2997 {
2998     my $object = shift;
2999
3000     my @chars = split(/ */, $_[0]);
3001
3002     # This hash is designed to work on 16-bit chunks at a time. But since the normal case
3003     # (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
3004     # were 16-bit chunks, which should give matching results
3005
3006     my $EXP2_32 = 4294967296;
3007
3008     my $hash = 0x9e3779b9;
3009     my $l    = scalar @chars; #I wish this was in Ruby --- Maks
3010     my $rem  = $l & 1;
3011     $l = $l >> 1;
3012
3013     my $s = 0;
3014
3015     # Main loop
3016     for (; $l > 0; $l--) {
3017         $hash   += ord($chars[$s]);
3018         my $tmp = leftShift(ord($chars[$s+1]), 11) ^ $hash;
3019         $hash   = (leftShift($hash, 16)% $EXP2_32) ^ $tmp;
3020         $s += 2;
3021         $hash += $hash >> 11;
3022         $hash %= $EXP2_32;
3023     }
3024
3025     # Handle end case
3026     if ($rem != 0) {
3027         $hash += ord($chars[$s]);
3028         $hash ^= (leftShift($hash, 11)% $EXP2_32);
3029         $hash += $hash >> 17;
3030     }
3031
3032     # Force "avalanching" of final 127 bits
3033     $hash ^= leftShift($hash, 3);
3034     $hash += ($hash >> 5);
3035     $hash = ($hash% $EXP2_32);
3036     $hash ^= (leftShift($hash, 2)% $EXP2_32);
3037     $hash += ($hash >> 15);
3038     $hash = $hash% $EXP2_32;
3039     $hash ^= (leftShift($hash, 10)% $EXP2_32);
3040
3041     # this avoids ever returning a hash code of 0, since that is used to
3042     # signal "hash not computed yet", using a value that is likely to be
3043     # effectively the same as 0 when the low bits are masked
3044     $hash = 0x80000000 if ($hash == 0);
3045
3046     return $hash;
3047 }
3048
3049 # Internal helper
3050 sub WriteData
3051 {
3052     if (defined($IMPL)) {
3053         # Write content to file.
3054         print $IMPL @implContentHeader;
3055
3056         my @includes = ();
3057         my %implIncludeConditions = ();
3058         foreach my $include (keys %implIncludes) {
3059             my $condition = $implIncludes{$include};
3060             my $checkType = $include;
3061             $checkType =~ s/\.h//;
3062             next if $codeGenerator->IsSVGAnimatedType($checkType);
3063
3064             $include = "\"$include\"" unless $include =~ /^["<]/; # "
3065
3066             if ($condition eq 1) {
3067                 push @includes, $include;
3068             } else {
3069                 push @{$implIncludeConditions{$condition}}, $include;
3070             }
3071         }
3072         foreach my $include (sort @includes) {
3073             print $IMPL "#include $include\n";
3074         }
3075         foreach my $condition (sort keys %implIncludeConditions) {
3076             print $IMPL "\n#if " . GenerateConditionalStringFromAttributeValue($condition) . "\n";
3077             foreach my $include (sort @{$implIncludeConditions{$condition}}) {
3078                 print $IMPL "#include $include\n";
3079             }
3080             print $IMPL "#endif\n";
3081         }
3082
3083         print $IMPL @implContent;
3084         close($IMPL);
3085         undef($IMPL);
3086
3087         @implContentHeader = ();
3088         @implContent = ();
3089         %implIncludes = ();
3090     }
3091
3092     if (defined($HEADER)) {
3093         # Write content to file.
3094         print $HEADER @headerContentHeader;
3095
3096         my @includes = ();
3097         foreach my $include (keys %headerIncludes) {
3098             $include = "\"$include\"" unless $include =~ /^["<]/; # "
3099             push @includes, $include;
3100         }
3101         foreach my $include (sort @includes) {
3102             print $HEADER "#include $include\n";
3103         }
3104
3105         print $HEADER @headerContent;
3106
3107         @includes = ();
3108         foreach my $include (keys %headerTrailingIncludes) {
3109             $include = "\"$include\"" unless $include =~ /^["<]/; # "
3110             push @includes, $include;
3111         }
3112         foreach my $include (sort @includes) {
3113             print $HEADER "#include $include\n";
3114         }
3115
3116         close($HEADER);
3117         undef($HEADER);
3118
3119         @headerContentHeader = ();
3120         @headerContent = ();
3121         %headerIncludes = ();
3122         %headerTrailingIncludes = ();
3123     }
3124
3125     if (defined($DEPS)) {
3126         # Write dependency file.
3127         print $DEPS @depsContent;
3128         close($DEPS);
3129         undef($DEPS);
3130
3131         @depsContent = ();
3132     }
3133 }
3134
3135 sub GenerateConstructorDeclaration
3136 {
3137     my $outputArray = shift;
3138     my $className = shift;
3139     my $dataNode = shift;
3140
3141     my $constructorClassName = "${className}Constructor";
3142     my $canConstruct = $dataNode->extendedAttributes->{"CanBeConstructed"};
3143     my $callWith = $dataNode->extendedAttributes->{"CallWith"};
3144
3145     push(@$outputArray, "class ${constructorClassName} : public DOMConstructorObject {\n");
3146     push(@$outputArray, "private:\n");
3147     push(@$outputArray, "    ${constructorClassName}(JSC::Structure*, JSDOMGlobalObject*);\n");
3148     push(@$outputArray, "    void finishCreation(JSC::ExecState*, JSDOMGlobalObject*);\n\n");
3149
3150     push(@$outputArray, "public:\n");
3151     push(@$outputArray, "    typedef DOMConstructorObject Base;\n");
3152     push(@$outputArray, "    static $constructorClassName* create(JSC::ExecState* exec, JSC::Structure* structure, JSDOMGlobalObject* globalObject)\n");
3153     push(@$outputArray, "    {\n");
3154     push(@$outputArray, "        $constructorClassName* ptr = new (JSC::allocateCell<$constructorClassName>(*exec->heap())) $constructorClassName(structure, globalObject);\n");
3155     push(@$outputArray, "        ptr->finishCreation(exec, globalObject);\n");
3156     push(@$outputArray, "        return ptr;\n");
3157     push(@$outputArray, "    }\n\n");
3158
3159     push(@$outputArray, "    virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier&, JSC::PropertySlot&);\n");
3160     push(@$outputArray, "    virtual bool getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier&, JSC::PropertyDescriptor&);\n");
3161     push(@$outputArray, "    static const JSC::ClassInfo s_info;\n");
3162
3163     push(@$outputArray, "    static JSC::Structure* createStructure(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)\n");
3164     push(@$outputArray, "    {\n");
3165     push(@$outputArray, "        return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), &s_info);\n");
3166     push(@$outputArray, "    }\n");
3167
3168     push(@$outputArray, "protected:\n");
3169     push(@$outputArray, "    static const unsigned StructureFlags = JSC::OverridesGetOwnPropertySlot | JSC::ImplementsHasInstance | DOMConstructorObject::StructureFlags;\n");
3170
3171     if ($canConstruct) {
3172         push(@$outputArray, "    static JSC::EncodedJSValue JSC_HOST_CALL construct${className}(JSC::ExecState*);\n");
3173         push(@$outputArray, "    virtual JSC::ConstructType getConstructDataVirtual(JSC::ConstructData&);\n");
3174         push(@$outputArray, "    static JSC::ConstructType getConstructData(JSC::JSCell*, JSC::ConstructData&);\n");
3175     }
3176     push(@$outputArray, "};\n\n");
3177 }
3178
3179 sub GenerateConstructorDefinition
3180 {
3181     my $outputArray = shift;
3182
3183     my $className = shift;
3184     my $protoClassName = shift;
3185     my $interfaceName = shift;
3186     my $visibleClassName = shift;
3187     my $dataNode = shift;
3188
3189     my $constructorClassName = "${className}Constructor";
3190     my $canConstruct = $dataNode->extendedAttributes->{"CanBeConstructed"};
3191     my $customConstructor = $dataNode->extendedAttributes->{"JSCustomConstructor"} || $dataNode->extendedAttributes->{"CustomConstructor"};
3192     my $callWith = $dataNode->extendedAttributes->{"CallWith"};
3193     my $numberOfconstructParameters = $dataNode->extendedAttributes->{"ConstructorParameters"};
3194
3195     push(@$outputArray, "const ClassInfo ${constructorClassName}::s_info = { \"${visibleClassName}Constructor\", &DOMConstructorObject::s_info, &${constructorClassName}Table, 0, CREATE_METHOD_TABLE($constructorClassName) };\n\n");
3196
3197     push(@$outputArray, "${constructorClassName}::${constructorClassName}(Structure* structure, JSDOMGlobalObject* globalObject)\n");
3198     push(@$outputArray, "    : DOMConstructorObject(structure, globalObject)\n");
3199     push(@$outputArray, "{\n");
3200     push(@$outputArray, "}\n\n");
3201
3202     push(@$outputArray, "void ${constructorClassName}::finishCreation(ExecState* exec, JSDOMGlobalObject* globalObject)\n");
3203     push(@$outputArray, "{\n");
3204     push(@$outputArray, "    Base::finishCreation(exec->globalData());\n");
3205     push(@$outputArray, "    ASSERT(inherits(&s_info));\n");
3206     if ($interfaceName eq "DOMWindow") {
3207         push(@$outputArray, "    putDirect(exec->globalData(), exec->propertyNames().prototype, globalObject->prototype(), DontDelete | ReadOnly);\n");
3208     } else {
3209         push(@$outputArray, "    putDirect(exec->globalData(), exec->propertyNames().prototype, ${protoClassName}::self(exec, globalObject), DontDelete | ReadOnly);\n");
3210     }
3211     push(@$outputArray, "    putDirect(exec->globalData(), exec->propertyNames().length, jsNumber(${numberOfconstructParameters}), ReadOnly | DontDelete | DontEnum);\n") if $numberOfconstructParameters;
3212     push(@$outputArray, "}\n\n");
3213
3214     push(@$outputArray, "bool ${constructorClassName}::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\n");
3215     push(@$outputArray, "{\n");
3216     push(@$outputArray, "    return getStaticValueSlot<${constructorClassName}, JSDOMWrapper>(exec, &${constructorClassName}Table, this, propertyName, slot);\n");
3217     push(@$outputArray, "}\n\n");
3218
3219     push(@$outputArray, "bool ${constructorClassName}::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)\n");
3220     push(@$outputArray, "{\n");
3221     push(@$outputArray, "    return getStaticValueDescriptor<${constructorClassName}, JSDOMWrapper>(exec, &${constructorClassName}Table, this, propertyName, descriptor);\n");
3222     push(@$outputArray, "}\n\n");
3223
3224     if ($canConstruct) {
3225         if (!$customConstructor) {
3226             push(@$outputArray, "EncodedJSValue JSC_HOST_CALL ${constructorClassName}::construct${className}(ExecState* exec)\n");
3227             push(@$outputArray, "{\n");
3228             my $constructorArg = "";
3229             if ($callWith and $callWith eq "ScriptExecutionContext") {
3230                 $constructorArg = "context";
3231                 push(@$outputArray, "    ScriptExecutionContext* context = static_cast<${constructorClassName}*>(exec->callee())->scriptExecutionContext();\n");
3232                 push(@$outputArray, "    if (!context)\n");
3233                 push(@$outputArray, "        return throwVMError(exec, createReferenceError(exec, \"Reference error\"));\n");
3234             }
3235             push(@$outputArray, "    return JSValue::encode(asObject(toJS(exec, static_cast<${constructorClassName}*>(exec->callee())->globalObject(), ${interfaceName}::create(${constructorArg}))));\n");
3236             push(@$outputArray, "}\n\n");
3237         }
3238
3239         push(@$outputArray, "ConstructType ${constructorClassName}::getConstructDataVirtual(ConstructData& constructData)\n");
3240         push(@$outputArray, "{\n");
3241         push(@$outputArray, "    return getConstructData(this, constructData);\n");
3242         push(@$outputArray, "}\n\n");
3243
3244         push(@$outputArray, "ConstructType ${constructorClassName}::getConstructData(JSCell*, ConstructData& constructData)\n");
3245         push(@$outputArray, "{\n");
3246         push(@$outputArray, "    constructData.native.function = construct${className};\n");
3247         push(@$outputArray, "    return ConstructTypeHost;\n");
3248         push(@$outputArray, "}\n\n");
3249     }
3250 }
3251
3252 1;