diff -r 000000000000 -r 6474c204b198 js/src/yarr/wtfbridge.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/js/src/yarr/wtfbridge.h Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,386 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * vim: set ts=8 sts=4 et sw=4 tw=99: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef yarr_wtfbridge_h +#define yarr_wtfbridge_h + +/* + * WTF compatibility layer. This file provides various type and data + * definitions for use by Yarr. + */ + +#include +#include +#include "jscntxt.h" +#include "jsstr.h" +#include "vm/String.h" +#include "assembler/wtf/Platform.h" +#include "assembler/jit/ExecutableAllocator.h" +#include "yarr/CheckedArithmetic.h" + +namespace JSC { namespace Yarr { + +/* + * Basic type definitions. + */ + +typedef char LChar; +typedef jschar UChar; +typedef JSLinearString UString; +typedef JSLinearString String; + + +class Unicode { + public: + static UChar toUpper(UChar c) { return js::unicode::ToUpperCase(c); } + static UChar toLower(UChar c) { return js::unicode::ToLowerCase(c); } +}; + +/* + * Do-nothing smart pointer classes. These have a compatible interface + * with the smart pointers used by Yarr, but they don't actually do + * reference counting. + */ +template +class RefCounted { +}; + +template +class RefPtr { + T *ptr; + public: + RefPtr(T *p) { ptr = p; } + operator bool() const { return ptr != NULL; } + const T *operator ->() const { return ptr; } + T *get() { return ptr; } +}; + +template +class PassRefPtr { + T *ptr; + public: + PassRefPtr(T *p) { ptr = p; } + operator T*() { return ptr; } +}; + +template +class PassOwnPtr { + T *ptr; + public: + PassOwnPtr(T *p) { ptr = p; } + + T *get() { return ptr; } +}; + +template +class OwnPtr { + T *ptr; + public: + OwnPtr() : ptr(NULL) { } + OwnPtr(PassOwnPtr p) : ptr(p.get()) { } + + ~OwnPtr() { + js_delete(ptr); + } + + OwnPtr &operator=(PassOwnPtr p) { + ptr = p.get(); + return *this; + } + + T *operator ->() { return ptr; } + + T *get() { return ptr; } + + T *release() { + T *result = ptr; + ptr = NULL; + return result; + } +}; + +template +PassRefPtr adoptRef(T *p) { return PassRefPtr(p); } + +template +PassOwnPtr adoptPtr(T *p) { return PassOwnPtr(p); } + +// Dummy wrapper. +#define WTF_MAKE_FAST_ALLOCATED void make_fast_allocated_() + +template +class Ref { + T &val; + public: + Ref(T &val) : val(val) { } + operator T&() const { return val; } +}; + +/* + * Vector class for Yarr. This wraps js::Vector and provides all + * the API method signatures used by Yarr. + */ +template +class Vector { + public: + js::Vector impl; + public: + Vector() {} + + Vector(const Vector &v) { + append(v); + } + + size_t size() const { + return impl.length(); + } + + T &operator[](size_t i) { + return impl[i]; + } + + const T &operator[](size_t i) const { + return impl[i]; + } + + T &at(size_t i) { + return impl[i]; + } + + const T *begin() const { + return impl.begin(); + } + + T &last() { + return impl.back(); + } + + bool isEmpty() const { + return impl.empty(); + } + + template + void append(const U &u) { + if (!impl.append(static_cast(u))) + js::CrashAtUnhandlableOOM("Yarr"); + } + + template + void append(const Vector &v) { + if (!impl.appendAll(v.impl)) + js::CrashAtUnhandlableOOM("Yarr"); + } + + void insert(size_t i, const T& t) { + if (!impl.insert(&impl[i], t)) + js::CrashAtUnhandlableOOM("Yarr"); + } + + void remove(size_t i) { + impl.erase(&impl[i]); + } + + void clear() { + return impl.clear(); + } + + void shrink(size_t newLength) { + JS_ASSERT(newLength <= impl.length()); + if (!impl.resize(newLength)) + js::CrashAtUnhandlableOOM("Yarr"); + } + + void swap(Vector &other) { + impl.swap(other.impl); + } + + void deleteAllValues() { + for (T *p = impl.begin(); p != impl.end(); ++p) + js_delete(*p); + } + + bool reserve(size_t capacity) { + return impl.reserve(capacity); + } +}; + +template +class Vector > { + public: + js::Vector impl; + public: + Vector() {} + + size_t size() const { + return impl.length(); + } + + void append(T *t) { + if (!impl.append(t)) + js::CrashAtUnhandlableOOM("Yarr"); + } + + PassOwnPtr operator[](size_t i) { + return PassOwnPtr(impl[i]); + } + + void clear() { + for (T **p = impl.begin(); p != impl.end(); ++p) + delete_(*p); + return impl.clear(); + } + + void reserve(size_t capacity) { + if (!impl.reserve(capacity)) + js::CrashAtUnhandlableOOM("Yarr"); + } +}; + +template +inline void +deleteAllValues(Vector &v) { + v.deleteAllValues(); +} + +static inline void +dataLogF(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); +} + +#if ENABLE_YARR_JIT + +/* + * Minimal JSGlobalData. This used by Yarr to get the allocator. + */ +class JSGlobalData { + public: + ExecutableAllocator *regexAllocator; + + JSGlobalData(ExecutableAllocator *regexAllocator) + : regexAllocator(regexAllocator) { } +}; + +#endif + + /* + * Do-nothing version of a macro used by WTF to avoid unused + * parameter warnings. + */ +#define UNUSED_PARAM(e) + +/* + * Like SpiderMonkey's allocation templates, but with more crashing. + */ +template +T *newOrCrash() +{ + T *t = js_new(); + if (!t) + js::CrashAtUnhandlableOOM("Yarr"); + return t; +} + +template +T *newOrCrash(P1 &&p1) +{ + T *t = js_new(mozilla::Forward(p1)); + if (!t) + js::CrashAtUnhandlableOOM("Yarr"); + return t; +} + +template +T *newOrCrash(P1 &&p1, P2 &&p2) +{ + T *t = js_new(mozilla::Forward(p1), mozilla::Forward(p2)); + if (!t) + js::CrashAtUnhandlableOOM("Yarr"); + return t; +} + +template +T *newOrCrash(P1 &&p1, P2 &&p2, P3 &&p3) +{ + T *t = js_new(mozilla::Forward(p1), mozilla::Forward(p2), mozilla::Forward(p3)); + if (!t) + js::CrashAtUnhandlableOOM("Yarr"); + return t; +} + +template +T *newOrCrash(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4) +{ + T *t = js_new(mozilla::Forward(p1), + mozilla::Forward(p2), + mozilla::Forward(p3), + mozilla::Forward(p4)); + if (!t) + js::CrashAtUnhandlableOOM("Yarr"); + return t; +} + +} /* namespace Yarr */ + +/* + * Replacements for std:: functions used in Yarr. We put them in + * namespace JSC::std so that they can still be called as std::X + * in Yarr. + */ +namespace std { + +/* + * windows.h defines a 'min' macro that would mangle the function + * name. + */ +#if WTF_COMPILER_MSVC +# undef min +# undef max +#endif + +#define NO_RETURN_DUE_TO_ASSERT + +template +inline T +min(T t1, T t2) +{ + return js::Min(t1, t2); +} + +template +inline T +max(T t1, T t2) +{ + return js::Max(t1, t2); +} + +template +inline void +swap(T &t1, T &t2) +{ + T tmp = t1; + t1 = t2; + t2 = tmp; +} +} /* namespace std */ + +} /* namespace JSC */ + +namespace WTF { + +/* + * Sentinel value used in Yarr. + */ +const size_t notFound = size_t(-1); + +} + +#define JS_EXPORT_PRIVATE + +#endif /* yarr_wtfbridge_h */