4 # Copyright (C) 2005 Nikolas Zimmermann <wildfox@kde.org>
5 # Copyright (C) 2006 Anders Carlsson <andersca@mac.com>
6 # Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
7 # Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org>
8 # Copyright (C) 2006 Apple Computer, Inc.
10 # This file is part of the KDE project
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.
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.
22 # You should have received a copy of the GNU Library General Public License
23 # aint with this library; see the file COPYING.LIB. If not, write to
24 # the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 # Boston, MA 02111-1307, USA.
28 package CodeGeneratorJS;
35 my @headerContent = ();
36 my @implContentHeader = ();
38 my %implIncludes = ();
41 my $headerTemplate = << "EOF";
43 This file is part of the WebKit open source project.
44 This file has been generated by generate-bindings.pl. DO NOT MODIFY!
46 This library is free software; you can redistribute it and/or
47 modify it under the terms of the GNU Library General Public
48 License as published by the Free Software Foundation; either
49 version 2 of the License, or (at your option) any later version.
51 This library is distributed in the hope that it will be useful,
52 but WITHOUT ANY WARRANTY; without even the implied warranty of
53 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
54 Library General Public License for more details.
56 You should have received a copy of the GNU Library General Public License
57 along with this library; see the file COPYING.LIB. If not, write to
58 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
59 Boston, MA 02111-1307, USA.
69 $codeGenerator = shift;
72 bless($reference, $object);
85 my ($value, $distance) = @_;
86 return (($value << $distance) & 0xFFFFFFFF);
89 # Uppercase the first letter, while respecting WebKit style guidelines.
90 # E.g., xmlEncoding becomes XMLEncoding, but xmlllang becomes Xmllang.
94 my $ret = ucfirst($param);
95 $ret =~ s/Xml/XML/ if $ret =~ /^Xml[^a-z]/;
99 # Params: 'domClass' struct
100 sub GenerateInterface
103 my $dataNode = shift;
106 # Start actual generation
107 $object->GenerateHeader($dataNode);
108 $object->GenerateImplementation($dataNode);
110 my $name = $dataNode->name;
112 # Open files for writing
113 my $headerFileName = "$outputDir/JS$name.h";
114 my $implFileName = "$outputDir/JS$name.cpp";
116 open($IMPL, ">$implFileName") || die "Couldn't open file $implFileName";
117 open($HEADER, ">$headerFileName") || die "Couldn't open file $headerFileName";
120 # Params: 'idlDocument' struct
124 my $dataNode = shift;
126 $module = $dataNode->module;
129 sub GetParentClassName
131 my $dataNode = shift;
133 return $dataNode->extendedAttributes->{"LegacyParent"} if $dataNode->extendedAttributes->{"LegacyParent"};
134 return "KJS::DOMObject" if @{$dataNode->parents} eq 0;
135 return "JS" . $codeGenerator->StripModule($dataNode->parents(0));
138 sub GetLegacyHeaderIncludes
140 my $legacyParent = shift;
142 return "#include \"JSHTMLInputElementBase.h\"\n\n" if $legacyParent eq "JSHTMLInputElementBase";
143 return "#include \"kjs_window.h\"\n\n" if $legacyParent eq "KJS::Window";
144 return "#include \"kjs_domnode.h\"\n\n" if $legacyParent eq "KJS::DOMNode";
145 return "#include \"kjs_events.h\"\n\n" if $module eq "events";
146 return "#include \"kjs_dom.h\"\n\n" if $module eq "core";
147 return "#include \"kjs_css.h\"\n\n" if $module eq "css";
148 return "#include \"kjs_html.h\"\n\n" if $module eq "html";
149 return "#include \"kjs_traversal.h\"\n\n" if $module eq "traversal";
151 die "Don't know what headers to include for module $module";
154 sub AvoidInclusionOfType
158 # Special case: SVGRect.h / SVGPoint.h / SVGNumber.h do not exist.
159 return 1 if $type eq "SVGRect" or $type eq "SVGPoint" or $type eq "SVGNumber";
163 sub AddIncludesForType
165 my $type = $codeGenerator->StripModule(shift);
167 # When we're finished with the one-file-per-class
168 # reorganization, we won't need these special cases.
169 if ($codeGenerator->IsPrimitiveType($type) or AvoidInclusionOfType($type)
170 or $type eq "DOMString" or $type eq "DOMObject" or $type eq "RGBColor" or $type eq "Rect") {
171 } elsif ($type =~ /SVGPathSeg/) {
173 $joinedName =~ s/Abs|Rel//;
174 $implIncludes{"${joinedName}.h"} = 1;
176 # default, include the same named file
177 $implIncludes{"${type}.h"} = 1;
180 # additional includes (things needed to compile the bindings but not the header)
182 if ($type eq "CanvasRenderingContext2D") {
183 $implIncludes{"CanvasGradient.h"} = 1;
184 $implIncludes{"CanvasPattern.h"} = 1;
185 $implIncludes{"CanvasStyle.h"} = 1;
188 if ($type eq "CanvasGradient" or $type eq "XPathNSResolver") {
189 $implIncludes{"PlatformString.h"} = 1;
193 sub AddIncludesForSVGAnimatedType
196 $type =~ s/SVGAnimated//;
198 if ($type eq "SVGRect" or $type eq "SVGPoint" or $type eq "SVGNumber") {
199 $implIncludes{"JSSVG$type.h"} = 1;
200 } elsif ($type eq "String") {
201 $implIncludes{"PlatformString.h"} = 1;
205 sub AddClassForwardIfNeeded
207 my $implClassName = shift;
209 # SVGAnimatedLength/Number/etc.. are typedefs to SVGAnimtatedTemplate, so don't use class forwards for them!
210 push(@headerContent, "class $implClassName;\n\n") unless $codeGenerator->IsSVGAnimatedType($implClassName);
213 sub HashValueForClassAndName
218 # SVG Filter enums live in WebCore namespace (platform/graphics/)
219 if ($class =~ /^SVGFE*/ or $class =~ /^SVGComponentTransferFunctionElement$/) {
220 return "WebCore::$name";
223 return "${class}::$name";
229 my $dataNode = shift;
231 my $interfaceName = $dataNode->name;
232 my $className = "JS$interfaceName";
233 my $implClassName = $interfaceName;
235 # We only support multiple parents with SVG (for now).
236 if (@{$dataNode->parents} > 1) {
237 die "A class can't have more than one parent" unless $interfaceName =~ /SVG/;
238 $codeGenerator->AddMethodsConstantsAndAttributesFromParentClasses($dataNode);
241 my $hasLegacyParent = $dataNode->extendedAttributes->{"LegacyParent"};
242 my $hasRealParent = @{$dataNode->parents} > 0;
243 my $hasParent = $hasLegacyParent || $hasRealParent;
244 my $parentClassName = GetParentClassName($dataNode);
245 my $conditional = $dataNode->extendedAttributes->{"Conditional"};
247 # - Add default header template
248 @headerContent = split("\r", $headerTemplate);
250 # - Add header protection
251 push(@headerContent, "\n#ifndef $className" . "_H");
252 push(@headerContent, "\n#define $className" . "_H\n\n");
254 push(@headerContent, "#ifdef ${conditional}_SUPPORT\n\n") if $conditional;
256 if (exists $dataNode->extendedAttributes->{"LegacyParent"}) {
257 push(@headerContent, GetLegacyHeaderIncludes($dataNode->extendedAttributes->{"LegacyParent"}));
260 push(@headerContent, "#include \"$parentClassName.h\"\n");
262 push(@headerContent, "#include \"kjs_binding.h\"\n");
266 my $numConstants = @{$dataNode->constants};
267 my $numAttributes = @{$dataNode->attributes};
268 my $numFunctions = @{$dataNode->functions};
270 push(@headerContent, "\nnamespace WebCore {\n\n");
272 # Implementation class forward declaration
273 AddClassForwardIfNeeded($implClassName);
276 push(@headerContent, "class $className : public $parentClassName {\n");
277 push(@headerContent, "public:\n");
280 if ($dataNode->extendedAttributes->{"DoNotCache"}) {
281 push(@headerContent, " $className($implClassName*);\n");
283 push(@headerContent, " $className(KJS::ExecState*, $implClassName*);\n");
287 if (!$hasParent or $interfaceName eq "Document") {
288 push(@headerContent, " virtual ~$className();\n");
292 if ($numAttributes > 0) {
293 push(@headerContent, " virtual bool getOwnPropertySlot(KJS::ExecState*, const KJS::Identifier&, KJS::PropertySlot&);\n");
294 push(@headerContent, " KJS::JSValue* getValueProperty(KJS::ExecState*, int token) const;\n");
297 # Check if we have any writable properties
298 my $hasReadWriteProperties = 0;
299 foreach (@{$dataNode->attributes}) {
300 if ($_->type !~ /^readonly\ attribute$/) {
301 $hasReadWriteProperties = 1;
305 if ($hasReadWriteProperties) {
306 push(@headerContent, " virtual void put(KJS::ExecState*, const KJS::Identifier&, KJS::JSValue*, int attr = KJS::None);\n");
307 push(@headerContent, " void putValueProperty(KJS::ExecState*, int, KJS::JSValue*, int attr);\n");
311 push(@headerContent, " virtual const KJS::ClassInfo* classInfo() const { return &info; }\n");
312 push(@headerContent, " static const KJS::ClassInfo info;\n");
314 # Custom mark function
315 if ($dataNode->extendedAttributes->{"CustomMarkFunction"}) {
316 push(@headerContent, "\n virtual void mark();\n\n");
319 # Constructor object getter
320 if ($dataNode->extendedAttributes->{"GenerateConstructor"}) {
321 push(@headerContent, " static KJS::JSValue* getConstructor(KJS::ExecState*);\n")
324 my $numCustomFunctions = 0;
325 my $numCustomAttributes = 0;
327 # Attribute and function enums
328 if ($numAttributes + $numFunctions > 0) {
329 push(@headerContent, " enum {\n")
332 if ($numAttributes > 0) {
333 push(@headerContent, " // Attributes\n ");
336 foreach (@{$dataNode->attributes}) {
339 $numCustomAttributes++ if $attribute->signature->extendedAttributes->{"Custom"};
342 if ((($i % 4) eq 0) and ($i ne 0)) {
343 push(@headerContent, "\n ");
346 my $value = $attribute->signature->type =~ /Constructor$/
347 ? $attribute->signature->name . "ConstructorAttrNum"
348 : WK_ucfirst($attribute->signature->name) . "AttrNum";
349 $value .= ", " if (($i < $numAttributes - 1));
350 $value .= ", " if (($i eq $numAttributes - 1) and ($numFunctions ne 0));
351 push(@headerContent, $value);
355 if ($numFunctions > 0) {
356 push(@headerContent, "\n\n") if $numAttributes > 0;
357 push(@headerContent," // Functions\n ");
360 foreach my $function (@{$dataNode->functions}) {
363 push(@headerContent, "\n ") if ((($i % 4) eq 0) and ($i ne 0));
365 $numCustomFunctions++ if $function->signature->extendedAttributes->{"Custom"};
367 my $value = WK_ucfirst($function->signature->name) . "FuncNum";
368 $value .= ", " if ($i < $numFunctions - 1);
369 push(@headerContent, $value);
373 push(@headerContent, "\n };\n") if ($numAttributes + $numFunctions > 0);
375 if ($numCustomAttributes > 0) {
376 push(@headerContent, "\n // Custom attributes\n");
378 foreach my $attribute (@{$dataNode->attributes}) {
379 if ($attribute->signature->extendedAttributes->{"Custom"}) {
380 push(@headerContent, " KJS::JSValue* " . $attribute->signature->name . "(KJS::ExecState*) const;\n");
381 if ($attribute->type !~ /^readonly/) {
382 push(@headerContent, " void set" . WK_ucfirst($attribute->signature->name) . "(KJS::ExecState*, KJS::JSValue*);\n");
388 if ($numCustomFunctions > 0) {
389 push(@headerContent, "\n // Custom functions\n");
390 foreach my $function (@{$dataNode->functions}) {
391 if ($function->signature->extendedAttributes->{"Custom"}) {
392 push(@headerContent, " KJS::JSValue* " . $function->signature->name . "(KJS::ExecState*, const KJS::List&);\n");
398 if ($dataNode->extendedAttributes->{"HasCustomIndexSetter"}) {
399 push(@headerContent, " void indexSetter(KJS::ExecState*, const KJS::Identifier &propertyName, KJS::JSValue*, int attr);\n");
403 push(@headerContent, " $implClassName* impl() const { return m_impl.get(); }\n");
404 push(@headerContent, "private:\n");
405 push(@headerContent, " RefPtr<$implClassName> m_impl;\n");
406 } elsif ($dataNode->extendedAttributes->{"GenerateNativeConverter"}) {
407 push(@headerContent, " $implClassName* impl() const;\n");
411 if ($dataNode->extendedAttributes->{"HasIndexGetter"}) {
412 push(@headerContent, "private:\n");
413 push(@headerContent, " static KJS::JSValue* indexGetter(KJS::ExecState*, KJS::JSObject*, const KJS::Identifier&, const KJS::PropertySlot&);\n");
416 if ($dataNode->extendedAttributes->{"HasNameGetter"} || $dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
417 push(@headerContent, "private:\n");
418 push(@headerContent, " static KJS::JSValue* nameGetter(KJS::ExecState*, KJS::JSObject*, const KJS::Identifier&, const KJS::PropertySlot&);\n");
419 push(@headerContent, " static bool canGetItemsForName(KJS::ExecState*, $implClassName*, const AtomicString&);\n")
422 push(@headerContent, "};\n\n");
425 push(@headerContent, "KJS::JSValue* toJS(KJS::ExecState*, $implClassName*);\n");
427 if (!$hasParent || $dataNode->extendedAttributes->{"GenerateNativeConverter"}) {
428 push(@headerContent, "$implClassName* to${interfaceName}(KJS::JSValue*);\n");
430 push(@headerContent, "\n");
432 # Add prototype declaration -- code adopted from the KJS_DEFINE_PROTOTYPE and KJS_DEFINE_PROTOTYPE_WITH_PROTOTYPE macros
433 push(@headerContent, "class ${className}Proto : public KJS::JSObject {\n");
434 push(@headerContent, "public:\n");
435 if ($dataNode->extendedAttributes->{"DoNotCache"}) {
436 push(@headerContent, " static KJS::JSObject* self();\n");
438 push(@headerContent, " static KJS::JSObject* self(KJS::ExecState* exec);\n");
440 push(@headerContent, " virtual const KJS::ClassInfo* classInfo() const { return &info; }\n");
441 push(@headerContent, " static const KJS::ClassInfo info;\n");
442 if ($numFunctions > 0 || $numConstants > 0) {
443 push(@headerContent, " bool getOwnPropertySlot(KJS::ExecState*, const KJS::Identifier&, KJS::PropertySlot&);\n");
445 if ($numConstants ne 0) {
446 push(@headerContent, " KJS::JSValue* getValueProperty(KJS::ExecState*, int token) const;\n");
448 if ($dataNode->extendedAttributes->{"DoNotCache"}) {
449 push(@headerContent, " ${className}Proto() { }\n");
451 push(@headerContent, " ${className}Proto(KJS::ExecState* exec)\n");
452 if ($hasParent && $parentClassName ne "KJS::DOMCSSRule" && $parentClassName ne "KJS::DOMNodeFilter") {
453 push(@headerContent, " : KJS::JSObject(${parentClassName}Proto::self(exec)) { }\n");
455 push(@headerContent, " : KJS::JSObject(exec->lexicalInterpreter()->builtinObjectPrototype()) { }\n");
459 push(@headerContent, "};\n\n");
461 push(@headerContent, "}\n\n");
463 push(@headerContent, "#endif // ${conditional}_SUPPORT\n\n") if $conditional;
465 push(@headerContent, "#endif\n");
468 sub GenerateImplementation
471 my $dataNode = shift;
473 my $interfaceName = $dataNode->name;
474 my $className = "JS$interfaceName";
475 my $implClassName = $interfaceName;
477 my $hasLegacyParent = $dataNode->extendedAttributes->{"LegacyParent"};
478 my $hasRealParent = @{$dataNode->parents} > 0;
479 my $hasParent = $hasLegacyParent || $hasRealParent;
480 my $parentClassName = GetParentClassName($dataNode);
481 my $conditional = $dataNode->extendedAttributes->{"Conditional"};
483 # - Add default header template
484 @implContentHeader = split("\r", $headerTemplate);
485 push(@implContentHeader, "\n#include \"config.h\"\n\n");
487 push(@implContentHeader, "#ifdef ${conditional}_SUPPORT\n\n") if $conditional;
489 if ($className =~ /^JSSVGAnimated/) {
490 AddIncludesForSVGAnimatedType($interfaceName);
493 push(@implContentHeader, "#include \"SVGAnimatedTemplate.h\"\n") if ($className =~ /SVG/);
494 push(@implContentHeader, "#include \"$className.h\"\n\n");
495 push(@implContentHeader, "#include <wtf/GetPtr.h>\n\n");
497 AddIncludesForType($interfaceName);
501 push(@implContent, "\nusing namespace KJS;\n\n");
502 push(@implContent, "namespace WebCore {\n\n");
504 # - Add all attributes in a hashtable definition
505 my $numAttributes = @{$dataNode->attributes};
506 if ($numAttributes > 0) {
507 my $hashSize = $numAttributes;
508 my $hashName = $className . "Table";
510 my @hashKeys = (); # ie. 'insertBefore'
511 my @hashValues = (); # ie. 'JSNode::InsertBefore'
512 my @hashSpecials = (); # ie. 'DontDelete|Function'
513 my @hashParameters = (); # ie. '2'
515 foreach my $attribute (@{$dataNode->attributes}) {
516 my $name = $attribute->signature->name;
517 push(@hashKeys, $name);
519 my $value = $className . "::" . ($attribute->signature->type =~ /Constructor$/
520 ? $attribute->signature->name . "ConstructorAttrNum"
521 : WK_ucfirst($attribute->signature->name) . "AttrNum");
522 push(@hashValues, $value);
524 my $special = "DontDelete";
525 $special .= "|ReadOnly" if ($attribute->type =~ /readonly/);
526 push(@hashSpecials, $special);
528 my $numParameters = "0";
529 push(@hashParameters, $numParameters);
532 $object->GenerateHashTable($hashName, $hashSize,
533 \@hashKeys, \@hashValues,
534 \@hashSpecials, \@hashParameters);
537 my $numConstants = @{$dataNode->constants};
538 my $numFunctions = @{$dataNode->functions};
540 # - Add all constants
541 if ($dataNode->extendedAttributes->{"GenerateConstructor"}) {
542 $hashSize = $numConstants;
543 $hashName = $className . "ConstructorTable";
548 @hashParameters = ();
550 foreach my $constant (@{$dataNode->constants}) {
551 my $name = $constant->name;
552 push(@hashKeys, $name);
554 my $value = HashValueForClassAndName($implClassName, $name);
555 push(@hashValues, $value);
557 my $special = "DontDelete|ReadOnly";
558 push(@hashSpecials, $special);
560 my $numParameters = 0;
561 push(@hashParameters, $numParameters);
564 $object->GenerateHashTable($hashName, $hashSize,
565 \@hashKeys, \@hashValues,
566 \@hashSpecials, \@hashParameters);
569 $protoClassName = "${className}Proto";
571 push(@implContent, constructorFor($className, $protoClassName, $interfaceName, $dataNode->extendedAttributes->{"CanBeConstructed"}));
574 # - Add functions and constants to a hashtable definition
575 $hashSize = $numFunctions + $numConstants;
576 $hashName = $className . "ProtoTable";
581 @hashParameters = ();
583 foreach my $constant (@{$dataNode->constants}) {
584 my $name = $constant->name;
585 push(@hashKeys, $name);
587 my $value = HashValueForClassAndName($implClassName, $name);
588 push(@hashValues, $value);
590 my $special = "DontDelete|ReadOnly";
591 push(@hashSpecials, $special);
593 my $numParameters = 0;
594 push(@hashParameters, $numParameters);
597 foreach my $function (@{$dataNode->functions}) {
598 my $name = $function->signature->name;
599 push(@hashKeys, $name);
601 my $value = $className . "::" . WK_ucfirst($name) . "FuncNum";
602 push(@hashValues, $value);
604 my $special = "DontDelete|Function";
605 push(@hashSpecials, $special);
607 my $numParameters = @{$function->parameters};
608 push(@hashParameters, $numParameters);
611 $object->GenerateHashTable($hashName, $hashSize,
612 \@hashKeys, \@hashValues,
613 \@hashSpecials, \@hashParameters);
615 if ($numFunctions > 0) {
616 push(@implContent, protoFuncFor($className));
619 push(@implContent, "const ClassInfo ${className}Proto::info = { \"$interfaceName\", 0, &${className}ProtoTable, 0 };\n\n");
620 if ($dataNode->extendedAttributes->{"DoNotCache"}) {
621 push(@implContent, "JSObject* ${className}Proto::self()\n");
622 push(@implContent, "{\n");
623 push(@implContent, " return new ${className}Proto();\n");
624 push(@implContent, "}\n\n");
626 push(@implContent, "JSObject* ${className}Proto::self(ExecState* exec)\n");
627 push(@implContent, "{\n");
628 push(@implContent, " return KJS::cacheGlobalObject<${className}Proto>(exec, \"[[${className}.prototype]]\");\n");
629 push(@implContent, "}\n\n");
631 if ($numConstants > 0 || $numFunctions > 0) {
632 push(@implContent, "bool ${className}Proto::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\n");
633 push(@implContent, "{\n");
634 if ($numConstants eq 0) {
635 push(@implContent, " return getStaticFunctionSlot<${className}ProtoFunc, JSObject>(exec, &${className}ProtoTable, this, propertyName, slot);\n");
636 } elsif ($numFunctions eq 0) {
637 push(@implContent, " return getStaticValueSlot<${className}Proto, JSObject>(exec, &${className}ProtoTable, this, propertyName, slot);\n");
639 push(@implContent, " return getStaticPropertySlot<${className}ProtoFunc, ${className}Proto, JSObject>(exec, &${className}ProtoTable, this, propertyName, slot);\n");
641 push(@implContent, "}\n\n");
643 if ($numConstants ne 0) {
644 push(@implContent, "JSValue* ${className}Proto::getValueProperty(ExecState*, int token) const\n{\n");
645 push(@implContent, " // The token is the numeric value of its associated constant\n");
646 push(@implContent, " return jsNumber(token);\n}\n\n");
649 # - Initialize static ClassInfo object
650 push(@implContent, "const ClassInfo $className" . "::info = { \"$interfaceName\", ");
652 push(@implContent, "&" .$parentClassName . "::info, ");
654 push(@implContent, "0, ");
657 if ($numAttributes > 0) {
658 push(@implContent, "&${className}Table, ");
660 push(@implContent, "0, ")
662 push(@implContent, "0 };\n\n");
665 if ($dataNode->extendedAttributes->{"DoNotCache"}) {
666 push(@implContent, "${className}::$className($implClassName* impl)\n");
667 push(@implContent, " : $parentClassName(impl)\n");
669 push(@implContent, "${className}::$className(ExecState* exec, $implClassName* impl)\n");
671 push(@implContent, " : $parentClassName(exec, impl)\n");
673 push(@implContent, " : m_impl(impl)\n");
677 if ($dataNode->extendedAttributes->{"DoNotCache"}) {
678 push(@implContent, "{\n setPrototype(${className}Proto::self());\n}\n\n");
680 push(@implContent, "{\n setPrototype(${className}Proto::self(exec));\n}\n\n");
685 push(@implContent, "${className}::~$className()\n");
686 push(@implContent, "{\n ScriptInterpreter::forgetDOMObject(m_impl.get());\n}\n\n");
689 # Document needs a special destructor because it's a special case for caching. It needs
690 # its own special handling rather than relying on the caching that Node normally does.
691 if ($interfaceName eq "Document") {
692 push(@implContent, "${className}::~$className()\n");
693 push(@implContent, "{\n ScriptInterpreter::forgetDOMObject(static_cast<${implClassName}*>(m_impl.get()));\n}\n\n");
697 if ($numAttributes ne 0) {
698 push(@implContent, "bool ${className}::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)\n");
699 push(@implContent, "{\n");
700 # FIXME: We need to provide scalable hooks/attributes for this kind of extension
701 if ($interfaceName eq "DOMWindow") {
702 push(@implContent, " if (getOverridePropertySlot(exec, propertyName, slot))\n");
703 push(@implContent, " return true;\n");
706 my $hasNameGetterGeneration = sub {
707 push(@implContent, " if (canGetItemsForName(exec, static_cast<$implClassName*>(impl()), propertyName)) {\n");
708 push(@implContent, " slot.setCustom(this, nameGetter);\n");
709 push(@implContent, " return true;\n");
710 push(@implContent, " }\n");
713 if ($dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
714 &$hasNameGetterGeneration();
717 my $requiresManualLookup = $dataNode->extendedAttributes->{"HasIndexGetter"} || $dataNode->extendedAttributes->{"HasNameGetter"};
718 if ($requiresManualLookup) {
719 push(@implContent, " const HashEntry* entry = Lookup::findEntry(&${className}Table, propertyName);\n");
720 push(@implContent, " if (entry) {\n");
721 push(@implContent, " slot.setStaticEntry(this, entry, staticValueGetter<$className>);\n");
722 push(@implContent, " return true;\n");
723 push(@implContent, " }\n");
726 if ($dataNode->extendedAttributes->{"HasNameGetter"} || $dataNode->extendedAttributes->{"HasOverridingNameGetter"}) {
727 # if it has a prototype, we need to check that first too
728 push(@implContent, " if (prototype()->isObject() && static_cast<JSObject*>(prototype())->hasProperty(exec, propertyName))\n");
729 push(@implContent, " return false;\n");
732 if ($dataNode->extendedAttributes->{"HasIndexGetter"}) {
733 push(@implContent, " bool ok;\n");
734 push(@implContent, " unsigned u = propertyName.toUInt32(&ok);\n");
735 push(@implContent, " if (ok && u < static_cast<$implClassName*>(impl())->length()) {\n");
736 push(@implContent, " slot.setCustomIndex(this, u, indexGetter);\n");
737 push(@implContent, " return true;\n");
738 push(@implContent, " }\n");
741 if ($dataNode->extendedAttributes->{"HasNameGetter"}) {
742 &$hasNameGetterGeneration();
745 if ($requiresManualLookup) {
746 push(@implContent, " return ${parentClassName}::getOwnPropertySlot(exec, propertyName, slot);\n");
748 push(@implContent, " return getStaticValueSlot<$className, $parentClassName>(exec, &${className}Table, this, propertyName, slot);\n");
750 push(@implContent, "}\n\n");
752 push(@implContent, "JSValue* ${className}::getValueProperty(ExecState* exec, int token) const\n{\n");
753 push(@implContent, " $implClassName* imp = static_cast<$implClassName*>(impl());\n\n");
754 push(@implContent, " switch (token) {\n");
756 foreach my $attribute (@{$dataNode->attributes}) {
757 my $name = $attribute->signature->name;
759 if ($attribute->signature->extendedAttributes->{"Custom"}) {
760 push(@implContent, " case " . WK_ucfirst($name) . "AttrNum:\n");
761 push(@implContent, " return $name(exec);\n");
762 } elsif ($attribute->signature->type =~ /Constructor$/) {
763 my $constructorType = $codeGenerator->StripModule($attribute->signature->type);
764 $constructorType =~ s/Constructor$//;
766 push(@implContent, " case " . $name . "ConstructorAttrNum:\n");
767 push(@implContent, " return JS" . $constructorType . "::getConstructor(exec);\n");
768 } elsif (!@{$attribute->getterExceptions}) {
769 push(@implContent, " case " . WK_ucfirst($name) . "AttrNum:\n");
770 push(@implContent, " return " . NativeToJSValue($attribute->signature, "imp->$name()") . ";\n");
772 push(@implContent, " case " . WK_ucfirst($name) . "AttrNum: {\n");
773 push(@implContent, " ExceptionCode ec = 0;\n");
774 push(@implContent, " KJS::JSValue* result = " . NativeToJSValue($attribute->signature, "imp->$name(ec)") . ";\n");
775 push(@implContent, " setDOMException(exec, ec);\n");
776 push(@implContent, " return result;\n");
777 push(@implContent, " }\n");
781 push(@implContent, " }\n return 0;\n}\n\n");
783 # Check if we have any writable attributes
784 my $hasReadWriteProperties = 0;
785 foreach my $attribute (@{$dataNode->attributes}) {
786 $hasReadWriteProperties = 1 if $attribute->type !~ /^readonly/;
788 if ($hasReadWriteProperties) {
789 push(@implContent, "void ${className}::put(ExecState* exec, const Identifier& propertyName, JSValue* value, int attr)\n");
790 push(@implContent, "{\n");
791 if ($dataNode->extendedAttributes->{"HasCustomIndexSetter"}) {
792 push(@implContent, " if (!lookupPut<$className>(exec, propertyName, value, attr, &${className}Table, this))\n");
793 push(@implContent, " indexSetter(exec, propertyName, value, attr);\n");
795 push(@implContent, " lookupPut<$className, $parentClassName>(exec, propertyName, value, attr, &${className}Table, this);\n");
797 push(@implContent, "}\n\n");
799 push(@implContent, "void ${className}::putValueProperty(ExecState* exec, int token, JSValue* value, int /*attr*/)\n");
800 push(@implContent, "{\n");
801 push(@implContent, " $implClassName* imp = static_cast<$implClassName*>(impl());\n\n");
802 push(@implContent, " switch (token) {\n");
804 foreach my $attribute (@{$dataNode->attributes}) {
805 if ($attribute->type !~ /^readonly/) {
806 my $name = $attribute->signature->name;
808 if ($attribute->signature->extendedAttributes->{"Custom"}) {
809 push(@implContent, " case " . WK_ucfirst($name) . "AttrNum: {\n");
810 push(@implContent, " set" . WK_ucfirst($name) . "(exec, value);\n");
811 } elsif ($attribute->signature->type =~ /Constructor$/) {
812 my $constructorType = $attribute->signature->type;
813 $constructorType =~ s/Constructor$//;
815 $implIncludes{"JS" . $constructorType . ".h"} = 1;
816 push(@implContent, " case " . $name ."ConstructorAttrNum: {\n");
817 push(@implContent, " // Shadowing a built-in constructor\n");
819 # FIXME: We need to provide scalable hooks/attributes for this kind of extension
820 push(@implContent, " if (isSafeScript(exec))\n");
821 push(@implContent, " JSObject::put(exec, \"$name\", value);\n");
823 push(@implContent, " case " . WK_ucfirst($name) ."AttrNum: {\n");
824 push(@implContent, " ExceptionCode ec = 0;\n") if @{$attribute->setterExceptions};
825 push(@implContent, " imp->set" . WK_ucfirst($name) . "(" . JSValueToNative($attribute->signature, "value"));
826 push(@implContent, ", ec") if @{$attribute->setterExceptions};
827 push(@implContent, ");\n");
828 push(@implContent, " setDOMException(exec, ec);\n") if @{$attribute->setterExceptions};
830 push(@implContent, " break;\n");
831 push(@implContent, " }\n");
834 push(@implContent, " }\n"); # end switch
836 if ($interfaceName eq "DOMWindow") {
837 push(@implContent, " // FIXME: Hack to prevent unused variable warning -- remove once DOMWindow includes a settable property\n");
838 push(@implContent, " (void)imp;\n");
840 push(@implContent, "}\n\n"); # end function
844 if ($dataNode->extendedAttributes->{"GenerateConstructor"}) {
845 push(@implContent, "JSValue* ${className}::getConstructor(ExecState* exec)\n{\n");
846 push(@implContent, " return KJS::cacheGlobalObject<${className}Constructor>(exec, \"[[${interfaceName}.constructor]]\");\n");
847 push(@implContent, "}\n");
851 if ($numFunctions ne 0) {
852 push(@implContent, "JSValue* ${className}ProtoFunc::callAsFunction(ExecState* exec, JSObject* thisObj, const List& args)\n{\n");
853 push(@implContent, " if (!thisObj->inherits(&${className}::info))\n");
854 push(@implContent, " return throwError(exec, TypeError);\n\n");
856 push(@implContent, " $implClassName* imp = static_cast<$implClassName*>(static_cast<$className*>(thisObj)->impl());\n\n");
858 push(@implContent, " switch (id) {\n");
859 foreach my $function (@{$dataNode->functions}) {
860 push(@implContent, " case ${className}::" . WK_ucfirst($function->signature->name) . "FuncNum: {\n");
862 if ($function->signature->extendedAttributes->{"Custom"}) {
863 push(@implContent, " return static_cast<${className}*>(thisObj)->" . $function->signature->name . "(exec, args);\n }\n");
867 AddIncludesForType($function->signature->type);
869 if (@{$function->raisesExceptions}) {
870 push(@implContent, " ExceptionCode ec = 0;\n");
874 my $functionString = "imp->" . $function->signature->name . "(";
875 my $numParameters = @{$function->parameters};
876 my $hasOptionalArguments = 0;
878 foreach my $parameter (@{$function->parameters}) {
879 if (!$hasOptionalArguments && $parameter->extendedAttributes->{"Optional"}) {
880 push(@implContent, "\n int argsCount = args.size();\n");
881 $hasOptionalArguments = 1;
884 if ($hasOptionalArguments) {
885 push(@implContent, " if (argsCount < " . ($paramIndex + 1) . ") {\n");
886 GenerateImplementationFunctionCall($function, $functionString, $paramIndex, " " x 3);
887 push(@implContent, " }\n\n");
890 my $name = $parameter->name;
891 push(@implContent, " bool ${name}Ok;\n") if TypeCanFailConversion($parameter);
892 push(@implContent, " " . GetNativeType($parameter) . " $name = " . JSValueToNative($parameter, "args[$paramIndex]", TypeCanFailConversion($parameter) ? "${name}Ok" : undef) . ";\n");
893 if (TypeCanFailConversion($parameter)) {
894 push(@implContent, " if (!${name}Ok) {\n");
895 push(@implContent, " setDOMException(exec, TYPE_MISMATCH_ERR);\n");
896 push(@implContent, " return jsUndefined();\n }\n");
899 # If a parameter is "an index", it should throw an INDEX_SIZE_ERR
901 if ($parameter->extendedAttributes->{"IsIndex"}) {
902 $implIncludes{"ExceptionCode.h"} = 1;
903 push(@implContent, " if ($name < 0) {\n");
904 push(@implContent, " setDOMException(exec, INDEX_SIZE_ERR);\n");
905 push(@implContent, " return jsUndefined();\n }\n");
908 $functionString .= ", " if $paramIndex;
909 $functionString .= $name;
914 push(@implContent, "\n");
915 GenerateImplementationFunctionCall($function, $functionString, $paramIndex, " " x 2);
917 push(@implContent, " }\n"); # end case
919 push(@implContent, " }\n"); # end switch
920 push(@implContent, " return 0;\n");
921 push(@implContent, "}\n");
924 if ($dataNode->extendedAttributes->{"HasIndexGetter"}) {
925 push(@implContent, "\nJSValue* ${className}::indexGetter(ExecState* exec, JSObject* originalObject, const Identifier& propertyName, const PropertySlot& slot)\n");
926 push(@implContent, "{\n");
927 push(@implContent, " ${className}* thisObj = static_cast<$className*>(slot.slotBase());\n");
928 push(@implContent, " return toJS(exec, static_cast<$implClassName*>(thisObj->impl())->item(slot.index()));\n");
929 push(@implContent, "}\n");
933 push(@implContent, "KJS::JSValue* toJS(KJS::ExecState* exec, $implClassName* obj)\n");
934 push(@implContent, "{\n");
935 push(@implContent, " return KJS::cacheDOMObject<$implClassName, $className>(exec, obj);\n");
936 push(@implContent, "}\n");
939 if (!$hasParent || $dataNode->extendedAttributes->{"GenerateNativeConverter"}) {
940 push(@implContent, "$implClassName* to${interfaceName}(KJS::JSValue* val)\n");
941 push(@implContent, "{\n");
942 push(@implContent, " return val->isObject(&${className}::info) ? static_cast<$className*>(val)->impl() : 0;\n");
943 push(@implContent, "}\n");
946 if ($dataNode->extendedAttributes->{"GenerateNativeConverter"} && $hasParent) {
947 push(@implContent, "\n$implClassName* ${className}::impl() const\n");
948 push(@implContent, "{\n");
949 push(@implContent, " return static_cast<$implClassName*>(${parentClassName}::impl());\n");
950 push(@implContent, "}\n");
953 push(@implContent, "\n}\n");
955 push(@implContent, "\n#endif // ${conditional}_SUPPORT\n") if $conditional;
958 sub GenerateImplementationFunctionCall()
960 my $function = shift;
961 my $functionString = shift;
962 my $paramIndex = shift;
965 if (@{$function->raisesExceptions}) {
966 $functionString .= ", " if $paramIndex;
967 $functionString .= "ec";
969 $functionString .= ")";
971 if ($function->signature->type eq "void") {
972 push(@implContent, $indent . "$functionString;\n");
973 push(@implContent, $indent . "setDOMException(exec, ec);\n") if @{$function->raisesExceptions};
974 push(@implContent, $indent . "return jsUndefined();\n");
976 push(@implContent, "\n" . $indent . "KJS::JSValue* result = " . NativeToJSValue($function->signature, $functionString) . ";\n");
977 push(@implContent, $indent . "setDOMException(exec, ec);\n") if @{$function->raisesExceptions};
978 push(@implContent, $indent . "return result;\n");
984 my $signature = shift;
986 my $type = $codeGenerator->StripModule($signature->type);
988 if ($type eq "unsigned long") {
989 # Special-case index arguments because we need to check that they aren't < 0.
990 return "int" if $signature->extendedAttributes->{"IsIndex"};
994 return $type if $type eq "unsigned short" or $type eq "float" or $type eq "AtomicString";
995 return "bool" if $type eq "boolean";
996 return "int" if $type eq "long";
997 return "String" if $type eq "DOMString";
998 return "PassRefPtr<${type}>" if $type eq "NodeFilter";
999 return "Range::CompareHow" if $type eq "CompareHow";
1000 return "EventTargetNode*" if $type eq "EventTarget";
1001 return "FloatRect" if $type eq "SVGRect";
1002 return "FloatPoint" if $type eq "SVGPoint";
1003 return "double" if $type eq "SVGNumber";
1004 return "SVGPaint::SVGPaintType" if $type eq "SVGPaintType";
1006 # Default, assume native type is a pointer with same type name as idl type
1010 sub TypeCanFailConversion
1012 my $signature = shift;
1014 my $type = $codeGenerator->StripModule($signature->type);
1016 # FIXME: convert to use a hash
1018 return 0 if $type eq "boolean" or
1020 $type eq "AtomicString" or
1021 $type eq "DOMString" or
1023 $type eq "Element" or
1024 $type eq "DocumentType" or
1025 $type eq "EventTarget" or
1027 $type eq "NodeFilter" or
1028 $type eq "DOMWindow" or
1029 $type eq "XPathEvaluator" or
1030 $type eq "XPathNSResolver" or
1031 $type eq "XPathResult" or
1032 $type eq "SVGAngle" or
1033 $type eq "SVGLength" or
1034 $type eq "SVGNumber" or
1035 $type eq "SVGPoint" or
1036 $type eq "SVGTransform" or
1037 $type eq "SVGPathSeg" or
1038 $type eq "SVGMatrix" or
1039 $type eq "SVGRect" or
1040 $type eq "SVGElement" or
1041 $type eq "HTMLOptionElement" or
1042 $type eq "unsigned short" or # or can it?
1043 $type eq "CompareHow" or # or can it?
1044 $type eq "SVGPaintType"; # or can it?
1046 if ($type eq "unsigned long" or $type eq "long" or $type eq "Attr") {
1047 $implIncludes{"ExceptionCode.h"} = 1;
1051 die "Don't know whether a JS value can fail conversion to type $type."
1056 my $signature = shift;
1058 my $okParam = shift;
1059 my $maybeOkParam = $okParam ? ", ${okParam}" : "";
1061 my $type = $codeGenerator->StripModule($signature->type);
1063 return "$value->toBoolean(exec)" if $type eq "boolean";
1064 return "$value->toNumber(exec)" if $type eq "float" or $type eq "SVGNumber";
1065 return "$value->toInt32(exec${maybeOkParam})" if $type eq "unsigned long" or $type eq "long" or $type eq "unsigned short";
1067 return "static_cast<Range::CompareHow>($value->toInt32(exec))" if $type eq "CompareHow";
1068 return "static_cast<SVGPaint::SVGPaintType>($value->toInt32(exec))" if $type eq "SVGPaintType";
1070 return "$value->toString(exec)" if $type eq "AtomicString";
1071 if ($type eq "DOMString") {
1072 return "valueToStringWithNullCheck(exec, $value)" if $signature->extendedAttributes->{"ConvertNullToNullString"};
1073 return "$value->toString(exec)";
1076 if ($type eq "Node") {
1077 $implIncludes{"kjs_dom.h"} = 1;
1078 return "toNode($value)";
1081 if ($type eq "EventTarget") {
1082 $implIncludes{"kjs_dom.h"} = 1;
1083 return "toEventTargetNode($value)";
1086 if ($type eq "Attr") {
1087 $implIncludes{"kjs_dom.h"} = 1;
1088 return "toAttr($value${maybeOkParam})";
1091 if ($type eq "DocumentType") {
1092 $implIncludes{"kjs_dom.h"} = 1;
1093 return "toDocumentType($value)";
1096 if ($type eq "Element") {
1097 $implIncludes{"kjs_dom.h"} = 1;
1098 return "toElement($value)";
1101 if ($type eq "NodeFilter") {
1102 $implIncludes{"kjs_traversal.h"} = 1;
1103 return "toNodeFilter($value)";
1106 if ($type eq "DOMWindow") {
1107 $implIncludes{"kjs_window.h"} = 1;
1108 return "toDOMWindow($value)";
1111 if ($type eq "SVGRect") {
1112 $implIncludes{"JSSVGRect.h"} = 1;
1113 return "toFloatRect($value)";
1116 if ($type eq "SVGPoint") {
1117 $implIncludes{"JSSVGPoint.h"} = 1;
1118 return "toFloatPoint($value)";
1122 # Default, assume autogenerated type conversion routines
1123 $implIncludes{"JS$type.h"} = 1;
1124 return "to$type($value)";
1129 my $signature = shift;
1132 my $type = $codeGenerator->StripModule($signature->type);
1134 return "jsBoolean($value)" if $type eq "boolean";
1135 return "jsNumber($value)" if $codeGenerator->IsPrimitiveType($type) or $type eq "SVGPaintType";
1137 if ($codeGenerator->IsStringType($type)) {
1138 my $conv = $signature->extendedAttributes->{"ConvertNullStringTo"};
1139 if (defined $conv) {
1140 return "jsStringOrNull($value)" if $conv eq "Null";
1141 return "jsStringOrUndefined($value)" if $conv eq "Undefined";
1142 return "jsStringOrFalse($value)" if $conv eq "False";
1144 die "Unknown value for ConvertNullStringTo extended attribute";
1146 return "jsString($value)";
1149 if ($type eq "RGBColor") {
1150 $implIncludes{"kjs_css.h"} = 1;
1151 return "getDOMRGBColor(exec, $value)";
1154 if ($type eq "SVGRect" or $type eq "SVGPoint" or $type eq "SVGNumber") {
1155 $implIncludes{"JS$type.h"} = 1;
1156 return "getJS$type(exec, $value)";
1159 if ($type eq "HTMLCollection") {
1160 $implIncludes{"kjs_html.h"} = 1;
1161 $implIncludes{"HTMLCollection.h"} = 1;
1162 return "getHTMLCollection(exec, WTF::getPtr($value))";
1165 if ($type eq "StyleSheetList") {
1166 $implIncludes{"StyleSheetList.h"} = 1;
1167 $implIncludes{"kjs_css.h"} = 1;
1168 return "toJS(exec, WTF::getPtr($value), imp)";
1171 if ($codeGenerator->IsSVGAnimatedType($type)) {
1173 $value .= "Animated()";
1176 if ($type eq "DOMImplementation") {
1177 $implIncludes{"kjs_dom.h"} = 1;
1178 $implIncludes{"JSDOMImplementation.h"} = 1;
1179 } elsif ($type eq "Attr" or
1180 $type eq "CDATASection" or
1181 $type eq "Comment" or
1182 $type eq "Document" or
1183 $type eq "DocumentFragment" or
1184 $type eq "DocumentType" or
1185 $type eq "Element" or
1186 $type eq "EntityReference" or
1187 $type eq "HTMLDocument" or
1189 $type eq "ProcessingInstruction" or
1191 $implIncludes{"kjs_dom.h"} = 1;
1192 $implIncludes{"Comment.h"} = 1;
1193 $implIncludes{"CDATASection.h"} = 1;
1194 $implIncludes{"Node.h"} = 1;
1195 $implIncludes{"Element.h"} = 1;
1196 $implIncludes{"DocumentType.h"} = 1;
1197 } elsif ($type eq "EventTarget") {
1198 $implIncludes{"kjs_dom.h"} = 1;
1199 $implIncludes{"EventTargetNode.h"} = 1;
1200 } elsif ($type eq "Event") {
1201 $implIncludes{"kjs_events.h"} = 1;
1202 $implIncludes{"Event.h"} = 1;
1203 } elsif ($type eq "NodeList" or $type eq "NamedNodeMap") {
1204 $implIncludes{"kjs_dom.h"} = 1;
1205 } elsif ($type eq "CSSStyleSheet" or $type eq "StyleSheet" or $type eq "MediaList") {
1206 $implIncludes{"CSSStyleSheet.h"} = 1;
1207 $implIncludes{"MediaList.h"} = 1;
1208 $implIncludes{"kjs_css.h"} = 1;
1209 } elsif ($type eq "CSSStyleDeclaration" or $type eq "Rect") {
1210 $implIncludes{"CSSStyleDeclaration.h"} = 1;
1211 $implIncludes{"RectImpl.h"} = 1;
1212 $implIncludes{"kjs_css.h"} = 1;
1213 } elsif ($type eq "HTMLCanvasElement") {
1214 $implIncludes{"kjs_dom.h"} = 1;
1215 $implIncludes{"HTMLCanvasElement.h"} = 1;
1216 } elsif ($type eq "DOMWindow") {
1217 $implIncludes{"kjs_window.h"} = 1;
1218 } elsif ($type eq "DOMObject") {
1219 $implIncludes{"JSCanvasRenderingContext2D.h"} = 1;
1220 } elsif ($type eq "HTMLFormElement") {
1221 $implIncludes{"kjs_html.h"} = 1;
1222 $implIncludes{"HTMLFormElement.h"} = 1;
1223 } elsif ($type =~ /SVGPathSeg/) {
1224 $implIncludes{"JS$type.h"} = 1;
1225 $joinedName = $type;
1226 $joinedName =~ s/Abs|Rel//;
1227 $implIncludes{"$joinedName.h"} = 1;
1229 # Default, include header with same name.
1230 $implIncludes{"JS$type.h"} = 1;
1231 $implIncludes{"$type.h"} = 1;
1234 return "toJS(exec, WTF::getPtr($value))";
1238 sub GenerateHashTable
1246 my $specials = shift;
1247 my $parameters = shift;
1255 my $numEntries = $size;
1257 # Collect hashtable information
1259 foreach (@{$keys}) {
1261 my $h = $object->GenerateHashValue($_) % $numEntries;
1263 while (defined($table[$h])) {
1264 if (defined($links[$h])) {
1278 $maxDepth = $depth if ($depth > $maxDepth);
1281 # Ensure table is big enough (in case of undef entries at the end)
1282 if ($#table + 1 < $size) {
1283 $#table = $size - 1;
1286 # Start outputing the hashtables
1287 my $nameEntries = "${name}Entries";
1288 $nameEntries =~ s/:/_/g;
1290 # first, build the string table
1292 if (($name =~ /Proto/) or ($name =~ /Constructor/)) {
1296 if ($name =~ /Proto/) {
1297 $type =~ s/Proto.*//;
1298 $implClass = $type; $implClass =~ s/Wrapper$//;
1299 push(@implContent, "/* Hash table for prototype */\n");
1301 $type =~ s/Constructor.*//;
1302 $implClass = $type; $implClass =~ s/Constructor$//;
1303 push(@implContent, "/* Hash table for constructor */\n");
1306 push(@implContent, "/* Hash table */\n");
1309 # Dump the hash table
1310 push(@implContent, "\nstatic const HashEntry $nameEntries\[\] =\n\{\n");
1313 foreach $entry (@table) {
1314 if (defined($entry)) {
1315 my $key = @$keys[$entry];
1317 push(@implContent, " \{ \"" . $key . "\"");
1318 push(@implContent, ", " . @$values[$entry]);
1319 push(@implContent, ", " . @$specials[$entry]);
1320 push(@implContent, ", " . @$parameters[$entry]);
1321 push(@implContent, ", ");
1323 if (defined($links[$i])) {
1324 push(@implContent, "&${nameEntries}[$links[$i]]" . " \}");
1326 push(@implContent, "0 \}");
1329 push(@implContent, " \{ 0, 0, 0, 0, 0 \}");
1332 push(@implContent, ",") unless($i eq $size - 1);
1333 push(@implContent, "\n");
1339 # dummy bucket -- an empty table would crash Lookup::findEntry
1340 push(@implContent, " \{ 0, 0, 0, 0, 0 \}\n") ;
1344 push(@implContent, "};\n\n");
1345 push(@implContent, "static const HashTable $name = \n");
1346 push(@implContent, "{\n 2, $size, $nameEntries, $numEntries\n};\n\n");
1350 sub GenerateHashValue
1354 @chars = split(/ */, $_[0]);
1356 # This hash is designed to work on 16-bit chunks at a time. But since the normal case
1357 # (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
1358 # were 16-bit chunks, which should give matching results
1360 my $EXP2_32 = 4294967296;
1362 my $hash = 0x9e3779b9;
1363 my $l = scalar @chars; #I wish this was in Ruby --- Maks
1370 for (; $l > 0; $l--) {
1371 $hash += ord($chars[$s]);
1372 my $tmp = leftShift(ord($chars[$s+1]), 11) ^ $hash;
1373 $hash = (leftShift($hash, 16)% $EXP2_32) ^ $tmp;
1375 $hash += $hash >> 11;
1380 $hash += ord($chars[$s]);
1381 $hash ^= (leftShift($hash, 11)% $EXP2_32);
1382 $hash += $hash >> 17;
1385 # Force "avalanching" of final 127 bits
1386 $hash ^= leftShift($hash, 3);
1387 $hash += ($hash >> 5);
1388 $hash = ($hash% $EXP2_32);
1389 $hash ^= (leftShift($hash, 2)% $EXP2_32);
1390 $hash += ($hash >> 15);
1391 $hash = $hash% $EXP2_32;
1392 $hash ^= (leftShift($hash, 10)% $EXP2_32);
1394 # this avoids ever returning a hash code of 0, since that is used to
1395 # signal "hash not computed yet", using a value that is likely to be
1396 # effectively the same as 0 when the low bits are masked
1397 $hash = 0x80000000 if ($hash == 0);
1405 if (defined($IMPL)) {
1406 # Write content to file.
1407 print $IMPL @implContentHeader;
1409 foreach my $implInclude (sort keys(%implIncludes)) {
1410 my $checkType = $implInclude;
1411 $checkType =~ s/\.h//;
1413 print $IMPL "#include \"$implInclude\"\n" unless($codeGenerator->IsSVGAnimatedType($checkType));
1416 print $IMPL @implContent;
1420 @implHeaderContent = ();
1425 if (defined($HEADER)) {
1426 # Write content to file.
1427 print $HEADER @headerContent;
1431 @headerContent = ();
1437 my $className = shift;
1438 my $protoClassName = shift;
1439 my $interfaceName = shift;
1440 my $canConstruct = shift;
1442 my $implContent = << "EOF";
1443 class ${className}Constructor : public DOMObject {
1445 ${className}Constructor(ExecState* exec)
1447 setPrototype(exec->lexicalInterpreter()->builtinObjectPrototype());
1448 putDirect(prototypePropertyName, ${protoClassName}::self(exec), None);
1450 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
1451 JSValue* getValueProperty(ExecState*, int token) const;
1452 virtual const ClassInfo* classInfo() const { return &info; }
1453 static const ClassInfo info;
1456 if ($canConstruct) {
1457 $implContent .= << "EOF";
1458 virtual bool implementsConstruct() const { return true; }
1459 virtual JSObject* construct(ExecState* exec, const List& args) { return static_cast<JSObject*>(toJS(exec, new $interfaceName)); }
1463 $implContent .= << "EOF";
1466 const ClassInfo ${className}Constructor::info = { "${interfaceName}Constructor", 0, &${className}ConstructorTable, 0 };
1468 bool ${className}Constructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
1470 return getStaticValueSlot<${className}Constructor, DOMObject>(exec, &${className}ConstructorTable, this, propertyName, slot);
1473 JSValue* ${className}Constructor::getValueProperty(ExecState*, int token) const
1475 // The token is the numeric value of its associated constant
1476 return jsNumber(token);
1481 return $implContent;
1486 my $className = shift;
1488 my $implContent = << "EOF";
1489 class ${className}ProtoFunc : public InternalFunctionImp {
1491 ${className}ProtoFunc(ExecState* exec, int i, int len, const Identifier& name)
1492 : InternalFunctionImp(static_cast<FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()), name)
1495 put(exec, lengthPropertyName, jsNumber(len), DontDelete|ReadOnly|DontEnum);
1497 virtual JSValue* callAsFunction(ExecState* exec, JSObject* thisObj, const List& args);
1504 return $implContent;