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