Refactor IDL attributes about constructor
[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, ptr);\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, "    virtual void put(JSC::ExecState*, unsigned propertyName, JSC::JSValue);\n") if $dataNode->extendedAttributes->{"HasCustomIndexSetter"};
798         push(@headerContent, "    bool putDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::JSValue, JSC::PutPropertySlot&);\n") if $dataNode->extendedAttributes->{"DelegatingPutFunction"};
799     }
800
801     # Class info
802     push(@headerContent, "    static const JSC::ClassInfo s_info;\n\n");
803
804     # Structure ID
805     if ($interfaceName eq "DOMWindow") {
806         $structureFlags{"JSC::ImplementsHasInstance"} = 1;
807     }
808     push(@headerContent,
809         "    static JSC::Structure* createStructure(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)\n" .
810         "    {\n" .
811         "        return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), &s_info);\n" .
812         "    }\n\n");
813
814     # Custom pushEventHandlerScope function
815     push(@headerContent, "    virtual JSC::ScopeChainNode* pushEventHandlerScope(JSC::ExecState*, JSC::ScopeChainNode*) const;\n\n") if $dataNode->extendedAttributes->{"CustomPushEventHandlerScope"};
816
817     # Custom call functions
818     push(@headerContent, "    virtual JSC::CallType getCallDataVirtual(JSC::CallData&);\n\n") if $dataNode->extendedAttributes->{"CustomCall"};
819     push(@headerContent, "    static JSC::CallType getCallData(JSC::JSCell*, JSC::CallData&);\n\n") if $dataNode->extendedAttributes->{"CustomCall"};
820
821     # Custom deleteProperty function
822     push(@headerContent, "    virtual bool deleteProperty(JSC::ExecState*, const JSC::Identifier&);\n") if $dataNode->extendedAttributes->{"CustomDeleteProperty"};
823
824     # Custom getPropertyNames function exists on DOMWindow
825     if ($interfaceName eq "DOMWindow") {
826         push(@headerContent, "    virtual void getPropertyNames(JSC::ExecState*, JSC::PropertyNameArray&, JSC::EnumerationMode mode = JSC::ExcludeDontEnumProperties);\n");
827         $structureFlags{"JSC::OverridesGetPropertyNames"} = 1;
828     }
829
830     # Custom defineProperty function exists on DOMWindow
831     push(@headerContent, "    virtual bool defineOwnProperty(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor&, bool shouldThrow);\n") if $interfaceName eq "DOMWindow";
832
833     # Custom getOwnPropertyNames function
834     if ($dataNode->extendedAttributes->{"CustomGetPropertyNames"} || $dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
835         push(@headerContent, "    virtual void getOwnPropertyNames(JSC::ExecState*, JSC::PropertyNameArray&, JSC::EnumerationMode mode = JSC::ExcludeDontEnumProperties);\n");
836         $structureFlags{"JSC::OverridesGetPropertyNames"} = 1;       
837     }
838
839     # Custom defineGetter function
840     push(@headerContent, "    virtual void defineGetter(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSObject* getterFunction, unsigned attributes);\n") if $dataNode->extendedAttributes->{"CustomDefineGetter"};
841
842     # Custom defineSetter function
843     push(@headerContent, "    virtual void defineSetter(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSObject* setterFunction, unsigned attributes);\n") if $dataNode->extendedAttributes->{"CustomDefineSetter"};
844
845     # Custom lookupGetter function
846     push(@headerContent, "    virtual JSC::JSValue lookupGetter(JSC::ExecState*, const JSC::Identifier& propertyName);\n") if $dataNode->extendedAttributes->{"CustomLookupGetter"};
847
848     # Custom lookupSetter function
849     push(@headerContent, "    virtual JSC::JSValue lookupSetter(JSC::ExecState*, const JSC::Identifier& propertyName);\n") if $dataNode->extendedAttributes->{"CustomLookupSetter"};
850
851     # Override toBoolean to return false for objects that want to 'MasqueradesAsUndefined'.
852     if ($dataNode->extendedAttributes->{"MasqueradesAsUndefined"}) {
853         push(@headerContent, "    virtual bool toBoolean(JSC::ExecState*) const { return false; };\n");
854         $structureFlags{"JSC::MasqueradesAsUndefined"} = 1;
855     }
856
857     # Constructor object getter
858     push(@headerContent, "    static JSC::JSValue getConstructor(JSC::ExecState*, JSC::JSGlobalObject*);\n") if !$dataNode->extendedAttributes->{"OmitConstructor"};
859
860     my $numCustomFunctions = 0;
861     my $numCustomAttributes = 0;
862
863     # Attribute and function enums
864     if ($numAttributes > 0) {
865         foreach (@{$dataNode->attributes}) {
866             my $attribute = $_;
867             $numCustomAttributes++ if $attribute->signature->extendedAttributes->{"Custom"} || $attribute->signature->extendedAttributes->{"JSCCustom"};
868             $numCustomAttributes++ if $attribute->signature->extendedAttributes->{"CustomGetter"} || $attribute->signature->extendedAttributes->{"JSCCustomGetter"};
869             $numCustomAttributes++ if $attribute->signature->extendedAttributes->{"CustomSetter"} || $attribute->signature->extendedAttributes->{"JSCCustomSetter"};
870             if ($attribute->signature->extendedAttributes->{"CachedAttribute"}) {
871                 push(@headerContent, "    JSC::WriteBarrier<JSC::Unknown> m_" . $attribute->signature->name . ";\n");
872                 $numCachedAttributes++;
873                 $needsMarkChildren = 1;
874             }
875         }
876     }
877
878     # visit function
879     if ($needsMarkChildren) {
880         push(@headerContent, "    static void visitChildren(JSCell*, JSC::SlotVisitor&);\n\n");
881         $structureFlags{"JSC::OverridesVisitChildren"} = 1;
882     }
883
884     if ($numCustomAttributes > 0) {
885         push(@headerContent, "\n    // Custom attributes\n");
886
887         foreach my $attribute (@{$dataNode->attributes}) {
888             if ($attribute->signature->extendedAttributes->{"Custom"} || $attribute->signature->extendedAttributes->{"JSCCustom"}) {
889                 push(@headerContent, "    JSC::JSValue " . $codeGenerator->WK_lcfirst($attribute->signature->name) . "(JSC::ExecState*) const;\n");
890                 if ($attribute->type !~ /^readonly/) {
891                     push(@headerContent, "    void set" . $codeGenerator->WK_ucfirst($attribute->signature->name) . "(JSC::ExecState*, JSC::JSValue);\n");
892                 }
893             } elsif ($attribute->signature->extendedAttributes->{"CustomGetter"} || $attribute->signature->extendedAttributes->{"JSCCustomGetter"}) {
894                 push(@headerContent, "    JSC::JSValue " . $codeGenerator->WK_lcfirst($attribute->signature->name) . "(JSC::ExecState*) const;\n");
895             } elsif ($attribute->signature->extendedAttributes->{"CustomSetter"} || $attribute->signature->extendedAttributes->{"JSCCustomSetter"}) {
896                 if ($attribute->type !~ /^readonly/) {
897                     push(@headerContent, "    void set" . $codeGenerator->WK_ucfirst($attribute->signature->name) . "(JSC::ExecState*, JSC::JSValue);\n");
898                 }
899             }
900         }
901     }
902
903     foreach my $function (@{$dataNode->functions}) {
904         $numCustomFunctions++ if $function->signature->extendedAttributes->{"Custom"} || $function->signature->extendedAttributes->{"JSCCustom"};
905     }
906
907     if ($numCustomFunctions > 0) {
908         push(@headerContent, "\n    // Custom functions\n");
909         foreach my $function (@{$dataNode->functions}) {
910             if ($function->signature->extendedAttributes->{"Custom"} || $function->signature->extendedAttributes->{"JSCCustom"}) {
911                 my $functionImplementationName = $function->signature->extendedAttributes->{"ImplementationFunction"} || $codeGenerator->WK_lcfirst($function->signature->name);
912                 push(@headerContent, "    JSC::JSValue " . $functionImplementationName . "(JSC::ExecState*);\n");
913             }
914         }
915     }
916
917     if (!$hasParent) {
918         push(@headerContent, "    $implType* impl() const { return m_impl.get(); }\n\n");
919         push(@headerContent, "private:\n");
920         push(@headerContent, "    RefPtr<$implType> m_impl;\n");
921     } elsif ($dataNode->extendedAttributes->{"GenerateNativeConverter"}) {
922         push(@headerContent, "    $implClassName* impl() const\n");
923         push(@headerContent, "    {\n");
924         push(@headerContent, "        return static_cast<$implClassName*>(Base::impl());\n");
925         push(@headerContent, "    }\n");
926     }
927
928     push(@headerContent, "protected:\n");
929     # Constructor
930     if ($interfaceName eq "DOMWindow") {
931         push(@headerContent, "    $className(JSC::JSGlobalData&, JSC::Structure*, PassRefPtr<$implType>, JSDOMWindowShell*);\n");
932     } elsif ($dataNode->extendedAttributes->{"IsWorkerContext"}) {
933         push(@headerContent, "    $className(JSC::JSGlobalData&, JSC::Structure*, PassRefPtr<$implType>);\n");
934     } else {
935         push(@headerContent, "    $className(JSC::Structure*, JSDOMGlobalObject*, PassRefPtr<$implType>);\n");
936         push(@headerContent, "    void finishCreation(JSC::JSGlobalData&);\n");
937     }
938
939     # structure flags
940     push(@headerContent, "    static const unsigned StructureFlags = ");
941     foreach my $structureFlag (keys %structureFlags) {
942         push(@headerContent, $structureFlag . " | ");
943     }
944     push(@headerContent, "Base::StructureFlags;\n");
945
946     # Index getter
947     if ($dataNode->extendedAttributes->{"HasIndexGetter"}) {
948         push(@headerContent, "    static JSC::JSValue indexGetter(JSC::ExecState*, JSC::JSValue, unsigned);\n");
949     }
950     if ($dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
951         push(@headerContent, "    JSC::JSValue getByIndex(JSC::ExecState*, unsigned index);\n");
952         
953     }
954     
955     # Index setter
956     if ($dataNode->extendedAttributes->{"HasCustomIndexSetter"}) {
957         push(@headerContent, "    void indexSetter(JSC::ExecState*, unsigned index, JSC::JSValue);\n");
958     }
959     # Name getter
960     if ($dataNode->extendedAttributes->{"HasNameGetter"} || $dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
961         push(@headerContent, "private:\n");
962         push(@headerContent, "    static bool canGetItemsForName(JSC::ExecState*, $implClassName*, const JSC::Identifier&);\n");
963         push(@headerContent, "    static JSC::JSValue nameGetter(JSC::ExecState*, JSC::JSValue, const JSC::Identifier&);\n");
964     }
965
966     push(@headerContent, "};\n\n");
967
968     if ($dataNode->extendedAttributes->{"InlineGetOwnPropertySlot"} && !$dataNode->extendedAttributes->{"CustomGetOwnPropertySlot"}) {
969         push(@headerContent, "ALWAYS_INLINE bool ${className}::getOwnPropertySlot(JSC::ExecState* exec, const JSC::Identifier& propertyName, JSC::PropertySlot& slot)\n");
970         push(@headerContent, "{\n");
971         push(@headerContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
972         push(@headerContent, GenerateGetOwnPropertySlotBody($dataNode, $interfaceName, $className, $implClassName, $numAttributes > 0, 1));
973         push(@headerContent, "}\n\n");
974         push(@headerContent, "ALWAYS_INLINE bool ${className}::getOwnPropertyDescriptor(JSC::ExecState* exec, const JSC::Identifier& propertyName, JSC::PropertyDescriptor& descriptor)\n");
975         push(@headerContent, "{\n");
976         push(@headerContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
977         push(@headerContent, GenerateGetOwnPropertyDescriptorBody($dataNode, $interfaceName, $className, $implClassName, $numAttributes > 0, 1));
978         push(@headerContent, "}\n\n");
979     }
980
981     if ($dataNode->extendedAttributes->{"GenerateIsReachable"} || 
982         $dataNode->extendedAttributes->{"CustomIsReachable"} || 
983         $dataNode->extendedAttributes->{"CustomFinalize"} ||
984         $dataNode->extendedAttributes->{"ActiveDOMObject"}) {
985         push(@headerContent, "class JS${implType}Owner : public JSC::WeakHandleOwner {\n");
986         push(@headerContent, "    virtual bool isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown>, void* context, JSC::SlotVisitor&);\n");
987         push(@headerContent, "    virtual void finalize(JSC::Handle<JSC::Unknown>, void* context);\n");
988         push(@headerContent, "};\n");
989         push(@headerContent, "\n");
990         push(@headerContent, "inline JSC::WeakHandleOwner* wrapperOwner(DOMWrapperWorld*, $implType*)\n");
991         push(@headerContent, "{\n");
992         push(@headerContent, "    DEFINE_STATIC_LOCAL(JS${implType}Owner, js${implType}Owner, ());\n");
993         push(@headerContent, "    return &js${implType}Owner;\n");
994         push(@headerContent, "}\n");
995         push(@headerContent, "\n");
996         push(@headerContent, "inline void* wrapperContext(DOMWrapperWorld* world, $implType*)\n");
997         push(@headerContent, "{\n");
998         push(@headerContent, "    return world;\n");
999         push(@headerContent, "}\n");
1000         push(@headerContent, "\n");
1001     }
1002
1003     if (!$hasParent || $dataNode->extendedAttributes->{"GenerateToJS"} || $dataNode->extendedAttributes->{"CustomToJS"}) {
1004         push(@headerContent, "JSC::JSValue toJS(JSC::ExecState*, JSDOMGlobalObject*, $implType*);\n");
1005     }
1006     if (!$hasParent || $dataNode->extendedAttributes->{"GenerateNativeConverter"}) {
1007         if ($interfaceName eq "NodeFilter") {
1008             push(@headerContent, "PassRefPtr<NodeFilter> toNodeFilter(JSC::JSGlobalData&, JSC::JSValue);\n");
1009         } else {
1010             push(@headerContent, "$implType* to${interfaceName}(JSC::JSValue);\n");
1011         }
1012     }
1013     if ($usesToJSNewlyCreated{$interfaceName}) {
1014         push(@headerContent, "JSC::JSValue toJSNewlyCreated(JSC::ExecState*, JSDOMGlobalObject*, $interfaceName*);\n");
1015     }
1016     
1017     push(@headerContent, "\n");
1018
1019     # Add prototype declaration.
1020     %structureFlags = ();
1021     push(@headerContent, "class ${className}Prototype : public JSC::JSNonFinalObject {\n");
1022     push(@headerContent, "public:\n");
1023     push(@headerContent, "    typedef JSC::JSNonFinalObject Base;\n");
1024     if ($interfaceName ne "DOMWindow" && !$dataNode->extendedAttributes->{"IsWorkerContext"}) {
1025         push(@headerContent, "    static JSC::JSObject* self(JSC::ExecState*, JSC::JSGlobalObject*);\n");
1026     }
1027
1028     push(@headerContent, "    static ${className}Prototype* create(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::Structure* structure)\n");
1029     push(@headerContent, "    {\n");
1030     push(@headerContent, "        ${className}Prototype* ptr = new (JSC::allocateCell<${className}Prototype>(globalData.heap)) ${className}Prototype(globalData, globalObject, structure);\n");
1031     push(@headerContent, "        ptr->finishCreation(globalData);\n");
1032     push(@headerContent, "        return ptr;\n");
1033     push(@headerContent, "    }\n\n");
1034
1035     push(@headerContent, "    static const JSC::ClassInfo s_info;\n");
1036     if ($numFunctions > 0 || $numConstants > 0 || $dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"}) {
1037         push(@headerContent, "    virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier&, JSC::PropertySlot&);\n");
1038         push(@headerContent, "    virtual bool getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier&, JSC::PropertyDescriptor&);\n");
1039         push(@headerContent, "    bool getOwnPropertySlotDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::PropertySlot&);\n") if $dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"};
1040         push(@headerContent, "    bool getOwnPropertyDescriptorDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::PropertyDescriptor&);\n") if $dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"};
1041         $structureFlags{"JSC::OverridesGetOwnPropertySlot"} = 1;
1042     }
1043     if ($dataNode->extendedAttributes->{"CustomMarkFunction"} or $needsMarkChildren) {
1044         $structureFlags{"JSC::OverridesVisitChildren"} = 1;
1045     }
1046     push(@headerContent,
1047         "    static JSC::Structure* createStructure(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)\n" .
1048         "    {\n" .
1049         "        return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), &s_info);\n" .
1050         "    }\n");
1051     if ($dataNode->extendedAttributes->{"DelegatingPrototypePutFunction"}) {
1052         push(@headerContent, "    virtual void put(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&);\n");
1053         push(@headerContent, "    bool putDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::JSValue, JSC::PutPropertySlot&);\n");
1054     }
1055
1056     # Custom defineGetter function
1057     push(@headerContent, "    virtual void defineGetter(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSObject* getterFunction, unsigned attributes);\n") if $dataNode->extendedAttributes->{"CustomPrototypeDefineGetter"};
1058
1059     push(@headerContent, "\nprivate:\n");
1060     push(@headerContent, "    ${className}Prototype(JSC::JSGlobalData& globalData, JSC::JSGlobalObject*, JSC::Structure* structure) : JSC::JSNonFinalObject(globalData, structure) { }\n");
1061
1062     # structure flags
1063     push(@headerContent, "protected:\n");
1064     push(@headerContent, "    static const unsigned StructureFlags = ");
1065     foreach my $structureFlag (keys %structureFlags) {
1066         push(@headerContent, $structureFlag . " | ");
1067     }
1068     push(@headerContent, "Base::StructureFlags;\n");
1069
1070     push(@headerContent, "};\n\n");
1071
1072     # Conditionally emit the constructor object's declaration
1073     if ($dataNode->extendedAttributes->{"JSCustomConstructor"} || $dataNode->extendedAttributes->{"CustomConstructor"}) {
1074         GenerateConstructorDeclaration(\@headerContent, $className, $dataNode);
1075     }
1076
1077
1078     if ($numFunctions > 0) {
1079         push(@headerContent,"// Functions\n\n");
1080         foreach my $function (@{$dataNode->functions}) {
1081             next if $function->{overloadIndex} && $function->{overloadIndex} > 1;
1082             my $functionName = $codeGenerator->WK_lcfirst($className) . "PrototypeFunction" . $codeGenerator->WK_ucfirst($function->signature->name);
1083             push(@headerContent, "JSC::EncodedJSValue JSC_HOST_CALL ${functionName}(JSC::ExecState*);\n");
1084         }
1085     }
1086
1087     if ($numAttributes > 0 || !$dataNode->extendedAttributes->{"OmitConstructor"}) {
1088         push(@headerContent,"// Attributes\n\n");
1089         foreach my $attribute (@{$dataNode->attributes}) {
1090             my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($attribute->signature->name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1091             push(@headerContent, "JSC::JSValue ${getter}(JSC::ExecState*, JSC::JSValue, const JSC::Identifier&);\n");
1092             unless ($attribute->type =~ /readonly/) {
1093                 my $setter = "setJS" . $interfaceName . $codeGenerator->WK_ucfirst($attribute->signature->name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1094                 push(@headerContent, "void ${setter}(JSC::ExecState*, JSC::JSObject*, JSC::JSValue);\n");
1095             }
1096         }
1097         
1098         if (!$dataNode->extendedAttributes->{"OmitConstructor"}) {
1099             my $getter = "js" . $interfaceName . "Constructor";
1100             push(@headerContent, "JSC::JSValue ${getter}(JSC::ExecState*, JSC::JSValue, const JSC::Identifier&);\n");
1101         }
1102
1103         if ($dataNode->extendedAttributes->{"ReplaceableConstructor"}) {
1104             my $constructorFunctionName = "setJS" . $interfaceName . "Constructor";
1105             push(@headerContent, "void ${constructorFunctionName}(JSC::ExecState*, JSC::JSObject*, JSC::JSValue);\n");
1106         }
1107     }
1108
1109     if ($numConstants > 0) {
1110         push(@headerContent,"// Constants\n\n");
1111         foreach my $constant (@{$dataNode->constants}) {
1112             my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($constant->name);
1113             push(@headerContent, "JSC::JSValue ${getter}(JSC::ExecState*, JSC::JSValue, const JSC::Identifier&);\n");
1114         }
1115     }
1116
1117     my $conditionalString = GenerateConditionalString($dataNode);
1118     push(@headerContent, "\n} // namespace WebCore\n\n");
1119     push(@headerContent, "#endif // ${conditionalString}\n\n") if $conditionalString;
1120     push(@headerContent, "#endif\n");
1121
1122     # - Generate dependencies.
1123     if ($writeDependencies && @ancestorInterfaceNames) {
1124         push(@depsContent, "$className.h : ", join(" ", map { "$_.idl" } @ancestorInterfaceNames), "\n");
1125         push(@depsContent, map { "$_.idl :\n" } @ancestorInterfaceNames); 
1126     }
1127 }
1128
1129 sub GenerateAttributesHashTable($$)
1130 {
1131     my ($object, $dataNode) = @_;
1132
1133     # FIXME: These should be functions on $dataNode.
1134     my $interfaceName = $dataNode->name;
1135     my $className = "JS$interfaceName";
1136     
1137     # - Add all attributes in a hashtable definition
1138     my $numAttributes = @{$dataNode->attributes};
1139     $numAttributes++ if !$dataNode->extendedAttributes->{"OmitConstructor"};
1140
1141     return 0  if !$numAttributes;
1142
1143     my $hashSize = $numAttributes;
1144     my $hashName = $className . "Table";
1145
1146     my @hashKeys = ();
1147     my @hashSpecials = ();
1148     my @hashValue1 = ();
1149     my @hashValue2 = ();
1150     my %conditionals = ();
1151
1152     my @entries = ();
1153
1154     foreach my $attribute (@{$dataNode->attributes}) {
1155         my $name = $attribute->signature->name;
1156         push(@hashKeys, $name);
1157
1158         my @specials = ();
1159         push(@specials, "DontDelete") unless $attribute->signature->extendedAttributes->{"Deletable"};
1160         push(@specials, "DontEnum") if $attribute->signature->extendedAttributes->{"DontEnum"};
1161         push(@specials, "ReadOnly") if $attribute->type =~ /readonly/;
1162         my $special = (@specials > 0) ? join(" | ", @specials) : "0";
1163         push(@hashSpecials, $special);
1164
1165         my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($attribute->signature->name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1166         push(@hashValue1, $getter);
1167
1168         if ($attribute->type =~ /readonly/) {
1169             push(@hashValue2, "0");
1170         } else {
1171             my $setter = "setJS" . $interfaceName . $codeGenerator->WK_ucfirst($attribute->signature->name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1172             push(@hashValue2, $setter);
1173         }
1174
1175         my $conditional = $attribute->signature->extendedAttributes->{"Conditional"};
1176         if ($conditional) {
1177             $conditionals{$name} = $conditional;
1178         }
1179     }
1180
1181     if (!$dataNode->extendedAttributes->{"OmitConstructor"}) {
1182         push(@hashKeys, "constructor");
1183         my $getter = "js" . $interfaceName . "Constructor";
1184         push(@hashValue1, $getter);
1185         if ($dataNode->extendedAttributes->{"ReplaceableConstructor"}) {
1186             my $setter = "setJS" . $interfaceName . "Constructor";
1187             push(@hashValue2, $setter);
1188             push(@hashSpecials, "DontEnum | DontDelete");
1189         } else {            
1190             push(@hashValue2, "0");
1191             push(@hashSpecials, "DontEnum | ReadOnly");
1192         }
1193     }
1194
1195     $object->GenerateHashTable($hashName, $hashSize,
1196                                \@hashKeys, \@hashSpecials,
1197                                \@hashValue1, \@hashValue2,
1198                                \%conditionals);
1199     return $numAttributes;
1200 }
1201
1202 sub GenerateParametersCheckExpression
1203 {
1204     my $numParameters = shift;
1205     my $function = shift;
1206
1207     my @andExpression = ();
1208     push(@andExpression, "argsCount == $numParameters");
1209     my $parameterIndex = 0;
1210     my %usedArguments = ();
1211     foreach my $parameter (@{$function->parameters}) {
1212         last if $parameterIndex >= $numParameters;
1213         my $value = "arg$parameterIndex";
1214         my $type = $codeGenerator->StripModule($parameter->type);
1215
1216         # Only DOMString or wrapper types are checked.
1217         # For DOMString, Null, Undefined and any Object are accepted too, as
1218         # these are acceptable values for a DOMString argument (any Object can
1219         # be converted to a string via .toString).
1220         if ($codeGenerator->IsStringType($type)) {
1221             push(@andExpression, "(${value}.isUndefinedOrNull() || ${value}.isString() || ${value}.isObject())");
1222             $usedArguments{$parameterIndex} = 1;
1223         } elsif ($parameter->extendedAttributes->{"Callback"}) {
1224             # For Callbacks only checks if the value is null or object.
1225             push(@andExpression, "(${value}.isNull() || ${value}.isObject())");
1226             $usedArguments{$parameterIndex} = 1;
1227         } elsif (!IsNativeType($type)) {
1228             push(@andExpression, "(${value}.isNull() || (${value}.isObject() && asObject(${value})->inherits(&JS${type}::s_info)))");
1229             $usedArguments{$parameterIndex} = 1;
1230         }
1231         $parameterIndex++;
1232     }
1233     my $res = join(" && ", @andExpression);
1234     $res = "($res)" if @andExpression > 1;
1235     return ($res, keys %usedArguments);
1236 }
1237
1238 sub GenerateFunctionParametersCheck
1239 {
1240     my $function = shift;
1241
1242     my @orExpression = ();
1243     my $numParameters = 0;
1244     my @neededArguments = ();
1245
1246     foreach my $parameter (@{$function->parameters}) {
1247         if ($parameter->extendedAttributes->{"Optional"}) {
1248             my ($expression, @usedArguments) = GenerateParametersCheckExpression($numParameters, $function);
1249             push(@orExpression, $expression);
1250             push(@neededArguments, @usedArguments);
1251         }
1252         $numParameters++;
1253     }
1254     my ($expression, @usedArguments) = GenerateParametersCheckExpression($numParameters, $function);
1255     push(@orExpression, $expression);
1256     push(@neededArguments, @usedArguments);
1257     return (join(" || ", @orExpression), @neededArguments);
1258 }
1259
1260 sub GenerateOverloadedPrototypeFunction
1261 {
1262     my $function = shift;
1263     my $dataNode = shift;
1264     my $implClassName = shift;
1265
1266     # Generate code for choosing the correct overload to call. Overloads are
1267     # chosen based on the total number of arguments passed and the type of
1268     # values passed in non-primitive argument slots. When more than a single
1269     # overload is applicable, precedence is given according to the order of
1270     # declaration in the IDL.
1271
1272     my $functionName = "js${implClassName}PrototypeFunction" . $codeGenerator->WK_ucfirst($function->signature->name);
1273
1274     push(@implContent, "EncodedJSValue JSC_HOST_CALL ${functionName}(ExecState* exec)\n");
1275     push(@implContent, <<END);
1276 {
1277     size_t argsCount = exec->argumentCount();
1278 END
1279
1280     my %fetchedArguments = ();
1281
1282     foreach my $overload (@{$function->{overloads}}) {
1283         my ($parametersCheck, @neededArguments) = GenerateFunctionParametersCheck($overload);
1284
1285         foreach my $parameterIndex (@neededArguments) {
1286             next if exists $fetchedArguments{$parameterIndex};
1287             push(@implContent, "    JSValue arg$parameterIndex(exec->argument($parameterIndex));\n");
1288             $fetchedArguments{$parameterIndex} = 1;
1289         }
1290
1291         push(@implContent, "    if ($parametersCheck)\n");
1292         push(@implContent, "        return ${functionName}$overload->{overloadIndex}(exec);\n");
1293     }
1294     push(@implContent, <<END);
1295     return throwVMTypeError(exec);
1296 }
1297
1298 END
1299 }
1300
1301 sub GenerateImplementation
1302 {
1303     my ($object, $dataNode) = @_;
1304
1305     my $interfaceName = $dataNode->name;
1306     my $className = "JS$interfaceName";
1307     my $implClassName = $interfaceName;
1308
1309     my $hasLegacyParent = $dataNode->extendedAttributes->{"LegacyParent"};
1310     my $hasRealParent = @{$dataNode->parents} > 0;
1311     my $hasParent = $hasLegacyParent || $hasRealParent;
1312     my $parentClassName = GetParentClassName($dataNode);
1313     my $visibleClassName = GetVisibleClassName($interfaceName);
1314     my $eventTarget = $dataNode->extendedAttributes->{"EventTarget"};
1315     my $needsMarkChildren = $dataNode->extendedAttributes->{"CustomMarkFunction"} || $dataNode->extendedAttributes->{"EventTarget"};
1316
1317     # - Add default header template
1318     push(@implContentHeader, GenerateImplementationContentHeader($dataNode));
1319
1320     AddIncludesForSVGAnimatedType($interfaceName) if $className =~ /^JSSVGAnimated/;
1321
1322     $implIncludes{"<wtf/GetPtr.h>"} = 1;
1323     $implIncludes{"<runtime/PropertyNameArray.h>"} = 1 if $dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"};
1324
1325     AddIncludesForTypeInImpl($interfaceName);
1326
1327     @implContent = ();
1328
1329     push(@implContent, "\nusing namespace JSC;\n\n");
1330     push(@implContent, "namespace WebCore {\n\n");
1331
1332     push(@implContent, "ASSERT_CLASS_FITS_IN_CELL($className);\n\n");
1333
1334     my $numAttributes = GenerateAttributesHashTable($object, $dataNode);
1335
1336     my $numConstants = @{$dataNode->constants};
1337     my $numFunctions = @{$dataNode->functions};
1338
1339     # - Add all constants
1340     if (!$dataNode->extendedAttributes->{"OmitConstructor"}) {
1341         my $hashSize = $numConstants;
1342         my $hashName = $className . "ConstructorTable";
1343
1344         my @hashKeys = ();
1345         my @hashValue1 = ();
1346         my @hashValue2 = ();
1347         my @hashSpecials = ();
1348
1349         # FIXME: we should not need a function for every constant.
1350         foreach my $constant (@{$dataNode->constants}) {
1351             push(@hashKeys, $constant->name);
1352             my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($constant->name);
1353             push(@hashValue1, $getter);
1354             push(@hashValue2, "0");
1355             push(@hashSpecials, "DontDelete | ReadOnly");
1356         }
1357
1358         $object->GenerateHashTable($hashName, $hashSize,
1359                                    \@hashKeys, \@hashSpecials,
1360                                    \@hashValue1, \@hashValue2);
1361
1362         push(@implContent, $codeGenerator->GenerateCompileTimeCheckForEnumsIfNeeded($dataNode));
1363
1364         my $protoClassName = "${className}Prototype";
1365
1366         GenerateConstructorDeclaration(\@implContent, $className, $dataNode) unless ($dataNode->extendedAttributes->{"JSCustomConstructor"} || $dataNode->extendedAttributes->{"CustomConstructor"});
1367         GenerateConstructorDefinition(\@implContent, $className, $protoClassName, $interfaceName, $visibleClassName, $dataNode);
1368     }
1369
1370     # - Add functions and constants to a hashtable definition
1371     my $hashSize = $numFunctions + $numConstants;
1372     my $hashName = $className . "PrototypeTable";
1373
1374     my @hashKeys = ();
1375     my @hashValue1 = ();
1376     my @hashValue2 = ();
1377     my @hashSpecials = ();
1378     my %conditionals = ();
1379
1380     # FIXME: we should not need a function for every constant.
1381     foreach my $constant (@{$dataNode->constants}) {
1382         push(@hashKeys, $constant->name);
1383         my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($constant->name);
1384         push(@hashValue1, $getter);
1385         push(@hashValue2, "0");
1386         push(@hashSpecials, "DontDelete | ReadOnly");
1387     }
1388
1389     foreach my $function (@{$dataNode->functions}) {
1390         next if $function->{overloadIndex} && $function->{overloadIndex} > 1;
1391         my $name = $function->signature->name;
1392         push(@hashKeys, $name);
1393
1394         my $value = $codeGenerator->WK_lcfirst($className) . "PrototypeFunction" . $codeGenerator->WK_ucfirst($name);
1395         push(@hashValue1, $value);
1396
1397         my $numParameters = @{$function->parameters};
1398         push(@hashValue2, $numParameters);
1399
1400         my @specials = ();
1401         push(@specials, "DontDelete") unless $function->signature->extendedAttributes->{"Deletable"};
1402         push(@specials, "DontEnum") if $function->signature->extendedAttributes->{"DontEnum"};
1403         push(@specials, "Function");
1404         my $special = (@specials > 0) ? join(" | ", @specials) : "0";
1405         push(@hashSpecials, $special);
1406
1407         my $conditional = $function->signature->extendedAttributes->{"Conditional"};
1408         if ($conditional) {
1409             $conditionals{$name} = $conditional;
1410         }
1411     }
1412
1413     $object->GenerateHashTable($hashName, $hashSize,
1414                                \@hashKeys, \@hashSpecials,
1415                                \@hashValue1, \@hashValue2,
1416                                \%conditionals);
1417
1418     if ($dataNode->extendedAttributes->{"NoStaticTables"}) {
1419         push(@implContent, "static const HashTable* get${className}PrototypeTable(ExecState* exec)\n");
1420         push(@implContent, "{\n");
1421         push(@implContent, "    return getHashTableForGlobalData(exec->globalData(), &${className}PrototypeTable);\n");
1422         push(@implContent, "}\n\n");
1423         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");
1424     } else {
1425         push(@implContent, "const ClassInfo ${className}Prototype::s_info = { \"${visibleClassName}Prototype\", &JSC::JSNonFinalObject::s_info, &${className}PrototypeTable, 0, CREATE_METHOD_TABLE(${className}Prototype) };\n\n");
1426     }
1427     if ($interfaceName ne "DOMWindow" && !$dataNode->extendedAttributes->{"IsWorkerContext"}) {
1428         push(@implContent, "JSObject* ${className}Prototype::self(ExecState* exec, JSGlobalObject* globalObject)\n");
1429         push(@implContent, "{\n");
1430         push(@implContent, "    return getDOMPrototype<${className}>(exec, globalObject);\n");
1431         push(@implContent, "}\n\n");
1432     }
1433     if ($numConstants > 0 || $numFunctions > 0 || $dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"}) {
1434         push(@implContent, "bool ${className}Prototype::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\n");
1435         push(@implContent, "{\n");
1436
1437         if ($dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"}) {
1438             push(@implContent, "    if (getOwnPropertySlotDelegate(exec, propertyName, slot))\n");
1439             push(@implContent, "        return true;\n");
1440         }
1441
1442         if ($numConstants eq 0 && $numFunctions eq 0) {
1443             push(@implContent, "    return Base::getOwnPropertySlot(exec, propertyName, slot);\n");        
1444         } elsif ($numConstants eq 0) {
1445             push(@implContent, "    return getStaticFunctionSlot<JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, slot);\n");
1446         } elsif ($numFunctions eq 0) {
1447             push(@implContent, "    return getStaticValueSlot<${className}Prototype, JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, slot);\n");
1448         } else {
1449             push(@implContent, "    return getStaticPropertySlot<${className}Prototype, JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, slot);\n");
1450         }
1451         push(@implContent, "}\n\n");
1452
1453         push(@implContent, "bool ${className}Prototype::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)\n");
1454         push(@implContent, "{\n");
1455         
1456         if ($dataNode->extendedAttributes->{"DelegatingPrototypeGetOwnPropertySlot"}) {
1457             push(@implContent, "    if (getOwnPropertyDescriptorDelegate(exec, propertyName, descriptor))\n");
1458             push(@implContent, "        return true;\n");
1459         }
1460         
1461         if ($numConstants eq 0 && $numFunctions eq 0) {
1462             push(@implContent, "    return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor);\n");        
1463         } elsif ($numConstants eq 0) {
1464             push(@implContent, "    return getStaticFunctionDescriptor<JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, descriptor);\n");
1465         } elsif ($numFunctions eq 0) {
1466             push(@implContent, "    return getStaticValueDescriptor<${className}Prototype, JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, descriptor);\n");
1467         } else {
1468             push(@implContent, "    return getStaticPropertyDescriptor<${className}Prototype, JSObject>(exec, " . prototypeHashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, propertyName, descriptor);\n");
1469         }
1470         push(@implContent, "}\n\n");
1471     }
1472
1473     if ($dataNode->extendedAttributes->{"DelegatingPrototypePutFunction"}) {
1474         push(@implContent, "void ${className}Prototype::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)\n");
1475         push(@implContent, "{\n");
1476         push(@implContent, "    if (putDelegate(exec, propertyName, value, slot))\n");
1477         push(@implContent, "        return;\n");
1478         push(@implContent, "    Base::put(exec, propertyName, value, slot);\n");
1479         push(@implContent, "}\n\n");
1480     }
1481
1482     # - Initialize static ClassInfo object
1483     if ($numAttributes > 0 && $dataNode->extendedAttributes->{"NoStaticTables"}) {
1484         push(@implContent, "static const HashTable* get${className}Table(ExecState* exec)\n");
1485         push(@implContent, "{\n");
1486         push(@implContent, "    return getHashTableForGlobalData(exec->globalData(), &${className}Table);\n");
1487         push(@implContent, "}\n\n");
1488     }
1489
1490     push(@implContent, "const ClassInfo $className" . "::s_info = { \"${visibleClassName}\", &" . $parentClassName . "::s_info, ");
1491
1492     if ($numAttributes > 0 && !$dataNode->extendedAttributes->{"NoStaticTables"}) {
1493         push(@implContent, "&${className}Table");
1494     } else {
1495         push(@implContent, "0");
1496     }
1497     if ($numAttributes > 0 && $dataNode->extendedAttributes->{"NoStaticTables"}) {
1498         push(@implContent, ", get${className}Table ");
1499     } else {
1500         push(@implContent, ", 0 ");
1501     }
1502     push(@implContent, ", CREATE_METHOD_TABLE($className) };\n\n");
1503
1504     my $implType = $implClassName;
1505     my ($svgPropertyType, $svgListPropertyType, $svgNativeType) = GetSVGPropertyTypes($implType);
1506     $implType = $svgNativeType if $svgNativeType;
1507
1508     my $svgPropertyOrListPropertyType;
1509     $svgPropertyOrListPropertyType = $svgPropertyType if $svgPropertyType;
1510     $svgPropertyOrListPropertyType = $svgListPropertyType if $svgListPropertyType;
1511
1512     # Constructor
1513     if ($interfaceName eq "DOMWindow") {
1514         AddIncludesForTypeInImpl("JSDOMWindowShell");
1515         push(@implContent, "${className}::$className(JSGlobalData& globalData, Structure* structure, PassRefPtr<$implType> impl, JSDOMWindowShell* shell)\n");
1516         push(@implContent, "    : $parentClassName(globalData, structure, impl, shell)\n");
1517         push(@implContent, "{\n");
1518         push(@implContent, "}\n\n");
1519     } elsif ($dataNode->extendedAttributes->{"IsWorkerContext"}) {
1520         AddIncludesForTypeInImpl($interfaceName);
1521         push(@implContent, "${className}::$className(JSGlobalData& globalData, Structure* structure, PassRefPtr<$implType> impl)\n");
1522         push(@implContent, "    : $parentClassName(globalData, structure, impl)\n");
1523         push(@implContent, "{\n");
1524         push(@implContent, "}\n\n");
1525     } else {
1526         push(@implContent, "${className}::$className(Structure* structure, JSDOMGlobalObject* globalObject, PassRefPtr<$implType> impl)\n");
1527         if ($hasParent) {
1528             push(@implContent, "    : $parentClassName(structure, globalObject, impl)\n");
1529         } else {
1530             push(@implContent, "    : $parentClassName(structure, globalObject)\n");
1531             push(@implContent, "    , m_impl(impl)\n");
1532         }
1533         push(@implContent, "{\n");
1534         push(@implContent, "}\n\n");
1535
1536         push(@implContent, "void ${className}::finishCreation(JSGlobalData& globalData)\n");
1537         push(@implContent, "{\n");
1538         push(@implContent, "    Base::finishCreation(globalData);\n");
1539         push(@implContent, "    ASSERT(inherits(&s_info));\n");
1540         push(@implContent, "}\n\n");
1541     }
1542
1543     if (!$dataNode->extendedAttributes->{"ExtendsDOMGlobalObject"}) {
1544         push(@implContent, "JSObject* ${className}::createPrototype(ExecState* exec, JSGlobalObject* globalObject)\n");
1545         push(@implContent, "{\n");
1546         if ($hasParent && $parentClassName ne "JSC::DOMNodeFilter") {
1547             push(@implContent, "    return ${className}Prototype::create(exec->globalData(), globalObject, ${className}Prototype::createStructure(exec->globalData(), globalObject, ${parentClassName}Prototype::self(exec, globalObject)));\n");
1548         } else {
1549             push(@implContent, "    return ${className}Prototype::create(exec->globalData(), globalObject, ${className}Prototype::createStructure(globalObject->globalData(), globalObject, globalObject->objectPrototype()));\n");
1550         }
1551         push(@implContent, "}\n\n");
1552     }
1553
1554     my $hasGetter = $numAttributes > 0 
1555                  || !$dataNode->extendedAttributes->{"OmitConstructor"} 
1556                  || $dataNode->extendedAttributes->{"HasIndexGetter"}
1557                  || $dataNode->extendedAttributes->{"HasCustomIndexGetter"}
1558                  || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}
1559                  || $dataNode->extendedAttributes->{"DelegatingGetOwnPropertySlot"}
1560                  || $dataNode->extendedAttributes->{"CustomGetOwnPropertySlot"}
1561                  || $dataNode->extendedAttributes->{"HasNameGetter"}
1562                  || $dataNode->extendedAttributes->{"HasOverridingNameGetter"};
1563
1564     # Attributes
1565     if ($hasGetter) {
1566         if (!$dataNode->extendedAttributes->{"InlineGetOwnPropertySlot"} && !$dataNode->extendedAttributes->{"CustomGetOwnPropertySlot"}) {
1567             push(@implContent, "bool ${className}::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\n");
1568             push(@implContent, "{\n");
1569             push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
1570             push(@implContent, GenerateGetOwnPropertySlotBody($dataNode, $interfaceName, $className, $implClassName, $numAttributes > 0, 0));
1571             push(@implContent, "}\n\n");
1572             push(@implContent, "bool ${className}::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)\n");
1573             push(@implContent, "{\n");
1574             push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
1575             push(@implContent, GenerateGetOwnPropertyDescriptorBody($dataNode, $interfaceName, $className, $implClassName, $numAttributes > 0, 0));
1576             push(@implContent, "}\n\n");
1577         }
1578
1579         if (($dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) 
1580                 && !$dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
1581             push(@implContent, "bool ${className}::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)\n");
1582             push(@implContent, "{\n");
1583             push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
1584             push(@implContent, "    if (propertyName < static_cast<$implClassName*>(impl())->length()) {\n");
1585             if ($dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
1586                 push(@implContent, "        slot.setValue(getByIndex(exec, propertyName));\n");
1587             } else {
1588                 push(@implContent, "        slot.setCustomIndex(this, propertyName, indexGetter);\n");
1589             }
1590             push(@implContent, "        return true;\n");
1591             push(@implContent, "    }\n");
1592             push(@implContent, "    return getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);\n");
1593             push(@implContent, "}\n\n");
1594         }
1595         
1596         if ($numAttributes > 0) {
1597             foreach my $attribute (@{$dataNode->attributes}) {
1598                 my $name = $attribute->signature->name;
1599                 my $type = $codeGenerator->StripModule($attribute->signature->type);
1600                 my $getFunctionName = "js" . $interfaceName .  $codeGenerator->WK_ucfirst($attribute->signature->name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1601                 my $implGetterFunctionName = $codeGenerator->WK_lcfirst($name);
1602
1603                 my $attributeConditionalString = GenerateConditionalString($attribute->signature);
1604                 push(@implContent, "#if ${attributeConditionalString}\n") if $attributeConditionalString;
1605
1606                 push(@implContent, "JSValue ${getFunctionName}(ExecState* exec, JSValue slotBase, const Identifier&)\n");
1607                 push(@implContent, "{\n");
1608                 push(@implContent, "    ${className}* castedThis = static_cast<$className*>(asObject(slotBase));\n");
1609
1610                 if ($attribute->signature->extendedAttributes->{"CachedAttribute"}) {
1611                     $needsMarkChildren = 1;
1612                 }
1613
1614                 if ($dataNode->extendedAttributes->{"CheckDomainSecurity"} && 
1615                         !$attribute->signature->extendedAttributes->{"DoNotCheckDomainSecurity"} &&
1616                         !$attribute->signature->extendedAttributes->{"DoNotCheckDomainSecurityOnGet"}) {
1617                     push(@implContent, "    if (!castedThis->allowsAccessFrom(exec))\n");
1618                     push(@implContent, "        return jsUndefined();\n");
1619                 }
1620
1621                 if ($attribute->signature->extendedAttributes->{"Custom"} || $attribute->signature->extendedAttributes->{"JSCCustom"} || $attribute->signature->extendedAttributes->{"CustomGetter"} || $attribute->signature->extendedAttributes->{"JSCCustomGetter"}) {
1622                     push(@implContent, "    return castedThis->$implGetterFunctionName(exec);\n");
1623                 } elsif ($attribute->signature->extendedAttributes->{"CheckNodeSecurity"}) {
1624                     $implIncludes{"JSDOMBinding.h"} = 1;
1625                     push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1626                     push(@implContent, "    return checkNodeSecurity(exec, imp->$implGetterFunctionName()) ? " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp->$implGetterFunctionName()", "castedThis") . " : jsUndefined();\n");
1627                 } elsif ($attribute->signature->extendedAttributes->{"CheckFrameSecurity"}) {
1628                     $implIncludes{"Document.h"} = 1;
1629                     $implIncludes{"JSDOMBinding.h"} = 1;
1630                     push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1631                     push(@implContent, "    return checkNodeSecurity(exec, imp->contentDocument()) ? " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp->$implGetterFunctionName()", "castedThis") . " : jsUndefined();\n");
1632                 } elsif ($type eq "EventListener") {
1633                     $implIncludes{"EventListener.h"} = 1;
1634                     push(@implContent, "    UNUSED_PARAM(exec);\n");
1635                     push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1636                     push(@implContent, "    if (EventListener* listener = imp->$implGetterFunctionName()) {\n");
1637                     push(@implContent, "        if (const JSEventListener* jsListener = JSEventListener::cast(listener)) {\n");
1638                     if ($implClassName eq "Document" || $implClassName eq "WorkerContext" || $implClassName eq "SharedWorkerContext" || $implClassName eq "DedicatedWorkerContext") {
1639                         push(@implContent, "            if (JSObject* jsFunction = jsListener->jsFunction(imp))\n");
1640                     } else {
1641                         push(@implContent, "            if (JSObject* jsFunction = jsListener->jsFunction(imp->scriptExecutionContext()))\n");
1642                     }
1643                     push(@implContent, "                return jsFunction;\n");
1644                     push(@implContent, "        }\n");
1645                     push(@implContent, "    }\n");
1646                     push(@implContent, "    return jsNull();\n");
1647                 } elsif ($attribute->signature->type =~ /Constructor$/) {
1648                     my $constructorType = $codeGenerator->StripModule($attribute->signature->type);
1649                     $constructorType =~ s/Constructor$//;
1650                     # Constructor attribute is only used by DOMWindow.idl, so it's correct to pass castedThis as the global object
1651                     # Once JSDOMWrappers have a back-pointer to the globalObject we can pass castedThis->globalObject()
1652                     push(@implContent, "    return JS" . $constructorType . "::getConstructor(exec, castedThis);\n");
1653                 } elsif (!@{$attribute->getterExceptions}) {
1654                     push(@implContent, "    UNUSED_PARAM(exec);\n");
1655                     my $cacheIndex = 0;
1656                     if ($attribute->signature->extendedAttributes->{"CachedAttribute"}) {
1657                         $cacheIndex = $currentCachedAttribute;
1658                         $currentCachedAttribute++;
1659                         push(@implContent, "    if (JSValue cachedValue = m_" . $attribute->signature->name . ".get())\n");
1660                         push(@implContent, "        return cachedValue;\n");
1661                     }
1662
1663                     if ($svgListPropertyType) {
1664                         push(@implContent, "    JSValue result =  " . NativeToJSValue($attribute->signature, 0, $implClassName, "castedThis->impl()->$implGetterFunctionName()", "castedThis") . ";\n");
1665                     } elsif ($svgPropertyOrListPropertyType) {
1666                         push(@implContent, "    $svgPropertyOrListPropertyType& imp = castedThis->impl()->propertyReference();\n");
1667                         if ($svgPropertyOrListPropertyType eq "float") { # Special case for JSSVGNumber
1668                             push(@implContent, "    JSValue result =  " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp", "castedThis") . ";\n");
1669                         } else {
1670                             push(@implContent, "    JSValue result =  " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp.$implGetterFunctionName()", "castedThis") . ";\n");
1671                         }
1672                     } else {
1673                         my $getterExpression = "imp->" . $codeGenerator->GetterExpressionPrefix(\%implIncludes, $interfaceName, $attribute) . ")";
1674                         my $jsType = NativeToJSValue($attribute->signature, 0, $implClassName, $getterExpression, "castedThis");
1675                         push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1676                         if ($codeGenerator->IsSVGAnimatedType($type)) {
1677                             push(@implContent, "    RefPtr<$type> obj = $jsType;\n");
1678                             push(@implContent, "    JSValue result =  toJS(exec, castedThis->globalObject(), obj.get());\n");
1679                         } else {
1680                             push(@implContent, "    JSValue result = $jsType;\n");
1681                         }
1682                     }
1683                     
1684                     push(@implContent, "    m_" . $attribute->signature->name . ".set(exec->globalData(), this, result);\n") if ($attribute->signature->extendedAttributes->{"CachedAttribute"});
1685                     push(@implContent, "    return result;\n");
1686
1687                 } else {
1688                     push(@implContent, "    ExceptionCode ec = 0;\n");                    
1689                     if ($svgPropertyOrListPropertyType) {
1690                         push(@implContent, "    $svgPropertyOrListPropertyType imp(*castedThis->impl());\n");
1691                         push(@implContent, "    JSC::JSValue result = " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp.$implGetterFunctionName(ec)", "castedThis") . ";\n");
1692                     } else {
1693                         push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1694                         push(@implContent, "    JSC::JSValue result = " . NativeToJSValue($attribute->signature, 0, $implClassName, "imp->$implGetterFunctionName(ec)", "castedThis") . ";\n");
1695                     }
1696
1697                     push(@implContent, "    setDOMException(exec, ec);\n");
1698                     push(@implContent, "    return result;\n");
1699                 }
1700
1701                 push(@implContent, "}\n\n");
1702
1703                 push(@implContent, "#endif\n") if $attributeConditionalString;
1704
1705                 push(@implContent, "\n");
1706             }
1707
1708             if (!$dataNode->extendedAttributes->{"OmitConstructor"}) {
1709                 my $constructorFunctionName = "js" . $interfaceName . "Constructor";
1710
1711                 push(@implContent, "JSValue ${constructorFunctionName}(ExecState* exec, JSValue slotBase, const Identifier&)\n");
1712                 push(@implContent, "{\n");
1713                 push(@implContent, "    ${className}* domObject = static_cast<$className*>(asObject(slotBase));\n");
1714
1715                 if ($dataNode->extendedAttributes->{"CheckDomainSecurity"}) {
1716                     push(@implContent, "    if (!domObject->allowsAccessFrom(exec))\n");
1717                     push(@implContent, "        return jsUndefined();\n");
1718                 }
1719
1720                 push(@implContent, "    return ${className}::getConstructor(exec, domObject->globalObject());\n");
1721                 push(@implContent, "}\n\n");
1722             }
1723         }
1724
1725         # Check if we have any writable attributes
1726         my $hasReadWriteProperties = 0;
1727         foreach my $attribute (@{$dataNode->attributes}) {
1728             $hasReadWriteProperties = 1 if $attribute->type !~ /^readonly/;
1729         }
1730
1731         my $hasSetter = $hasReadWriteProperties
1732                      || $dataNode->extendedAttributes->{"DelegatingPutFunction"}
1733                      || $dataNode->extendedAttributes->{"HasCustomIndexSetter"};
1734
1735         if ($hasSetter) {
1736             if (!$dataNode->extendedAttributes->{"CustomPutFunction"}) {
1737                 push(@implContent, "void ${className}::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)\n");
1738                 push(@implContent, "{\n");
1739                 push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
1740                 if ($dataNode->extendedAttributes->{"HasCustomIndexSetter"}) {
1741                     push(@implContent, "    bool ok;\n");
1742                     push(@implContent, "    unsigned index = propertyName.toUInt32(ok);\n");
1743                     push(@implContent, "    if (ok) {\n");
1744                     push(@implContent, "        indexSetter(exec, index, value);\n");
1745                     push(@implContent, "        return;\n");
1746                     push(@implContent, "    }\n");
1747                 }
1748                 if ($dataNode->extendedAttributes->{"DelegatingPutFunction"}) {
1749                     push(@implContent, "    if (putDelegate(exec, propertyName, value, slot))\n");
1750                     push(@implContent, "        return;\n");
1751                 }
1752
1753                 if ($hasReadWriteProperties) {
1754                     push(@implContent, "    lookupPut<$className, Base>(exec, propertyName, value, " . hashTableAccessor($dataNode->extendedAttributes->{"NoStaticTables"}, $className) . ", this, slot);\n");
1755                 } else {
1756                     push(@implContent, "    Base::put(exec, propertyName, value, slot);\n");
1757                 }
1758                 push(@implContent, "}\n\n");
1759             }
1760
1761             if ($dataNode->extendedAttributes->{"HasCustomIndexSetter"}) {
1762                 push(@implContent, "void ${className}::put(ExecState* exec, unsigned propertyName, JSValue value)\n");
1763                 push(@implContent, "{\n");
1764                 push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
1765                 push(@implContent, "    indexSetter(exec, propertyName, value);\n");
1766                 push(@implContent, "    return;\n");
1767                 push(@implContent, "}\n\n");
1768             }
1769
1770             if ($hasReadWriteProperties) {
1771                 foreach my $attribute (@{$dataNode->attributes}) {
1772                     if ($attribute->type !~ /^readonly/) {
1773                         my $name = $attribute->signature->name;
1774                         my $type = $codeGenerator->StripModule($attribute->signature->type);
1775                         my $putFunctionName = "setJS" . $interfaceName .  $codeGenerator->WK_ucfirst($name) . ($attribute->signature->type =~ /Constructor$/ ? "Constructor" : "");
1776                         my $implSetterFunctionName = $codeGenerator->WK_ucfirst($name);
1777
1778                         my $attributeConditionalString = GenerateConditionalString($attribute->signature);
1779                         push(@implContent, "#if ${attributeConditionalString}\n") if $attributeConditionalString;
1780
1781                         push(@implContent, "void ${putFunctionName}(ExecState* exec, JSObject* thisObject, JSValue value)\n");
1782                         push(@implContent, "{\n");
1783
1784                         if ($dataNode->extendedAttributes->{"CheckDomainSecurity"} && !$attribute->signature->extendedAttributes->{"DoNotCheckDomainSecurity"}) {
1785                             if ($interfaceName eq "DOMWindow") {
1786                                 push(@implContent, "    if (!static_cast<$className*>(thisObject)->allowsAccessFrom(exec))\n");
1787                             } else {
1788                                 push(@implContent, "    if (!allowsAccessFromFrame(exec, static_cast<$className*>(thisObject)->impl()->frame()))\n");
1789                             }
1790                             push(@implContent, "        return;\n");
1791                         }
1792
1793                         if ($attribute->signature->extendedAttributes->{"Custom"} || $attribute->signature->extendedAttributes->{"JSCCustom"} || $attribute->signature->extendedAttributes->{"CustomSetter"} || $attribute->signature->extendedAttributes->{"JSCCustomSetter"}) {
1794                             push(@implContent, "    static_cast<$className*>(thisObject)->set$implSetterFunctionName(exec, value);\n");
1795                         } elsif ($type eq "EventListener") {
1796                             $implIncludes{"JSEventListener.h"} = 1;
1797                             push(@implContent, "    UNUSED_PARAM(exec);\n");
1798                             push(@implContent, "    ${className}* castedThis = static_cast<${className}*>(thisObject);\n");
1799                             my $windowEventListener = $attribute->signature->extendedAttributes->{"WindowEventListener"};
1800                             if ($windowEventListener) {
1801                                 push(@implContent, "    JSDOMGlobalObject* globalObject = castedThis->globalObject();\n");
1802                             }
1803                             push(@implContent, "    $implClassName* imp = static_cast<$implClassName*>(castedThis->impl());\n");
1804                             if ((($interfaceName eq "DOMWindow") or ($interfaceName eq "WorkerContext")) and $name eq "onerror") {
1805                                 $implIncludes{"JSErrorHandler.h"} = 1;
1806                                 push(@implContent, "    imp->set$implSetterFunctionName(createJSErrorHandler(exec, value, thisObject));\n");
1807                             } else {
1808                                 push(@implContent, GenerateAttributeEventListenerCall($className, $implSetterFunctionName, $windowEventListener));
1809                             }
1810                         } elsif ($attribute->signature->type =~ /Constructor$/) {
1811                             my $constructorType = $attribute->signature->type;
1812                             $constructorType =~ s/Constructor$//;
1813                             if ($constructorType ne "DOMObject") {
1814                                 AddToImplIncludes("JS" . $constructorType . ".h", $attribute->signature->extendedAttributes->{"Conditional"});
1815                             }
1816                             push(@implContent, "    // Shadowing a built-in constructor\n");
1817                             if ($interfaceName eq "DOMWindow" && $className eq "JSblah") {
1818                                 # FIXME: This branch never executes and should be removed.
1819                                 push(@implContent, "    static_cast<$className*>(thisObject)->putDirect(exec->globalData(), exec->propertyNames().constructor, value);\n");
1820                             } else {
1821                                 push(@implContent, "    static_cast<$className*>(thisObject)->putDirect(exec->globalData(), Identifier(exec, \"$name\"), value);\n");
1822                             }
1823                         } elsif ($attribute->signature->extendedAttributes->{"Replaceable"}) {
1824                             push(@implContent, "    // Shadowing a built-in object\n");
1825                             push(@implContent, "    static_cast<$className*>(thisObject)->putDirect(exec->globalData(), Identifier(exec, \"$name\"), value);\n");
1826                         } else {
1827                             push(@implContent, "    $className* castedThis = static_cast<$className*>(thisObject);\n");
1828                             push(@implContent, "    $implType* imp = static_cast<$implType*>(castedThis->impl());\n");
1829                             push(@implContent, "    ExceptionCode ec = 0;\n") if @{$attribute->setterExceptions};
1830
1831                             # If the "StrictTypeChecking" extended attribute is present, and the attribute's type is an
1832                             # interface type, then if the incoming value does not implement that interface, a TypeError
1833                             # is thrown rather than silently passing NULL to the C++ code.
1834                             # Per the Web IDL and ECMAScript specifications, incoming values can always be converted to
1835                             # both strings and numbers, so do not throw TypeError if the attribute is of these types.
1836                             if ($attribute->signature->extendedAttributes->{"StrictTypeChecking"}) {
1837                                 $implIncludes{"<runtime/Error.h>"} = 1;
1838
1839                                 my $argType = $attribute->signature->type;
1840                                 if (!IsNativeType($argType)) {
1841                                     push(@implContent, "    if (!value.isUndefinedOrNull() && !value.inherits(&JS${argType}::s_info)) {\n");
1842                                     push(@implContent, "        throwVMTypeError(exec);\n");
1843                                     push(@implContent, "        return;\n");
1844                                     push(@implContent, "    };\n");
1845                                 }
1846                             }
1847
1848                             my $nativeValue = JSValueToNative($attribute->signature, "value");
1849                             if ($svgPropertyOrListPropertyType) {
1850                                 if ($svgPropertyType) {
1851                                     push(@implContent, "    if (imp->role() == AnimValRole) {\n");
1852                                     push(@implContent, "        setDOMException(exec, NO_MODIFICATION_ALLOWED_ERR);\n");
1853                                     push(@implContent, "        return;\n");
1854                                     push(@implContent, "    }\n");
1855                                 }
1856                                 push(@implContent, "    $svgPropertyOrListPropertyType& podImp = imp->propertyReference();\n");
1857                                 if ($svgPropertyOrListPropertyType eq "float") { # Special case for JSSVGNumber
1858                                     push(@implContent, "    podImp = $nativeValue;\n");
1859                                 } else {
1860                                     push(@implContent, "    podImp.set$implSetterFunctionName($nativeValue");
1861                                     push(@implContent, ", ec") if @{$attribute->setterExceptions};
1862                                     push(@implContent, ");\n");
1863                                     push(@implContent, "    setDOMException(exec, ec);\n") if @{$attribute->setterExceptions};
1864                                 }
1865                                 if ($svgPropertyType) {
1866                                     if (@{$attribute->setterExceptions}) {
1867                                         push(@implContent, "    if (!ec)\n"); 
1868                                         push(@implContent, "        imp->commitChange();\n");
1869                                     } else {
1870                                         push(@implContent, "    imp->commitChange();\n");
1871                                     }
1872                                 }
1873                             } else {
1874                                 my $setterExpressionPrefix = $codeGenerator->SetterExpressionPrefix(\%implIncludes, $interfaceName, $attribute);
1875                                 push(@implContent, "    imp->$setterExpressionPrefix$nativeValue");
1876                                 push(@implContent, ", ec") if @{$attribute->setterExceptions};
1877                                 push(@implContent, ");\n");
1878                                 push(@implContent, "    setDOMException(exec, ec);\n") if @{$attribute->setterExceptions};
1879                             }
1880                         }
1881                         
1882                         push(@implContent, "}\n\n");
1883
1884                         push(@implContent, "#endif\n") if $attributeConditionalString;
1885
1886                         push(@implContent, "\n");
1887                     }
1888                 }
1889             }
1890             
1891             if ($dataNode->extendedAttributes->{"ReplaceableConstructor"}) {
1892                 my $constructorFunctionName = "setJS" . $interfaceName . "Constructor";
1893                 
1894                 push(@implContent, "void ${constructorFunctionName}(ExecState* exec, JSObject* thisObject, JSValue value)\n");
1895                 push(@implContent, "{\n");
1896                 if ($dataNode->extendedAttributes->{"CheckDomainSecurity"}) {
1897                     if ($interfaceName eq "DOMWindow") {
1898                         push(@implContent, "    if (!static_cast<$className*>(thisObject)->allowsAccessFrom(exec))\n");
1899                     } else {
1900                         push(@implContent, "    if (!allowsAccessFromFrame(exec, static_cast<$className*>(thisObject)->impl()->frame()))\n");
1901                     }
1902                     push(@implContent, "        return;\n");
1903                 }
1904
1905                 push(@implContent, "    // Shadowing a built-in constructor\n");
1906
1907                 if ($interfaceName eq "DOMWindow") {
1908                     push(@implContent, "    static_cast<$className*>(thisObject)->putDirect(exec->globalData(), exec->propertyNames().constructor, value);\n");
1909                 } else {
1910                     die "No way to handle interface with ReplaceableConstructor extended attribute: $interfaceName";
1911                 }
1912                 push(@implContent, "}\n\n");
1913             }        
1914         }
1915     }
1916
1917     if (($dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) && !$dataNode->extendedAttributes->{"CustomGetPropertyNames"}) {
1918         push(@implContent, "void ${className}::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)\n");
1919         push(@implContent, "{\n");
1920         push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
1921         if ($dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasCustomIndexGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
1922             push(@implContent, "    for (unsigned i = 0; i < static_cast<${implClassName}*>(impl())->length(); ++i)\n");
1923             push(@implContent, "        propertyNames.add(Identifier::from(exec, i));\n");
1924         }
1925         push(@implContent, "     Base::getOwnPropertyNames(exec, propertyNames, mode);\n");
1926         push(@implContent, "}\n\n");
1927     }
1928
1929     if (!$dataNode->extendedAttributes->{"OmitConstructor"}) {
1930         push(@implContent, "JSValue ${className}::getConstructor(ExecState* exec, JSGlobalObject* globalObject)\n{\n");
1931         push(@implContent, "    return getDOMConstructor<${className}Constructor>(exec, static_cast<JSDOMGlobalObject*>(globalObject));\n");
1932         push(@implContent, "}\n\n");
1933     }
1934
1935     # Functions
1936     if ($numFunctions > 0) {
1937         foreach my $function (@{$dataNode->functions}) {
1938             AddIncludesForTypeInImpl($function->signature->type);
1939
1940             my $functionName = $codeGenerator->WK_lcfirst($className) . "PrototypeFunction" . $codeGenerator->WK_ucfirst($function->signature->name);
1941
1942             if ($function->{overloads} && @{$function->{overloads}} > 1) {
1943                 # Append a number to an overloaded method's name to make it unique:
1944                 $functionName = $functionName . $function->{overloadIndex};
1945                 # Make this function static to avoid compiler warnings, since we
1946                 # don't generate a prototype for it in the header.
1947                 push(@implContent, "static ");
1948             }
1949             
1950             my $functionImplementationName = $function->signature->extendedAttributes->{"ImplementationFunction"} || $codeGenerator->WK_lcfirst($function->signature->name);
1951
1952             my $conditional = $function->signature->extendedAttributes->{"Conditional"};
1953             if ($conditional) {
1954                 my $conditionalString = GenerateConditionalStringFromAttributeValue($conditional);
1955                 push(@implContent, "#if ${conditionalString}\n");
1956             }
1957
1958             push(@implContent, "EncodedJSValue JSC_HOST_CALL ${functionName}(ExecState* exec)\n");
1959             push(@implContent, "{\n");
1960
1961             $implIncludes{"<runtime/Error.h>"} = 1;
1962
1963             if ($interfaceName eq "DOMWindow") {
1964                 push(@implContent, "    $className* castedThis = toJSDOMWindow(exec->hostThisValue().toThisObject(exec));\n");
1965                 push(@implContent, "    if (!castedThis)\n");
1966                 push(@implContent, "        return throwVMTypeError(exec);\n");
1967             } elsif ($dataNode->extendedAttributes->{"IsWorkerContext"}) {
1968                 push(@implContent, "    $className* castedThis = to${className}(exec->hostThisValue().toThisObject(exec));\n");
1969                 push(@implContent, "    if (!castedThis)\n");
1970                 push(@implContent, "        return throwVMTypeError(exec);\n");
1971             } else {
1972                 push(@implContent, "    JSValue thisValue = exec->hostThisValue();\n");
1973                 push(@implContent, "    if (!thisValue.inherits(&${className}::s_info))\n");
1974                 push(@implContent, "        return throwVMTypeError(exec);\n");
1975                 push(@implContent, "    $className* castedThis = static_cast<$className*>(asObject(thisValue));\n");
1976             }
1977             push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(castedThis, &${className}::s_info);\n");
1978
1979             if ($dataNode->extendedAttributes->{"CheckDomainSecurity"} && 
1980                 !$function->signature->extendedAttributes->{"DoNotCheckDomainSecurity"}) {
1981                 push(@implContent, "    if (!castedThis->allowsAccessFrom(exec))\n");
1982                 push(@implContent, "        return JSValue::encode(jsUndefined());\n");
1983             }
1984
1985             if ($function->signature->extendedAttributes->{"Custom"} || $function->signature->extendedAttributes->{"JSCCustom"}) {
1986                 push(@implContent, "    return JSValue::encode(castedThis->" . $functionImplementationName . "(exec));\n");
1987             } else {
1988                 push(@implContent, "    $implType* imp = static_cast<$implType*>(castedThis->impl());\n");
1989                 if ($svgPropertyType) {
1990                     push(@implContent, "    if (imp->role() == AnimValRole) {\n");
1991                     push(@implContent, "        setDOMException(exec, NO_MODIFICATION_ALLOWED_ERR);\n");
1992                     push(@implContent, "        return JSValue::encode(jsUndefined());\n");
1993                     push(@implContent, "    }\n");
1994                     push(@implContent, "    $svgPropertyType& podImp = imp->propertyReference();\n");
1995                 }
1996
1997                 my $numParameters = @{$function->parameters};
1998
1999                 my $requiresAllArguments;
2000                 my $requiresAllArgumentsDefault = "";
2001                 if (!$dataNode->extendedAttributes->{"LegacyDefaultOptionalArguments"}) {
2002                     $requiresAllArgumentsDefault = "Raise";
2003                 }
2004                 $requiresAllArguments = $function->signature->extendedAttributes->{"RequiresAllArguments"} || $requiresAllArgumentsDefault;
2005                 if ($requiresAllArguments) {
2006                     my $numMandatoryParams = @{$function->parameters};
2007                     foreach my $param (reverse(@{$function->parameters})) {
2008                         if ($param->extendedAttributes->{"Optional"}) {
2009                             $numMandatoryParams--;
2010                         } else {
2011                             last;
2012                         }
2013                     }
2014                     if ($numMandatoryParams > 0)
2015                     {
2016                         push(@implContent, "    if (exec->argumentCount() < $numMandatoryParams)\n");
2017                         if ($requiresAllArguments eq "Raise") {
2018                             push(@implContent, "        return throwVMError(exec, createTypeError(exec, \"Not enough arguments\"));\n");
2019                         } else {
2020                             push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2021                         }
2022                     }
2023                 }
2024
2025                 if (@{$function->raisesExceptions}) {
2026                     push(@implContent, "    ExceptionCode ec = 0;\n");
2027                 }
2028
2029                 if ($function->signature->extendedAttributes->{"SVGCheckSecurityDocument"}) {
2030                     push(@implContent, "    if (!checkNodeSecurity(exec, imp->getSVGDocument(" . (@{$function->raisesExceptions} ? "ec" : "") .")))\n");
2031                     push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2032                     $implIncludes{"JSDOMBinding.h"} = 1;
2033                 }
2034
2035                 if ($function->signature->name eq "addEventListener") {
2036                     push(@implContent, GenerateEventListenerCall($className, "add"));
2037                 } elsif ($function->signature->name eq "removeEventListener") {
2038                     push(@implContent, GenerateEventListenerCall($className, "remove"));
2039                 } else {
2040                     my $argsIndex = 0;
2041                     my $paramIndex = 0;
2042                     my $functionString = (($svgPropertyOrListPropertyType and !$svgListPropertyType) ? "podImp." : "imp->") . $functionImplementationName . "(";
2043                     my $hasOptionalArguments = 0;
2044
2045                     if ($function->signature->extendedAttributes->{"CustomArgumentHandling"}) {
2046                         push(@implContent, "    RefPtr<ScriptArguments> scriptArguments(createScriptArguments(exec, $numParameters));\n");
2047                         push(@implContent, "    size_t maxStackSize = imp->shouldCaptureFullStackTrace() ? ScriptCallStack::maxCallStackSizeToCapture : 1;\n");
2048                         push(@implContent, "    RefPtr<ScriptCallStack> callStack(createScriptCallStack(exec, maxStackSize));\n");
2049                         $implIncludes{"ScriptArguments.h"} = 1;
2050                         $implIncludes{"ScriptCallStack.h"} = 1;
2051                         $implIncludes{"ScriptCallStackFactory.h"} = 1;
2052                     }
2053
2054                     my $callWith = $function->signature->extendedAttributes->{"CallWith"};
2055                     if ($callWith) {
2056                         my $callWithArg = "COMPILE_ASSERT(false)";
2057                         if ($callWith eq "DynamicFrame") {
2058                             push(@implContent, "    Frame* dynamicFrame = toDynamicFrame(exec);\n");
2059                             push(@implContent, "    if (!dynamicFrame)\n");
2060                             push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2061                             $callWithArg = "dynamicFrame";
2062                         } elsif ($callWith eq "ScriptState") {
2063                             $callWithArg = "exec";
2064                         } elsif ($callWith eq "ScriptExecutionContext") {
2065                             push(@implContent, "    ScriptExecutionContext* scriptContext = static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject())->scriptExecutionContext();\n");
2066                             push(@implContent, "    if (!scriptContext)\n");
2067                             push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2068                             $callWithArg = "scriptContext"; 
2069                         }
2070                         $functionString .= ", " if $paramIndex;
2071                         $functionString .= $callWithArg;
2072                         $paramIndex++;
2073                     }
2074
2075                     $implIncludes{"ExceptionCode.h"} = 1;
2076                     $implIncludes{"JSDOMBinding.h"} = 1;
2077                     foreach my $parameter (@{$function->parameters}) {
2078                         # Optional callbacks should be treated differently, because they always have a default value (0),
2079                         # and we can reduce the number of overloaded functions that take a different number of parameters.
2080                         # Optional arguments with default values [Optional=CallWithDefaultValue] should not generate an early call.
2081                         my $optional = $parameter->extendedAttributes->{"Optional"};        
2082                         if ($optional && $optional ne "CallWithDefaultValue" && !$parameter->extendedAttributes->{"Callback"}) {
2083                             # Generate early call if there are enough parameters.
2084                             if (!$hasOptionalArguments) {
2085                                 push(@implContent, "\n    size_t argsCount = exec->argumentCount();\n");
2086                                 $hasOptionalArguments = 1;
2087                             }
2088                             push(@implContent, "    if (argsCount <= $argsIndex) {\n");
2089                             GenerateImplementationFunctionCall($function, $functionString, $paramIndex, "    " x 2, $svgPropertyType, $implClassName);
2090                             push(@implContent, "    }\n\n");
2091                         }
2092
2093                         my $name = $parameter->name;
2094                         my $argType = $codeGenerator->StripModule($parameter->type);
2095
2096                         if ($argType eq "XPathNSResolver") {
2097                             push(@implContent, "    RefPtr<XPathNSResolver> customResolver;\n");
2098                             push(@implContent, "    XPathNSResolver* resolver = toXPathNSResolver(exec->argument($argsIndex));\n");
2099                             push(@implContent, "    if (!resolver) {\n");
2100                             push(@implContent, "        customResolver = JSCustomXPathNSResolver::create(exec, exec->argument($argsIndex));\n");
2101                             push(@implContent, "        if (exec->hadException())\n");
2102                             push(@implContent, "            return JSValue::encode(jsUndefined());\n");
2103                             push(@implContent, "        resolver = customResolver.get();\n");
2104                             push(@implContent, "    }\n");
2105                         } elsif ($parameter->extendedAttributes->{"Callback"}) {
2106                             my $callbackClassName = GetCallbackClassName($argType);
2107                             $implIncludes{"$callbackClassName.h"} = 1;
2108                             if ($parameter->extendedAttributes->{"Optional"}) {
2109                                 push(@implContent, "    RefPtr<$argType> $name;\n");
2110                                 push(@implContent, "    if (exec->argumentCount() > $argsIndex && !exec->argument($argsIndex).isUndefinedOrNull()) {\n");
2111                                 push(@implContent, "        if (!exec->argument($argsIndex).isObject()) {\n");
2112                                 push(@implContent, "            setDOMException(exec, TYPE_MISMATCH_ERR);\n");
2113                                 push(@implContent, "            return JSValue::encode(jsUndefined());\n");
2114                                 push(@implContent, "        }\n");
2115                                 push(@implContent, "        $name = ${callbackClassName}::create(asObject(exec->argument($argsIndex)), castedThis->globalObject());\n");
2116                                 push(@implContent, "    }\n");
2117                             } else {
2118                                 push(@implContent, "    if (exec->argumentCount() <= $argsIndex || !exec->argument($argsIndex).isObject()) {\n");
2119                                 push(@implContent, "        setDOMException(exec, TYPE_MISMATCH_ERR);\n");
2120                                 push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2121                                 push(@implContent, "    }\n");
2122                                 push(@implContent, "    RefPtr<$argType> $name = ${callbackClassName}::create(asObject(exec->argument($argsIndex)), castedThis->globalObject());\n");
2123                             }
2124                         } else {
2125                             # If the "StrictTypeChecking" extended attribute is present, and the argument's type is an
2126                             # interface type, then if the incoming value does not implement that interface, a TypeError
2127                             # is thrown rather than silently passing NULL to the C++ code.
2128                             # Per the Web IDL and ECMAScript semantics, incoming values can always be converted to both
2129                             # strings and numbers, so do not throw TypeError if the argument is of these types.
2130                             if ($function->signature->extendedAttributes->{"StrictTypeChecking"}) {
2131                                 $implIncludes{"<runtime/Error.h>"} = 1;
2132
2133                                 my $argValue = "exec->argument($argsIndex)";
2134                                 if (!IsNativeType($argType)) {
2135                                     push(@implContent, "    if (exec->argumentCount() > $argsIndex && !${argValue}.isUndefinedOrNull() && !${argValue}.inherits(&JS${argType}::s_info))\n");
2136                                     push(@implContent, "        return throwVMTypeError(exec);\n");
2137                                 }
2138                             }
2139
2140                             push(@implContent, "    " . GetNativeTypeFromSignature($parameter) . " $name(" . JSValueToNative($parameter, "exec->argument($argsIndex)") . ");\n");
2141
2142                             # If a parameter is "an index" and it's negative it should throw an INDEX_SIZE_ERR exception.
2143                             # But this needs to be done in the bindings, because the type is unsigned and the fact that it
2144                             # was negative will be lost by the time we're inside the DOM.
2145                             if ($parameter->extendedAttributes->{"IsIndex"}) {
2146                                 push(@implContent, "    if ($name < 0) {\n");
2147                                 push(@implContent, "        setDOMException(exec, INDEX_SIZE_ERR);\n");
2148                                 push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2149                                 push(@implContent, "    }\n");
2150                             }
2151
2152                             # Check if the type conversion succeeded.
2153                             push(@implContent, "    if (exec->hadException())\n");
2154                             push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2155
2156                             if ($codeGenerator->IsSVGTypeNeedingTearOff($argType) and not $implClassName =~ /List$/) {
2157                                 push(@implContent, "    if (!$name) {\n");
2158                                 push(@implContent, "        setDOMException(exec, TYPE_MISMATCH_ERR);\n");
2159                                 push(@implContent, "        return JSValue::encode(jsUndefined());\n");
2160                                 push(@implContent, "    }\n");
2161                             }
2162                         }
2163
2164                         $functionString .= ", " if $paramIndex;
2165
2166                         if ($argType eq "NodeFilter") {
2167                             $functionString .= "$name.get()";
2168                         } elsif ($codeGenerator->IsSVGTypeNeedingTearOff($argType) and not $implClassName =~ /List$/) {
2169                             $functionString .= "$name->propertyReference()";
2170                         } else {
2171                             $functionString .= $name;
2172                         }
2173                         $argsIndex++;
2174                         $paramIndex++;
2175                     }
2176
2177                     if ($function->signature->extendedAttributes->{"NeedsUserGestureCheck"}) {
2178                         $functionString .= ", " if $paramIndex;
2179                         $functionString .= "ScriptController::processingUserGesture()";
2180                         $paramIndex++;
2181                         $implIncludes{"ScriptController.h"} = 1;
2182                     }
2183
2184                     push(@implContent, "\n");
2185                     GenerateImplementationFunctionCall($function, $functionString, $paramIndex, "    ", $svgPropertyType, $implClassName);
2186                 }
2187             }
2188
2189             push(@implContent, "}\n\n");
2190
2191             if ($function->{overloads} && @{$function->{overloads}} > 1 && $function->{overloadIndex} == @{$function->{overloads}}) {
2192                 # Generate a function dispatching call to the rest of the overloads.
2193                 GenerateOverloadedPrototypeFunction($function, $dataNode, $implClassName);
2194             }
2195
2196             if ($conditional) {
2197                 push(@implContent, "#endif\n\n");
2198             }
2199         }
2200         
2201         if ($needsMarkChildren && !$dataNode->extendedAttributes->{"CustomMarkFunction"}) {
2202             push(@implContent, "void ${className}::visitChildren(JSCell* cell, SlotVisitor& visitor)\n");
2203             push(@implContent, "{\n");
2204             push(@implContent, "    ${className}* thisObject = static_cast<${className}*>(cell);\n");
2205             push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);\n");
2206             push(@implContent, "    COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);\n");
2207             push(@implContent, "    ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());\n");
2208             push(@implContent, "    Base::visitChildren(thisObject, visitor);\n");
2209             if ($dataNode->extendedAttributes->{"EventTarget"}) {
2210                 push(@implContent, "    thisObject->impl()->visitJSEventListeners(visitor);\n");
2211             }
2212             if ($numCachedAttributes > 0) {
2213                 foreach (@{$dataNode->attributes}) {
2214                     my $attribute = $_;
2215                     if ($attribute->signature->extendedAttributes->{"CachedAttribute"}) {
2216                         push(@implContent, "    if (thisObject->m_" . $attribute->signature->name . ")\n");
2217                         push(@implContent, "        visitor.append(&thisObject->m_" . $attribute->signature->name . ");\n");
2218                     }
2219                 }
2220             }
2221             push(@implContent, "}\n\n");
2222         }
2223         die "Can't generate binding for class with cached attribute and custom mark." if (($numCachedAttributes > 0) and ($dataNode->extendedAttributes->{"CustomMarkFunction"}));
2224     }
2225
2226     if ($numConstants > 0) {
2227         push(@implContent, "// Constant getters\n\n");
2228
2229         foreach my $constant (@{$dataNode->constants}) {
2230             my $getter = "js" . $interfaceName . $codeGenerator->WK_ucfirst($constant->name);
2231
2232             # FIXME: this casts into int to match our previous behavior which turned 0xFFFFFFFF in -1 for NodeFilter.SHOW_ALL
2233             push(@implContent, "JSValue ${getter}(ExecState* exec, JSValue, const Identifier&)\n");
2234             push(@implContent, "{\n");
2235             if ($constant->type eq "DOMString") {
2236                 push(@implContent, "    return jsStringOrNull(exec, String(" . $constant->value . "));\n");
2237             } else {
2238                 push(@implContent, "    UNUSED_PARAM(exec);\n");
2239                 push(@implContent, "    return jsNumber(static_cast<int>(" . $constant->value . "));\n");
2240             }
2241             push(@implContent, "}\n\n");
2242         }
2243     }
2244
2245     if ($dataNode->extendedAttributes->{"HasIndexGetter"}) {
2246         push(@implContent, "\nJSValue ${className}::indexGetter(ExecState* exec, JSValue slotBase, unsigned index)\n");
2247         push(@implContent, "{\n");
2248         push(@implContent, "    ${className}* thisObj = static_cast<$className*>(asObject(slotBase));\n");
2249         push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(thisObj, &s_info);\n");
2250         if (IndexGetterReturnsStrings($implClassName)) {
2251             $implIncludes{"KURL.h"} = 1;
2252             push(@implContent, "    return jsStringOrNull(exec, thisObj->impl()->item(index));\n");
2253         } else {
2254             push(@implContent, "    return toJS(exec, thisObj->globalObject(), static_cast<$implClassName*>(thisObj->impl())->item(index));\n");
2255         }
2256         push(@implContent, "}\n\n");
2257         if ($interfaceName eq "HTMLCollection" or $interfaceName eq "HTMLAllCollection") {
2258             $implIncludes{"JSNode.h"} = 1;
2259             $implIncludes{"Node.h"} = 1;
2260         }
2261     }
2262     
2263     if ($dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
2264         push(@implContent, "\nJSValue ${className}::getByIndex(ExecState*, unsigned index)\n");
2265         push(@implContent, "{\n");
2266         push(@implContent, "    ASSERT_GC_OBJECT_INHERITS(this, &s_info);\n");
2267         push(@implContent, "    double result = static_cast<$implClassName*>(impl())->item(index);\n");
2268         # jsNumber conversion doesn't suppress signalling NaNs, so enforce that here.
2269         push(@implContent, "    if (isnan(result))\n");
2270         push(@implContent, "        return jsNaN();\n");
2271         push(@implContent, "    return JSValue(result);\n");
2272         push(@implContent, "}\n\n");
2273         if ($interfaceName eq "HTMLCollection" or $interfaceName eq "HTMLAllCollection") {
2274             $implIncludes{"JSNode.h"} = 1;
2275             $implIncludes{"Node.h"} = 1;
2276         }
2277     }
2278
2279     if ($dataNode->extendedAttributes->{"GenerateIsReachable"} || $dataNode->extendedAttributes->{"ActiveDOMObject"}) {
2280         push(@implContent, "static inline bool isObservable(JS${implType}* js${implType})\n");
2281         push(@implContent, "{\n");
2282         push(@implContent, "    if (js${implType}->hasCustomProperties())\n");
2283         push(@implContent, "        return true;\n");
2284         if ($eventTarget) {
2285             push(@implContent, "    if (js${implType}->impl()->hasEventListeners())\n");
2286             push(@implContent, "        return true;\n");
2287         }
2288         push(@implContent, "    return false;\n");
2289         push(@implContent, "}\n\n");
2290
2291         push(@implContent, "bool JS${implType}Owner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)\n");
2292         push(@implContent, "{\n");
2293         push(@implContent, "    JS${implType}* js${implType} = static_cast<JS${implType}*>(handle.get().asCell());\n");
2294         # All ActiveDOMObjects implement hasPendingActivity(), but not all of them
2295         # increment their C++ reference counts when hasPendingActivity() becomes
2296         # true. As a result, ActiveDOMObjects can be prematurely destroyed before
2297         # their pending activities complete. To wallpaper over this bug, JavaScript
2298         # wrappers unconditionally keep ActiveDOMObjects with pending activity alive.
2299         # FIXME: Fix this lifetime issue in the DOM, and move this hasPendingActivity
2300         # check below the isObservable check.
2301         if ($dataNode->extendedAttributes->{"ActiveDOMObject"}) {
2302             push(@implContent, "    if (js${implType}->impl()->hasPendingActivity())\n");
2303             push(@implContent, "        return true;\n");
2304         }
2305         push(@implContent, "    if (!isObservable(js${implType}))\n");
2306         push(@implContent, "        return false;\n");
2307         if ($dataNode->extendedAttributes->{"GenerateIsReachable"}) {
2308             my $rootString;
2309             if ($dataNode->extendedAttributes->{"GenerateIsReachable"} eq "Impl") {
2310                 $rootString  = "    ${implType}* root = js${implType}->impl();\n";
2311             } elsif ($dataNode->extendedAttributes->{"GenerateIsReachable"} eq "ImplContext") {
2312                 $rootString  = "    WebGLRenderingContext* root = js${implType}->impl()->context();\n";
2313             } elsif ($dataNode->extendedAttributes->{"GenerateIsReachable"} eq "ImplFrame") {
2314                 $rootString  = "    Frame* root = js${implType}->impl()->frame();\n";
2315                 $rootString .= "    if (!root)\n";
2316                 $rootString .= "        return false;\n";
2317             } elsif ($dataNode->extendedAttributes->{"GenerateIsReachable"} eq "ImplDocument") {
2318                 $rootString  = "    Document* root = js${implType}->impl()->document();\n";
2319                 $rootString .= "    if (!root)\n";
2320                 $rootString .= "        return false;\n";
2321             } elsif ($dataNode->extendedAttributes->{"GenerateIsReachable"} eq "ImplElementRoot") {
2322                 $rootString  = "    Element* element = js${implType}->impl()->element();\n";
2323                 $rootString .= "    if (!element)\n";
2324                 $rootString .= "        return false;\n";
2325                 $rootString .= "    void* root = WebCore::root(element);\n";
2326             } elsif ($interfaceName eq "CanvasRenderingContext") {
2327                 $rootString  = "    void* root = WebCore::root(js${implType}->impl()->canvas());\n";
2328             } elsif ($interfaceName eq "HTMLCollection") {
2329                 $rootString  = "    void* root = WebCore::root(js${implType}->impl()->base());\n";
2330             } else {
2331                 $rootString  = "    void* root = WebCore::root(js${implType}->impl());\n";
2332             }
2333
2334             push(@implContent, $rootString);
2335             push(@implContent, "    return visitor.containsOpaqueRoot(root);\n");
2336         } else {
2337             push(@implContent, "    UNUSED_PARAM(visitor);\n");
2338             push(@implContent, "    return false;\n");
2339         }
2340         push(@implContent, "}\n\n");
2341     }
2342
2343     if (!$dataNode->extendedAttributes->{"CustomFinalize"} &&
2344         ($dataNode->extendedAttributes->{"GenerateIsReachable"} || 
2345          $dataNode->extendedAttributes->{"CustomIsReachable"} ||
2346          $dataNode->extendedAttributes->{"ActiveDOMObject"})) {
2347         push(@implContent, "void JS${implType}Owner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)\n");
2348         push(@implContent, "{\n");
2349         push(@implContent, "    JS${implType}* js${implType} = static_cast<JS${implType}*>(handle.get().asCell());\n");
2350         push(@implContent, "    DOMWrapperWorld* world = static_cast<DOMWrapperWorld*>(context);\n");
2351         push(@implContent, "    uncacheWrapper(world, js${implType}->impl(), js${implType});\n");
2352         push(@implContent, "}\n\n");
2353     }
2354
2355     if ((!$hasParent or $dataNode->extendedAttributes->{"GenerateToJS"}) and !$dataNode->extendedAttributes->{"CustomToJS"}) {
2356         push(@implContent, "JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, $implType* impl)\n");
2357         push(@implContent, "{\n");
2358         if ($svgPropertyType) {
2359             push(@implContent, "    return wrap<$className, $implType>(exec, globalObject, impl);\n");
2360         } else {
2361             push(@implContent, "    return wrap<$className>(exec, globalObject, impl);\n");
2362         }
2363         push(@implContent, "}\n\n");
2364     }
2365
2366     if ((!$hasParent or $dataNode->extendedAttributes->{"GenerateNativeConverter"}) and !$dataNode->extendedAttributes->{"CustomNativeConverter"}) {
2367         push(@implContent, "$implType* to${interfaceName}(JSC::JSValue value)\n");
2368         push(@implContent, "{\n");
2369         push(@implContent, "    return value.inherits(&${className}::s_info) ? static_cast<$className*>(asObject(value))->impl() : 0");
2370         push(@implContent, ";\n}\n");
2371     }
2372
2373     push(@implContent, "\n}\n");
2374
2375     my $conditionalString = GenerateConditionalString($dataNode);
2376     push(@implContent, "\n#endif // ${conditionalString}\n") if $conditionalString;
2377 }
2378
2379 sub GenerateCallbackHeader
2380 {
2381     my $object = shift;
2382     my $dataNode = shift;
2383
2384     my $interfaceName = $dataNode->name;
2385     my $className = "JS$interfaceName";
2386
2387     # - Add default header template and header protection
2388     push(@headerContentHeader, GenerateHeaderContentHeader($dataNode));
2389
2390     $headerIncludes{"ActiveDOMCallback.h"} = 1;
2391     $headerIncludes{"$interfaceName.h"} = 1;
2392     $headerIncludes{"JSCallbackData.h"} = 1;
2393     $headerIncludes{"<wtf/Forward.h>"} = 1;
2394
2395     push(@headerContent, "\nnamespace WebCore {\n\n");
2396     push(@headerContent, "class $className : public $interfaceName, public ActiveDOMCallback {\n");
2397     push(@headerContent, "public:\n");
2398
2399     # The static create() method.
2400     push(@headerContent, "    static PassRefPtr<$className> create(JSC::JSObject* callback, JSDOMGlobalObject* globalObject)\n");
2401     push(@headerContent, "    {\n");
2402     push(@headerContent, "        return adoptRef(new $className(callback, globalObject));\n");
2403     push(@headerContent, "    }\n\n");
2404
2405     # Destructor
2406     push(@headerContent, "    virtual ~$className();\n");
2407
2408     # Functions
2409     my $numFunctions = @{$dataNode->functions};
2410     if ($numFunctions > 0) {
2411         push(@headerContent, "\n    // Functions\n");
2412         foreach my $function (@{$dataNode->functions}) {
2413             my @params = @{$function->parameters};
2414             if (!$function->signature->extendedAttributes->{"Custom"} &&
2415                 !(GetNativeType($function->signature->type) eq "bool")) {
2416                 push(@headerContent, "    COMPILE_ASSERT(false)");
2417             }
2418
2419             push(@headerContent, "    virtual " . GetNativeType($function->signature->type) . " " . $function->signature->name . "(");
2420
2421             my @args = ();
2422             foreach my $param (@params) {
2423                 push(@args, GetNativeType($param->type) . " " . $param->name);
2424             }
2425             push(@headerContent, join(", ", @args));
2426
2427             push(@headerContent, ");\n");
2428         }
2429     }
2430
2431     push(@headerContent, "\nprivate:\n");
2432
2433     # Constructor
2434     push(@headerContent, "    $className(JSC::JSObject* callback, JSDOMGlobalObject*);\n\n");
2435
2436     # Private members
2437     push(@headerContent, "    JSCallbackData* m_data;\n");
2438     push(@headerContent, "};\n\n");
2439
2440     push(@headerContent, "} // namespace WebCore\n\n");
2441     my $conditionalString = GenerateConditionalString($dataNode);
2442     push(@headerContent, "#endif // ${conditionalString}\n\n") if $conditionalString;
2443     push(@headerContent, "#endif\n");
2444 }
2445
2446 sub GenerateCallbackImplementation
2447 {
2448     my ($object, $dataNode) = @_;
2449
2450     my $interfaceName = $dataNode->name;
2451     my $className = "JS$interfaceName";
2452
2453     # - Add default header template
2454     push(@implContentHeader, GenerateImplementationContentHeader($dataNode));
2455
2456     $implIncludes{"ScriptExecutionContext.h"} = 1;
2457     $implIncludes{"<runtime/JSLock.h>"} = 1;
2458
2459     @implContent = ();
2460
2461     push(@implContent, "\nusing namespace JSC;\n\n");
2462     push(@implContent, "namespace WebCore {\n\n");
2463
2464     # Constructor
2465     push(@implContent, "${className}::${className}(JSObject* callback, JSDOMGlobalObject* globalObject)\n");
2466     push(@implContent, "    : ActiveDOMCallback(globalObject->scriptExecutionContext())\n");
2467     push(@implContent, "    , m_data(new JSCallbackData(callback, globalObject))\n");
2468     push(@implContent, "{\n");
2469     push(@implContent, "}\n\n");
2470
2471     # Destructor
2472     push(@implContent, "${className}::~${className}()\n");
2473     push(@implContent, "{\n");
2474     push(@implContent, "    ScriptExecutionContext* context = scriptExecutionContext();\n");
2475     push(@implContent, "    // When the context is destroyed, all tasks with a reference to a callback\n");
2476     push(@implContent, "    // should be deleted. So if the context is 0, we are on the context thread.\n");
2477     push(@implContent, "    if (!context || context->isContextThread())\n");
2478     push(@implContent, "        delete m_data;\n");
2479     push(@implContent, "    else\n");
2480     push(@implContent, "        context->postTask(DeleteCallbackDataTask::create(m_data));\n");
2481     push(@implContent, "#ifndef NDEBUG\n");
2482     push(@implContent, "    m_data = 0;\n");
2483     push(@implContent, "#endif\n");
2484     push(@implContent, "}\n");
2485
2486     # Functions
2487     my $numFunctions = @{$dataNode->functions};
2488     if ($numFunctions > 0) {
2489         push(@implContent, "\n// Functions\n");
2490         foreach my $function (@{$dataNode->functions}) {
2491             my @params = @{$function->parameters};
2492             if ($function->signature->extendedAttributes->{"Custom"} ||
2493                 !(GetNativeType($function->signature->type) eq "bool")) {
2494                 next;
2495             }
2496
2497             AddIncludesForTypeInImpl($function->signature->type);
2498             push(@implContent, "\n" . GetNativeType($function->signature->type) . " ${className}::" . $function->signature->name . "(");
2499
2500             my @args = ();
2501             foreach my $param (@params) {
2502                 AddIncludesForTypeInImpl($param->type, 1);
2503                 push(@args, GetNativeType($param->type) . " " . $param->name);
2504             }
2505             push(@implContent, join(", ", @args));
2506             push(@implContent, ")\n");
2507
2508             push(@implContent, "{\n");
2509             push(@implContent, "    if (!canInvokeCallback())\n");
2510             push(@implContent, "        return true;\n\n");
2511             push(@implContent, "    RefPtr<$className> protect(this);\n\n");
2512             push(@implContent, "    JSLock lock(SilenceAssertionsOnly);\n\n");
2513             push(@implContent, "    ExecState* exec = m_data->globalObject()->globalExec();\n");
2514             push(@implContent, "    MarkedArgumentBuffer args;\n");
2515
2516             foreach my $param (@params) {
2517                 my $paramName = $param->name;
2518                 if ($param->type eq "DOMString") {
2519                     push(@implContent, "    args.append(jsString(exec, ${paramName}));\n");
2520                 } else {
2521                     push(@implContent, "    args.append(toJS(exec, m_data->globalObject(), ${paramName}));\n");
2522                 }
2523             }
2524
2525             push(@implContent, "\n    bool raisedException = false;\n");
2526             push(@implContent, "    m_data->invokeCallback(args, &raisedException);\n");
2527             push(@implContent, "    return !raisedException;\n");
2528             push(@implContent, "}\n");
2529         }
2530     }
2531
2532     push(@implContent, "\n}\n");
2533     my $conditionalString = GenerateConditionalString($dataNode);
2534     push(@implContent, "\n#endif // ${conditionalString}\n") if $conditionalString;
2535 }
2536
2537 sub GenerateImplementationFunctionCall()
2538 {
2539     my $function = shift;
2540     my $functionString = shift;
2541     my $paramIndex = shift;
2542     my $indent = shift;
2543     my $svgPropertyType = shift;
2544     my $implClassName = shift;
2545
2546     if ($function->signature->extendedAttributes->{"CustomArgumentHandling"}) {
2547         $functionString .= ", " if $paramIndex;
2548         $paramIndex += 2;
2549         $functionString .= "scriptArguments, callStack";
2550     }
2551
2552     if (@{$function->raisesExceptions}) {
2553         $functionString .= ", " if $paramIndex;
2554         $functionString .= "ec";
2555     }
2556     $functionString .= ")";
2557
2558     if ($function->signature->type eq "void") {
2559         push(@implContent, $indent . "$functionString;\n");
2560         push(@implContent, $indent . "setDOMException(exec, ec);\n") if @{$function->raisesExceptions};
2561
2562         if ($svgPropertyType) {
2563             if (@{$function->raisesExceptions}) {
2564                 push(@implContent, $indent . "if (!ec)\n"); 
2565                 push(@implContent, $indent . "    imp->commitChange();\n");
2566             } else {
2567                 push(@implContent, $indent . "imp->commitChange();\n");
2568             }
2569         }
2570
2571         push(@implContent, $indent . "return JSValue::encode(jsUndefined());\n");
2572     } else {
2573         push(@implContent, "\n" . $indent . "JSC::JSValue result = " . NativeToJSValue($function->signature, 1, $implClassName, $functionString, "castedThis") . ";\n");
2574         push(@implContent, $indent . "setDOMException(exec, ec);\n") if @{$function->raisesExceptions};
2575
2576         my $callWith = $function->signature->extendedAttributes->{"CallWith"};
2577         if ($callWith and $callWith eq "ScriptState") {
2578             push(@implContent, $indent . "if (exec->hadException())\n");
2579             push(@implContent, $indent . "    return JSValue::encode(jsUndefined());\n");
2580         }
2581
2582         push(@implContent, $indent . "return JSValue::encode(result);\n");
2583     }
2584 }
2585
2586 sub GetNativeTypeFromSignature
2587 {
2588     my $signature = shift;
2589     my $type = $codeGenerator->StripModule($signature->type);
2590
2591     if ($type eq "unsigned long" and $signature->extendedAttributes->{"IsIndex"}) {
2592         # Special-case index arguments because we need to check that they aren't < 0.
2593         return "int";
2594     }
2595
2596     return GetNativeType($type);
2597 }
2598
2599 my %nativeType = (
2600     "CompareHow" => "Range::CompareHow",
2601     "DOMString" => "const String&",
2602     "DOMObject" => "ScriptValue",
2603     "NodeFilter" => "RefPtr<NodeFilter>",
2604     "SerializedScriptValue" => "RefPtr<SerializedScriptValue>",
2605     "IDBKey" => "RefPtr<IDBKey>",
2606     "boolean" => "bool",
2607     "double" => "double",
2608     "float" => "float",
2609     "short" => "short",
2610     "long" => "int",
2611     "unsigned long" => "unsigned",
2612     "unsigned short" => "unsigned short",
2613     "long long" => "long long",
2614     "unsigned long long" => "unsigned long long",
2615     "MediaQueryListListener" => "RefPtr<MediaQueryListListener>",
2616     "DOMTimeStamp" => "DOMTimeStamp"
2617 );
2618
2619 sub GetNativeType
2620 {
2621     my $type = shift;
2622
2623     my $svgNativeType = $codeGenerator->GetSVGTypeNeedingTearOff($type);
2624     return "${svgNativeType}*" if $svgNativeType;
2625     return $nativeType{$type} if exists $nativeType{$type};
2626
2627     # For all other types, the native type is a pointer with same type name as the IDL type.
2628     return "${type}*";
2629 }
2630
2631 sub GetSVGPropertyTypes
2632 {
2633     my $implType = shift;
2634
2635     my $svgPropertyType;
2636     my $svgListPropertyType;
2637     my $svgNativeType;
2638
2639     return ($svgPropertyType, $svgListPropertyType, $svgNativeType) if not $implType =~ /SVG/;
2640     
2641     $svgNativeType = $codeGenerator->GetSVGTypeNeedingTearOff($implType);
2642     return ($svgPropertyType, $svgListPropertyType, $svgNativeType) if not $svgNativeType;
2643
2644     # Append space to avoid compilation errors when using  PassRefPtr<$svgNativeType>
2645     $svgNativeType = "$svgNativeType ";
2646
2647     my $svgWrappedNativeType = $codeGenerator->GetSVGWrappedTypeNeedingTearOff($implType);
2648     if ($svgNativeType =~ /SVGPropertyTearOff/) {
2649         $svgPropertyType = $svgWrappedNativeType;
2650         $headerIncludes{"$svgWrappedNativeType.h"} = 1;
2651         $headerIncludes{"SVGAnimatedPropertyTearOff.h"} = 1;
2652     } elsif ($svgNativeType =~ /SVGListPropertyTearOff/ or $svgNativeType =~ /SVGStaticListPropertyTearOff/) {
2653         $svgListPropertyType = $svgWrappedNativeType;
2654         $headerIncludes{"$svgWrappedNativeType.h"} = 1;
2655         $headerIncludes{"SVGAnimatedListPropertyTearOff.h"} = 1;
2656     } elsif ($svgNativeType =~ /SVGTransformListPropertyTearOff/) {
2657         $svgListPropertyType = $svgWrappedNativeType;
2658         $headerIncludes{"$svgWrappedNativeType.h"} = 1;
2659         $headerIncludes{"SVGAnimatedListPropertyTearOff.h"} = 1;
2660         $headerIncludes{"SVGTransformListPropertyTearOff.h"} = 1;
2661     } elsif ($svgNativeType =~ /SVGPathSegListPropertyTearOff/) {
2662         $svgListPropertyType = $svgWrappedNativeType;
2663         $headerIncludes{"$svgWrappedNativeType.h"} = 1;
2664         $headerIncludes{"SVGAnimatedListPropertyTearOff.h"} = 1;
2665         $headerIncludes{"SVGPathSegListPropertyTearOff.h"} = 1;
2666     }
2667
2668     return ($svgPropertyType, $svgListPropertyType, $svgNativeType);
2669 }
2670
2671 sub IsNativeType
2672 {
2673     my $type = shift;
2674     return exists $nativeType{$type};
2675 }
2676
2677 sub JSValueToNative
2678 {
2679     my $signature = shift;
2680     my $value = shift;
2681
2682     my $conditional = $signature->extendedAttributes->{"Conditional"};
2683     my $type = $codeGenerator->StripModule($signature->type);
2684
2685     return "$value.toBoolean(exec)" if $type eq "boolean";
2686     return "$value.toNumber(exec)" if $type eq "double";
2687     return "$value.toFloat(exec)" if $type eq "float";
2688     return "$value.toInt32(exec)" if $type eq "long" or $type eq "short";
2689     return "$value.toUInt32(exec)" if $type eq "unsigned long" or $type eq "unsigned short";
2690     return "static_cast<$type>($value.toInteger(exec))" if $type eq "long long" or $type eq "unsigned long long";
2691
2692     return "valueToDate(exec, $value)" if $type eq "Date";
2693     return "static_cast<Range::CompareHow>($value.toInt32(exec))" if $type eq "CompareHow";
2694
2695     if ($type eq "DOMString") {
2696         return "valueToStringWithNullCheck(exec, $value)" if $signature->extendedAttributes->{"ConvertNullToNullString"} || $signature->extendedAttributes->{"Reflect"};
2697         return "valueToStringWithUndefinedOrNullCheck(exec, $value)" if $signature->extendedAttributes->{"ConvertUndefinedOrNullToNullString"};
2698         return "ustringToString($value.toString(exec))";
2699     }
2700
2701     if ($type eq "DOMObject") {
2702         return "exec->globalData(), $value";
2703     }
2704
2705     if ($type eq "NodeFilter") {
2706         AddToImplIncludes("JS$type.h", $conditional);
2707         return "to$type(exec->globalData(), $value)";
2708     }
2709
2710     if ($type eq "MediaQueryListListener") {
2711         AddToImplIncludes("MediaQueryListListener.h", $conditional);
2712         return "MediaQueryListListener::create(ScriptValue(exec->globalData(), " . $value ."))";
2713     }
2714
2715     if ($type eq "SerializedScriptValue" or $type eq "any") {
2716         AddToImplIncludes("SerializedScriptValue.h", $conditional);
2717         return "SerializedScriptValue::create(exec, $value)";
2718     }
2719
2720     if ($type eq "IDBKey") {
2721         AddToImplIncludes("IDBBindingUtilities.h", $conditional);
2722         AddToImplIncludes("IDBKey.h", $conditional);
2723         return "createIDBKeyFromValue(exec, $value)";
2724     }
2725
2726     AddToImplIncludes("HTMLOptionElement.h", $conditional) if $type eq "HTMLOptionElement";
2727     AddToImplIncludes("JSCustomVoidCallback.h", $conditional) if $type eq "VoidCallback";
2728     AddToImplIncludes("Event.h", $conditional) if $type eq "Event";
2729
2730     # Default, assume autogenerated type conversion routines
2731     AddToImplIncludes("JS$type.h", $conditional);
2732     return "to$type($value)";
2733 }
2734
2735 sub NativeToJSValue
2736 {
2737     my $signature = shift;
2738     my $inFunctionCall = shift;
2739     my $implClassName = shift;
2740     my $value = shift;
2741     my $thisValue = shift;
2742
2743     my $conditional = $signature->extendedAttributes->{"Conditional"};
2744     my $type = $codeGenerator->StripModule($signature->type);
2745
2746     return "jsBoolean($value)" if $type eq "boolean";
2747
2748     # Need to check Date type before IsPrimitiveType().
2749     if ($type eq "Date") {
2750         return "jsDateOrNull(exec, $value)";
2751     }
2752
2753     if ($signature->extendedAttributes->{"Reflect"} and ($type eq "unsigned long" or $type eq "unsigned short")) {
2754         $value =~ s/getUnsignedIntegralAttribute/getIntegralAttribute/g;
2755         return "jsNumber(std::max(0, " . $value . "))";
2756     }
2757
2758     if ($codeGenerator->IsPrimitiveType($type) or $type eq "DOMTimeStamp") {
2759         return "jsNumber($value)";
2760     }
2761
2762     if ($codeGenerator->IsStringType($type)) {
2763         AddToImplIncludes("KURL.h", $conditional);
2764         my $conv = $signature->extendedAttributes->{"ConvertNullStringTo"};
2765         if (defined $conv) {
2766             return "jsStringOrNull(exec, $value)" if $conv eq "Null";
2767             return "jsStringOrUndefined(exec, $value)" if $conv eq "Undefined";
2768             return "jsStringOrFalse(exec, $value)" if $conv eq "False";
2769
2770             die "Unknown value for ConvertNullStringTo extended attribute";
2771         }
2772         $conv = $signature->extendedAttributes->{"ConvertScriptString"};
2773         return "jsOwnedStringOrNull(exec, $value)" if $conv;
2774         AddToImplIncludes("<runtime/JSString.h>", $conditional);
2775         return "jsString(exec, $value)";
2776     }
2777     
2778     my $globalObject = "$thisValue->globalObject()";
2779
2780     if ($type eq "CSSStyleDeclaration") {
2781         AddToImplIncludes("CSSMutableStyleDeclaration.h", $conditional);
2782     }
2783
2784     if ($type eq "NodeList") {
2785         AddToImplIncludes("NameNodeList.h", $conditional);
2786     }
2787
2788     if ($type eq "DOMObject") {
2789         if ($implClassName eq "Document") {
2790             AddToImplIncludes("JSCanvasRenderingContext2D.h", $conditional);
2791         } else {
2792             return "($value.hasNoValue() ? jsNull() : $value.jsValue())";
2793         }
2794     } elsif ($type =~ /SVGPathSeg/) {
2795         AddToImplIncludes("JS$type.h", $conditional);
2796         my $joinedName = $type;
2797         $joinedName =~ s/Abs|Rel//;
2798         AddToImplIncludes("$joinedName.h", $conditional);
2799     } elsif ($type eq "SerializedScriptValue" or $type eq "any") {
2800         AddToImplIncludes("SerializedScriptValue.h", $conditional);
2801         return "$value ? $value->deserialize(exec, castedThis->globalObject()) : jsNull()";
2802     } else {
2803         # Default, include header with same name.
2804         AddToImplIncludes("JS$type.h", $conditional);
2805         AddToImplIncludes("$type.h", $conditional) if not $codeGenerator->AvoidInclusionOfType($type);
2806     }
2807
2808     return $value if $codeGenerator->IsSVGAnimatedType($type);
2809
2810     if ($signature->extendedAttributes->{"ReturnsNew"}) {        
2811         return "toJSNewlyCreated(exec, $globalObject, WTF::getPtr($value))";
2812     }
2813
2814     if ($codeGenerator->IsSVGAnimatedType($implClassName)) {
2815         # Convert from abstract SVGProperty to real type, so the right toJS() method can be invoked.
2816         $value = "static_cast<" . GetNativeType($type) . ">($value)";
2817     } elsif ($codeGenerator->IsSVGTypeNeedingTearOff($type) and not $implClassName =~ /List$/) {
2818         my $tearOffType = $codeGenerator->GetSVGTypeNeedingTearOff($type);
2819         if ($codeGenerator->IsSVGTypeWithWritablePropertiesNeedingTearOff($type) and $inFunctionCall eq 0 and not defined $signature->extendedAttributes->{"Immutable"}) {
2820             my $getter = $value;
2821             $getter =~ s/imp\.//;
2822             $getter =~ s/imp->//;
2823             $getter =~ s/\(\)//;
2824             my $updateMethod = "&${implClassName}::update" . $codeGenerator->WK_ucfirst($getter);
2825
2826             my $selfIsTearOffType = $codeGenerator->IsSVGTypeNeedingTearOff($implClassName);
2827             if ($selfIsTearOffType) {
2828                 AddToImplIncludes("SVGStaticPropertyWithParentTearOff.h", $conditional);
2829                 $tearOffType =~ s/SVGPropertyTearOff</SVGStaticPropertyWithParentTearOff<$implClassName, /;
2830
2831                 if ($value =~ /matrix/ and $implClassName eq "SVGTransform") {
2832                     # SVGTransform offers a matrix() method for internal usage that returns an AffineTransform
2833                     # and a svgMatrix() method returning a SVGMatrix, used for the bindings.
2834                     $value =~ s/matrix/svgMatrix/;
2835                 }
2836
2837                 $value = "${tearOffType}::create(castedThis->impl(), $value, $updateMethod)";
2838             } else {
2839                 AddToImplIncludes("SVGStaticPropertyTearOff.h", $conditional);
2840                 $tearOffType =~ s/SVGPropertyTearOff</SVGStaticPropertyTearOff<$implClassName, /;
2841                 $value = "${tearOffType}::create(imp, $value, $updateMethod)";
2842             }
2843         } elsif ($tearOffType =~ /SVGStaticListPropertyTearOff/) {
2844             $value = "${tearOffType}::create(imp, $value)";
2845         } elsif (not $tearOffType =~ /SVG(Point|PathSeg)List/) {
2846             $value = "${tearOffType}::create($value)";
2847         }
2848     }
2849
2850     return "toJS(exec, $globalObject, WTF::getPtr($value))";
2851 }
2852
2853 sub ceilingToPowerOf2
2854 {
2855     my ($size) = @_;
2856
2857     my $powerOf2 = 1;
2858     while ($size > $powerOf2) {
2859         $powerOf2 <<= 1;
2860     }
2861
2862     return $powerOf2;
2863 }
2864
2865 # Internal Helper
2866 sub GenerateHashTable
2867 {
2868     my $object = shift;
2869
2870     my $name = shift;
2871     my $size = shift;
2872     my $keys = shift;
2873     my $specials = shift;
2874     my $value1 = shift;
2875     my $value2 = shift;
2876     my $conditionals = shift;
2877
2878     # Generate size data for compact' size hash table
2879
2880     my @table = ();
2881     my @links = ();
2882
2883     my $compactSize = ceilingToPowerOf2($size * 2);
2884
2885     my $maxDepth = 0;
2886     my $collisions = 0;
2887     my $numEntries = $compactSize;
2888
2889     my $i = 0;
2890     foreach (@{$keys}) {
2891         my $depth = 0;
2892         my $h = $object->GenerateHashValue($_) % $numEntries;
2893
2894         while (defined($table[$h])) {
2895             if (defined($links[$h])) {
2896                 $h = $links[$h];
2897                 $depth++;
2898             } else {
2899                 $collisions++;
2900                 $links[$h] = $compactSize;
2901                 $h = $compactSize;
2902                 $compactSize++;
2903             }
2904         }
2905
2906         $table[$h] = $i;
2907
2908         $i++;
2909         $maxDepth = $depth if ($depth > $maxDepth);
2910     }
2911
2912     # Start outputing the hashtables
2913     my $nameEntries = "${name}Values";
2914     $nameEntries =~ s/:/_/g;
2915
2916     if (($name =~ /Prototype/) or ($name =~ /Constructor/)) {
2917         my $type = $name;
2918         my $implClass;
2919
2920         if ($name =~ /Prototype/) {
2921             $type =~ s/Prototype.*//;
2922             $implClass = $type; $implClass =~ s/Wrapper$//;
2923             push(@implContent, "/* Hash table for prototype */\n");
2924         } else {
2925             $type =~ s/Constructor.*//;
2926             $implClass = $type; $implClass =~ s/Constructor$//;
2927             push(@implContent, "/* Hash table for constructor */\n");
2928         }
2929     } else {
2930         push(@implContent, "/* Hash table */\n");
2931     }
2932
2933     # Dump the hash table
2934     push(@implContent, "#if ENABLE(JIT)\n");
2935     push(@implContent, "#define THUNK_GENERATOR(generator) , generator\n");
2936     push(@implContent, "#else\n");
2937     push(@implContent, "#define THUNK_GENERATOR(generator)\n");
2938     push(@implContent, "#endif\n");
2939     push(@implContent, "#if ENABLE(DFG_JIT)\n");
2940     push(@implContent, "#define INTRINSIC(intrinsic) , intrinsic\n");
2941     push(@implContent, "#else\n");
2942     push(@implContent, "#define INTRINSIC(intrinsic)\n");
2943     push(@implContent, "#endif\n");
2944     push(@implContent, "\nstatic const HashTableValue $nameEntries\[\] =\n\{\n");
2945     $i = 0;
2946     foreach my $key (@{$keys}) {
2947         my $conditional;
2948         my $targetType;
2949
2950         if ($conditionals) {
2951             $conditional = $conditionals->{$key};
2952         }
2953         if ($conditional) {
2954             my $conditionalString = GenerateConditionalStringFromAttributeValue($conditional);
2955             push(@implContent, "#if ${conditionalString}\n");
2956         }
2957         
2958         if ("@$specials[$i]" =~ m/Function/) {
2959             $targetType = "static_cast<NativeFunction>";
2960         } else {
2961             $targetType = "static_cast<PropertySlot::GetValueFunc>";
2962         }
2963         push(@implContent, "    { \"$key\", @$specials[$i], (intptr_t)" . $targetType . "(@$value1[$i]), (intptr_t)@$value2[$i] THUNK_GENERATOR(0) INTRINSIC(DFG::NoIntrinsic) },\n");
2964         if ($conditional) {
2965             push(@implContent, "#endif\n");
2966         }
2967         ++$i;
2968     }
2969     push(@implContent, "    { 0, 0, 0, 0 THUNK_GENERATOR(0) INTRINSIC(DFG::NoIntrinsic) }\n");
2970     push(@implContent, "};\n\n");
2971     push(@implContent, "#undef THUNK_GENERATOR\n");
2972     my $compactSizeMask = $numEntries - 1;
2973     push(@implContent, "static JSC_CONST_HASHTABLE HashTable $name = { $compactSize, $compactSizeMask, $nameEntries, 0 };\n");
2974 }
2975
2976 # Internal helper
2977 sub GenerateHashValue
2978 {
2979     my $object = shift;
2980
2981     my @chars = split(/ */, $_[0]);
2982
2983     # This hash is designed to work on 16-bit chunks at a time. But since the normal case
2984     # (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
2985     # were 16-bit chunks, which should give matching results
2986
2987     my $EXP2_32 = 4294967296;
2988
2989     my $hash = 0x9e3779b9;
2990     my $l    = scalar @chars; #I wish this was in Ruby --- Maks
2991     my $rem  = $l & 1;
2992     $l = $l >> 1;
2993
2994     my $s = 0;
2995
2996     # Main loop
2997     for (; $l > 0; $l--) {
2998         $hash   += ord($chars[$s]);
2999         my $tmp = leftShift(ord($chars[$s+1]), 11) ^ $hash;
3000         $hash   = (leftShift($hash, 16)% $EXP2_32) ^ $tmp;
3001         $s += 2;
3002         $hash += $hash >> 11;
3003         $hash %= $EXP2_32;
3004     }
3005
3006     # Handle end case
3007     if ($rem != 0) {
3008         $hash += ord($chars[$s]);
3009         $hash ^= (leftShift($hash, 11)% $EXP2_32);
3010         $hash += $hash >> 17;
3011     }
3012
3013     # Force "avalanching" of final 127 bits
3014     $hash ^= leftShift($hash, 3);
3015     $hash += ($hash >> 5);
3016     $hash = ($hash% $EXP2_32);
3017     $hash ^= (leftShift($hash, 2)% $EXP2_32);
3018     $hash += ($hash >> 15);
3019     $hash = $hash% $EXP2_32;
3020     $hash ^= (leftShift($hash, 10)% $EXP2_32);
3021
3022     # this avoids ever returning a hash code of 0, since that is used to
3023     # signal "hash not computed yet", using a value that is likely to be
3024     # effectively the same as 0 when the low bits are masked
3025     $hash = 0x80000000 if ($hash == 0);
3026
3027     return $hash;
3028 }
3029
3030 # Internal helper
3031 sub WriteData
3032 {
3033     if (defined($IMPL)) {
3034         # Write content to file.
3035         print $IMPL @implContentHeader;
3036
3037         my @includes = ();
3038         my %implIncludeConditions = ();
3039         foreach my $include (keys %implIncludes) {
3040             my $condition = $implIncludes{$include};
3041             my $checkType = $include;
3042             $checkType =~ s/\.h//;
3043             next if $codeGenerator->IsSVGAnimatedType($checkType);
3044
3045             $include = "\"$include\"" unless $include =~ /^["<]/; # "
3046
3047             if ($condition eq 1) {
3048                 push @includes, $include;
3049             } else {
3050                 push @{$implIncludeConditions{$condition}}, $include;
3051             }
3052         }
3053         foreach my $include (sort @includes) {
3054             print $IMPL "#include $include\n";
3055         }
3056         foreach my $condition (sort keys %implIncludeConditions) {
3057             print $IMPL "\n#if " . GenerateConditionalStringFromAttributeValue($condition) . "\n";
3058             foreach my $include (sort @{$implIncludeConditions{$condition}}) {
3059                 print $IMPL "#include $include\n";
3060             }
3061             print $IMPL "#endif\n";
3062         }
3063
3064         print $IMPL @implContent;
3065         close($IMPL);
3066         undef($IMPL);
3067
3068         @implContentHeader = ();
3069         @implContent = ();
3070         %implIncludes = ();
3071     }
3072
3073     if (defined($HEADER)) {
3074         # Write content to file.
3075         print $HEADER @headerContentHeader;
3076
3077         my @includes = ();
3078         foreach my $include (keys %headerIncludes) {
3079             $include = "\"$include\"" unless $include =~ /^["<]/; # "
3080             push @includes, $include;
3081         }
3082         foreach my $include (sort @includes) {
3083             print $HEADER "#include $include\n";
3084         }
3085
3086         print $HEADER @headerContent;
3087
3088         @includes = ();
3089         foreach my $include (keys %headerTrailingIncludes) {
3090             $include = "\"$include\"" unless $include =~ /^["<]/; # "
3091             push @includes, $include;
3092         }
3093         foreach my $include (sort @includes) {
3094             print $HEADER "#include $include\n";
3095         }
3096
3097         close($HEADER);
3098         undef($HEADER);
3099
3100         @headerContentHeader = ();
3101         @headerContent = ();
3102         %headerIncludes = ();
3103         %headerTrailingIncludes = ();
3104     }
3105
3106     if (defined($DEPS)) {
3107         # Write dependency file.
3108         print $DEPS @depsContent;
3109         close($DEPS);
3110         undef($DEPS);
3111
3112         @depsContent = ();
3113     }
3114 }
3115
3116 sub GenerateConstructorDeclaration
3117 {
3118     my $outputArray = shift;
3119     my $className = shift;
3120     my $dataNode = shift;
3121
3122     my $constructorClassName = "${className}Constructor";
3123     my $canConstruct = $dataNode->extendedAttributes->{"CanBeConstructed"};
3124     my $callWith = $dataNode->extendedAttributes->{"CallWith"};
3125
3126     push(@$outputArray, "class ${constructorClassName} : public DOMConstructorObject {\n");
3127     push(@$outputArray, "private:\n");
3128     push(@$outputArray, "    ${constructorClassName}(JSC::Structure*, JSDOMGlobalObject*);\n");
3129     push(@$outputArray, "    void finishCreation(JSC::ExecState*, JSDOMGlobalObject*);\n\n");
3130
3131     push(@$outputArray, "public:\n");
3132     push(@$outputArray, "    typedef DOMConstructorObject Base;\n");
3133     push(@$outputArray, "    static $constructorClassName* create(JSC::ExecState* exec, JSC::Structure* structure, JSDOMGlobalObject* globalObject)\n");
3134     push(@$outputArray, "    {\n");
3135     push(@$outputArray, "        $constructorClassName* ptr = new (JSC::allocateCell<$constructorClassName>(*exec->heap())) $constructorClassName(structure, globalObject);\n");
3136     push(@$outputArray, "        ptr->finishCreation(exec, globalObject);\n");
3137     push(@$outputArray, "        return ptr;\n");
3138     push(@$outputArray, "    }\n\n");
3139
3140     push(@$outputArray, "    virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier&, JSC::PropertySlot&);\n");
3141     push(@$outputArray, "    virtual bool getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier&, JSC::PropertyDescriptor&);\n");
3142     push(@$outputArray, "    static const JSC::ClassInfo s_info;\n");
3143
3144     push(@$outputArray, "    static JSC::Structure* createStructure(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)\n");
3145     push(@$outputArray, "    {\n");
3146     push(@$outputArray, "        return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), &s_info);\n");
3147     push(@$outputArray, "    }\n");
3148
3149     push(@$outputArray, "protected:\n");
3150     push(@$outputArray, "    static const unsigned StructureFlags = JSC::OverridesGetOwnPropertySlot | JSC::ImplementsHasInstance | DOMConstructorObject::StructureFlags;\n");
3151
3152     if ($canConstruct) {
3153         push(@$outputArray, "    static JSC::EncodedJSValue JSC_HOST_CALL construct${className}(JSC::ExecState*);\n");
3154         push(@$outputArray, "    virtual JSC::ConstructType getConstructData(JSC::ConstructData&);\n");
3155     }
3156     push(@$outputArray, "};\n\n");
3157 }
3158
3159 sub GenerateConstructorDefinition
3160 {
3161     my $outputArray = shift;
3162
3163     my $className = shift;
3164     my $protoClassName = shift;
3165     my $interfaceName = shift;
3166     my $visibleClassName = shift;
3167     my $dataNode = shift;
3168
3169     my $constructorClassName = "${className}Constructor";
3170     my $canConstruct = $dataNode->extendedAttributes->{"CanBeConstructed"};
3171     my $customConstructor = $dataNode->extendedAttributes->{"JSCustomConstructor"} || $dataNode->extendedAttributes->{"CustomConstructor"};
3172     my $callWith = $dataNode->extendedAttributes->{"CallWith"};
3173     my $numberOfconstructParameters = $dataNode->extendedAttributes->{"ConstructorParameters"};
3174
3175     push(@$outputArray, "const ClassInfo ${constructorClassName}::s_info = { \"${visibleClassName}Constructor\", &DOMConstructorObject::s_info, &${constructorClassName}Table, 0, CREATE_METHOD_TABLE($constructorClassName) };\n\n");
3176
3177     push(@$outputArray, "${constructorClassName}::${constructorClassName}(Structure* structure, JSDOMGlobalObject* globalObject)\n");
3178     push(@$outputArray, "    : DOMConstructorObject(structure, globalObject)\n");
3179     push(@$outputArray, "{\n");
3180     push(@$outputArray, "}\n\n");
3181
3182     push(@$outputArray, "void ${constructorClassName}::finishCreation(ExecState* exec, JSDOMGlobalObject* globalObject)\n");
3183     push(@$outputArray, "{\n");
3184     push(@$outputArray, "    Base::finishCreation(exec->globalData());\n");
3185     push(@$outputArray, "    ASSERT(inherits(&s_info));\n");
3186     if ($interfaceName eq "DOMWindow") {
3187         push(@$outputArray, "    putDirect(exec->globalData(), exec->propertyNames().prototype, globalObject->prototype(), DontDelete | ReadOnly);\n");
3188     } else {
3189         push(@$outputArray, "    putDirect(exec->globalData(), exec->propertyNames().prototype, ${protoClassName}::self(exec, globalObject), DontDelete | ReadOnly);\n");
3190     }
3191     push(@$outputArray, "    putDirect(exec->globalData(), exec->propertyNames().length, jsNumber(${numberOfconstructParameters}), ReadOnly | DontDelete | DontEnum);\n") if $numberOfconstructParameters;
3192     push(@$outputArray, "}\n\n");
3193
3194     push(@$outputArray, "bool ${constructorClassName}::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\n");
3195     push(@$outputArray, "{\n");
3196     push(@$outputArray, "    return getStaticValueSlot<${constructorClassName}, JSDOMWrapper>(exec, &${constructorClassName}Table, this, propertyName, slot);\n");
3197     push(@$outputArray, "}\n\n");
3198
3199     push(@$outputArray, "bool ${constructorClassName}::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)\n");
3200     push(@$outputArray, "{\n");
3201     push(@$outputArray, "    return getStaticValueDescriptor<${constructorClassName}, JSDOMWrapper>(exec, &${constructorClassName}Table, this, propertyName, descriptor);\n");
3202     push(@$outputArray, "}\n\n");
3203
3204     if ($canConstruct) {
3205         if (!$customConstructor) {
3206             push(@$outputArray, "EncodedJSValue JSC_HOST_CALL ${constructorClassName}::construct${className}(ExecState* exec)\n");
3207             push(@$outputArray, "{\n");
3208             my $constructorArg = "";
3209             if ($callWith and $callWith eq "ScriptExecutionContext") {
3210                 $constructorArg = "context";
3211                 push(@$outputArray, "    ScriptExecutionContext* context = static_cast<${constructorClassName}*>(exec->callee())->scriptExecutionContext();\n");
3212                 push(@$outputArray, "    if (!context)\n");
3213                 push(@$outputArray, "        return throwVMError(exec, createReferenceError(exec, \"Reference error\"));\n");
3214             }
3215             push(@$outputArray, "    return JSValue::encode(asObject(toJS(exec, static_cast<${constructorClassName}*>(exec->callee())->globalObject(), ${interfaceName}::create(${constructorArg}))));\n");
3216             push(@$outputArray, "}\n\n");
3217         }
3218
3219         push(@$outputArray, "ConstructType ${constructorClassName}::getConstructData(ConstructData& constructData)\n");
3220         push(@$outputArray, "{\n");
3221         push(@$outputArray, "    constructData.native.function = construct${className};\n");
3222         push(@$outputArray, "    return ConstructTypeHost;\n");
3223         push(@$outputArray, "}\n\n");
3224     }
3225 }
3226
3227 1;