1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/yarr/wtfbridge.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,386 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef yarr_wtfbridge_h 1.11 +#define yarr_wtfbridge_h 1.12 + 1.13 +/* 1.14 + * WTF compatibility layer. This file provides various type and data 1.15 + * definitions for use by Yarr. 1.16 + */ 1.17 + 1.18 +#include <stdio.h> 1.19 +#include <stdarg.h> 1.20 +#include "jscntxt.h" 1.21 +#include "jsstr.h" 1.22 +#include "vm/String.h" 1.23 +#include "assembler/wtf/Platform.h" 1.24 +#include "assembler/jit/ExecutableAllocator.h" 1.25 +#include "yarr/CheckedArithmetic.h" 1.26 + 1.27 +namespace JSC { namespace Yarr { 1.28 + 1.29 +/* 1.30 + * Basic type definitions. 1.31 + */ 1.32 + 1.33 +typedef char LChar; 1.34 +typedef jschar UChar; 1.35 +typedef JSLinearString UString; 1.36 +typedef JSLinearString String; 1.37 + 1.38 + 1.39 +class Unicode { 1.40 + public: 1.41 + static UChar toUpper(UChar c) { return js::unicode::ToUpperCase(c); } 1.42 + static UChar toLower(UChar c) { return js::unicode::ToLowerCase(c); } 1.43 +}; 1.44 + 1.45 +/* 1.46 + * Do-nothing smart pointer classes. These have a compatible interface 1.47 + * with the smart pointers used by Yarr, but they don't actually do 1.48 + * reference counting. 1.49 + */ 1.50 +template<typename T> 1.51 +class RefCounted { 1.52 +}; 1.53 + 1.54 +template<typename T> 1.55 +class RefPtr { 1.56 + T *ptr; 1.57 + public: 1.58 + RefPtr(T *p) { ptr = p; } 1.59 + operator bool() const { return ptr != NULL; } 1.60 + const T *operator ->() const { return ptr; } 1.61 + T *get() { return ptr; } 1.62 +}; 1.63 + 1.64 +template<typename T> 1.65 +class PassRefPtr { 1.66 + T *ptr; 1.67 + public: 1.68 + PassRefPtr(T *p) { ptr = p; } 1.69 + operator T*() { return ptr; } 1.70 +}; 1.71 + 1.72 +template<typename T> 1.73 +class PassOwnPtr { 1.74 + T *ptr; 1.75 + public: 1.76 + PassOwnPtr(T *p) { ptr = p; } 1.77 + 1.78 + T *get() { return ptr; } 1.79 +}; 1.80 + 1.81 +template<typename T> 1.82 +class OwnPtr { 1.83 + T *ptr; 1.84 + public: 1.85 + OwnPtr() : ptr(NULL) { } 1.86 + OwnPtr(PassOwnPtr<T> p) : ptr(p.get()) { } 1.87 + 1.88 + ~OwnPtr() { 1.89 + js_delete(ptr); 1.90 + } 1.91 + 1.92 + OwnPtr<T> &operator=(PassOwnPtr<T> p) { 1.93 + ptr = p.get(); 1.94 + return *this; 1.95 + } 1.96 + 1.97 + T *operator ->() { return ptr; } 1.98 + 1.99 + T *get() { return ptr; } 1.100 + 1.101 + T *release() { 1.102 + T *result = ptr; 1.103 + ptr = NULL; 1.104 + return result; 1.105 + } 1.106 +}; 1.107 + 1.108 +template<typename T> 1.109 +PassRefPtr<T> adoptRef(T *p) { return PassRefPtr<T>(p); } 1.110 + 1.111 +template<typename T> 1.112 +PassOwnPtr<T> adoptPtr(T *p) { return PassOwnPtr<T>(p); } 1.113 + 1.114 +// Dummy wrapper. 1.115 +#define WTF_MAKE_FAST_ALLOCATED void make_fast_allocated_() 1.116 + 1.117 +template<typename T> 1.118 +class Ref { 1.119 + T &val; 1.120 + public: 1.121 + Ref(T &val) : val(val) { } 1.122 + operator T&() const { return val; } 1.123 +}; 1.124 + 1.125 +/* 1.126 + * Vector class for Yarr. This wraps js::Vector and provides all 1.127 + * the API method signatures used by Yarr. 1.128 + */ 1.129 +template<typename T, size_t N = 0> 1.130 +class Vector { 1.131 + public: 1.132 + js::Vector<T, N, js::SystemAllocPolicy> impl; 1.133 + public: 1.134 + Vector() {} 1.135 + 1.136 + Vector(const Vector &v) { 1.137 + append(v); 1.138 + } 1.139 + 1.140 + size_t size() const { 1.141 + return impl.length(); 1.142 + } 1.143 + 1.144 + T &operator[](size_t i) { 1.145 + return impl[i]; 1.146 + } 1.147 + 1.148 + const T &operator[](size_t i) const { 1.149 + return impl[i]; 1.150 + } 1.151 + 1.152 + T &at(size_t i) { 1.153 + return impl[i]; 1.154 + } 1.155 + 1.156 + const T *begin() const { 1.157 + return impl.begin(); 1.158 + } 1.159 + 1.160 + T &last() { 1.161 + return impl.back(); 1.162 + } 1.163 + 1.164 + bool isEmpty() const { 1.165 + return impl.empty(); 1.166 + } 1.167 + 1.168 + template <typename U> 1.169 + void append(const U &u) { 1.170 + if (!impl.append(static_cast<T>(u))) 1.171 + js::CrashAtUnhandlableOOM("Yarr"); 1.172 + } 1.173 + 1.174 + template <size_t M> 1.175 + void append(const Vector<T,M> &v) { 1.176 + if (!impl.appendAll(v.impl)) 1.177 + js::CrashAtUnhandlableOOM("Yarr"); 1.178 + } 1.179 + 1.180 + void insert(size_t i, const T& t) { 1.181 + if (!impl.insert(&impl[i], t)) 1.182 + js::CrashAtUnhandlableOOM("Yarr"); 1.183 + } 1.184 + 1.185 + void remove(size_t i) { 1.186 + impl.erase(&impl[i]); 1.187 + } 1.188 + 1.189 + void clear() { 1.190 + return impl.clear(); 1.191 + } 1.192 + 1.193 + void shrink(size_t newLength) { 1.194 + JS_ASSERT(newLength <= impl.length()); 1.195 + if (!impl.resize(newLength)) 1.196 + js::CrashAtUnhandlableOOM("Yarr"); 1.197 + } 1.198 + 1.199 + void swap(Vector &other) { 1.200 + impl.swap(other.impl); 1.201 + } 1.202 + 1.203 + void deleteAllValues() { 1.204 + for (T *p = impl.begin(); p != impl.end(); ++p) 1.205 + js_delete(*p); 1.206 + } 1.207 + 1.208 + bool reserve(size_t capacity) { 1.209 + return impl.reserve(capacity); 1.210 + } 1.211 +}; 1.212 + 1.213 +template<typename T> 1.214 +class Vector<OwnPtr<T> > { 1.215 + public: 1.216 + js::Vector<T *, 0, js::SystemAllocPolicy> impl; 1.217 + public: 1.218 + Vector() {} 1.219 + 1.220 + size_t size() const { 1.221 + return impl.length(); 1.222 + } 1.223 + 1.224 + void append(T *t) { 1.225 + if (!impl.append(t)) 1.226 + js::CrashAtUnhandlableOOM("Yarr"); 1.227 + } 1.228 + 1.229 + PassOwnPtr<T> operator[](size_t i) { 1.230 + return PassOwnPtr<T>(impl[i]); 1.231 + } 1.232 + 1.233 + void clear() { 1.234 + for (T **p = impl.begin(); p != impl.end(); ++p) 1.235 + delete_(*p); 1.236 + return impl.clear(); 1.237 + } 1.238 + 1.239 + void reserve(size_t capacity) { 1.240 + if (!impl.reserve(capacity)) 1.241 + js::CrashAtUnhandlableOOM("Yarr"); 1.242 + } 1.243 +}; 1.244 + 1.245 +template <typename T, size_t N> 1.246 +inline void 1.247 +deleteAllValues(Vector<T, N> &v) { 1.248 + v.deleteAllValues(); 1.249 +} 1.250 + 1.251 +static inline void 1.252 +dataLogF(const char *fmt, ...) 1.253 +{ 1.254 + va_list ap; 1.255 + va_start(ap, fmt); 1.256 + vfprintf(stderr, fmt, ap); 1.257 + va_end(ap); 1.258 +} 1.259 + 1.260 +#if ENABLE_YARR_JIT 1.261 + 1.262 +/* 1.263 + * Minimal JSGlobalData. This used by Yarr to get the allocator. 1.264 + */ 1.265 +class JSGlobalData { 1.266 + public: 1.267 + ExecutableAllocator *regexAllocator; 1.268 + 1.269 + JSGlobalData(ExecutableAllocator *regexAllocator) 1.270 + : regexAllocator(regexAllocator) { } 1.271 +}; 1.272 + 1.273 +#endif 1.274 + 1.275 + /* 1.276 + * Do-nothing version of a macro used by WTF to avoid unused 1.277 + * parameter warnings. 1.278 + */ 1.279 +#define UNUSED_PARAM(e) 1.280 + 1.281 +/* 1.282 + * Like SpiderMonkey's allocation templates, but with more crashing. 1.283 + */ 1.284 +template <class T> 1.285 +T *newOrCrash() 1.286 +{ 1.287 + T *t = js_new<T>(); 1.288 + if (!t) 1.289 + js::CrashAtUnhandlableOOM("Yarr"); 1.290 + return t; 1.291 +} 1.292 + 1.293 +template <class T, class P1> 1.294 +T *newOrCrash(P1 &&p1) 1.295 +{ 1.296 + T *t = js_new<T>(mozilla::Forward<P1>(p1)); 1.297 + if (!t) 1.298 + js::CrashAtUnhandlableOOM("Yarr"); 1.299 + return t; 1.300 +} 1.301 + 1.302 +template <class T, class P1, class P2> 1.303 +T *newOrCrash(P1 &&p1, P2 &&p2) 1.304 +{ 1.305 + T *t = js_new<T>(mozilla::Forward<P1>(p1), mozilla::Forward<P2>(p2)); 1.306 + if (!t) 1.307 + js::CrashAtUnhandlableOOM("Yarr"); 1.308 + return t; 1.309 +} 1.310 + 1.311 +template <class T, class P1, class P2, class P3> 1.312 +T *newOrCrash(P1 &&p1, P2 &&p2, P3 &&p3) 1.313 +{ 1.314 + T *t = js_new<T>(mozilla::Forward<P1>(p1), mozilla::Forward<P2>(p2), mozilla::Forward<P3>(p3)); 1.315 + if (!t) 1.316 + js::CrashAtUnhandlableOOM("Yarr"); 1.317 + return t; 1.318 +} 1.319 + 1.320 +template <class T, class P1, class P2, class P3, class P4> 1.321 +T *newOrCrash(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4) 1.322 +{ 1.323 + T *t = js_new<T>(mozilla::Forward<P1>(p1), 1.324 + mozilla::Forward<P2>(p2), 1.325 + mozilla::Forward<P3>(p3), 1.326 + mozilla::Forward<P4>(p4)); 1.327 + if (!t) 1.328 + js::CrashAtUnhandlableOOM("Yarr"); 1.329 + return t; 1.330 +} 1.331 + 1.332 +} /* namespace Yarr */ 1.333 + 1.334 +/* 1.335 + * Replacements for std:: functions used in Yarr. We put them in 1.336 + * namespace JSC::std so that they can still be called as std::X 1.337 + * in Yarr. 1.338 + */ 1.339 +namespace std { 1.340 + 1.341 +/* 1.342 + * windows.h defines a 'min' macro that would mangle the function 1.343 + * name. 1.344 + */ 1.345 +#if WTF_COMPILER_MSVC 1.346 +# undef min 1.347 +# undef max 1.348 +#endif 1.349 + 1.350 +#define NO_RETURN_DUE_TO_ASSERT 1.351 + 1.352 +template<typename T> 1.353 +inline T 1.354 +min(T t1, T t2) 1.355 +{ 1.356 + return js::Min(t1, t2); 1.357 +} 1.358 + 1.359 +template<typename T> 1.360 +inline T 1.361 +max(T t1, T t2) 1.362 +{ 1.363 + return js::Max(t1, t2); 1.364 +} 1.365 + 1.366 +template<typename T> 1.367 +inline void 1.368 +swap(T &t1, T &t2) 1.369 +{ 1.370 + T tmp = t1; 1.371 + t1 = t2; 1.372 + t2 = tmp; 1.373 +} 1.374 +} /* namespace std */ 1.375 + 1.376 +} /* namespace JSC */ 1.377 + 1.378 +namespace WTF { 1.379 + 1.380 +/* 1.381 + * Sentinel value used in Yarr. 1.382 + */ 1.383 +const size_t notFound = size_t(-1); 1.384 + 1.385 +} 1.386 + 1.387 +#define JS_EXPORT_PRIVATE 1.388 + 1.389 +#endif /* yarr_wtfbridge_h */