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