• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
ScriptValue()47     ScriptValue()
48         : m_isolate(0)
49     { }
50 
51     virtual ~ScriptValue();
52 
ScriptValue(v8::Handle<v8::Value> value,v8::Isolate * isolate)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 
ScriptValue(const ScriptValue & value)59     ScriptValue(const ScriptValue& value)
60         : m_isolate(value.m_isolate)
61         , m_value(value.m_value)
62     {
63     }
64 
isolate()65     v8::Isolate* isolate() const
66     {
67         if (!m_isolate)
68             m_isolate = v8::Isolate::GetCurrent();
69         return m_isolate;
70     }
71 
createUndefined(v8::Isolate * isolate)72     static ScriptValue createUndefined(v8::Isolate* isolate)
73     {
74         return ScriptValue(v8::Undefined(isolate), isolate);
75     }
76 
createNull()77     static ScriptValue createNull()
78     {
79         v8::Isolate* isolate = v8::Isolate::GetCurrent();
80         return ScriptValue(v8::Null(isolate), isolate);
81     }
createBoolean(bool b)82     static ScriptValue createBoolean(bool b)
83     {
84         v8::Isolate* isolate = v8::Isolate::GetCurrent();
85         return ScriptValue(b ? v8::True(isolate) : v8::False(isolate), isolate);
86     }
87 
88     ScriptValue& operator=(const ScriptValue& value)
89     {
90         if (this != &value) {
91             m_value = value.m_value;
92             m_isolate = value.m_isolate;
93         }
94         return *this;
95     }
96 
97     bool operator==(const ScriptValue& value) const
98     {
99         if (hasNoValue())
100             return value.hasNoValue();
101         if (value.hasNoValue())
102             return false;
103         return *m_value == *value.m_value;
104     }
105 
isEqual(ScriptState *,const ScriptValue & value)106     bool isEqual(ScriptState*, const ScriptValue& value) const
107     {
108         return operator==(value);
109     }
110 
111     // Note: This creates a new local Handle; not to be used in cases where is
112     // is an efficiency problem.
isFunction()113     bool isFunction() const
114     {
115         ASSERT(!hasNoValue());
116         v8::Handle<v8::Value> value = v8Value();
117         return !value.IsEmpty() && value->IsFunction();
118     }
119 
120     bool operator!=(const ScriptValue& value) const
121     {
122         return !operator==(value);
123     }
124 
125     // Note: This creates a new local Handle; not to be used in cases where is
126     // is an efficiency problem.
isNull()127     bool isNull() const
128     {
129         ASSERT(!hasNoValue());
130         v8::Handle<v8::Value> value = v8Value();
131         return !value.IsEmpty() && value->IsNull();
132     }
133 
134     // Note: This creates a new local Handle; not to be used in cases where is
135     // is an efficiency problem.
isUndefined()136     bool isUndefined() const
137     {
138         ASSERT(!hasNoValue());
139         v8::Handle<v8::Value> value = v8Value();
140         return !value.IsEmpty() && value->IsUndefined();
141     }
142 
143     // Note: This creates a new local Handle; not to be used in cases where is
144     // is an efficiency problem.
isObject()145     bool isObject() const
146     {
147         ASSERT(!hasNoValue());
148         v8::Handle<v8::Value> value = v8Value();
149         return !value.IsEmpty() && value->IsObject();
150     }
151 
hasNoValue()152     bool hasNoValue() const
153     {
154         return !m_value.get() || m_value->isEmpty();
155     }
156 
clear()157     void clear()
158     {
159         m_value = 0;
160     }
161 
v8Value()162     v8::Handle<v8::Value> v8Value() const
163     {
164         return m_value.get() ? m_value->newLocal(m_isolate) : v8::Handle<v8::Value>();
165     }
166 
167     bool getString(String& result) const;
168     String toString() const;
169 
170     PassRefPtr<JSONValue> toJSONValue(ScriptState*) const;
171 
172 private:
173     mutable v8::Isolate* m_isolate;
174     RefPtr<SharedPersistent<v8::Value> > m_value;
175 };
176 
177 } // namespace WebCore
178 
179 #endif // ScriptValue_h
180