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