• 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/core/v8/ScriptState.h"
35 #include "bindings/core/v8/SharedPersistent.h"
36 #include "wtf/PassRefPtr.h"
37 #include "wtf/RefPtr.h"
38 #include "wtf/text/WTFString.h"
39 #include <v8.h>
40 
41 namespace blink {
42 
43 class JSONValue;
44 
45 class ScriptValue FINAL {
46 public:
ScriptValue()47     ScriptValue() { }
48 
ScriptValue(ScriptState * scriptState,v8::Handle<v8::Value> value)49     ScriptValue(ScriptState* scriptState, v8::Handle<v8::Value> value)
50         : m_scriptState(scriptState)
51         , m_value(value.IsEmpty() ? nullptr : SharedPersistent<v8::Value>::create(value, scriptState->isolate()))
52     {
53         ASSERT(isEmpty() || m_scriptState);
54     }
55 
ScriptValue(const ScriptValue & value)56     ScriptValue(const ScriptValue& value)
57         : m_scriptState(value.m_scriptState)
58         , m_value(value.m_value)
59     {
60         ASSERT(isEmpty() || m_scriptState);
61     }
62 
scriptState()63     ScriptState* scriptState() const
64     {
65         return m_scriptState.get();
66     }
67 
isolate()68     v8::Isolate* isolate() const
69     {
70         return m_scriptState ? m_scriptState->isolate() : v8::Isolate::GetCurrent();
71     }
72 
73     ScriptValue& operator=(const ScriptValue& value)
74     {
75         if (this != &value) {
76             m_scriptState = value.m_scriptState;
77             m_value = value.m_value;
78         }
79         return *this;
80     }
81 
82     bool operator==(const ScriptValue& value) const
83     {
84         if (isEmpty())
85             return value.isEmpty();
86         if (value.isEmpty())
87             return false;
88         return *m_value == *value.m_value;
89     }
90 
91     bool operator!=(const ScriptValue& value) const
92     {
93         return !operator==(value);
94     }
95 
96     // This creates a new local Handle; Don't use this in performance-sensitive places.
isFunction()97     bool isFunction() const
98     {
99         ASSERT(!isEmpty());
100         v8::Handle<v8::Value> value = v8Value();
101         return !value.IsEmpty() && value->IsFunction();
102     }
103 
104     // This creates a new local Handle; Don't use this in performance-sensitive places.
isNull()105     bool isNull() const
106     {
107         ASSERT(!isEmpty());
108         v8::Handle<v8::Value> value = v8Value();
109         return !value.IsEmpty() && value->IsNull();
110     }
111 
112     // This creates a new local Handle; Don't use this in performance-sensitive places.
isUndefined()113     bool isUndefined() const
114     {
115         ASSERT(!isEmpty());
116         v8::Handle<v8::Value> value = v8Value();
117         return !value.IsEmpty() && value->IsUndefined();
118     }
119 
120     // This creates a new local Handle; Don't use this in performance-sensitive places.
isObject()121     bool isObject() const
122     {
123         ASSERT(!isEmpty());
124         v8::Handle<v8::Value> value = v8Value();
125         return !value.IsEmpty() && value->IsObject();
126     }
127 
isEmpty()128     bool isEmpty() const
129     {
130         return !m_value.get() || m_value->isEmpty();
131     }
132 
clear()133     void clear()
134     {
135         m_value = nullptr;
136     }
137 
138     v8::Handle<v8::Value> v8Value() const;
139     v8::Handle<v8::Value> v8ValueUnsafe() const;
140 
141     bool toString(String&) const;
142     PassRefPtr<JSONValue> toJSONValue(ScriptState*) const;
143 
144 private:
145     RefPtr<ScriptState> m_scriptState;
146     RefPtr<SharedPersistent<v8::Value> > m_value;
147 };
148 
149 } // namespace blink
150 
151 #endif // ScriptValue_h
152