2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
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"
45 class ScriptValue final {
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()))
53 ASSERT(isEmpty() || m_scriptState);
56 ScriptValue(const ScriptValue& value)
57 : m_scriptState(value.m_scriptState)
58 , m_value(value.m_value)
60 ASSERT(isEmpty() || m_scriptState);
63 ScriptState* scriptState() const
65 return m_scriptState.get();
68 v8::Isolate* isolate() const
70 return m_scriptState ? m_scriptState->isolate() : v8::Isolate::GetCurrent();
73 ScriptValue& operator=(const ScriptValue& value)
76 m_scriptState = value.m_scriptState;
77 m_value = value.m_value;
82 bool operator==(const ScriptValue& value) const
85 return value.isEmpty();
88 return *m_value == *value.m_value;
91 bool operator!=(const ScriptValue& value) const
93 return !operator==(value);
96 // This creates a new local Handle; Don't use this in performance-sensitive places.
97 bool isFunction() const
100 v8::Handle<v8::Value> value = v8Value();
101 return !value.IsEmpty() && value->IsFunction();
104 // This creates a new local Handle; Don't use this in performance-sensitive places.
108 v8::Handle<v8::Value> value = v8Value();
109 return !value.IsEmpty() && value->IsNull();
112 // This creates a new local Handle; Don't use this in performance-sensitive places.
113 bool isUndefined() const
116 v8::Handle<v8::Value> value = v8Value();
117 return !value.IsEmpty() && value->IsUndefined();
120 // This creates a new local Handle; Don't use this in performance-sensitive places.
121 bool isObject() const
124 v8::Handle<v8::Value> value = v8Value();
125 return !value.IsEmpty() && value->IsObject();
130 return !m_value.get() || m_value->isEmpty();
138 v8::Handle<v8::Value> v8Value() const;
139 v8::Handle<v8::Value> v8ValueUnsafe() const;
141 bool toString(String&) const;
142 PassRefPtr<JSONValue> toJSONValue(ScriptState*) const;
145 RefPtr<ScriptState> m_scriptState;
146 RefPtr<SharedPersistent<v8::Value> > m_value;
151 #endif // ScriptValue_h