Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / ScriptValue.h
1 /*
2  * Copyright (C) 2008, 2009 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef ScriptValue_h
32 #define ScriptValue_h
33
34 #include "bindings/v8/SharedPersistent.h"
35 #include "wtf/PassRefPtr.h"
36 #include "wtf/RefPtr.h"
37 #include "wtf/text/WTFString.h"
38 #include <v8.h>
39
40 namespace WebCore {
41
42 class JSONValue;
43 class ScriptState;
44
45 class ScriptValue {
46 public:
47     ScriptValue()
48         : m_isolate(0)
49     { }
50
51     virtual ~ScriptValue();
52
53     ScriptValue(v8::Handle<v8::Value> value, v8::Isolate* isolate)
54         : m_isolate(isolate)
55         , m_value(value.IsEmpty() ? 0 : SharedPersistent<v8::Value>::create(value, isolate))
56     {
57     }
58
59     ScriptValue(const ScriptValue& value)
60         : m_isolate(value.m_isolate)
61         , m_value(value.m_value)
62     {
63     }
64
65     v8::Isolate* isolate() const
66     {
67         if (!m_isolate)
68             m_isolate = v8::Isolate::GetCurrent();
69         return m_isolate;
70     }
71
72     static ScriptValue createNull()
73     {
74         v8::Isolate* isolate = v8::Isolate::GetCurrent();
75         return ScriptValue(v8::Null(isolate), isolate);
76     }
77     static ScriptValue createBoolean(bool b)
78     {
79         v8::Isolate* isolate = v8::Isolate::GetCurrent();
80         return ScriptValue(b ? v8::True(isolate) : v8::False(isolate), isolate);
81     }
82
83     ScriptValue& operator=(const ScriptValue& value)
84     {
85         if (this != &value) {
86             m_value = value.m_value;
87             m_isolate = value.m_isolate;
88         }
89         return *this;
90     }
91
92     bool operator==(const ScriptValue& value) const
93     {
94         if (hasNoValue())
95             return value.hasNoValue();
96         if (value.hasNoValue())
97             return false;
98         return *m_value == *value.m_value;
99     }
100
101     bool isEqual(ScriptState*, const ScriptValue& value) const
102     {
103         return operator==(value);
104     }
105
106     // Note: This creates a new local Handle; not to be used in cases where is
107     // is an efficiency problem.
108     bool isFunction() const
109     {
110         ASSERT(!hasNoValue());
111         v8::Handle<v8::Value> value = v8Value();
112         return !value.IsEmpty() && value->IsFunction();
113     }
114
115     bool operator!=(const ScriptValue& value) const
116     {
117         return !operator==(value);
118     }
119
120     // Note: This creates a new local Handle; not to be used in cases where is
121     // is an efficiency problem.
122     bool isNull() const
123     {
124         ASSERT(!hasNoValue());
125         v8::Handle<v8::Value> value = v8Value();
126         return !value.IsEmpty() && value->IsNull();
127     }
128
129     // Note: This creates a new local Handle; not to be used in cases where is
130     // is an efficiency problem.
131     bool isUndefined() const
132     {
133         ASSERT(!hasNoValue());
134         v8::Handle<v8::Value> value = v8Value();
135         return !value.IsEmpty() && value->IsUndefined();
136     }
137
138     // Note: This creates a new local Handle; not to be used in cases where is
139     // is an efficiency problem.
140     bool isObject() const
141     {
142         ASSERT(!hasNoValue());
143         v8::Handle<v8::Value> value = v8Value();
144         return !value.IsEmpty() && value->IsObject();
145     }
146
147     bool hasNoValue() const
148     {
149         return !m_value.get() || m_value->isEmpty();
150     }
151
152     void clear()
153     {
154         m_value = 0;
155     }
156
157     v8::Handle<v8::Value> v8Value() const
158     {
159         return m_value.get() ? m_value->newLocal(m_isolate) : v8::Handle<v8::Value>();
160     }
161
162     bool getString(String& result) const;
163     String toString() const;
164
165     PassRefPtr<JSONValue> toJSONValue(ScriptState*) const;
166
167 private:
168     mutable v8::Isolate* m_isolate;
169     RefPtr<SharedPersistent<v8::Value> > m_value;
170 };
171
172 } // namespace WebCore
173
174 #endif // ScriptValue_h