• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "MathExtras.h"
35 #include "PlatformString.h"
36 
37 #include <v8.h>
38 
39 namespace WebCore {
40 
41     enum ExternalMode {
42         Externalize,
43         DoNotExternalize
44     };
45 
46     enum StringType {
47         PlainStringType,
48         AtomicStringType
49     };
50 
51     // Convert v8 types to a WebCore::String. If the V8 string is not already
52     // an external string then it is transformed into an external string at this
53     // point to avoid repeated conversions.
54     String v8StringToWebCoreString(v8::Handle<v8::String>, ExternalMode mode, StringType type);
55     String v8ValueToWebCoreString(v8::Handle<v8::Value>);
56 
57     // Convert v8 types to a WebCore::AtomicString.
58     AtomicString v8StringToAtomicWebCoreString(v8::Handle<v8::String>);
59     AtomicString v8ValueToAtomicWebCoreString(v8::Handle<v8::Value>);
60 
61     // Convert a string to a V8 string.
62     v8::Handle<v8::String> v8String(const String&);
63 
toString(const String & string)64     inline String toString(const String& string)
65     {
66         return string;
67     }
68 
69     // Return a V8 external string that shares the underlying buffer with the given
70     // WebCore string. The reference counting mechanism is used to keep the
71     // underlying buffer alive while the string is still live in the V8 engine.
72     v8::Local<v8::String> v8ExternalString(const String&);
73 
74     // Enables caching v8 wrappers created for WebCore::StringImpl.  Currently this cache requires
75     // all the calls (both to convert WebCore::String to v8::String and to GC the handle)
76     // to be performed on the main thread.
77     void enableStringImplCache();
78 
79     // Convert a value to a 32-bit integer.  The conversion fails if the
80     // value cannot be converted to an integer or converts to nan or to an infinity.
toInt32(v8::Handle<v8::Value> value,bool & ok)81     inline int toInt32(v8::Handle<v8::Value> value, bool& ok)
82     {
83         ok = true;
84 
85         // Fast case.  The value is already a 32-bit integer.
86         if (value->IsInt32())
87             return value->Int32Value();
88 
89         // Can the value be converted to a number?
90         v8::Local<v8::Number> numberObject = value->ToNumber();
91         if (numberObject.IsEmpty()) {
92             ok = false;
93             return 0;
94         }
95 
96         // Does the value convert to nan or to an infinity?
97         double numberValue = numberObject->Value();
98         if (isnan(numberValue) || isinf(numberValue)) {
99             ok = false;
100             return 0;
101         }
102 
103         // Can the value be converted to a 32-bit integer?
104         v8::Local<v8::Int32> intValue = value->ToInt32();
105         if (intValue.IsEmpty()) {
106             ok = false;
107             return 0;
108         }
109 
110         // Return the result of the int32 conversion.
111         return intValue->Value();
112     }
113 
114     // Convert a value to a 32-bit integer assuming the conversion cannot fail.
toInt32(v8::Handle<v8::Value> value)115     inline int toInt32(v8::Handle<v8::Value> value)
116     {
117         bool ok;
118         return toInt32(value, ok);
119     }
120 
toFloat(v8::Local<v8::Value> value)121     inline float toFloat(v8::Local<v8::Value> value)
122     {
123         return static_cast<float>(value->NumberValue());
124     }
125 
126     // FIXME: Drop this in favor of the type specific v8ValueToWebCoreString when we rework the code generation.
toWebCoreString(v8::Handle<v8::Value> object)127     inline String toWebCoreString(v8::Handle<v8::Value> object)
128     {
129         return v8ValueToWebCoreString(object);
130     }
131 
132     // The string returned by this function is still owned by the argument
133     // and will be deallocated when the argument is deallocated.
fromWebCoreString(const String & str)134     inline const uint16_t* fromWebCoreString(const String& str)
135     {
136         return reinterpret_cast<const uint16_t*>(str.characters());
137     }
138 
isUndefinedOrNull(v8::Handle<v8::Value> value)139     inline bool isUndefinedOrNull(v8::Handle<v8::Value> value)
140     {
141         return value->IsNull() || value->IsUndefined();
142     }
143 
v8Boolean(bool value)144     inline v8::Handle<v8::Boolean> v8Boolean(bool value)
145     {
146         return value ? v8::True() : v8::False();
147     }
148 
toWebCoreStringWithNullCheck(v8::Handle<v8::Value> value)149     inline String toWebCoreStringWithNullCheck(v8::Handle<v8::Value> value)
150     {
151         if (value->IsNull())
152             return String();
153         return v8ValueToWebCoreString(value);
154     }
155 
toWebCoreStringWithNullOrUndefinedCheck(v8::Handle<v8::Value> value)156     inline String toWebCoreStringWithNullOrUndefinedCheck(v8::Handle<v8::Value> value)
157     {
158         if (value->IsNull() || value->IsUndefined())
159             return String();
160         return toWebCoreString(value);
161     }
162 
v8UndetectableString(const String & str)163     inline v8::Handle<v8::String> v8UndetectableString(const String& str)
164     {
165         return v8::String::NewUndetectable(fromWebCoreString(str), str.length());
166     }
167 
v8StringOrNull(const String & str)168     inline v8::Handle<v8::Value> v8StringOrNull(const String& str)
169     {
170         return str.isNull() ? v8::Handle<v8::Value>(v8::Null()) : v8::Handle<v8::Value>(v8String(str));
171     }
172 
v8StringOrUndefined(const String & str)173     inline v8::Handle<v8::Value> v8StringOrUndefined(const String& str)
174     {
175         return str.isNull() ? v8::Handle<v8::Value>(v8::Undefined()) : v8::Handle<v8::Value>(v8String(str));
176     }
177 
v8StringOrFalse(const String & str)178     inline v8::Handle<v8::Value> v8StringOrFalse(const String& str)
179     {
180         return str.isNull() ? v8::Handle<v8::Value>(v8::False()) : v8::Handle<v8::Value>(v8String(str));
181     }
182 } // namespace WebCore
183 
184 #endif // V8Binding_h
185