LCOV - code coverage report
Current view: directory - js/xpconnect/src - dombindings.h (source / functions) Found Hit Coverage
Test: app.info Lines: 27 17 63.0 %
Date: 2012-04-21 Functions: 94 37 39.4 %

       1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
       2                 :  * vim: set ts=4 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.org code, released
      18                 :  * June 24, 2010.
      19                 :  *
      20                 :  * The Initial Developer of the Original Code is
      21                 :  *    The Mozilla Foundation
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *    Andreas Gal <gal@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 dombindings_h
      41                 : #define dombindings_h
      42                 : 
      43                 : #include "jsapi.h"
      44                 : #include "jsproxy.h"
      45                 : #include "xpcpublic.h"
      46                 : #include "nsString.h"
      47                 : 
      48                 : namespace mozilla {
      49                 : namespace dom {
      50                 : namespace binding {
      51                 : 
      52                 : inline nsWrapperCache*
      53               0 : GetWrapperCache(nsWrapperCache *cache)
      54                 : {
      55               0 :     return cache;
      56                 : }
      57                 : 
      58                 : // nsGlobalWindow implements nsWrapperCache, but doesn't always use it. Don't
      59                 : // try to use it without fixing that first.
      60                 : class nsGlobalWindow;
      61                 : inline nsWrapperCache*
      62                 : GetWrapperCache(nsGlobalWindow *not_allowed);
      63                 : 
      64                 : inline nsWrapperCache*
      65               0 : GetWrapperCache(void *p)
      66                 : {
      67               0 :     return nsnull;
      68                 : }
      69                 : 
      70                 : 
      71           39732 : class ProxyHandler : public js::ProxyHandler {
      72                 : protected:
      73           19544 :     ProxyHandler() : js::ProxyHandler(ProxyFamily())
      74                 :     {
      75           19544 :     }
      76                 : 
      77                 : public:
      78                 :     virtual bool isInstanceOf(JSObject *prototype) = 0;
      79                 : };
      80                 : 
      81                 : class NoType;
      82                 : class NoOp {
      83                 : public:
      84                 :     typedef NoType* T;
      85                 :     enum {
      86                 :         hasOp = 0
      87                 :     };
      88                 : };
      89                 : 
      90                 : template<typename Type>
      91                 : class Op {
      92                 : public:
      93                 :     typedef Type T;
      94                 :     enum {
      95                 :         hasOp = 1
      96                 :     };
      97                 : };
      98                 : 
      99                 : template<typename Type>
     100                 : class Getter : public Op<Type>
     101                 : {
     102                 : };
     103                 : 
     104                 : template<typename Type>
     105                 : class Setter : public Op<Type>
     106                 : {
     107                 : };
     108                 : 
     109                 : template<class Getter, class Setter=NoOp>
     110                 : class Ops
     111                 : {
     112                 : public:
     113                 :     typedef Getter G;
     114                 :     typedef Setter S;
     115                 : };
     116                 : 
     117                 : typedef Ops<NoOp, NoOp> NoOps;
     118                 : 
     119                 : template<class ListType, class Base, class IndexOps, class NameOps=NoOps>
     120                 : class DerivedListClass {
     121                 : public:
     122                 :     typedef ListType LT;
     123                 :     typedef Base B;
     124                 :     typedef IndexOps IO;
     125                 :     typedef NameOps NO;
     126                 : };
     127                 : 
     128                 : class NoBase {
     129                 : public:
     130                 :     static JSObject *getPrototype(JSContext *cx, XPCWrappedNativeScope *scope);
     131            4315 :     static bool shouldCacheProtoShape(JSContext *cx, JSObject *proto, bool *shouldCache)
     132                 :     {
     133            4315 :         *shouldCache = true;
     134            4315 :         return true;
     135                 :     }
     136               0 :     static bool resolveNativeName(JSContext *cx, JSObject *proxy, jsid id, JSPropertyDescriptor *desc)
     137                 :     {
     138               0 :         return true;
     139                 :     }
     140               8 :     static bool nativeGet(JSContext *cx, JSObject *proxy, JSObject *proto, jsid id, bool *found,
     141                 :                           JS::Value *vp)
     142                 :     {
     143               8 :         *found = false;
     144               8 :         return true;
     145                 :     }
     146                 : };
     147                 : 
     148                 : template<class ListType, class IndexOps, class NameOps=NoOps>
     149                 : class ListClass : public DerivedListClass<ListType, NoBase, IndexOps, NameOps> {
     150                 : };
     151                 : 
     152                 : template<class LC>
     153           59276 : class ListBase : public ProxyHandler {
     154                 : protected:
     155                 :     typedef typename LC::LT ListType;
     156                 :     typedef typename LC::B Base;
     157                 :     typedef typename LC::IO::G::T IndexGetterType;
     158                 :     typedef typename LC::IO::S::T IndexSetterType;
     159                 :     typedef typename LC::NO::G::T NameGetterType;
     160                 :     typedef typename LC::NO::S::T NameSetterType;
     161                 :     enum {
     162                 :         hasIndexGetter = LC::IO::G::hasOp,
     163                 :         hasIndexSetter = LC::IO::S::hasOp,
     164                 :         hasNameGetter = LC::NO::G::hasOp,
     165                 :         hasNameSetter = LC::NO::S::hasOp
     166                 :     };
     167                 : 
     168                 : private:
     169                 :     friend void Register(nsDOMClassInfoData *aData);
     170                 : 
     171                 :     static ListBase<LC> instance;
     172                 : 
     173                 :     static js::Class sInterfaceClass;
     174                 : 
     175                 :     struct Properties {
     176                 :         jsid &id;
     177                 :         JSPropertyOp getter;
     178                 :         JSStrictPropertyOp setter;
     179                 :     };
     180                 :     struct Methods {
     181                 :         jsid &id;
     182                 :         JSNative native;
     183                 :         unsigned nargs;
     184                 :     };
     185                 : 
     186                 :     static Properties sProtoProperties[];
     187                 :     static size_t sProtoPropertiesCount;
     188                 :     static Methods sProtoMethods[];
     189                 :     static size_t sProtoMethodsCount;
     190                 : 
     191                 :     static JSObject *ensureExpandoObject(JSContext *cx, JSObject *obj);
     192                 : 
     193                 :     static js::Shape *getProtoShape(JSObject *obj);
     194                 :     static void setProtoShape(JSObject *obj, js::Shape *shape);
     195                 : 
     196                 :     static JSBool length_getter(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
     197                 : 
     198                 :     static inline bool getItemAt(ListType *list, uint32_t i, IndexGetterType &item);
     199                 :     static inline bool setItemAt(JSContext *cx, ListType *list, uint32_t i, IndexSetterType item);
     200                 : 
     201                 :     static inline bool namedItem(JSContext *cx, JSObject *obj, jsval *name, NameGetterType &result,
     202                 :                                  bool *hasResult);
     203                 : 
     204                 :     static inline bool getNamedItem(ListType *list, const nsAString& aName, NameGetterType &item);
     205                 :     static inline bool setNamedItem(JSContext *cx, ListType *list, const nsAString& aName,
     206                 :                                     NameSetterType item);
     207                 : 
     208                 :     static bool getPropertyOnPrototype(JSContext *cx, JSObject *proxy, jsid id, bool *found,
     209                 :                                        JS::Value *vp);
     210                 :     static bool hasPropertyOnPrototype(JSContext *cx, JSObject *proxy, jsid id);
     211                 : 
     212                 : public:
     213                 :     static JSObject *create(JSContext *cx, XPCWrappedNativeScope *scope, ListType *list,
     214                 :                             nsWrapperCache* cache, bool *triedToWrap);
     215                 : 
     216            4321 :     static JSObject *getPrototype(JSContext *cx, XPCWrappedNativeScope *scope, bool *enabled)
     217                 :     {
     218            4321 :         *enabled = true;
     219            4321 :         return getPrototype(cx, scope);
     220                 :     }
     221                 : 
     222                 :     bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
     223                 :                                JSPropertyDescriptor *desc);
     224                 :     bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
     225                 :                                   JSPropertyDescriptor *desc);
     226                 :     bool defineProperty(JSContext *cx, JSObject *proxy, jsid id,
     227                 :                         JSPropertyDescriptor *desc);
     228                 :     bool getOwnPropertyNames(JSContext *cx, JSObject *proxy, JS::AutoIdVector &props);
     229                 :     bool delete_(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
     230                 :     bool enumerate(JSContext *cx, JSObject *proxy, JS::AutoIdVector &props);
     231                 :     bool fix(JSContext *cx, JSObject *proxy, JS::Value *vp);
     232                 : 
     233                 :     bool has(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
     234                 :     bool hasOwn(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
     235                 :     bool get(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, JS::Value *vp);
     236                 :     bool getElementIfPresent(JSContext *cx, JSObject *proxy, JSObject *receiver,
     237                 :                              uint32_t index, JS::Value *vp, bool *present);
     238                 :     bool set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, bool strict,
     239                 :              JS::Value *vp);
     240                 :     bool keys(JSContext *cx, JSObject *proxy, JS::AutoIdVector &props);
     241                 :     bool iterate(JSContext *cx, JSObject *proxy, unsigned flags, JS::Value *vp);
     242                 : 
     243                 :     /* Spidermonkey extensions. */
     244                 :     bool hasInstance(JSContext *cx, JSObject *proxy, const JS::Value *vp, bool *bp);
     245                 :     JSString *obj_toString(JSContext *cx, JSObject *proxy);
     246                 :     void finalize(JSContext *cx, JSObject *proxy);
     247                 : 
     248           55072 :     static bool proxyHandlerIsList(js::ProxyHandler *handler) {
     249           55072 :         return handler == &instance;
     250                 :     }
     251           28104 :     static bool objIsList(JSObject *obj) {
     252           28104 :         return js::IsProxy(obj) && proxyHandlerIsList(js::GetProxyHandler(obj));
     253                 :     }
     254                 :     static inline bool instanceIsListObject(JSContext *cx, JSObject *obj, JSObject *callee);
     255               0 :     virtual bool isInstanceOf(JSObject *prototype)
     256                 :     {
     257               0 :         return js::GetObjectClass(prototype) == &sInterfaceClass;
     258                 :     }
     259                 :     static inline ListType *getListObject(JSObject *obj);
     260                 : 
     261                 :     static JSObject *getPrototype(JSContext *cx, XPCWrappedNativeScope *scope);
     262                 :     static inline bool protoIsClean(JSContext *cx, JSObject *proto, bool *isClean);
     263                 :     static bool shouldCacheProtoShape(JSContext *cx, JSObject *proto, bool *shouldCache);
     264                 :     static bool resolveNativeName(JSContext *cx, JSObject *proxy, jsid id,
     265                 :                                   JSPropertyDescriptor *desc);
     266                 :     static bool nativeGet(JSContext *cx, JSObject *proxy, JSObject *proto, jsid id, bool *found,
     267                 :                           JS::Value *vp);
     268                 :     static ListType *getNative(JSObject *proxy);
     269                 : };
     270                 : 
     271                 : struct nsISupportsResult
     272                 : {
     273               0 :     nsISupportsResult()
     274                 :     {
     275               0 :     }
     276                 :     nsISupports *mResult;
     277                 :     nsWrapperCache *mCache;
     278                 : };
     279                 : 
     280                 : }
     281                 : }
     282                 : }
     283                 : 
     284                 : #include "dombindings_gen.h"
     285                 : 
     286                 : #endif /* dombindings_h */

Generated by: LCOV version 1.7