Back to index

lightning-sunbird  0.9+nobinonly
jsinterp.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef jsinterp_h___
00041 #define jsinterp_h___
00042 /*
00043  * JS interpreter interface.
00044  */
00045 #include "jsprvtd.h"
00046 #include "jspubtd.h"
00047 
00048 JS_BEGIN_EXTERN_C
00049 
00050 /*
00051  * JS stack frame, may be allocated on the C stack by native callers.  Always
00052  * allocated on cx->stackPool for calls from the interpreter to an interpreted
00053  * function.
00054  *
00055  * NB: This struct is manually initialized in jsinterp.c and jsiter.c.  If you
00056  * add new members, update both files.  But first, try to remove members.  The
00057  * sharp* and xml* members should be moved onto the stack as local variables
00058  * with well-known slots, if possible.
00059  */
00060 struct JSStackFrame {
00061     JSObject        *callobj;       /* lazily created Call object */
00062     JSObject        *argsobj;       /* lazily created arguments object */
00063     JSObject        *varobj;        /* variables object, where vars go */
00064     JSObject        *callee;        /* function or script object */
00065     JSScript        *script;        /* script being interpreted */
00066     JSFunction      *fun;           /* function being called or null */
00067     JSObject        *thisp;         /* "this" pointer if in method */
00068     uintN           argc;           /* actual argument count */
00069     jsval           *argv;          /* base of argument stack slots */
00070     jsval           rval;           /* function return value */
00071     uintN           nvars;          /* local variable count */
00072     jsval           *vars;          /* base of variable stack slots */
00073     JSStackFrame    *down;          /* previous frame */
00074     void            *annotation;    /* used by Java security */
00075     JSObject        *scopeChain;    /* scope chain */
00076     jsbytecode      *pc;            /* program counter */
00077     jsval           *sp;            /* stack pointer */
00078     jsval           *spbase;        /* operand stack base */
00079     uintN           sharpDepth;     /* array/object initializer depth */
00080     JSObject        *sharpArray;    /* scope for #n= initializer vars */
00081     uint32          flags;          /* frame flags -- see below */
00082     JSStackFrame    *dormantNext;   /* next dormant frame chain */
00083     JSObject        *xmlNamespace;  /* null or default xml namespace in E4X */
00084     JSObject        *blockChain;    /* active compile-time block scopes */
00085 };
00086 
00087 typedef struct JSInlineFrame {
00088     JSStackFrame    frame;          /* base struct */
00089     jsval           *rvp;           /* ptr to caller's return value slot */
00090     void            *mark;          /* mark before inline frame */
00091     void            *hookData;      /* debugger call hook data */
00092     JSVersion       callerVersion;  /* dynamic version of calling script */
00093 } JSInlineFrame;
00094 
00095 /* JS stack frame flags. */
00096 #define JSFRAME_CONSTRUCTING  0x01  /* frame is for a constructor invocation */
00097 #define JSFRAME_INTERNAL      0x02  /* internal call, not invoked by a script */
00098 #define JSFRAME_SKIP_CALLER   0x04  /* skip one link when evaluating f.caller
00099                                        for this invocation of f */
00100 #define JSFRAME_ASSIGNING     0x08  /* a complex (not simplex JOF_ASSIGNING) op
00101                                        is currently assigning to a property */
00102 #define JSFRAME_DEBUGGER      0x10  /* frame for JS_EvaluateInStackFrame */
00103 #define JSFRAME_EVAL          0x20  /* frame for obj_eval */
00104 #define JSFRAME_SPECIAL       0x30  /* special evaluation frame flags */
00105 #define JSFRAME_COMPILING     0x40  /* frame is being used by compiler */
00106 #define JSFRAME_COMPILE_N_GO  0x80  /* compiler-and-go mode, can optimize name
00107                                        references based on scope chain */
00108 #define JSFRAME_SCRIPT_OBJECT 0x100 /* compiling source for a Script object */
00109 #define JSFRAME_YIELDING      0x200 /* js_Interpret dispatched JSOP_YIELD */
00110 #define JSFRAME_FILTERING     0x400 /* XML filtering predicate expression */
00111 #define JSFRAME_ITERATOR      0x800 /* trying to get an iterator for for-in */
00112 #define JSFRAME_POP_BLOCKS   0x1000 /* scope chain contains blocks to pop */
00113 #define JSFRAME_GENERATOR    0x2000 /* frame belongs to generator-iterator */
00114 
00115 #define JSFRAME_OVERRIDE_SHIFT 24   /* override bit-set params; see jsfun.c */
00116 #define JSFRAME_OVERRIDE_BITS  8
00117 
00118 /*
00119  * Property cache for quickened get/set property opcodes.
00120  */
00121 #define PROPERTY_CACHE_LOG2     10
00122 #define PROPERTY_CACHE_SIZE     JS_BIT(PROPERTY_CACHE_LOG2)
00123 #define PROPERTY_CACHE_MASK     JS_BITMASK(PROPERTY_CACHE_LOG2)
00124 
00125 #define PROPERTY_CACHE_HASH(obj, id) \
00126     ((((jsuword)(obj) >> JSVAL_TAGBITS) ^ (jsuword)(id)) & PROPERTY_CACHE_MASK)
00127 
00128 #ifdef JS_THREADSAFE
00129 
00130 #if HAVE_ATOMIC_DWORD_ACCESS
00131 
00132 #define PCE_LOAD(cache, pce, entry)     JS_ATOMIC_DWORD_LOAD(pce, entry)
00133 #define PCE_STORE(cache, pce, entry)    JS_ATOMIC_DWORD_STORE(pce, entry)
00134 
00135 #else  /* !HAVE_ATOMIC_DWORD_ACCESS */
00136 
00137 #define JS_PROPERTY_CACHE_METERING      1
00138 
00139 #define PCE_LOAD(cache, pce, entry)                                           \
00140     JS_BEGIN_MACRO                                                            \
00141         uint32 prefills_;                                                     \
00142         uint32 fills_ = (cache)->fills;                                       \
00143         do {                                                                  \
00144             /* Load until cache->fills is stable (see FILL macro below). */   \
00145             prefills_ = fills_;                                               \
00146             (entry) = *(pce);                                                 \
00147         } while ((fills_ = (cache)->fills) != prefills_);                     \
00148     JS_END_MACRO
00149 
00150 #define PCE_STORE(cache, pce, entry)                                          \
00151     JS_BEGIN_MACRO                                                            \
00152         do {                                                                  \
00153             /* Store until no racing collider stores half or all of pce. */   \
00154             *(pce) = (entry);                                                 \
00155         } while (PCE_OBJECT(*pce) != PCE_OBJECT(entry) ||                     \
00156                  PCE_PROPERTY(*pce) != PCE_PROPERTY(entry));                  \
00157     JS_END_MACRO
00158 
00159 #endif /* !HAVE_ATOMIC_DWORD_ACCESS */
00160 
00161 #else  /* !JS_THREADSAFE */
00162 
00163 #define PCE_LOAD(cache, pce, entry)     ((entry) = *(pce))
00164 #define PCE_STORE(cache, pce, entry)    (*(pce) = (entry))
00165 
00166 #endif /* !JS_THREADSAFE */
00167 
00168 typedef union JSPropertyCacheEntry {
00169     struct {
00170         JSObject        *object;        /* weak link to object */
00171         JSScopeProperty *property;      /* weak link to property */
00172     } s;
00173 #ifdef HAVE_ATOMIC_DWORD_ACCESS
00174     prdword align;
00175 #endif
00176 } JSPropertyCacheEntry;
00177 
00178 /* These may be called in lvalue or rvalue position. */
00179 #define PCE_OBJECT(entry)       ((entry).s.object)
00180 #define PCE_PROPERTY(entry)     ((entry).s.property)
00181 
00182 typedef struct JSPropertyCache {
00183     JSPropertyCacheEntry table[PROPERTY_CACHE_SIZE];
00184     JSBool               empty;
00185     JSBool               disabled;
00186 #ifdef JS_PROPERTY_CACHE_METERING
00187     uint32               fills;
00188     uint32               recycles;
00189     uint32               tests;
00190     uint32               misses;
00191     uint32               flushes;
00192 # define PCMETER(x)      x
00193 #else
00194 # define PCMETER(x)      /* nothing */
00195 #endif
00196 } JSPropertyCache;
00197 
00198 #define PROPERTY_CACHE_FILL(cache, obj, id, sprop)                            \
00199     JS_BEGIN_MACRO                                                            \
00200         JSPropertyCache *cache_ = (cache);                                    \
00201         if (!cache_->disabled) {                                              \
00202             uintN hashIndex_ = (uintN) PROPERTY_CACHE_HASH(obj, id);          \
00203             JSPropertyCacheEntry *pce_ = &cache_->table[hashIndex_];          \
00204             JSPropertyCacheEntry entry_;                                      \
00205             JSScopeProperty *pce_sprop_;                                      \
00206             PCE_LOAD(cache_, pce_, entry_);                                   \
00207             pce_sprop_ = PCE_PROPERTY(entry_);                                \
00208             PCMETER(if (pce_sprop_ && pce_sprop_ != sprop)                    \
00209                         cache_->recycles++);                                  \
00210             PCE_OBJECT(entry_) = obj;                                         \
00211             PCE_PROPERTY(entry_) = sprop;                                     \
00212             cache_->empty = JS_FALSE;                                         \
00213             PCMETER(cache_->fills++);                                         \
00214             PCE_STORE(cache_, pce_, entry_);                                  \
00215         }                                                                     \
00216     JS_END_MACRO
00217 
00218 #define PROPERTY_CACHE_TEST(cache, obj, id, sprop)                            \
00219     JS_BEGIN_MACRO                                                            \
00220         uintN hashIndex_ = (uintN) PROPERTY_CACHE_HASH(obj, id);              \
00221         JSPropertyCache *cache_ = (cache);                                    \
00222         JSPropertyCacheEntry *pce_ = &cache_->table[hashIndex_];              \
00223         JSPropertyCacheEntry entry_;                                          \
00224         JSScopeProperty *pce_sprop_;                                          \
00225         PCE_LOAD(cache_, pce_, entry_);                                       \
00226         pce_sprop_ = PCE_PROPERTY(entry_);                                    \
00227         PCMETER(cache_->tests++);                                             \
00228         if (pce_sprop_ &&                                                     \
00229             PCE_OBJECT(entry_) == obj &&                                      \
00230             pce_sprop_->id == id) {                                           \
00231             sprop = pce_sprop_;                                               \
00232         } else {                                                              \
00233             PCMETER(cache_->misses++);                                        \
00234             sprop = NULL;                                                     \
00235         }                                                                     \
00236     JS_END_MACRO
00237 
00238 extern void
00239 js_FlushPropertyCache(JSContext *cx);
00240 
00241 extern void
00242 js_DisablePropertyCache(JSContext *cx);
00243 
00244 extern void
00245 js_EnablePropertyCache(JSContext *cx);
00246 
00247 extern JS_FRIEND_API(jsval *)
00248 js_AllocStack(JSContext *cx, uintN nslots, void **markp);
00249 
00250 extern JS_FRIEND_API(void)
00251 js_FreeStack(JSContext *cx, void *mark);
00252 
00253 extern JSBool
00254 js_GetArgument(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
00255 
00256 extern JSBool
00257 js_SetArgument(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
00258 
00259 extern JSBool
00260 js_GetLocalVariable(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
00261 
00262 extern JSBool
00263 js_SetLocalVariable(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
00264 
00265 #ifdef DUMP_CALL_TABLE
00266 # define JSOPTION_LOGCALL_TOSOURCE JS_BIT(15)
00267 
00268 extern JSHashTable  *js_CallTable;
00269 extern size_t       js_LogCallToSourceLimit;
00270 
00271 extern void         js_DumpCallTable(JSContext *cx);
00272 #endif
00273 
00274 /*
00275  * Refresh and return fp->scopeChain.  It may be stale if block scopes are
00276  * active but not yet reflected by objects in the scope chain.  If a block
00277  * scope contains a with, eval, XML filtering predicate, or similar such
00278  * dynamically scoped construct, then compile-time block scope at fp->blocks
00279  * must reflect at runtime.
00280  */
00281 extern JSObject *
00282 js_GetScopeChain(JSContext *cx, JSStackFrame *fp);
00283 
00284 /*
00285  * Compute the 'this' parameter for a call with nominal 'this' given by thisp
00286  * and arguments including argv[-1] (nominal 'this') and argv[-2] (callee).
00287  * Activation objects ("Call" objects not created with "new Call()", i.e.,
00288  * "Call" objects that have private data) may not be referred to by 'this',
00289  * per ECMA-262, so js_ComputeThis censors them.
00290  */
00291 extern JSObject *
00292 js_ComputeThis(JSContext *cx, JSObject *thisp, jsval *argv);
00293 
00294 /*
00295  * NB: js_Invoke requires that cx is currently running JS (i.e., that cx->fp
00296  * is non-null), and that the callee, |this| parameter, and actual arguments
00297  * are already pushed on the stack under cx->fp->sp.
00298  */
00299 extern JS_FRIEND_API(JSBool)
00300 js_Invoke(JSContext *cx, uintN argc, uintN flags);
00301 
00302 /*
00303  * Consolidated js_Invoke flags simply rename certain JSFRAME_* flags, so that
00304  * we can share bits stored in JSStackFrame.flags and passed to:
00305  *
00306  *   js_Invoke
00307  *   js_InternalInvoke
00308  *   js_ValueToFunction
00309  *   js_ValueToFunctionObject
00310  *   js_ValueToCallableObject
00311  *   js_ReportIsNotFunction
00312  *
00313  * See jsfun.h for the latter four and flag renaming macros.
00314  */
00315 #define JSINVOKE_CONSTRUCT      JSFRAME_CONSTRUCTING
00316 #define JSINVOKE_INTERNAL       JSFRAME_INTERNAL
00317 #define JSINVOKE_SKIP_CALLER    JSFRAME_SKIP_CALLER
00318 #define JSINVOKE_ITERATOR       JSFRAME_ITERATOR
00319 
00320 /*
00321  * Mask to isolate construct and iterator flags for use with jsfun.h functions.
00322  */
00323 #define JSINVOKE_FUNFLAGS       (JSINVOKE_CONSTRUCT | JSINVOKE_ITERATOR)
00324 
00325 /*
00326  * "Internal" calls may come from C or C++ code using a JSContext on which no
00327  * JS is running (!cx->fp), so they may need to push a dummy JSStackFrame.
00328  */
00329 #define js_InternalCall(cx,obj,fval,argc,argv,rval)                           \
00330     js_InternalInvoke(cx, obj, fval, 0, argc, argv, rval)
00331 
00332 #define js_InternalConstruct(cx,obj,fval,argc,argv,rval)                      \
00333     js_InternalInvoke(cx, obj, fval, JSINVOKE_CONSTRUCT, argc, argv, rval)
00334 
00335 extern JSBool
00336 js_InternalInvoke(JSContext *cx, JSObject *obj, jsval fval, uintN flags,
00337                   uintN argc, jsval *argv, jsval *rval);
00338 
00339 extern JSBool
00340 js_InternalGetOrSet(JSContext *cx, JSObject *obj, jsid id, jsval fval,
00341                     JSAccessMode mode, uintN argc, jsval *argv, jsval *rval);
00342 
00343 extern JSBool
00344 js_Execute(JSContext *cx, JSObject *chain, JSScript *script,
00345            JSStackFrame *down, uintN flags, jsval *result);
00346 
00347 extern JSBool
00348 js_CheckRedeclaration(JSContext *cx, JSObject *obj, jsid id, uintN attrs,
00349                       JSObject **objp, JSProperty **propp);
00350 
00351 extern JSBool
00352 js_StrictlyEqual(jsval lval, jsval rval);
00353 
00354 extern JSBool
00355 js_InvokeConstructor(JSContext *cx, jsval *vp, uintN argc);
00356 
00357 extern JSBool
00358 js_Interpret(JSContext *cx, jsbytecode *pc, jsval *result);
00359 
00360 JS_END_EXTERN_C
00361 
00362 #endif /* jsinterp_h___ */