CodeGeneratorJS.pm 175 KB
Newer Older
1
#
zimmermann@webkit.org's avatar
zimmermann@webkit.org committed
2
# Copyright (C) 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
weinig's avatar
weinig committed
3
# Copyright (C) 2006 Anders Carlsson <andersca@mac.com>
weinig's avatar
weinig committed
4
# Copyright (C) 2006, 2007 Samuel Weinig <sam@webkit.org>
ap's avatar
ap committed
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 13 14 15
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
weinig's avatar
weinig committed
16
#
17 18 19 20
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
weinig's avatar
weinig committed
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
ddkilzer's avatar
ddkilzer committed
24 25
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
26 27 28

package CodeGeneratorJS;

29 30
use strict;

31 32
use constant FileNamePrefix => "JS";

33
my ($codeGenerator);
34

35 36
my $module = "";
my $outputDir = "";
37
my $writeDependencies = 0;
weinig's avatar
weinig committed
38

39
my @headerContentHeader = ();
weinig's avatar
weinig committed
40
my @headerContent = ();
41
my %headerIncludes = ();
42
my %headerTrailingIncludes = ();
43

weinig's avatar
weinig committed
44 45
my @implContentHeader = ();
my @implContent = ();
46
my %implIncludes = ();
47
my @depsContent = ();
48 49
my $numCachedAttributes = 0;
my $currentCachedAttribute = 0;
50 51 52 53

# Default .h template
my $headerTemplate = << "EOF";
/*
54 55
    This file is part of the WebKit open source project.
    This file has been generated by generate-bindings.pl. DO NOT MODIFY!
56 57 58 59 60 61 62 63 64 65 66 67 68

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
ddkilzer's avatar
ddkilzer committed
69 70
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
71 72 73 74 75 76
*/
EOF

# Default constructor
sub new
{
weinig's avatar
weinig committed
77 78 79 80 81
    my $object = shift;
    my $reference = { };

    $codeGenerator = shift;
    $outputDir = shift;
82
    shift; # $outputHeadersDir
83 84 85
    shift; # $useLayerOnTop
    shift; # $preprocessor
    $writeDependencies = shift;
weinig's avatar
weinig committed
86 87 88

    bless($reference, $object);
    return $reference;
89 90
}

91 92 93 94 95
sub leftShift($$) {
    my ($value, $distance) = @_;
    return (($value << $distance) & 0xFFFFFFFF);
}

96 97 98
# Params: 'domClass' struct
sub GenerateInterface
{
weinig's avatar
weinig committed
99 100
    my $object = shift;
    my $dataNode = shift;
101
    my $defines = shift;
weinig's avatar
weinig committed
102

103 104
    $codeGenerator->LinkOverloadedFunctions($dataNode);

105
    # Start actual generation
106 107 108 109 110 111 112
    if ($dataNode->extendedAttributes->{"Callback"}) {
        $object->GenerateCallbackHeader($dataNode);
        $object->GenerateCallbackImplementation($dataNode);
    } else {
        $object->GenerateHeader($dataNode);
        $object->GenerateImplementation($dataNode);
    }
113

114
    $object->WriteData($dataNode);
115 116
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130
sub GenerateAttributeEventListenerCall
{
    my $className = shift;
    my $implSetterFunctionName = shift;
    my $windowEventListener = shift;

    my $wrapperObject = $windowEventListener ? "globalObject" : "thisObject";
    my @GenerateEventListenerImpl = ();

    if ($className eq "JSSVGElementInstance") {
        # SVGElementInstances have to create JSEventListeners with the wrapper equal to the correspondingElement
        $wrapperObject = "asObject(correspondingElementWrapper)";

        push(@GenerateEventListenerImpl, <<END);
131
    JSValue correspondingElementWrapper = toJS(exec, castedThis->globalObject(), impl->correspondingElement());
132 133 134
    if (correspondingElementWrapper.isObject())
END

135
        # Add leading whitespace to format the impl->set... line correctly
136 137 138
        push(@GenerateEventListenerImpl, "    ");
    }

139
    push(@GenerateEventListenerImpl, "    impl->set$implSetterFunctionName(createJSAttributeEventListener(exec, value, $wrapperObject));\n");
140 141 142
    return @GenerateEventListenerImpl;
}

143 144 145 146 147 148 149 150 151
sub GenerateEventListenerCall
{
    my $className = shift;
    my $functionName = shift;
    my $passRefPtrHandling = ($functionName eq "add") ? "" : ".get()";

    $implIncludes{"JSEventListener.h"} = 1;

    my @GenerateEventListenerImpl = ();
152 153 154 155 156 157
    my $wrapperObject = "castedThis";
    if ($className eq "JSSVGElementInstance") {
        # SVGElementInstances have to create JSEventListeners with the wrapper equal to the correspondingElement
        $wrapperObject = "asObject(correspondingElementWrapper)";

        push(@GenerateEventListenerImpl, <<END);
158
    JSValue correspondingElementWrapper = toJS(exec, castedThis->globalObject(), impl->correspondingElement());
159
    if (!correspondingElementWrapper.isObject())
160
        return JSValue::encode(jsUndefined());
161 162 163
END
    }

164
    push(@GenerateEventListenerImpl, <<END);
165
    JSValue listener = exec->argument(1);
166
    if (!listener.isObject())
167
        return JSValue::encode(jsUndefined());
168
    impl->${functionName}EventListener(ustringToAtomicString(exec->argument(0).toString(exec)->value(exec)), JSEventListener::create(asObject(listener), $wrapperObject, false, currentWorld(exec))$passRefPtrHandling, exec->argument(2).toBoolean(exec));
169
    return JSValue::encode(jsUndefined());
170 171 172 173
END
    return @GenerateEventListenerImpl;
}

174 175 176
# Params: 'idlDocument' struct
sub GenerateModule
{
177
    my $object = shift;
weinig's avatar
weinig committed
178 179 180
    my $dataNode = shift;

    $module = $dataNode->module;
181 182 183 184
}

sub GetParentClassName
{
185
    my $dataNode = shift;
weinig's avatar
weinig committed
186

187
    return $dataNode->extendedAttributes->{"JSLegacyParent"} if $dataNode->extendedAttributes->{"JSLegacyParent"};
weinig@apple.com's avatar
weinig@apple.com committed
188
    return "JSDOMWrapper" if (@{$dataNode->parents} eq 0);
189
    return "JS" . $codeGenerator->StripModule($dataNode->parents(0));
190 191
}

weinig@apple.com's avatar
weinig@apple.com committed
192 193 194 195 196
sub GetVisibleClassName
{
    my $className = shift;

    return "DOMException" if $className eq "DOMCoreException";
197
    return "FormData" if $className eq "DOMFormData";
198 199 200 201 202
    return "MimeType" if $className eq "DOMMimeType";
    return "MimeTypeArray" if $className eq "DOMMimeTypeArray";
    return "Plugin" if $className eq "DOMPlugin";
    return "PluginArray" if $className eq "DOMPluginArray";
    
weinig@apple.com's avatar
weinig@apple.com committed
203 204 205
    return $className;
}

206 207 208 209 210 211 212 213
sub GetCallbackClassName
{
    my $className = shift;

    return "JSCustomVoidCallback" if $className eq "VoidCallback";
    return "JS$className";
}

weinig's avatar
weinig committed
214 215 216 217
sub IndexGetterReturnsStrings
{
    my $type = shift;

ap@apple.com's avatar
ap@apple.com committed
218
    return 1 if $type eq "CSSStyleDeclaration" or $type eq "MediaList" or $type eq "DOMStringList" or $type eq "DOMTokenList" or $type eq "DOMSettableTokenList";
weinig's avatar
weinig committed
219 220 221
    return 0;
}

222
sub AddIncludesForTypeInImpl
223
{
224
    my $type = $codeGenerator->StripModule(shift);
225
    my $isCallback = @_ ? shift : 0;
226 227 228
    
    AddIncludesForType($type, $isCallback, \%implIncludes);
    
darin's avatar
darin committed
229 230
    # additional includes (things needed to compile the bindings but not the header)
    if ($type eq "CanvasRenderingContext2D") {
231 232 233
        $implIncludes{"CanvasGradient.h"} = 1;
        $implIncludes{"CanvasPattern.h"} = 1;
        $implIncludes{"CanvasStyle.h"} = 1;
darin's avatar
darin committed
234 235
    }

236
    if ($type eq "CanvasGradient" or $type eq "XPathNSResolver" or $type eq "MessagePort") {
237 238
        $implIncludes{"PlatformString.h"} = 1;
    }
darin@apple.com's avatar
darin@apple.com committed
239 240 241 242

    if ($type eq "Document") {
        $implIncludes{"NodeFilter.h"} = 1;
    }
243 244 245 246

    if ($type eq "MediaQueryListListener") {
        $implIncludes{"MediaQueryListListener.h"} = 1;
    }
247 248
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
sub AddIncludesForTypeInHeader
{
    my $type = $codeGenerator->StripModule(shift);
    my $isCallback = @_ ? shift : 0;
    
    AddIncludesForType($type, $isCallback, \%headerIncludes);
}

sub AddIncludesForType
{
    my $type = shift;
    my $isCallback = shift;
    my $includesRef = shift;

    # When we're finished with the one-file-per-class
    # reorganization, we won't need these special cases.
    if ($codeGenerator->IsPrimitiveType($type) or $codeGenerator->AvoidInclusionOfType($type)
266
        or $type eq "DOMString" or $type eq "DOMObject" or $type eq "Array" or $type eq "DOMTimeStamp") {
267
    } elsif ($type =~ /SVGPathSeg/) {
268
        my $joinedName = $type;
269 270 271 272 273
        $joinedName =~ s/Abs|Rel//;
        $includesRef->{"${joinedName}.h"} = 1;
    } elsif ($type eq "XPathNSResolver") {
        $includesRef->{"JSXPathNSResolver.h"} = 1;
        $includesRef->{"JSCustomXPathNSResolver.h"} = 1;
274 275 276
    } elsif ($type eq "DOMString[]") {
        # FIXME: Add proper support for T[], T[]?, sequence<T>
        $includesRef->{"JSDOMStringList.h"} = 1;
277 278
    } elsif ($isCallback) {
        $includesRef->{"JS${type}.h"} = 1;
279 280
    } elsif (IsTypedArrayType($type)) {
        $includesRef->{"<wtf/${type}.h>"} = 1;
281 282 283 284 285 286
    } else {
        # default, include the same named file
        $includesRef->{"${type}.h"} = 1;
    }
}

287
# FIXME: This method will go away once all SVG animated properties are converted to the new scheme.
288 289 290
sub AddIncludesForSVGAnimatedType
{
    my $type = shift;
weinig's avatar
weinig committed
291
    $type =~ s/SVGAnimated//;
292

zimmermann's avatar
zimmermann committed
293 294
    if ($type eq "Point" or $type eq "Rect") {
        $implIncludes{"Float$type.h"} = 1;
weinig's avatar
weinig committed
295
    } elsif ($type eq "String") {
weinig's avatar
weinig committed
296
        $implIncludes{"PlatformString.h"} = 1;
297 298 299
    }
}

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
sub AddToImplIncludes
{
    my $header = shift;
    my $conditional = shift;

    if (not $conditional) {
        $implIncludes{$header} = 1;
    } elsif (not exists($implIncludes{$header})) {
        $implIncludes{$header} = $conditional;
    } else {
        my $oldValue = $implIncludes{$header};
        if ($oldValue ne 1) {
            my %newValue = ();
            $newValue{$conditional} = 1;
            foreach my $condition (split(/\|/, $oldValue)) {
                $newValue{$condition} = 1;
            }
            $implIncludes{$header} = join("|", sort keys %newValue);
        }
    }
}

322 323 324
sub IsScriptProfileType
{
    my $type = shift;
325
    return 1 if ($type eq "ScriptProfileNode");
326 327 328
    return 0;
}

329 330 331 332
sub IsTypedArrayType
{
    my $type = shift;
    return 1 if (($type eq "ArrayBuffer") or ($type eq "ArrayBufferView"));
333
    return 1 if (($type eq "Uint8Array") or ($type eq "Uint8ClampedArray") or ($type eq "Uint16Array") or ($type eq "Uint32Array"));
334 335 336 337 338
    return 1 if (($type eq "Int8Array") or ($type eq "Int16Array") or ($type eq "Int32Array"));
    return 1 if (($type eq "Float32Array") or ($type eq "Float64Array"));
    return 0;
}

339 340 341 342 343 344 345 346
sub AddTypedefForScriptProfileType
{
    my $type = shift;
    (my $jscType = $type) =~ s/Script//;

    push(@headerContent, "typedef JSC::$jscType $type;\n\n");
}

347 348 349 350
sub AddClassForwardIfNeeded
{
    my $implClassName = shift;

351
    # SVGAnimatedLength/Number/etc. are typedefs to SVGAnimatedTemplate, so don't use class forwards for them!
352
    unless ($codeGenerator->IsSVGAnimatedType($implClassName) or IsScriptProfileType($implClassName) or IsTypedArrayType($implClassName)) {
353 354 355
        push(@headerContent, "class $implClassName;\n\n");
    # ScriptProfile and ScriptProfileNode are typedefs to JSC::Profile and JSC::ProfileNode.
    } elsif (IsScriptProfileType($implClassName)) {
356
        $headerIncludes{"<profiler/ProfileNode.h>"} = 1;
357 358
        AddTypedefForScriptProfileType($implClassName);
    }
359 360
}

zimmermann's avatar
zimmermann committed
361 362 363 364 365 366 367 368 369 370 371 372 373
sub HashValueForClassAndName
{
    my $class = shift;
    my $name = shift;

    # SVG Filter enums live in WebCore namespace (platform/graphics/)
    if ($class =~ /^SVGFE*/ or $class =~ /^SVGComponentTransferFunctionElement$/) {
        return "WebCore::$name";
    }

    return "${class}::$name";
}

ap@webkit.org's avatar
ap@webkit.org committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
sub hashTableAccessor
{
    my $noStaticTables = shift;
    my $className = shift;
    if ($noStaticTables) {
        return "get${className}Table(exec)";
    } else {
        return "&${className}Table";
    }
}

sub prototypeHashTableAccessor
{
    my $noStaticTables = shift;
    my $className = shift;
    if ($noStaticTables) {
        return "get${className}PrototypeTable(exec)";
    } else {
        return "&${className}PrototypeTable";
    }
}

396 397 398 399 400
sub GenerateConditionalString
{
    my $node = shift;
    my $conditional = $node->extendedAttributes->{"Conditional"};
    if ($conditional) {
401
        return $codeGenerator->GenerateConditionalStringFromAttributeValue($conditional);
402 403 404 405 406
    } else {
        return "";
    }
}

407 408 409 410
sub GenerateGetOwnPropertySlotBody
{
    my ($dataNode, $interfaceName, $className, $implClassName, $hasAttributes, $inlined) = @_;

411
    my $namespaceMaybe = ($inlined ? "JSC::" : "");
412 413 414

    my @getOwnPropertySlotImpl = ();

415
    if ($interfaceName eq "NamedNodeMap" or $interfaceName eq "HTMLCollection" or $interfaceName eq "HTMLAllCollection") {
416
        push(@getOwnPropertySlotImpl, "    ${namespaceMaybe}JSValue proto = thisObject->prototype();\n");
weinig@apple.com's avatar
weinig@apple.com committed
417
        push(@getOwnPropertySlotImpl, "    if (proto.isObject() && static_cast<${namespaceMaybe}JSObject*>(asObject(proto))->hasProperty(exec, propertyName))\n");
418 419 420
        push(@getOwnPropertySlotImpl, "        return false;\n\n");
    }

ap@webkit.org's avatar
ap@webkit.org committed
421
    my $manualLookupGetterGeneration = sub {
422
        my $requiresManualLookup = $dataNode->extendedAttributes->{"IndexedGetter"} || $dataNode->extendedAttributes->{"NamedGetter"};
423
        if ($requiresManualLookup) {
ap@webkit.org's avatar
ap@webkit.org committed
424 425
            push(@getOwnPropertySlotImpl, "    const ${namespaceMaybe}HashEntry* entry = ${className}Table.entry(exec, propertyName);\n");
            push(@getOwnPropertySlotImpl, "    if (entry) {\n");
426
            push(@getOwnPropertySlotImpl, "        slot.setCustom(thisObject, entry->propertyGetter());\n");
ap@webkit.org's avatar
ap@webkit.org committed
427 428
            push(@getOwnPropertySlotImpl, "        return true;\n");
            push(@getOwnPropertySlotImpl, "    }\n");
429 430 431
        }
    };

432
    if (!$dataNode->extendedAttributes->{"CustomNamedGetter"}) {
ap@webkit.org's avatar
ap@webkit.org committed
433
        &$manualLookupGetterGeneration();
434 435
    }

436
    if ($dataNode->extendedAttributes->{"IndexedGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
437
        push(@getOwnPropertySlotImpl, "    bool ok;\n");
438
        push(@getOwnPropertySlotImpl, "    unsigned index = propertyName.toUInt32(ok);\n");
439 440 441 442 443 444

        # If the item function returns a string then we let the ConvertNullStringTo handle the cases
        # where the index is out of range.
        if (IndexGetterReturnsStrings($implClassName)) {
            push(@getOwnPropertySlotImpl, "    if (ok) {\n");
        } else {
445
            push(@getOwnPropertySlotImpl, "    if (ok && index < static_cast<$implClassName*>(thisObject->impl())->length()) {\n");
446
        }
447
        if ($dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
448
            push(@getOwnPropertySlotImpl, "        slot.setValue(thisObject->getByIndex(exec, index));\n");
449
        } else {
450
            push(@getOwnPropertySlotImpl, "        slot.setCustomIndex(thisObject, index, indexGetter);\n");
451
        }
452 453 454 455
        push(@getOwnPropertySlotImpl, "        return true;\n");
        push(@getOwnPropertySlotImpl, "    }\n");
    }

456
    if ($dataNode->extendedAttributes->{"NamedGetter"} || $dataNode->extendedAttributes->{"CustomNamedGetter"}) {
457 458
        push(@getOwnPropertySlotImpl, "    if (canGetItemsForName(exec, static_cast<$implClassName*>(thisObject->impl()), propertyName)) {\n");
        push(@getOwnPropertySlotImpl, "        slot.setCustom(thisObject, thisObject->nameGetter);\n");
ap@webkit.org's avatar
ap@webkit.org committed
459 460 461
        push(@getOwnPropertySlotImpl, "        return true;\n");
        push(@getOwnPropertySlotImpl, "    }\n");
        if ($inlined) {
462
            $headerIncludes{"wtf/text/AtomicString.h"} = 1;
ap@webkit.org's avatar
ap@webkit.org committed
463
        } else {
464
            $implIncludes{"wtf/text/AtomicString.h"} = 1;
ap@webkit.org's avatar
ap@webkit.org committed
465 466 467
        }
    }

468
    if ($dataNode->extendedAttributes->{"CustomNamedGetter"}) {
ap@webkit.org's avatar
ap@webkit.org committed
469
        &$manualLookupGetterGeneration();
470 471
    }

472
    if ($dataNode->extendedAttributes->{"JSCustomGetOwnPropertySlotDelegate"}) {
473
        push(@getOwnPropertySlotImpl, "    if (thisObject->getOwnPropertySlotDelegate(exec, propertyName, slot))\n");
474 475 476 477 478
        push(@getOwnPropertySlotImpl, "        return true;\n");
    }

    if ($hasAttributes) {
        if ($inlined) {
479
            die "Cannot inline if NoStaticTables is set." if ($dataNode->extendedAttributes->{"JSNoStaticTables"});
480
            push(@getOwnPropertySlotImpl, "    return ${namespaceMaybe}getStaticValueSlot<$className, Base>(exec, s_info.staticPropHashTable, thisObject, propertyName, slot);\n");
481
        } else {
482
            push(@getOwnPropertySlotImpl, "    return ${namespaceMaybe}getStaticValueSlot<$className, Base>(exec, " . hashTableAccessor($dataNode->extendedAttributes->{"JSNoStaticTables"}, $className) . ", thisObject, propertyName, slot);\n");
483 484
        }
    } else {
485
        push(@getOwnPropertySlotImpl, "    return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot);\n");
486 487 488 489 490
    }

    return @getOwnPropertySlotImpl;
}

491 492 493 494 495 496 497
sub GenerateGetOwnPropertyDescriptorBody
{
    my ($dataNode, $interfaceName, $className, $implClassName, $hasAttributes, $inlined) = @_;
    
    my $namespaceMaybe = ($inlined ? "JSC::" : "");
    
    my @getOwnPropertyDescriptorImpl = ();
498 499
    if ($dataNode->extendedAttributes->{"CheckDomainSecurity"}) {
        if ($interfaceName eq "DOMWindow") {
500
            push(@implContent, "    if (!thisObject->allowsAccessFrom(exec))\n");
501
        } else {
502
            push(@implContent, "    if (!shouldAllowAccessToFrame(exec, thisObject->impl()->frame()))\n");
503 504 505
        }
        push(@implContent, "        return false;\n");
    }
506
    
507
    if ($interfaceName eq "NamedNodeMap" or $interfaceName eq "HTMLCollection" or $interfaceName eq "HTMLAllCollection") {
508
        push(@getOwnPropertyDescriptorImpl, "    ${namespaceMaybe}JSValue proto = thisObject->prototype();\n");
509 510 511 512 513
        push(@getOwnPropertyDescriptorImpl, "    if (proto.isObject() && static_cast<${namespaceMaybe}JSObject*>(asObject(proto))->hasProperty(exec, propertyName))\n");
        push(@getOwnPropertyDescriptorImpl, "        return false;\n\n");
    }
    
    my $manualLookupGetterGeneration = sub {
514
        my $requiresManualLookup = $dataNode->extendedAttributes->{"IndexedGetter"} || $dataNode->extendedAttributes->{"NamedGetter"};
515
        if ($requiresManualLookup) {
516 517 518
            push(@getOwnPropertyDescriptorImpl, "    const ${namespaceMaybe}HashEntry* entry = ${className}Table.entry(exec, propertyName);\n");
            push(@getOwnPropertyDescriptorImpl, "    if (entry) {\n");
            push(@getOwnPropertyDescriptorImpl, "        PropertySlot slot;\n");
519
            push(@getOwnPropertyDescriptorImpl, "        slot.setCustom(thisObject, entry->propertyGetter());\n");
520 521 522 523 524
            push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(slot.getValue(exec, propertyName), entry->attributes());\n");
            push(@getOwnPropertyDescriptorImpl, "        return true;\n");
            push(@getOwnPropertyDescriptorImpl, "    }\n");
        }
    };
525 526

    if (!$dataNode->extendedAttributes->{"CustomNamedGetter"}) {
527 528
        &$manualLookupGetterGeneration();
    }
529

530
    if ($dataNode->extendedAttributes->{"IndexedGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
531
        push(@getOwnPropertyDescriptorImpl, "    bool ok;\n");
532
        push(@getOwnPropertyDescriptorImpl, "    unsigned index = propertyName.toUInt32(ok);\n");
533
        push(@getOwnPropertyDescriptorImpl, "    if (ok && index < static_cast<$implClassName*>(thisObject->impl())->length()) {\n");
534
        if ($dataNode->extendedAttributes->{"HasNumericIndexGetter"}) {
535
            # Assume that if there's a setter, the index will be writable
536
            if ($dataNode->extendedAttributes->{"CustomIndexedSetter"}) {
537
                push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(thisObject->getByIndex(exec, index), ${namespaceMaybe}DontDelete);\n");
538
            } else {
539
                push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(thisObject->getByIndex(exec, index), ${namespaceMaybe}DontDelete | ${namespaceMaybe}ReadOnly);\n");
540 541 542
            }
        } else {
            push(@getOwnPropertyDescriptorImpl, "        ${namespaceMaybe}PropertySlot slot;\n");
543
            push(@getOwnPropertyDescriptorImpl, "        slot.setCustomIndex(thisObject, index, indexGetter);\n");
544
            # Assume that if there's a setter, the index will be writable
545
            if ($dataNode->extendedAttributes->{"CustomIndexedSetter"}) {
546 547 548 549 550 551 552 553
                push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(slot.getValue(exec, propertyName), ${namespaceMaybe}DontDelete);\n");
            } else {
                push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(slot.getValue(exec, propertyName), ${namespaceMaybe}DontDelete | ${namespaceMaybe}ReadOnly);\n");
            }
        }
        push(@getOwnPropertyDescriptorImpl, "        return true;\n");
        push(@getOwnPropertyDescriptorImpl, "    }\n");
    }
554 555

    if ($dataNode->extendedAttributes->{"NamedGetter"} || $dataNode->extendedAttributes->{"CustomNamedGetter"}) {
556
        push(@getOwnPropertyDescriptorImpl, "    if (canGetItemsForName(exec, static_cast<$implClassName*>(thisObject->impl()), propertyName)) {\n");
557
        push(@getOwnPropertyDescriptorImpl, "        ${namespaceMaybe}PropertySlot slot;\n");
558
        push(@getOwnPropertyDescriptorImpl, "        slot.setCustom(thisObject, nameGetter);\n");
559 560 561 562
        push(@getOwnPropertyDescriptorImpl, "        descriptor.setDescriptor(slot.getValue(exec, propertyName), ReadOnly | DontDelete | DontEnum);\n");
        push(@getOwnPropertyDescriptorImpl, "        return true;\n");
        push(@getOwnPropertyDescriptorImpl, "    }\n");
        if ($inlined) {
563
            $headerIncludes{"wtf/text/AtomicString.h"} = 1;
564
        } else {
565
            $implIncludes{"wtf/text/AtomicString.h"} = 1;
566 567
        }
    }
568 569

    if ($dataNode->extendedAttributes->{"CustomNamedGetter"}) {
570 571
        &$manualLookupGetterGeneration();
    }
572

573
    if ($dataNode->extendedAttributes->{"JSCustomGetOwnPropertySlotDelegate"}) {
574
        push(@getOwnPropertyDescriptorImpl, "    if (thisObject->getOwnPropertyDescriptorDelegate(exec, propertyName, descriptor))\n");
575 576
        push(@getOwnPropertyDescriptorImpl, "        return true;\n");
    }
577

578 579
    if ($hasAttributes) {
        if ($inlined) {
580
            die "Cannot inline if NoStaticTables is set." if ($dataNode->extendedAttributes->{"JSNoStaticTables"});
581
            push(@getOwnPropertyDescriptorImpl, "    return ${namespaceMaybe}getStaticValueDescriptor<$className, Base>(exec, s_info.staticPropHashTable, thisObject, propertyName, descriptor);\n");
582
        } else {
583
            push(@getOwnPropertyDescriptorImpl, "    return ${namespaceMaybe}getStaticValueDescriptor<$className, Base>(exec, " . hashTableAccessor($dataNode->extendedAttributes->{"JSNoStaticTables"}, $className) . ", thisObject, propertyName, descriptor);\n");
584 585
        }
    } else {
586
        push(@getOwnPropertyDescriptorImpl, "    return Base::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);\n");
587 588 589 590 591
    }
    
    return @getOwnPropertyDescriptorImpl;
}

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
sub GenerateHeaderContentHeader
{
    my $dataNode = shift;
    my $className = "JS" . $dataNode->name;

    my @headerContentHeader = split("\r", $headerTemplate);

    # - Add header protection
    push(@headerContentHeader, "\n#ifndef $className" . "_h");
    push(@headerContentHeader, "\n#define $className" . "_h\n\n");

    my $conditionalString = GenerateConditionalString($dataNode);
    push(@headerContentHeader, "#if ${conditionalString}\n\n") if $conditionalString;
    return @headerContentHeader;
}

sub GenerateImplementationContentHeader
{
    my $dataNode = shift;
    my $className = "JS" . $dataNode->name;

    my @implContentHeader = split("\r", $headerTemplate);

    push(@implContentHeader, "\n#include \"config.h\"\n");
    my $conditionalString = GenerateConditionalString($dataNode);
    push(@implContentHeader, "\n#if ${conditionalString}\n\n") if $conditionalString;
    push(@implContentHeader, "#include \"$className.h\"\n\n");
    return @implContentHeader;
}

622 623 624 625 626 627 628 629 630
my %usesToJSNewlyCreated = (
    "CDATASection" => 1,
    "Element" => 1,
    "Node" => 1,
    "Text" => 1,
    "Touch" => 1,
    "TouchList" => 1
);

631 632 633
sub GetFunctionName
{
    my ($className, $function) = @_;
634
    my $kind = $function->isStatic ? "Constructor" : "Prototype";
635 636 637
    return $codeGenerator->WK_lcfirst($className) . $kind . "Function" . $codeGenerator->WK_ucfirst($function->signature->name);
}

638 639
sub GenerateHeader
{
640 641
    my $object = shift;
    my $dataNode = shift;
642

643 644 645
    my $interfaceName = $dataNode->name;
    my $className = "JS$interfaceName";
    my $implClassName = $interfaceName;
646
    my @ancestorInterfaceNames = ();
647
    my %structureFlags = ();
648

649 650 651
    # We only support multiple parents with SVG (for now).
    if (@{$dataNode->parents} > 1) {
        die "A class can't have more than one parent" unless $interfaceName =~ /SVG/;
652
        $codeGenerator->AddMethodsConstantsAndAttributesFromParentClasses($dataNode, \@ancestorInterfaceNames);
653
    }
weinig's avatar
weinig committed
654

655
    my $hasLegacyParent = $dataNode->extendedAttributes->{"JSLegacyParent"};
656 657 658
    my $hasRealParent = @{$dataNode->parents} > 0;
    my $hasParent = $hasLegacyParent || $hasRealParent;
    my $parentClassName = GetParentClassName($dataNode);
659
    my $eventTarget = $dataNode->extendedAttributes->{"EventTarget"};
660 661
    my $needsMarkChildren = $dataNode->extendedAttributes->{"JSCustomMarkFunction"} || $dataNode->extendedAttributes->{"EventTarget"};

662 663
    # - Add default header template and header protection
    push(@headerContentHeader, GenerateHeaderContentHeader($dataNode));
weinig's avatar
weinig committed
664

weinig@apple.com's avatar
weinig@apple.com committed
665
    if ($hasParent) {
666
        $headerIncludes{"$parentClassName.h"} = 1;
667
    } else {
668
        $headerIncludes{"JSDOMBinding.h"} = 1;
669 670
        $headerIncludes{"<runtime/JSGlobalObject.h>"} = 1;
        $headerIncludes{"<runtime/ObjectPrototype.h>"} = 1;
671
    }
weinig's avatar
weinig committed
672

673
    if ($dataNode->extendedAttributes->{"CustomCall"}) {
674
        $headerIncludes{"<runtime/CallData.h>"} = 1;
675 676
    }

677
    if ($dataNode->extendedAttributes->{"JSInlineGetOwnPropertySlot"}) {
678 679
        $headerIncludes{"<runtime/Lookup.h>"} = 1;
        $headerIncludes{"<wtf/AlwaysInline.h>"} = 1;
680 681
    }

682
    if ($hasParent && $dataNode->extendedAttributes->{"JSGenerateToNativeObject"}) {
683 684 685 686 687
        if (IsTypedArrayType($implClassName)) {
            $headerIncludes{"<wtf/$implClassName.h>"} = 1;
        } else {
            $headerIncludes{"$implClassName.h"} = 1;
        }
688
    }
689
    
690
    $headerIncludes{"<runtime/JSObject.h>"} = 1;
691 692
    $headerIncludes{"SVGElement.h"} = 1 if $className =~ /^JSSVG/;

693
    my $implType = $implClassName;
694 695
    my ($svgPropertyType, $svgListPropertyType, $svgNativeType) = GetSVGPropertyTypes($implType);
    $implType = $svgNativeType if $svgNativeType;
696

697 698 699
    my $svgPropertyOrListPropertyType;
    $svgPropertyOrListPropertyType = $svgPropertyType if $svgPropertyType;
    $svgPropertyOrListPropertyType = $svgListPropertyType if $svgListPropertyType;
zimmermann's avatar
zimmermann committed
700

weinig's avatar
weinig committed
701
    my $numConstants = @{$dataNode->constants};
702 703
    my $numAttributes = @{$dataNode->attributes};
    my $numFunctions = @{$dataNode->functions};
weinig's avatar
weinig committed
704

705
    push(@headerContent, "\nnamespace WebCore {\n\n");
weinig's avatar
weinig committed
706

707
    if ($codeGenerator->IsSVGAnimatedType($implClassName)) {
708 709 710
        $headerIncludes{"$implClassName.h"} = 1;
    } else {
        # Implementation class forward declaration
711 712 713
        if ($interfaceName eq "DOMWindow" || $dataNode->extendedAttributes->{"IsWorkerContext"}) {
            AddClassForwardIfNeeded($implClassName) unless $svgPropertyOrListPropertyType;
        }
714
    }
715

716
    AddClassForwardIfNeeded("JSDOMWindowShell") if $interfaceName eq "DOMWindow";
717
    AddClassForwardIfNeeded("JSDictionary") if IsConstructorTemplate($dataNode, "Event");
718

719 720
    # Class declaration
    push(@headerContent, "class $className : public $parentClassName {\n");
weinig's avatar
weinig committed
721

722 723
    # Static create methods
    push(@headerContent, "public:\n");
724
    push(@headerContent, "    typedef $parentClassName Base;\n");
725
    if ($interfaceName eq "DOMWindow") {
726 727
        push(@headerContent, "    static $className* create(JSC::JSGlobalData& globalData, JSC::Structure* structure, PassRefPtr<$implType> impl, JSDOMWindowShell* windowShell)\n");
        push(@headerContent, "    {\n");
728
        push(@headerContent, "        $className* ptr = new (NotNull, JSC::allocateCell<$className>(globalData.heap)) ${className}(globalData, structure, impl, windowShell);\n");
729
        push(@headerContent, "        ptr->finishCreation(globalData, windowShell);\n");
730
        push(@headerContent, "        return ptr;\n");
731
        push(@headerContent, "    }\n\n");
732
    } elsif ($dataNode->extendedAttributes->{"IsWorkerContext"}) {
733 734
        push(@headerContent, "    static $className* create(JSC::JSGlobalData& globalData, JSC::Structure* structure, PassRefPtr<$implType> impl)\n");
        push(@headerContent, "    {\n");
735
        push(@headerContent, "        $className* ptr = new (NotNull, JSC::allocateCell<$className>(globalData.heap)) ${className}(globalData, structure, impl);\n");
736
        push(@headerContent, "        ptr->finishCreation(globalData);\n");
737
        push(@headerContent, "        return ptr;\n");
738
        push(@headerContent, "    }\n\n");
739
    } else {
740 741 742
        AddIncludesForTypeInHeader($implType) unless $svgPropertyOrListPropertyType;
        push(@headerContent, "    static $className* create(JSC::Structure* structure, JSDOMGlobalObject* globalObject, PassRefPtr<$implType> impl)\n");
        push(@headerContent, "    {\n");
743
        push(@headerContent, "        $className* ptr = new (NotNull, JSC::allocateCell<$className>(globalObject->globalData().heap)) $className(structure, globalObject, impl);\n");
744 745
        push(@headerContent, "        ptr->finishCreation(globalObject->globalData());\n");
        push(@headerContent, "        return ptr;\n");
746
        push(@headerContent, "    }\n\n");
747
    }
weinig's avatar
weinig committed
748

darin@apple.com's avatar
darin@apple.com committed
749
    # Prototype
weinig@apple.com's avatar
weinig@apple.com committed
750
    push(@headerContent, "    static JSC::JSObject* createPrototype(JSC::ExecState*, JSC::JSGlobalObject*);\n") unless ($dataNode->extendedAttributes->{"ExtendsDOMGlobalObject"});
751

752 753 754
    $headerTrailingIncludes{"${className}Custom.h"} = 1 if $dataNode->extendedAttributes->{"CustomHeader"};

    $implIncludes{"${className}Custom.h"} = 1 if !$dataNode->extendedAttributes->{"CustomHeader"} && ($dataNode->extendedAttributes->{"CustomPutFunction"} || $dataNode->extendedAttributes->{"DelegatingPutFunction"});
755

756
    my $hasGetter = $numAttributes > 0
757
                 || !$dataNode->extendedAttributes->{"OmitConstructor"}
758
                 || $dataNode->extendedAttributes->{"IndexedGetter"}
759
                 || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}
760
                 || $dataNode->extendedAttributes->{"CustomGetOwnPropertySlot"}
761
                 || $dataNode->extendedAttributes->{"JSCustomGetOwnPropertySlotDelegate"}
762
                 || $dataNode->extendedAttributes->{"NamedGetter"}
763
                 || $dataNode->extendedAttributes->{"CustomNamedGetter"};
764

765
    # Getters
766
    if ($hasGetter) {
767
        push(@headerContent, "    static bool getOwnPropertySlot(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&);\n");
768
        push(@headerContent, "    static bool getOwnPropertyDescriptor(JSC::JSObject*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor&);\n");
769
        push(@headerContent, "    static bool getOwnPropertySlotByIndex(JSC::JSCell*, JSC::ExecState*, unsigned propertyName, JSC::PropertySlot&);\n") if ($dataNode->extendedAttributes->{"IndexedGetter"} || $dataNode->extendedAttributes->{"HasNumericIndexGetter"}) && !$dataNode->extendedAttributes->{"CustomNamedGetter"};
770 771
        push(@headerContent, "    bool getOwnPropertySlotDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::PropertySlot&);\n") if $dataNode->extendedAttributes->{"JSCustomGetOwnPropertySlotDelegate"};
        push(@headerContent, "    bool getOwnPropertyDescriptorDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::PropertyDescriptor&);\n") if $dataNode->extendedAttributes->{"JSCustomGetOwnPropertySlotDelegate"};
772
        $structureFlags{"JSC::OverridesGetOwnPropertySlot"} = 1;
773
    }
weinig's avatar
weinig committed
774

775 776
    # Check if we have any writable properties
    my $hasReadWriteProperties = 0;
weinig's avatar
weinig committed
777 778
    foreach (@{$dataNode->attributes}) {
        if ($_->type !~ /^readonly\ attribute$/) {
779 780 781
            $hasReadWriteProperties = 1;
        }
    }
weinig's avatar
weinig committed
782

783 784
    my $hasSetter = $hasReadWriteProperties
                 || $dataNode->extendedAttributes->{"CustomPutFunction"}
785
                 || $dataNode->extendedAttributes->{"DelegatingPutFunction"}
786
                 || $dataNode->extendedAttributes->{"CustomIndexedSetter"};
787 788 789

    # Getters
    if ($hasSetter) {
790
        push(@headerContent, "    static void put(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue, JSC::PutPropertySlot&);\n");
791
        push(@headerContent, "    static void putByIndex(JSC::JSCell*, JSC::ExecState*, unsigned propertyName, JSC::JSValue);\n") if $dataNode->extendedAttributes->{"CustomIndexedSetter"};
792
        push(@headerContent, "    bool putDelegate(JSC::ExecState*, const JSC::Identifier&, JSC::JSValue, JSC::PutPropertySlot&);\n") if $dataNode->extendedAttributes->{"DelegatingPutFunction"};
793
    }
weinig's avatar
weinig committed
794

795 796 797 798
    if (!$hasParent) {
        push(@headerContent, "    static void destroy(JSC::JSCell*);\n");
    }

799
    # Class info
800
    push(@headerContent, "    static const JSC::ClassInfo s_info;\n\n");
801

mjs@apple.com's avatar
mjs@apple.com committed
802 803
    # Structure ID
    if ($interfaceName eq "DOMWindow") {
804
        $structureFlags{"JSC::ImplementsHasInstance"} = 1;
mjs@apple.com's avatar
mjs@apple.com committed
805
    }
806 807 808 809 810 811 812 813
    push(@headerContent, "    static JSC::Structure* createStructure(JSC::JSGlobalData& globalData, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)\n");
    push(@headerContent, "    {\n");
    if ($interfaceName eq "DOMWindow" || $dataNode->extendedAttributes->{"IsWorkerContext"}) {
        push(@headerContent, "        return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::GlobalObjectType, StructureFlags), &s_info);\n");
    } else {
        push(@headerContent, "        return JSC::Structure::create(globalData, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), &s_info);\n");
    }
    push(@headerContent, "    }\n\n");
darin@apple.com's avatar
darin@apple.com committed
814

weinig's avatar
weinig committed
815
    # Custom pushEventHandlerScope function
816
    push(@headerContent, "    JSC::ScopeChainNode* pushEventHandlerScope(JSC::ExecState*, JSC::ScopeChainNode*) const;\n\n") if $dataNode->extendedAttributes->{"JSCustomPushEventHandlerScope"};
weinig's avatar
weinig committed
817

weinig's avatar
weinig committed
818
    # Custom call functions
819
    push(@headerContent, "    static JSC::CallType getCallData(JSC::JSCell*, JSC::CallData&);\n\n") if $dataNode->extendedAttributes->{"CustomCall"};
820

weinig@apple.com's avatar
weinig@apple.com committed
821
    # Custom deleteProperty function
822
    push(@headerContent, "    static bool deleteProperty(JSC::JSCell*, JSC::ExecState*, const JSC::Identifier&);\n") if $dataNode->extendedAttributes->{"CustomDeleteProperty"};
weinig@apple.com's avatar
weinig@apple.com committed
823

oliver@apple.com's avatar
oliver@apple.com committed
824
    # Custom getPropertyNames function exists on DOMWindow
825
    if ($interfaceName eq "DOMWindow") {
826
        push(@headerContent, "    static void getPropertyNames(JSC::JSObject*, JSC::ExecState*, JSC::PropertyNameArray&, JSC::EnumerationMode mode = JSC::ExcludeDontEnumProperties);\n");