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/v8/SharedPersistent.h"
35 #include "wtf/PassRefPtr.h"
36 #include "wtf/RefPtr.h"
37 #include "wtf/text/WTFString.h"
51 virtual ~ScriptValue();
53 ScriptValue(v8::Handle<v8::Value> value, v8::Isolate* isolate)
55 , m_value(value.IsEmpty() ? 0 : SharedPersistent<v8::Value>::create(value, isolate))
59 ScriptValue(const ScriptValue& value)
60 : m_isolate(value.m_isolate)
61 , m_value(value.m_value)
65 v8::Isolate* isolate() const
68 m_isolate = v8::Isolate::GetCurrent();
72 static ScriptValue createNull()
74 v8::Isolate* isolate = v8::Isolate::GetCurrent();
75 return ScriptValue(v8::Null(isolate), isolate);
77 static ScriptValue createBoolean(bool b)
79 v8::Isolate* isolate = v8::Isolate::GetCurrent();
80 return ScriptValue(b ? v8::True(isolate) : v8::False(isolate), isolate);
83 ScriptValue& operator=(const ScriptValue& value)
86 m_value = value.m_value;
87 m_isolate = value.m_isolate;
92 bool operator==(const ScriptValue& value) const
95 return value.hasNoValue();
96 if (value.hasNoValue())
98 return *m_value == *value.m_value;
101 bool isEqual(ScriptState*, const ScriptValue& value) const
103 return operator==(value);
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
110 ASSERT(!hasNoValue());
111 v8::Handle<v8::Value> value = v8Value();
112 return !value.IsEmpty() && value->IsFunction();
115 bool operator!=(const ScriptValue& value) const
117 return !operator==(value);
120 // Note: This creates a new local Handle; not to be used in cases where is
121 // is an efficiency problem.
124 ASSERT(!hasNoValue());
125 v8::Handle<v8::Value> value = v8Value();
126 return !value.IsEmpty() && value->IsNull();
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
133 ASSERT(!hasNoValue());
134 v8::Handle<v8::Value> value = v8Value();
135 return !value.IsEmpty() && value->IsUndefined();
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
142 ASSERT(!hasNoValue());
143 v8::Handle<v8::Value> value = v8Value();
144 return !value.IsEmpty() && value->IsObject();
147 bool hasNoValue() const
149 return !m_value.get() || m_value->isEmpty();
157 v8::Handle<v8::Value> v8Value() const
159 return m_value.get() ? m_value->newLocal(m_isolate) : v8::Handle<v8::Value>();
162 bool getString(String& result) const;
163 String toString() const;
165 PassRefPtr<JSONValue> toJSONValue(ScriptState*) const;
168 mutable v8::Isolate* m_isolate;
169 RefPtr<SharedPersistent<v8::Value> > m_value;
172 } // namespace WebCore
174 #endif // ScriptValue_h