LCOV - code coverage report
Current view: directory - js/src/yarr - wtfbridge.h (source / functions) Found Hit Coverage
Test: app.info Lines: 83 83 100.0 %
Date: 2012-06-02 Functions: 128 126 98.4 %

       1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
       2                 :  * vim: set ts=8 sw=4 et tw=99 ft=cpp:
       3                 :  *
       4                 :  * ***** BEGIN LICENSE BLOCK *****
       5                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       6                 :  *
       7                 :  * The contents of this file are subject to the Mozilla Public License Version
       8                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       9                 :  * the License. You may obtain a copy of the License at
      10                 :  * http://www.mozilla.org/MPL/
      11                 :  *
      12                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      13                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      14                 :  * for the specific language governing rights and limitations under the
      15                 :  * License.
      16                 :  *
      17                 :  * The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
      18                 :  * June 12, 2009.
      19                 :  *
      20                 :  * The Initial Developer of the Original Code is
      21                 :  *   the Mozilla Corporation.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *   David Mandelin <dmandelin@mozilla.com>
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      28                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #ifndef jswtfbridge_h__
      41                 : #define jswtfbridge_h__
      42                 : 
      43                 : /*
      44                 :  * WTF compatibility layer. This file provides various type and data
      45                 :  * definitions for use by Yarr.
      46                 :  */
      47                 : 
      48                 : #include "jsstr.h"
      49                 : #include "jsprvtd.h"
      50                 : #include "vm/String.h"
      51                 : #include "assembler/wtf/Platform.h"
      52                 : #if ENABLE_YARR_JIT
      53                 : #include "assembler/jit/ExecutableAllocator.h"
      54                 : #endif
      55                 : 
      56                 : namespace JSC { namespace Yarr {
      57                 : 
      58                 : /*
      59                 :  * Basic type definitions.
      60                 :  */
      61                 : 
      62                 : typedef jschar UChar;
      63                 : typedef JSLinearString UString;
      64                 : 
      65                 : using namespace js::unicode;
      66                 : 
      67                 : class Unicode {
      68                 :   public:
      69           15821 :     static UChar toUpper(UChar c) { return ToUpperCase(c); }
      70           16234 :     static UChar toLower(UChar c) { return ToLowerCase(c); }
      71                 : };
      72                 : 
      73                 : /*
      74                 :  * Do-nothing smart pointer classes. These have a compatible interface
      75                 :  * with the smart pointers used by Yarr, but they don't actually do
      76                 :  * reference counting.
      77                 :  */
      78                 : template<typename T>
      79            4820 : class RefCounted {
      80                 : };
      81                 : 
      82                 : template<typename T>
      83                 : class RefPtr {
      84                 :     T *ptr;
      85                 :   public:
      86           54364 :     RefPtr(T *p) { ptr = p; }
      87           62522 :     operator bool() const { return ptr != NULL; }
      88            6870 :     const T *operator ->() const { return ptr; }
      89           54364 :     T *get() { return ptr; }
      90                 : };
      91                 : 
      92                 : template<typename T>
      93                 : class PassRefPtr {
      94                 :     T *ptr;
      95                 :   public:
      96           54364 :     PassRefPtr(T *p) { ptr = p; }
      97           54364 :     operator T*() { return ptr; }
      98                 : };
      99                 : 
     100                 : template<typename T>
     101                 : class PassOwnPtr {
     102                 :     T *ptr;
     103                 :   public:
     104            8925 :     PassOwnPtr(T *p) { ptr = p; }
     105                 : 
     106            8925 :     T *get() { return ptr; }
     107                 : };
     108                 : 
     109                 : template<typename T>
     110                 : class OwnPtr {
     111                 :     T *ptr;
     112                 :   public:
     113            2975 :     OwnPtr() : ptr(NULL) { }
     114            2975 :     OwnPtr(PassOwnPtr<T> p) : ptr(p.get()) { }
     115                 : 
     116            5950 :     ~OwnPtr() {
     117            5950 :         if (ptr)
     118            2975 :             js::Foreground::delete_(ptr);
     119            5950 :     }
     120                 : 
     121            2975 :     OwnPtr<T> &operator=(PassOwnPtr<T> p) {
     122            2975 :         ptr = p.get();
     123            2975 :         return *this;
     124                 :     }
     125                 : 
     126         1113653 :     T *operator ->() { return ptr; }
     127                 : 
     128          135432 :     T *get() { return ptr; }
     129                 : 
     130            2975 :     T *release() {
     131            2975 :         T *result = ptr;
     132            2975 :         ptr = NULL;
     133            2975 :         return result;
     134                 :     }
     135                 : };
     136                 : 
     137                 : template<typename T>
     138            4820 : PassRefPtr<T> adoptRef(T *p) { return PassRefPtr<T>(p); }
     139                 : 
     140                 : template<typename T>
     141            5950 : PassOwnPtr<T> adoptPtr(T *p) { return PassOwnPtr<T>(p); }
     142                 : 
     143                 : #define WTF_MAKE_FAST_ALLOCATED
     144                 : 
     145                 : template<typename T>
     146                 : class Ref {
     147                 :     T &val;
     148                 :   public:
     149            2975 :     Ref(T &val) : val(val) { }
     150            2975 :     operator T&() const { return val; }
     151                 : };
     152                 : 
     153                 : /*
     154                 :  * Vector class for Yarr. This wraps js::Vector and provides all
     155                 :  * the API method signatures used by Yarr.
     156                 :  */
     157                 : template<typename T, size_t N = 0>
     158         1022912 : class Vector {
     159                 :   public:
     160                 :     js::Vector<T, N, js::SystemAllocPolicy> impl;
     161                 :   public:
     162         1019937 :     Vector() {}
     163                 : 
     164            2975 :     Vector(const Vector &v) {
     165                 :         // XXX yarr-oom
     166            2975 :         (void) append(v);
     167            2975 :     }
     168                 : 
     169        81378154 :     size_t size() const {
     170        81378154 :         return impl.length();
     171                 :     }
     172                 : 
     173       127507256 :     T &operator[](size_t i) {
     174       127507256 :         return impl[i];
     175                 :     }
     176                 : 
     177           80727 :     const T &operator[](size_t i) const {
     178           80727 :         return impl[i];
     179                 :     }
     180                 : 
     181                 :     T &at(size_t i) {
     182                 :         return impl[i];
     183                 :     }
     184                 : 
     185           69430 :     const T *begin() const {
     186           69430 :         return impl.begin();
     187                 :     }
     188                 : 
     189          298683 :     T &last() {
     190          298683 :         return impl.back();
     191                 :     }
     192                 : 
     193          113408 :     bool isEmpty() const {
     194          113408 :         return impl.empty();
     195                 :     }
     196                 : 
     197                 :     template <typename U>
     198         1692205 :     void append(const U &u) {
     199                 :         // XXX yarr-oom
     200         1692205 :         (void) impl.append(static_cast<T>(u));
     201         1692205 :     }
     202                 : 
     203                 :     template <size_t M>
     204          131073 :     void append(const Vector<T,M> &v) {
     205                 :         // XXX yarr-oom
     206          131073 :         (void) impl.append(v.impl);
     207          131073 :     }
     208                 : 
     209           49919 :     void insert(size_t i, const T& t) {
     210                 :         // XXX yarr-oom
     211           49919 :         (void) impl.insert(&impl[i], t);
     212           49919 :     }
     213                 : 
     214            8851 :     void remove(size_t i) {
     215            8851 :         impl.erase(&impl[i]);
     216            8851 :     }
     217                 : 
     218          131845 :     void clear() {
     219          131845 :         return impl.clear();
     220                 :     }
     221                 : 
     222           14813 :     void shrink(size_t newLength) {
     223                 :         // XXX yarr-oom
     224           14813 :         JS_ASSERT(newLength <= impl.length());
     225           14813 :         (void) impl.resize(newLength);
     226           14813 :     }
     227                 : 
     228          236405 :     void deleteAllValues() {
     229          529861 :         for (T *p = impl.begin(); p != impl.end(); ++p)
     230          293456 :             js::Foreground::delete_(*p);
     231          236405 :     }
     232                 : };
     233                 : 
     234                 : template<typename T>
     235                 : class Vector<OwnPtr<T> > {
     236                 :   public:
     237                 :     js::Vector<T *, 0, js::SystemAllocPolicy> impl;
     238                 :   public:
     239                 :     Vector() {}
     240                 : 
     241                 :     size_t size() const {
     242                 :         return impl.length();
     243                 :     }
     244                 : 
     245                 :     void append(T *t) {
     246                 :         // XXX yarr-oom
     247                 :         (void) impl.append(t);
     248                 :     }
     249                 : 
     250                 :     PassOwnPtr<T> operator[](size_t i) {
     251                 :         return PassOwnPtr<T>(impl[i]);
     252                 :     }
     253                 : 
     254                 :     void clear() {
     255                 :         for (T **p = impl.begin(); p != impl.end(); ++p)
     256                 :             js::Foreground::delete_(*p);
     257                 :         return impl.clear();
     258                 :     }
     259                 : };
     260                 : 
     261                 : template <typename T, size_t N>
     262                 : inline void
     263          236405 : deleteAllValues(Vector<T, N> &v) {
     264          236405 :     v.deleteAllValues();
     265          236405 : }
     266                 : 
     267                 : #if ENABLE_YARR_JIT
     268                 : 
     269                 : /*
     270                 :  * Minimal JSGlobalData. This used by Yarr to get the allocator.
     271                 :  */
     272                 : class JSGlobalData {
     273                 :   public:
     274                 :     ExecutableAllocator *regexAllocator;
     275                 : 
     276           58226 :     JSGlobalData(ExecutableAllocator *regexAllocator)
     277           58226 :      : regexAllocator(regexAllocator) { }
     278                 : };
     279                 : 
     280                 : #endif
     281                 : 
     282                 : /*
     283                 :  * Sentinel value used in Yarr.
     284                 :  */
     285                 : const size_t notFound = size_t(-1);
     286                 : 
     287                 :  /*
     288                 :   * Do-nothing version of a macro used by WTF to avoid unused
     289                 :   * parameter warnings.
     290                 :   */
     291                 : #define UNUSED_PARAM(e)
     292                 : 
     293                 : } /* namespace Yarr */
     294                 : 
     295                 : /*
     296                 :  * Replacements for std:: functions used in Yarr. We put them in 
     297                 :  * namespace JSC::std so that they can still be called as std::X
     298                 :  * in Yarr.
     299                 :  */
     300                 : namespace std {
     301                 : 
     302                 : /*
     303                 :  * windows.h defines a 'min' macro that would mangle the function
     304                 :  * name.
     305                 :  */
     306                 : #if WTF_COMPILER_MSVC
     307                 : # undef min
     308                 : # undef max
     309                 : #endif
     310                 : 
     311                 : template<typename T>
     312                 : inline T
     313          184314 : min(T t1, T t2)
     314                 : {
     315          184314 :     return JS_MIN(t1, t2);
     316                 : }
     317                 : 
     318                 : template<typename T>
     319                 : inline T
     320          142711 : max(T t1, T t2)
     321                 : {
     322          142711 :     return JS_MAX(t1, t2);
     323                 : }
     324                 : 
     325                 : template<typename T>
     326                 : inline void
     327           31359 : swap(T &t1, T &t2)
     328                 : {
     329           31359 :     T tmp = t1;
     330           31359 :     t1 = t2;
     331           31359 :     t2 = tmp;
     332           31359 : }
     333                 : } /* namespace std */
     334                 : 
     335                 : } /* namespace JSC */
     336                 : 
     337                 : #endif

Generated by: LCOV version 1.7