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