initial import
[vuplus_webkit] / Source / WebCore / bindings / js / JSDictionary.h
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef JSDictionary_h
27 #define JSDictionary_h
28
29 #include <interpreter/CallFrame.h>
30 #include <wtf/Forward.h>
31
32 namespace WebCore {
33
34 class DOMWindow;
35 class EventTarget;
36 class Node;
37 class ScriptValue;
38 class SerializedScriptValue;
39
40 class JSDictionary {
41 public:
42     JSDictionary(JSC::ExecState* exec, JSC::JSObject* initializerObject)
43         : m_exec(exec)
44         , m_initializerObject(initializerObject)
45     {
46     }
47
48     template <typename Result>
49     bool tryGetProperty(const char* propertyName, Result&);
50
51     template <typename T, typename Result>
52     bool tryGetProperty(const char* propertyName, T* context, void (*setter)(T* context, const Result&));
53
54 private:
55     enum GetPropertyResult {
56         ExceptionThrown,
57         NoPropertyFound,
58         PropertyFound
59     };
60     GetPropertyResult tryGetProperty(const char* propertyName, JSC::JSValue&);
61
62     static void convertValue(JSC::ExecState*, JSC::JSValue, bool& result);
63     static void convertValue(JSC::ExecState*, JSC::JSValue, int& result);
64     static void convertValue(JSC::ExecState*, JSC::JSValue, unsigned& result);
65     static void convertValue(JSC::ExecState*, JSC::JSValue, unsigned short& result);
66     static void convertValue(JSC::ExecState*, JSC::JSValue, unsigned long long& result);
67     static void convertValue(JSC::ExecState*, JSC::JSValue, double& result);
68     static void convertValue(JSC::ExecState*, JSC::JSValue, String& result);
69     static void convertValue(JSC::ExecState*, JSC::JSValue, ScriptValue& result);
70     static void convertValue(JSC::ExecState*, JSC::JSValue, RefPtr<SerializedScriptValue>& result);
71     static void convertValue(JSC::ExecState*, JSC::JSValue, RefPtr<DOMWindow>& result);
72     static void convertValue(JSC::ExecState*, JSC::JSValue, RefPtr<EventTarget>& result);
73     static void convertValue(JSC::ExecState*, JSC::JSValue, RefPtr<Node>& result);
74
75     JSC::ExecState* m_exec;
76     JSC::JSObject* m_initializerObject;
77 };
78
79
80 template <typename T, typename Result>
81 bool JSDictionary::tryGetProperty(const char* propertyName, T* context, void (*setter)(T* context, const Result&))
82 {
83     JSC::JSValue value;
84     switch (tryGetProperty(propertyName, value)) {
85     case ExceptionThrown:
86         return false;
87     case PropertyFound: {
88         Result result;
89         convertValue(m_exec, value, result);
90
91         if (m_exec->hadException())
92             return false;
93     
94         setter(context, result);
95         break;
96     }
97     case NoPropertyFound:
98         break;
99     }
100
101     return true;
102 }
103
104 template <typename Result>
105 bool JSDictionary::tryGetProperty(const char* propertyName, Result& finalResult)
106 {
107     struct IdentitySetter {
108         static void identitySetter(Result* context, const Result& result)
109         {
110             *context = result;
111         }
112     };
113
114     return tryGetProperty(propertyName, &finalResult, IdentitySetter::identitySetter);
115 }
116
117 } // namespace WebCore
118
119 #endif // JSDictionary_h