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 "PlatformString.h"
35#include "ScriptState.h"
36
37#include <v8.h>
38#include <wtf/PassRefPtr.h>
39
40#ifndef NDEBUG
41#include "V8Proxy.h"  // for register and unregister global handles.
42#endif
43
44namespace WebCore {
45
46class SerializedScriptValue;
47
48class ScriptValue {
49public:
50    ScriptValue() {}
51
52    ScriptValue(v8::Handle<v8::Value> value)
53    {
54        if (value.IsEmpty())
55            return;
56
57        m_value = v8::Persistent<v8::Value>::New(value);
58#ifndef NDEBUG
59        V8GCController::registerGlobalHandle(SCRIPTVALUE, this, m_value);
60#endif
61    }
62
63    ScriptValue(const ScriptValue& value)
64    {
65        if (value.m_value.IsEmpty())
66            return;
67
68        m_value = v8::Persistent<v8::Value>::New(value.m_value);
69#ifndef NDEBUG
70        V8GCController::registerGlobalHandle(SCRIPTVALUE, this, m_value);
71#endif
72    }
73
74    ScriptValue& operator=(const ScriptValue& value)
75    {
76        if (this == &value)
77            return *this;
78
79        clear();
80
81        if (value.m_value.IsEmpty())
82            return *this;
83
84        m_value = v8::Persistent<v8::Value>::New(value.m_value);
85#ifndef NDEBUG
86        V8GCController::registerGlobalHandle(SCRIPTVALUE, this, m_value);
87#endif
88
89        return *this;
90    }
91
92    bool operator==(const ScriptValue value) const
93    {
94        return m_value == value.m_value;
95    }
96
97    bool isEqual(ScriptState*, const ScriptValue& value) const
98    {
99        return m_value == value.m_value;
100    }
101
102    bool operator!=(const ScriptValue value) const
103    {
104        return !operator==(value);
105    }
106
107    bool isNull() const
108    {
109        return m_value->IsNull();
110    }
111
112    bool isUndefined() const
113    {
114        return m_value->IsUndefined();
115    }
116
117    bool isObject() const
118    {
119        return m_value->IsObject();
120    }
121
122    bool hasNoValue() const
123    {
124        return m_value.IsEmpty();
125    }
126
127    PassRefPtr<SerializedScriptValue> serialize(ScriptState*);
128    static ScriptValue deserialize(ScriptState*, SerializedScriptValue*);
129
130    void clear()
131    {
132        if (m_value.IsEmpty())
133            return;
134
135#ifndef NDEBUG
136        V8GCController::unregisterGlobalHandle(this, m_value);
137#endif
138        m_value.Dispose();
139        m_value.Clear();
140    }
141
142    virtual ~ScriptValue()
143    {
144        clear();
145    }
146
147    v8::Handle<v8::Value> v8Value() const { return m_value; }
148    bool getString(ScriptState*, String& result) const { return getString(result); }
149    bool getString(String& result) const;
150    String toString(ScriptState*) const;
151
152private:
153    mutable v8::Persistent<v8::Value> m_value;
154};
155
156} // namespace WebCore
157
158#endif // ScriptValue_h
159