1#!/usr/bin/perl -w
2
3# Copyright (C) 2005, 2006, 2007, 2009 Apple Inc. All rights reserved.
4# Copyright (C) 2009, Julien Chaffraix <jchaffraix@webkit.org>
5# Copyright (C) 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
6# Copyright (C) 2011 Ericsson AB. All rights reserved.
7# Copyright (C) 2011 Google, Inc. All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12#
13# 1.  Redistributions of source code must retain the above copyright
14#     notice, this list of conditions and the following disclaimer.
15# 2.  Redistributions in binary form must reproduce the above copyright
16#     notice, this list of conditions and the following disclaimer in the
17#     documentation and/or other materials provided with the distribution.
18# 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
19#     its contributors may be used to endorse or promote products derived
20#     from this software without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
23# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
26# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33use strict;
34
35use InFilesCompiler;
36
37my %defaultParameters = (
38    'namespace' => 0
39);
40
41sub defaultItemFactory
42{
43    return (
44        'interfaceName' => 0,
45        'conditional' => 0,
46        'runtimeConditional' => 0
47    );
48}
49
50my $InCompiler = InFilesCompiler->new(\%defaultParameters, \&defaultItemFactory);
51
52my $outputDir = $InCompiler->initializeFromCommandLine();
53$InCompiler->compile(\&generateCode);
54
55sub generateCode()
56{
57    my $parsedParametersRef = shift;
58    my $parsedItemsRef = shift;
59
60    generateImplementation($parsedParametersRef, $parsedItemsRef);
61    $InCompiler->generateInterfacesHeader();
62    $InCompiler->generateHeadersHeader()
63}
64
65sub generateImplementation()
66{
67    my $parsedParametersRef = shift;
68    my $parsedItemsRef = shift;
69
70    my $F;
71    my %parsedEvents = %{ $parsedItemsRef };
72    my %parsedParameters = %{ $parsedParametersRef };
73
74    my $namespace = $parsedParameters{"namespace"};
75
76    # Currently, only Events have factory files.
77    return if $namespace ne "Event";
78
79    my $outputFile = "$outputDir/${namespace}Factory.cpp";
80
81    open F, ">$outputFile" or die "Failed to open file: $!";
82
83    print F $InCompiler->license();
84
85    print F "#include \"config.h\"\n";
86    print F "#include \"${namespace}Factory.h\"\n";
87    print F "\n";
88    print F "#include \"${namespace}Headers.h\"\n";
89    print F "\n";
90    print F "namespace WebCore {\n";
91    print F "\n";
92    print F "PassRefPtr<$namespace> ${namespace}Factory::create(const String& type)\n";
93    print F "{\n";
94
95    for my $eventName (sort keys %parsedEvents) {
96        my $conditional = $parsedEvents{$eventName}{"conditional"};
97        my $runtimeConditional = $parsedEvents{$eventName}{"runtimeConditional"};
98        my $interfaceName = $InCompiler->interfaceForItem($eventName);
99
100        print F "#if ENABLE($conditional)\n" if $conditional;
101        # FIXEME JSC should support RuntimeEnabledFeatures
102        print F "    if (type == \"$eventName\")\n";
103        print F "        return ${interfaceName}::create();\n";
104        print F "#endif\n" if $conditional;
105    }
106
107    print F "    return 0;\n";
108    print F "}\n";
109    print F "\n";
110    print F "} // namespace WebCore\n";
111
112    close F;
113}
114