LCOV - app.info - js/src/jscntxtinlines.h

LCOV - code coverage report
| Current view: | directory - js/src - jscntxtinlines.h (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 191 | 170 | 89.0 %
| Date: | 2012-04-07 | Functions: | 64 | 58 | 90.6 %

           1                 : /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
           2                 :  * vim: set ts=4 sw=4 et tw=78:
           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 SpiderMonkey code.
          18                 :  *
          19                 :  * The Initial Developer of the Original Code is
          20                 :  * Mozilla Corporation.
          21                 :  * Portions created by the Initial Developer are Copyright (C) 2010
          22                 :  * the Initial Developer. All Rights Reserved.
          23                 :  *
          24                 :  * Contributor(s):
          25                 :  *   Jeff Walden <jwalden+code@mit.edu> (original author)
          26                 :  *
          27                 :  * Alternatively, the contents of this file may be used under the terms of
          28                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
          29                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          30                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          31                 :  * of those above. If you wish to allow use of your version of this file only
          32                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          33                 :  * use your version of this file under the terms of the MPL, indicate your
          34                 :  * decision by deleting the provisions above and replace them with the notice
          35                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          36                 :  * the provisions above, a recipient may use your version of this file under
          37                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          38                 :  *
          39                 :  * ***** END LICENSE BLOCK ***** */
          40                 :
          41                 : #ifndef jscntxtinlines_h___
          42                 : #define jscntxtinlines_h___
          43                 :
          44                 : #include "jscntxt.h"
          45                 : #include "jscompartment.h"
          46                 : #include "jsfriendapi.h"
          47                 : #include "jsinterp.h"
          48                 : #include "jsxml.h"
          49                 : #include "jsgc.h"
          50                 :
          51                 : #include "frontend/ParseMaps.h"
          52                 : #include "vm/RegExpObject.h"
          53                 :
          54                 : namespace js {
          55                 :
          56                 : struct PreserveRegsGuard
          57                 : {
          58        11712926 :     PreserveRegsGuard(JSContext *cx, FrameRegs &regs)
          59        11712926 :       : prevContextRegs(cx->maybeRegs()), cx(cx), regs_(regs) {
          60        11712926 :         cx->stack.repointRegs(&regs_);
          61        11712926 :     }
          62        11712926 :     ~PreserveRegsGuard() {
          63        11712926 :         JS_ASSERT(cx->maybeRegs() == &regs_);
          64        11712926 :         *prevContextRegs = regs_;
          65        11712926 :         cx->stack.repointRegs(prevContextRegs);
          66        11712926 :     }
          67                 :
          68                 :     FrameRegs *prevContextRegs;
          69                 :
          70                 :   private:
          71                 :     JSContext *cx;
          72                 :     FrameRegs &regs_;
          73                 : };
          74                 :
          75                 : static inline GlobalObject *
          76       913812696 : GetGlobalForScopeChain(JSContext *cx)
          77                 : {
          78       913812696 :     if (cx->hasfp())
          79       913809091 :         return &cx->fp()->scopeChain().global();
          80                 :
          81            3605 :     JSObject *scope = JS_ObjectToInnerObject(cx, cx->globalObject);
          82            3605 :     if (!scope)
          83               0 :         return NULL;
          84            3605 :     return &scope->asGlobal();
          85                 : }
          86                 :
          87                 : inline GSNCache *
          88        38338044 : GetGSNCache(JSContext *cx)
          89                 : {
          90        38338044 :     return &cx->runtime->gsnCache;
          91                 : }
          92                 :
          93                 : class AutoNamespaceArray : protected AutoGCRooter {
          94                 :   public:
          95            1837 :     AutoNamespaceArray(JSContext *cx)
          96            1837 :         : AutoGCRooter(cx, NAMESPACES), context(cx) {
          97            1837 :         array.init();
          98            1837 :     }
          99                 :
         100            3674 :     ~AutoNamespaceArray() {
         101            1837 :         array.finish(context->runtime->defaultFreeOp());
         102            1837 :     }
         103                 :
         104             234 :     uint32_t length() const { return array.length; }
         105                 :
         106                 :   private:
         107                 :     JSContext *context;
         108                 :     friend void AutoGCRooter::trace(JSTracer *trc);
         109                 :
         110                 :   public:
         111                 :     JSXMLArray<JSObject> array;
         112                 : };
         113                 :
         114                 : template <typename T>
         115                 : class AutoPtr
         116                 : {
         117                 :     JSContext *cx;
         118                 :     T *value;
         119                 :
         120                 :     AutoPtr(const AutoPtr &other) MOZ_DELETE;
         121                 :
         122                 :   public:
         123                 :     explicit AutoPtr(JSContext *cx) : cx(cx), value(NULL) {}
         124                 :     ~AutoPtr() {
         125                 :         cx->delete_<T>(value);
         126                 :     }
         127                 :
         128                 :     void operator=(T *ptr) { value = ptr; }
         129                 :
         130                 :     typedef void ***** ConvertibleToBool;
         131                 :     operator ConvertibleToBool() const { return (ConvertibleToBool) value; }
         132                 :
         133                 :     const T *operator->() const { return value; }
         134                 :     T *operator->() { return value; }
         135                 :
         136                 :     T *get() { return value; }
         137                 : };
         138                 :
         139                 : #ifdef DEBUG
         140                 : class CompartmentChecker
         141                 : {
         142                 :     JSContext *context;
         143                 :     JSCompartment *compartment;
         144                 :
         145                 :   public:
         146       913517956 :     explicit CompartmentChecker(JSContext *cx) : context(cx), compartment(cx->compartment) {
         147       913517956 :         check(cx->hasfp() ? JS_GetGlobalForScopeChain(cx) : cx->globalObject);
         148       913517956 :     }
         149                 :
         150                 :     /*
         151                 :      * Set a breakpoint here (break js::CompartmentChecker::fail) to debug
         152                 :      * compartment mismatches.
         153                 :      */
         154               0 :     static void fail(JSCompartment *c1, JSCompartment *c2) {
         155               0 :         printf("*** Compartment mismatch %p vs. %p\n", (void *) c1, (void *) c2);
         156               0 :         JS_NOT_REACHED("compartment mismatched");
         157                 :     }
         158                 :
         159                 :     /* Note: should only be used when neither c1 nor c2 may be the default compartment. */
         160                 :     static void check(JSCompartment *c1, JSCompartment *c2) {
         161                 :         JS_ASSERT(c1 != c1->rt->atomsCompartment);
         162                 :         JS_ASSERT(c2 != c2->rt->atomsCompartment);
         163                 :         if (c1 != c2)
         164                 :             fail(c1, c2);
         165                 :     }
         166                 :
         167      1144240721 :     void check(JSCompartment *c) {
         168      1144240721 :         if (c && c != context->runtime->atomsCompartment) {
         169      1144240721 :             if (!compartment)
         170               0 :                 compartment = c;
         171      1144240721 :             else if (c != compartment)
         172               0 :                 fail(compartment, c);
         173                 :         }
         174      1144240721 :     }
         175                 :
         176           37374 :     void check(JSPrincipals *) { /* nothing for now */ }
         177                 :
         178      1168033735 :     void check(JSObject *obj) {
         179      1168033735 :         if (obj)
         180      1134435017 :             check(obj->compartment());
         181      1168033735 :     }
         182                 :
         183        26049980 :     void check(JSString *str) {
         184        26049980 :         if (!str->isAtom())
         185         9677006 :             check(str->compartment());
         186        26049980 :     }
         187                 :
         188       941598552 :     void check(const js::Value &v) {
         189       941598552 :         if (v.isObject())
         190       185616870 :             check(&v.toObject());
         191       755981682 :         else if (v.isString())
         192        26048288 :             check(v.toString());
         193       941598552 :     }
         194                 :
         195                 :     void check(const ValueArray &arr) {
         196                 :         for (size_t i = 0; i < arr.length; i++)
         197                 :             check(arr.array[i]);
         198                 :     }
         199                 :
         200           35322 :     void check(const JSValueArray &arr) {
         201           53956 :         for (size_t i = 0; i < arr.length; i++)
         202           18634 :             check(arr.array[i]);
         203           35322 :     }
         204                 :
         205        14152197 :     void check(const CallArgs &args) {
         206        59755849 :         for (Value *p = args.base(); p != args.end(); ++p)
         207        45603652 :             check(*p);
         208        14152197 :     }
         209                 :
         210        21955465 :     void check(jsid id) {
         211        21955465 :         if (JSID_IS_OBJECT(id))
         212              27 :             check(JSID_TO_OBJECT(id));
         213        21955465 :     }
         214                 :
         215               0 :     void check(JSIdArray *ida) {
         216               0 :         if (ida) {
         217               0 :             for (int i = 0; i < ida->length; i++) {
         218               0 :                 if (JSID_IS_OBJECT(ida->vector[i]))
         219               0 :                     check(ida->vector[i]);
         220                 :             }
         221                 :         }
         222               0 :     }
         223                 :
         224            1308 :     void check(JSScript *script) {
         225            1308 :         if (script) {
         226            1308 :             check(script->compartment());
         227            1308 :             if (!script->isCachedEval && script->globalObject)
         228             660 :                 check(script->globalObject);
         229                 :         }
         230            1308 :     }
         231                 :
         232            4239 :     void check(StackFrame *fp) {
         233            4239 :         if (fp)
         234            4239 :             check(&fp->scopeChain());
         235            4239 :     }
         236                 : };
         237                 :
         238                 : #endif
         239                 :
         240                 : /*
         241                 :  * Don't perform these checks when called from a finalizer. The checking
         242                 :  * depends on other objects not having been swept yet.
         243                 :  */
         244                 : #define START_ASSERT_SAME_COMPARTMENT()                                       \
         245                 :     if (cx->runtime->gcRunning)                                               \
         246                 :         return;                                                               \
         247                 :     CompartmentChecker c(cx)
         248                 :
         249                 : template <class T1> inline void
         250       881318334 : assertSameCompartment(JSContext *cx, T1 t1)
         251                 : {
         252                 : #ifdef DEBUG
         253       881318334 :     START_ASSERT_SAME_COMPARTMENT();
         254       881318334 :     c.check(t1);
         255                 : #endif
         256                 : }
         257                 :
         258                 : template <class T1, class T2> inline void
         259        10942018 : assertSameCompartment(JSContext *cx, T1 t1, T2 t2)
         260                 : {
         261                 : #ifdef DEBUG
         262        10942018 :     START_ASSERT_SAME_COMPARTMENT();
         263        10942018 :     c.check(t1);
         264        10942018 :     c.check(t2);
         265                 : #endif
         266                 : }
         267                 :
         268                 : template <class T1, class T2, class T3> inline void
         269         4152577 : assertSameCompartment(JSContext *cx, T1 t1, T2 t2, T3 t3)
         270                 : {
         271                 : #ifdef DEBUG
         272         4152577 :     START_ASSERT_SAME_COMPARTMENT();
         273         4152577 :     c.check(t1);
         274         4152577 :     c.check(t2);
         275         4152577 :     c.check(t3);
         276                 : #endif
         277                 : }
         278                 :
         279                 : template <class T1, class T2, class T3, class T4> inline void
         280                 : assertSameCompartment(JSContext *cx, T1 t1, T2 t2, T3 t3, T4 t4)
         281                 : {
         282                 : #ifdef DEBUG
         283                 :     START_ASSERT_SAME_COMPARTMENT();
         284                 :     c.check(t1);
         285                 :     c.check(t2);
         286                 :     c.check(t3);
         287                 :     c.check(t4);
         288                 : #endif
         289                 : }
         290                 :
         291                 : template <class T1, class T2, class T3, class T4, class T5> inline void
         292        17105027 : assertSameCompartment(JSContext *cx, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
         293                 : {
         294                 : #ifdef DEBUG
         295        17105027 :     START_ASSERT_SAME_COMPARTMENT();
         296        17105027 :     c.check(t1);
         297        17105027 :     c.check(t2);
         298        17105027 :     c.check(t3);
         299        17105027 :     c.check(t4);
         300        17105027 :     c.check(t5);
         301                 : #endif
         302                 : }
         303                 :
         304                 : #undef START_ASSERT_SAME_COMPARTMENT
         305                 :
         306                 : STATIC_PRECONDITION_ASSUME(ubound(args.argv_) >= argc)
         307                 : JS_ALWAYS_INLINE bool
         308        14152197 : CallJSNative(JSContext *cx, Native native, const CallArgs &args)
         309                 : {
         310                 : #ifdef DEBUG
         311        14152197 :     JSBool alreadyThrowing = cx->isExceptionPending();
         312                 : #endif
         313        14152197 :     assertSameCompartment(cx, args);
         314        14152197 :     bool ok = native(cx, args.length(), args.base());
         315        14152197 :     if (ok) {
         316        14113346 :         assertSameCompartment(cx, args.rval());
         317        14113346 :         JS_ASSERT_IF(!alreadyThrowing, !cx->isExceptionPending());
         318                 :     }
         319        14152197 :     return ok;
         320                 : }
         321                 :
         322                 : extern JSBool CallOrConstructBoundFunction(JSContext *, unsigned, js::Value *);
         323                 :
         324                 : STATIC_PRECONDITION(ubound(args.argv_) >= argc)
         325                 : JS_ALWAYS_INLINE bool
         326          214868 : CallJSNativeConstructor(JSContext *cx, Native native, const CallArgs &args)
         327                 : {
         328                 : #ifdef DEBUG
         329          214868 :     JSObject &callee = args.callee();
         330                 : #endif
         331                 :
         332          214868 :     JS_ASSERT(args.thisv().isMagic());
         333          214868 :     if (!CallJSNative(cx, native, args))
         334             435 :         return false;
         335                 :
         336                 :     /*
         337                 :      * Native constructors must return non-primitive values on success.
         338                 :      * Although it is legal, if a constructor returns the callee, there is a
         339                 :      * 99.9999% chance it is a bug. If any valid code actually wants the
         340                 :      * constructor to return the callee, the assertion can be removed or
         341                 :      * (another) conjunct can be added to the antecedent.
         342                 :      *
         343                 :      * Proxies are exceptions to both rules: they can return primitives and
         344                 :      * they allow content to return the callee.
         345                 :      *
         346                 :      * CallOrConstructBoundFunction is an exception as well because we
         347                 :      * might have used bind on a proxy function.
         348                 :      *
         349                 :      * (new Object(Object)) returns the callee.
         350                 :      */
         351          850365 :     JS_ASSERT_IF(native != FunctionProxyClass.construct &&
         352                 :                  native != CallableObjectClass.construct &&
         353                 :                  native != js::CallOrConstructBoundFunction &&
         354                 :                  (!callee.isFunction() || callee.toFunction()->native() != js_Object),
         355         1064798 :                  !args.rval().isPrimitive() && callee != args.rval().toObject());
         356                 :
         357          214433 :     return true;
         358                 : }
         359                 :
         360                 : JS_ALWAYS_INLINE bool
         361         4130188 : CallJSPropertyOp(JSContext *cx, PropertyOp op, JSObject *receiver, jsid id, Value *vp)
         362                 : {
         363         4130188 :     assertSameCompartment(cx, receiver, id, *vp);
         364         4130188 :     JSBool ok = op(cx, receiver, id, vp);
         365         4130188 :     if (ok)
         366         4130188 :         assertSameCompartment(cx, receiver, *vp);
         367         4130188 :     return ok;
         368                 : }
         369                 :
         370                 : JS_ALWAYS_INLINE bool
         371           22383 : CallJSPropertyOpSetter(JSContext *cx, StrictPropertyOp op, JSObject *obj, jsid id,
         372                 :                        JSBool strict, Value *vp)
         373                 : {
         374           22383 :     assertSameCompartment(cx, obj, id, *vp);
         375           22383 :     return op(cx, obj, id, strict, vp);
         376                 : }
         377                 :
         378                 : inline bool
         379               0 : CallSetter(JSContext *cx, JSObject *obj, jsid id, StrictPropertyOp op, unsigned attrs,
         380                 :            unsigned shortid, JSBool strict, Value *vp)
         381                 : {
         382               0 :     if (attrs & JSPROP_SETTER)
         383               0 :         return InvokeGetterOrSetter(cx, obj, CastAsObjectJsval(op), 1, vp, vp);
         384                 :
         385               0 :     if (attrs & JSPROP_GETTER)
         386               0 :         return js_ReportGetterOnlyAssignment(cx);
         387                 :
         388               0 :     if (attrs & JSPROP_SHORTID)
         389               0 :         id = INT_TO_JSID(shortid);
         390               0 :     return CallJSPropertyOpSetter(cx, op, obj, id, strict, vp);
         391                 : }
         392                 :
         393                 : static inline JSAtom **
         394        19428155 : FrameAtomBase(JSContext *cx, js::StackFrame *fp)
         395                 : {
         396        19428155 :     return fp->script()->atoms;
         397                 : }
         398                 :
         399                 : }  /* namespace js */
         400                 :
         401                 : inline JSVersion
         402          558778 : JSContext::findVersion() const
         403                 : {
         404          558778 :     if (hasVersionOverride)
         405             139 :         return versionOverride;
         406                 :
         407          558639 :     if (stack.hasfp()) {
         408                 :         /* There may be a scripted function somewhere on the stack! */
         409          140508 :         js::StackFrame *f = fp();
         410          295553 :         while (f && !f->isScriptFrame())
         411           14537 :             f = f->prev();
         412          140508 :         if (f)
         413          140499 :             return f->script()->getVersion();
         414                 :     }
         415                 :
         416          418140 :     return defaultVersion;
         417                 : }
         418                 :
         419                 : inline bool
         420           18882 : JSContext::canSetDefaultVersion() const
         421                 : {
         422           18882 :     return !stack.hasfp() && !hasVersionOverride;
         423                 : }
         424                 :
         425                 : inline void
         426              60 : JSContext::overrideVersion(JSVersion newVersion)
         427                 : {
         428              60 :     JS_ASSERT(!canSetDefaultVersion());
         429              60 :     versionOverride = newVersion;
         430              60 :     hasVersionOverride = true;
         431              60 : }
         432                 :
         433                 : inline bool
         434           18822 : JSContext::maybeOverrideVersion(JSVersion newVersion)
         435                 : {
         436           18822 :     if (canSetDefaultVersion()) {
         437           18763 :         setDefaultVersion(newVersion);
         438           18763 :         return false;
         439                 :     }
         440              59 :     overrideVersion(newVersion);
         441              59 :     return true;
         442                 : }
         443                 :
         444                 : inline unsigned
         445          329347 : JSContext::getCompileOptions() const { return js::VersionFlagsToOptions(findVersion()); }
         446                 :
         447                 : inline unsigned
         448          181649 : JSContext::allOptions() const { return getRunOptions() | getCompileOptions(); }
         449                 :
         450                 : inline void
         451          109968 : JSContext::setCompileOptions(unsigned newcopts)
         452                 : {
         453          109968 :     JS_ASSERT((newcopts & JSCOMPILEOPTION_MASK) == newcopts);
         454          109968 :     if (JS_LIKELY(getCompileOptions() == newcopts))
         455          109965 :         return;
         456               3 :     JSVersion version = findVersion();
         457               3 :     JSVersion newVersion = js::OptionFlagsToVersion(newcopts, version);
         458               3 :     maybeOverrideVersion(newVersion);
         459                 : }
         460                 :
         461                 : inline void
         462                 : JSContext::assertValidStackDepth(unsigned depth)
         463                 : {
         464                 : #ifdef DEBUG
         465                 :     JS_ASSERT(0 <= regs().sp - fp()->base());
         466                 :     JS_ASSERT(depth <= uintptr_t(regs().sp - fp()->base()));
         467                 : #endif
         468                 : }
         469                 :
         470                 : #ifdef JS_METHODJIT
         471             202 : inline js::mjit::JaegerCompartment *JSContext::jaegerCompartment()
         472                 : {
         473             202 :     return compartment->jaegerCompartment();
         474                 : }
         475                 : #endif
         476                 :
         477                 : inline js::LifoAlloc &
         478         8657327 : JSContext::typeLifoAlloc()
         479                 : {
         480         8657327 :     return compartment->typeLifoAlloc;
         481                 : }
         482                 :
         483                 : inline bool
         484           19103 : JSContext::ensureGeneratorStackSpace()
         485                 : {
         486           19103 :     bool ok = genStack.reserve(genStack.length() + 1);
         487           19103 :     if (!ok)
         488               0 :         js_ReportOutOfMemory(this);
         489           19103 :     return ok;
         490                 : }
         491                 :
         492                 : inline void
         493           23637 : JSContext::setPendingException(js::Value v) {
         494           23637 :     this->throwing = true;
         495           23637 :     this->exception = v;
         496           23637 :     js::assertSameCompartment(this, v);
         497           23637 : }
         498                 :
         499                 : inline bool
         500          109194 : JSContext::ensureParseMapPool()
         501                 : {
         502          109194 :     if (parseMapPool_)
         503           89873 :         return true;
         504           19321 :     parseMapPool_ = js::OffTheBooks::new_<js::ParseMapPool>(this);
         505           19321 :     return parseMapPool_;
         506                 : }
         507                 :
         508                 : /* Get the current frame, first lazily instantiating stack frames if needed. */
         509                 : static inline js::StackFrame *
         510            4294 : js_GetTopStackFrame(JSContext *cx, FrameExpandKind expand)
         511                 : {
         512                 : #ifdef JS_METHODJIT
         513            4294 :     if (expand)
         514            2384 :         js::mjit::ExpandInlineFrames(cx->compartment);
         515                 : #endif
         516                 :
         517            4294 :     return cx->maybefp();
         518                 : }
         519                 :
         520                 : #endif /* jscntxtinlines_h___ */

Generated by: LCOV version 1.7