|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef yarr_wtfbridge_h |
|
8 #define yarr_wtfbridge_h |
|
9 |
|
10 /* |
|
11 * WTF compatibility layer. This file provides various type and data |
|
12 * definitions for use by Yarr. |
|
13 */ |
|
14 |
|
15 #include <stdio.h> |
|
16 #include <stdarg.h> |
|
17 #include "jscntxt.h" |
|
18 #include "jsstr.h" |
|
19 #include "vm/String.h" |
|
20 #include "assembler/wtf/Platform.h" |
|
21 #include "assembler/jit/ExecutableAllocator.h" |
|
22 #include "yarr/CheckedArithmetic.h" |
|
23 |
|
24 namespace JSC { namespace Yarr { |
|
25 |
|
26 /* |
|
27 * Basic type definitions. |
|
28 */ |
|
29 |
|
30 typedef char LChar; |
|
31 typedef jschar UChar; |
|
32 typedef JSLinearString UString; |
|
33 typedef JSLinearString String; |
|
34 |
|
35 |
|
36 class Unicode { |
|
37 public: |
|
38 static UChar toUpper(UChar c) { return js::unicode::ToUpperCase(c); } |
|
39 static UChar toLower(UChar c) { return js::unicode::ToLowerCase(c); } |
|
40 }; |
|
41 |
|
42 /* |
|
43 * Do-nothing smart pointer classes. These have a compatible interface |
|
44 * with the smart pointers used by Yarr, but they don't actually do |
|
45 * reference counting. |
|
46 */ |
|
47 template<typename T> |
|
48 class RefCounted { |
|
49 }; |
|
50 |
|
51 template<typename T> |
|
52 class RefPtr { |
|
53 T *ptr; |
|
54 public: |
|
55 RefPtr(T *p) { ptr = p; } |
|
56 operator bool() const { return ptr != NULL; } |
|
57 const T *operator ->() const { return ptr; } |
|
58 T *get() { return ptr; } |
|
59 }; |
|
60 |
|
61 template<typename T> |
|
62 class PassRefPtr { |
|
63 T *ptr; |
|
64 public: |
|
65 PassRefPtr(T *p) { ptr = p; } |
|
66 operator T*() { return ptr; } |
|
67 }; |
|
68 |
|
69 template<typename T> |
|
70 class PassOwnPtr { |
|
71 T *ptr; |
|
72 public: |
|
73 PassOwnPtr(T *p) { ptr = p; } |
|
74 |
|
75 T *get() { return ptr; } |
|
76 }; |
|
77 |
|
78 template<typename T> |
|
79 class OwnPtr { |
|
80 T *ptr; |
|
81 public: |
|
82 OwnPtr() : ptr(NULL) { } |
|
83 OwnPtr(PassOwnPtr<T> p) : ptr(p.get()) { } |
|
84 |
|
85 ~OwnPtr() { |
|
86 js_delete(ptr); |
|
87 } |
|
88 |
|
89 OwnPtr<T> &operator=(PassOwnPtr<T> p) { |
|
90 ptr = p.get(); |
|
91 return *this; |
|
92 } |
|
93 |
|
94 T *operator ->() { return ptr; } |
|
95 |
|
96 T *get() { return ptr; } |
|
97 |
|
98 T *release() { |
|
99 T *result = ptr; |
|
100 ptr = NULL; |
|
101 return result; |
|
102 } |
|
103 }; |
|
104 |
|
105 template<typename T> |
|
106 PassRefPtr<T> adoptRef(T *p) { return PassRefPtr<T>(p); } |
|
107 |
|
108 template<typename T> |
|
109 PassOwnPtr<T> adoptPtr(T *p) { return PassOwnPtr<T>(p); } |
|
110 |
|
111 // Dummy wrapper. |
|
112 #define WTF_MAKE_FAST_ALLOCATED void make_fast_allocated_() |
|
113 |
|
114 template<typename T> |
|
115 class Ref { |
|
116 T &val; |
|
117 public: |
|
118 Ref(T &val) : val(val) { } |
|
119 operator T&() const { return val; } |
|
120 }; |
|
121 |
|
122 /* |
|
123 * Vector class for Yarr. This wraps js::Vector and provides all |
|
124 * the API method signatures used by Yarr. |
|
125 */ |
|
126 template<typename T, size_t N = 0> |
|
127 class Vector { |
|
128 public: |
|
129 js::Vector<T, N, js::SystemAllocPolicy> impl; |
|
130 public: |
|
131 Vector() {} |
|
132 |
|
133 Vector(const Vector &v) { |
|
134 append(v); |
|
135 } |
|
136 |
|
137 size_t size() const { |
|
138 return impl.length(); |
|
139 } |
|
140 |
|
141 T &operator[](size_t i) { |
|
142 return impl[i]; |
|
143 } |
|
144 |
|
145 const T &operator[](size_t i) const { |
|
146 return impl[i]; |
|
147 } |
|
148 |
|
149 T &at(size_t i) { |
|
150 return impl[i]; |
|
151 } |
|
152 |
|
153 const T *begin() const { |
|
154 return impl.begin(); |
|
155 } |
|
156 |
|
157 T &last() { |
|
158 return impl.back(); |
|
159 } |
|
160 |
|
161 bool isEmpty() const { |
|
162 return impl.empty(); |
|
163 } |
|
164 |
|
165 template <typename U> |
|
166 void append(const U &u) { |
|
167 if (!impl.append(static_cast<T>(u))) |
|
168 js::CrashAtUnhandlableOOM("Yarr"); |
|
169 } |
|
170 |
|
171 template <size_t M> |
|
172 void append(const Vector<T,M> &v) { |
|
173 if (!impl.appendAll(v.impl)) |
|
174 js::CrashAtUnhandlableOOM("Yarr"); |
|
175 } |
|
176 |
|
177 void insert(size_t i, const T& t) { |
|
178 if (!impl.insert(&impl[i], t)) |
|
179 js::CrashAtUnhandlableOOM("Yarr"); |
|
180 } |
|
181 |
|
182 void remove(size_t i) { |
|
183 impl.erase(&impl[i]); |
|
184 } |
|
185 |
|
186 void clear() { |
|
187 return impl.clear(); |
|
188 } |
|
189 |
|
190 void shrink(size_t newLength) { |
|
191 JS_ASSERT(newLength <= impl.length()); |
|
192 if (!impl.resize(newLength)) |
|
193 js::CrashAtUnhandlableOOM("Yarr"); |
|
194 } |
|
195 |
|
196 void swap(Vector &other) { |
|
197 impl.swap(other.impl); |
|
198 } |
|
199 |
|
200 void deleteAllValues() { |
|
201 for (T *p = impl.begin(); p != impl.end(); ++p) |
|
202 js_delete(*p); |
|
203 } |
|
204 |
|
205 bool reserve(size_t capacity) { |
|
206 return impl.reserve(capacity); |
|
207 } |
|
208 }; |
|
209 |
|
210 template<typename T> |
|
211 class Vector<OwnPtr<T> > { |
|
212 public: |
|
213 js::Vector<T *, 0, js::SystemAllocPolicy> impl; |
|
214 public: |
|
215 Vector() {} |
|
216 |
|
217 size_t size() const { |
|
218 return impl.length(); |
|
219 } |
|
220 |
|
221 void append(T *t) { |
|
222 if (!impl.append(t)) |
|
223 js::CrashAtUnhandlableOOM("Yarr"); |
|
224 } |
|
225 |
|
226 PassOwnPtr<T> operator[](size_t i) { |
|
227 return PassOwnPtr<T>(impl[i]); |
|
228 } |
|
229 |
|
230 void clear() { |
|
231 for (T **p = impl.begin(); p != impl.end(); ++p) |
|
232 delete_(*p); |
|
233 return impl.clear(); |
|
234 } |
|
235 |
|
236 void reserve(size_t capacity) { |
|
237 if (!impl.reserve(capacity)) |
|
238 js::CrashAtUnhandlableOOM("Yarr"); |
|
239 } |
|
240 }; |
|
241 |
|
242 template <typename T, size_t N> |
|
243 inline void |
|
244 deleteAllValues(Vector<T, N> &v) { |
|
245 v.deleteAllValues(); |
|
246 } |
|
247 |
|
248 static inline void |
|
249 dataLogF(const char *fmt, ...) |
|
250 { |
|
251 va_list ap; |
|
252 va_start(ap, fmt); |
|
253 vfprintf(stderr, fmt, ap); |
|
254 va_end(ap); |
|
255 } |
|
256 |
|
257 #if ENABLE_YARR_JIT |
|
258 |
|
259 /* |
|
260 * Minimal JSGlobalData. This used by Yarr to get the allocator. |
|
261 */ |
|
262 class JSGlobalData { |
|
263 public: |
|
264 ExecutableAllocator *regexAllocator; |
|
265 |
|
266 JSGlobalData(ExecutableAllocator *regexAllocator) |
|
267 : regexAllocator(regexAllocator) { } |
|
268 }; |
|
269 |
|
270 #endif |
|
271 |
|
272 /* |
|
273 * Do-nothing version of a macro used by WTF to avoid unused |
|
274 * parameter warnings. |
|
275 */ |
|
276 #define UNUSED_PARAM(e) |
|
277 |
|
278 /* |
|
279 * Like SpiderMonkey's allocation templates, but with more crashing. |
|
280 */ |
|
281 template <class T> |
|
282 T *newOrCrash() |
|
283 { |
|
284 T *t = js_new<T>(); |
|
285 if (!t) |
|
286 js::CrashAtUnhandlableOOM("Yarr"); |
|
287 return t; |
|
288 } |
|
289 |
|
290 template <class T, class P1> |
|
291 T *newOrCrash(P1 &&p1) |
|
292 { |
|
293 T *t = js_new<T>(mozilla::Forward<P1>(p1)); |
|
294 if (!t) |
|
295 js::CrashAtUnhandlableOOM("Yarr"); |
|
296 return t; |
|
297 } |
|
298 |
|
299 template <class T, class P1, class P2> |
|
300 T *newOrCrash(P1 &&p1, P2 &&p2) |
|
301 { |
|
302 T *t = js_new<T>(mozilla::Forward<P1>(p1), mozilla::Forward<P2>(p2)); |
|
303 if (!t) |
|
304 js::CrashAtUnhandlableOOM("Yarr"); |
|
305 return t; |
|
306 } |
|
307 |
|
308 template <class T, class P1, class P2, class P3> |
|
309 T *newOrCrash(P1 &&p1, P2 &&p2, P3 &&p3) |
|
310 { |
|
311 T *t = js_new<T>(mozilla::Forward<P1>(p1), mozilla::Forward<P2>(p2), mozilla::Forward<P3>(p3)); |
|
312 if (!t) |
|
313 js::CrashAtUnhandlableOOM("Yarr"); |
|
314 return t; |
|
315 } |
|
316 |
|
317 template <class T, class P1, class P2, class P3, class P4> |
|
318 T *newOrCrash(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4) |
|
319 { |
|
320 T *t = js_new<T>(mozilla::Forward<P1>(p1), |
|
321 mozilla::Forward<P2>(p2), |
|
322 mozilla::Forward<P3>(p3), |
|
323 mozilla::Forward<P4>(p4)); |
|
324 if (!t) |
|
325 js::CrashAtUnhandlableOOM("Yarr"); |
|
326 return t; |
|
327 } |
|
328 |
|
329 } /* namespace Yarr */ |
|
330 |
|
331 /* |
|
332 * Replacements for std:: functions used in Yarr. We put them in |
|
333 * namespace JSC::std so that they can still be called as std::X |
|
334 * in Yarr. |
|
335 */ |
|
336 namespace std { |
|
337 |
|
338 /* |
|
339 * windows.h defines a 'min' macro that would mangle the function |
|
340 * name. |
|
341 */ |
|
342 #if WTF_COMPILER_MSVC |
|
343 # undef min |
|
344 # undef max |
|
345 #endif |
|
346 |
|
347 #define NO_RETURN_DUE_TO_ASSERT |
|
348 |
|
349 template<typename T> |
|
350 inline T |
|
351 min(T t1, T t2) |
|
352 { |
|
353 return js::Min(t1, t2); |
|
354 } |
|
355 |
|
356 template<typename T> |
|
357 inline T |
|
358 max(T t1, T t2) |
|
359 { |
|
360 return js::Max(t1, t2); |
|
361 } |
|
362 |
|
363 template<typename T> |
|
364 inline void |
|
365 swap(T &t1, T &t2) |
|
366 { |
|
367 T tmp = t1; |
|
368 t1 = t2; |
|
369 t2 = tmp; |
|
370 } |
|
371 } /* namespace std */ |
|
372 |
|
373 } /* namespace JSC */ |
|
374 |
|
375 namespace WTF { |
|
376 |
|
377 /* |
|
378 * Sentinel value used in Yarr. |
|
379 */ |
|
380 const size_t notFound = size_t(-1); |
|
381 |
|
382 } |
|
383 |
|
384 #define JS_EXPORT_PRIVATE |
|
385 |
|
386 #endif /* yarr_wtfbridge_h */ |