Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
7 #ifndef yarr_wtfbridge_h
8 #define yarr_wtfbridge_h
10 /*
11 * WTF compatibility layer. This file provides various type and data
12 * definitions for use by Yarr.
13 */
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"
24 namespace JSC { namespace Yarr {
26 /*
27 * Basic type definitions.
28 */
30 typedef char LChar;
31 typedef jschar UChar;
32 typedef JSLinearString UString;
33 typedef JSLinearString String;
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 };
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 };
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 };
61 template<typename T>
62 class PassRefPtr {
63 T *ptr;
64 public:
65 PassRefPtr(T *p) { ptr = p; }
66 operator T*() { return ptr; }
67 };
69 template<typename T>
70 class PassOwnPtr {
71 T *ptr;
72 public:
73 PassOwnPtr(T *p) { ptr = p; }
75 T *get() { return ptr; }
76 };
78 template<typename T>
79 class OwnPtr {
80 T *ptr;
81 public:
82 OwnPtr() : ptr(NULL) { }
83 OwnPtr(PassOwnPtr<T> p) : ptr(p.get()) { }
85 ~OwnPtr() {
86 js_delete(ptr);
87 }
89 OwnPtr<T> &operator=(PassOwnPtr<T> p) {
90 ptr = p.get();
91 return *this;
92 }
94 T *operator ->() { return ptr; }
96 T *get() { return ptr; }
98 T *release() {
99 T *result = ptr;
100 ptr = NULL;
101 return result;
102 }
103 };
105 template<typename T>
106 PassRefPtr<T> adoptRef(T *p) { return PassRefPtr<T>(p); }
108 template<typename T>
109 PassOwnPtr<T> adoptPtr(T *p) { return PassOwnPtr<T>(p); }
111 // Dummy wrapper.
112 #define WTF_MAKE_FAST_ALLOCATED void make_fast_allocated_()
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 };
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() {}
133 Vector(const Vector &v) {
134 append(v);
135 }
137 size_t size() const {
138 return impl.length();
139 }
141 T &operator[](size_t i) {
142 return impl[i];
143 }
145 const T &operator[](size_t i) const {
146 return impl[i];
147 }
149 T &at(size_t i) {
150 return impl[i];
151 }
153 const T *begin() const {
154 return impl.begin();
155 }
157 T &last() {
158 return impl.back();
159 }
161 bool isEmpty() const {
162 return impl.empty();
163 }
165 template <typename U>
166 void append(const U &u) {
167 if (!impl.append(static_cast<T>(u)))
168 js::CrashAtUnhandlableOOM("Yarr");
169 }
171 template <size_t M>
172 void append(const Vector<T,M> &v) {
173 if (!impl.appendAll(v.impl))
174 js::CrashAtUnhandlableOOM("Yarr");
175 }
177 void insert(size_t i, const T& t) {
178 if (!impl.insert(&impl[i], t))
179 js::CrashAtUnhandlableOOM("Yarr");
180 }
182 void remove(size_t i) {
183 impl.erase(&impl[i]);
184 }
186 void clear() {
187 return impl.clear();
188 }
190 void shrink(size_t newLength) {
191 JS_ASSERT(newLength <= impl.length());
192 if (!impl.resize(newLength))
193 js::CrashAtUnhandlableOOM("Yarr");
194 }
196 void swap(Vector &other) {
197 impl.swap(other.impl);
198 }
200 void deleteAllValues() {
201 for (T *p = impl.begin(); p != impl.end(); ++p)
202 js_delete(*p);
203 }
205 bool reserve(size_t capacity) {
206 return impl.reserve(capacity);
207 }
208 };
210 template<typename T>
211 class Vector<OwnPtr<T> > {
212 public:
213 js::Vector<T *, 0, js::SystemAllocPolicy> impl;
214 public:
215 Vector() {}
217 size_t size() const {
218 return impl.length();
219 }
221 void append(T *t) {
222 if (!impl.append(t))
223 js::CrashAtUnhandlableOOM("Yarr");
224 }
226 PassOwnPtr<T> operator[](size_t i) {
227 return PassOwnPtr<T>(impl[i]);
228 }
230 void clear() {
231 for (T **p = impl.begin(); p != impl.end(); ++p)
232 delete_(*p);
233 return impl.clear();
234 }
236 void reserve(size_t capacity) {
237 if (!impl.reserve(capacity))
238 js::CrashAtUnhandlableOOM("Yarr");
239 }
240 };
242 template <typename T, size_t N>
243 inline void
244 deleteAllValues(Vector<T, N> &v) {
245 v.deleteAllValues();
246 }
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 }
257 #if ENABLE_YARR_JIT
259 /*
260 * Minimal JSGlobalData. This used by Yarr to get the allocator.
261 */
262 class JSGlobalData {
263 public:
264 ExecutableAllocator *regexAllocator;
266 JSGlobalData(ExecutableAllocator *regexAllocator)
267 : regexAllocator(regexAllocator) { }
268 };
270 #endif
272 /*
273 * Do-nothing version of a macro used by WTF to avoid unused
274 * parameter warnings.
275 */
276 #define UNUSED_PARAM(e)
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 }
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 }
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 }
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 }
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 }
329 } /* namespace Yarr */
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 {
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
347 #define NO_RETURN_DUE_TO_ASSERT
349 template<typename T>
350 inline T
351 min(T t1, T t2)
352 {
353 return js::Min(t1, t2);
354 }
356 template<typename T>
357 inline T
358 max(T t1, T t2)
359 {
360 return js::Max(t1, t2);
361 }
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 */
373 } /* namespace JSC */
375 namespace WTF {
377 /*
378 * Sentinel value used in Yarr.
379 */
380 const size_t notFound = size_t(-1);
382 }
384 #define JS_EXPORT_PRIVATE
386 #endif /* yarr_wtfbridge_h */