Home | History | Annotate | Download | only in v8
      1 /*
      2 * Copyright (C) 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 V8Binding_h
     32 #define V8Binding_h
     33 
     34 #include "AtomicString.h"
     35 #include "BindingElement.h"
     36 #include "BindingSecurity.h"
     37 #include "MathExtras.h"
     38 #include "PlatformString.h"
     39 #include "V8DOMWrapper.h"
     40 
     41 #include <v8.h>
     42 
     43 namespace WebCore {
     44 
     45     class EventListener;
     46     class EventTarget;
     47     class V8BindingDOMWindow;
     48 
     49     // Instantiate binding template classes for V8.
     50     class V8Binding {
     51     public:
     52         typedef v8::Handle<v8::Value> Value;
     53         typedef V8BindingDOMWindow DOMWindow;
     54     };
     55     typedef BindingSecurity<V8Binding> V8BindingSecurity;
     56     typedef BindingElement<V8Binding> V8BindingElement;
     57 
     58     enum ExternalMode {
     59         Externalize,
     60         DoNotExternalize
     61     };
     62 
     63     template <typename StringType>
     64     StringType v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode external);
     65 
     66     // Convert v8 types to a WebCore::String. If the V8 string is not already
     67     // an external string then it is transformed into an external string at this
     68     // point to avoid repeated conversions.
     69     inline String v8StringToWebCoreString(v8::Handle<v8::String> v8String)
     70     {
     71         return v8StringToWebCoreString<String>(v8String, Externalize);
     72     }
     73     String v8NonStringValueToWebCoreString(v8::Handle<v8::Value>);
     74     String v8ValueToWebCoreString(v8::Handle<v8::Value> value);
     75 
     76     // Convert v8 types to a WebCore::AtomicString.
     77     inline AtomicString v8StringToAtomicWebCoreString(v8::Handle<v8::String> v8String)
     78     {
     79         return v8StringToWebCoreString<AtomicString>(v8String, Externalize);
     80     }
     81     AtomicString v8NonStringValueToAtomicWebCoreString(v8::Handle<v8::Value>);
     82     AtomicString v8ValueToAtomicWebCoreString(v8::Handle<v8::Value> value);
     83 
     84     // Return a V8 external string that shares the underlying buffer with the given
     85     // WebCore string. The reference counting mechanism is used to keep the
     86     // underlying buffer alive while the string is still live in the V8 engine.
     87     v8::Local<v8::String> v8ExternalString(const String&);
     88 
     89     // Convert a string to a V8 string.
     90     inline v8::Handle<v8::String> v8String(const String& string)
     91     {
     92         return v8ExternalString(string);
     93     }
     94 
     95     // Enables caching v8 wrappers created for WebCore::StringImpl.  Currently this cache requires
     96     // all the calls (both to convert WebCore::String to v8::String and to GC the handle)
     97     // to be performed on the main thread.
     98     void enableStringImplCache();
     99 
    100     // Convert a value to a 32-bit integer.  The conversion fails if the
    101     // value cannot be converted to an integer or converts to nan or to an infinity.
    102     int toInt32(v8::Handle<v8::Value> value, bool& ok);
    103 
    104     // Convert a value to a 32-bit integer assuming the conversion cannot fail.
    105     inline int toInt32(v8::Handle<v8::Value> value)
    106     {
    107         bool ok;
    108         return toInt32(value, ok);
    109     }
    110 
    111     inline float toFloat(v8::Local<v8::Value> value)
    112     {
    113         return static_cast<float>(value->NumberValue());
    114     }
    115 
    116     inline long long toInt64(v8::Local<v8::Value> value)
    117     {
    118         return static_cast<long long>(value->IntegerValue());
    119     }
    120 
    121     // FIXME: Drop this in favor of the type specific v8ValueToWebCoreString when we rework the code generation.
    122     inline String toWebCoreString(v8::Handle<v8::Value> object)
    123     {
    124         return v8ValueToWebCoreString(object);
    125     }
    126 
    127     String toWebCoreString(const v8::Arguments&, int index);
    128 
    129     // The string returned by this function is still owned by the argument
    130     // and will be deallocated when the argument is deallocated.
    131     inline const uint16_t* fromWebCoreString(const String& str)
    132     {
    133         return reinterpret_cast<const uint16_t*>(str.characters());
    134     }
    135 
    136     bool isUndefinedOrNull(v8::Handle<v8::Value> value);
    137 
    138     v8::Handle<v8::Boolean> v8Boolean(bool value);
    139 
    140     String toWebCoreStringWithNullCheck(v8::Handle<v8::Value> value);
    141 
    142     AtomicString toAtomicWebCoreStringWithNullCheck(v8::Handle<v8::Value> value);
    143 
    144     String toWebCoreStringWithNullOrUndefinedCheck(v8::Handle<v8::Value> value);
    145 
    146     v8::Handle<v8::String> v8UndetectableString(const String& str);
    147 
    148     v8::Handle<v8::Value> v8StringOrNull(const String& str);
    149 
    150     v8::Handle<v8::Value> v8StringOrUndefined(const String& str);
    151 
    152     v8::Handle<v8::Value> v8StringOrFalse(const String& str);
    153 
    154     double toWebCoreDate(v8::Handle<v8::Value> object);
    155 
    156     v8::Handle<v8::Value> v8DateOrNull(double value);
    157 
    158     v8::Persistent<v8::FunctionTemplate> createRawTemplate();
    159 
    160     struct BatchedAttribute;
    161     struct BatchedCallback;
    162 
    163     v8::Local<v8::Signature> configureTemplate(v8::Persistent<v8::FunctionTemplate>,
    164                                                const char* interfaceName,
    165                                                v8::Persistent<v8::FunctionTemplate> parentClass,
    166                                                int fieldCount,
    167                                                const BatchedAttribute*,
    168                                                size_t attributeCount,
    169                                                const BatchedCallback*,
    170                                                size_t callbackCount);
    171 
    172     v8::Handle<v8::Value> getElementStringAttr(const v8::AccessorInfo&,
    173                                                const QualifiedName&);
    174     void setElementStringAttr(const v8::AccessorInfo&,
    175                               const QualifiedName&,
    176                               v8::Local<v8::Value>);
    177 
    178 
    179     v8::Persistent<v8::String> getToStringName();
    180     v8::Persistent<v8::FunctionTemplate> getToStringTemplate();
    181 
    182     // V8Parameter is an adapter class that converts V8 values to Strings
    183     // or AtomicStrings as appropriate, using multiple typecast operators.
    184     enum V8ParameterMode {
    185         DefaultMode,
    186         WithNullCheck,
    187         WithUndefinedOrNullCheck
    188     };
    189     template <V8ParameterMode MODE = DefaultMode>
    190     class V8Parameter {
    191     public:
    192         V8Parameter (v8::Local<v8::Value> object) :m_v8Object(object) { }
    193         operator String();
    194         operator AtomicString();
    195     private:
    196         v8::Local<v8::Value> m_v8Object;
    197     };
    198 
    199     template<> inline V8Parameter<DefaultMode>::operator String() { return toWebCoreString(m_v8Object); }
    200     template<> inline V8Parameter<WithNullCheck>::operator String() { return toWebCoreStringWithNullCheck(m_v8Object); }
    201     template<> inline V8Parameter<WithUndefinedOrNullCheck>::operator String() { return toWebCoreStringWithNullOrUndefinedCheck(m_v8Object); }
    202 
    203     template<> inline V8Parameter<DefaultMode>::operator AtomicString() { return v8ValueToAtomicWebCoreString(m_v8Object); }
    204     template<> inline V8Parameter<WithNullCheck>::operator AtomicString() { return toAtomicWebCoreStringWithNullCheck(m_v8Object); }
    205     template<> inline V8Parameter<WithUndefinedOrNullCheck>::operator AtomicString() { return toAtomicWebCoreStringWithNullCheck(m_v8Object); }
    206 
    207 } // namespace WebCore
    208 
    209 #endif // V8Binding_h
    210