1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
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 ***** */
40 #ifndef jsinterp_h___
41 #define jsinterp_h___
42 /*
43 * JS interpreter interface.
44 */
45 #include "jsprvtd.h"
46 #include "jspubtd.h"
48 JS_BEGIN_EXTERN_C
50 /*
51 * JS stack frame, allocated on the C stack.
52 */
53 struct JSStackFrame {
54 JSObject *callobj; /* lazily created Call object */
55 JSObject *argsobj; /* lazily created arguments object */
56 JSObject *varobj; /* variables object, where vars go */
57 JSScript *script; /* script being interpreted */
58 JSFunction *fun; /* function being called or null */
59 JSObject *thisp; /* "this" pointer if in method */
60 uintN argc; /* actual argument count */
61 jsval *argv; /* base of argument stack slots */
62 jsval rval; /* function return value */
63 uintN nvars; /* local variable count */
64 jsval *vars; /* base of variable stack slots */
65 JSStackFrame *down; /* previous frame */
66 void *annotation; /* used by Java security */
67 JSObject *scopeChain; /* scope chain */
68 jsbytecode *pc; /* program counter */
69 jsval *sp; /* stack pointer */
70 jsval *spbase; /* operand stack base */
71 uintN sharpDepth; /* array/object initializer depth */
72 JSObject *sharpArray; /* scope for #n= initializer vars */
73 uint32 flags; /* frame flags -- see below */
74 JSStackFrame *dormantNext; /* next dormant frame chain */
75 JSAtomMap *objAtomMap; /* object atom map, non-null only if we
76 hit a regexp object literal */
77 };
79 typedef struct JSInlineFrame {
80 JSStackFrame frame; /* base struct */
81 void *mark; /* mark before inline frame */
82 void *hookData; /* debugger call hook data */
83 JSVersion callerVersion; /* dynamic version of calling script */
84 } JSInlineFrame;
86 /* JS stack frame flags. */
87 #define JSFRAME_CONSTRUCTING 0x01 /* frame is for a constructor invocation */
88 #define JSFRAME_INTERNAL 0x02 /* internal call, not invoked by a script */
89 #define JSFRAME_SKIP_CALLER 0x04 /* skip one link when evaluating f.caller
90 for this invocation of f */
91 #define JSFRAME_ASSIGNING 0x08 /* a complex (not simplex JOF_ASSIGNING) op
92 is currently assigning to a property */
93 #define JSFRAME_DEBUGGER 0x10 /* frame for JS_EvaluateInStackFrame */
94 #define JSFRAME_EVAL 0x20 /* frame for obj_eval */
95 #define JSFRAME_SPECIAL 0x30 /* special evaluation frame flags */
97 #define JSFRAME_OVERRIDE_SHIFT 24 /* override bit-set params; see jsfun.c */
98 #define JSFRAME_OVERRIDE_BITS 8
100 /*
101 * Property cache for quickened get/set property opcodes.
102 */
103 #define PROPERTY_CACHE_LOG2 10
104 #define PROPERTY_CACHE_SIZE JS_BIT(PROPERTY_CACHE_LOG2)
105 #define PROPERTY_CACHE_MASK JS_BITMASK(PROPERTY_CACHE_LOG2)
107 #define PROPERTY_CACHE_HASH(obj, id) \
108 ((((jsuword)(obj) >> JSVAL_TAGBITS) ^ (jsuword)(id)) & PROPERTY_CACHE_MASK)
110 #ifdef JS_THREADSAFE
112 #if HAVE_ATOMIC_DWORD_ACCESS
114 #define PCE_LOAD(cache, pce, entry) JS_ATOMIC_DWORD_LOAD(pce, entry)
115 #define PCE_STORE(cache, pce, entry) JS_ATOMIC_DWORD_STORE(pce, entry)
117 #else /* !HAVE_ATOMIC_DWORD_ACCESS */
119 #define JS_PROPERTY_CACHE_METERING 1
121 #define PCE_LOAD(cache, pce, entry) \
122 JS_BEGIN_MACRO \
123 uint32 prefills_; \
124 uint32 fills_ = (cache)->fills; \
125 do { \
126 /* Load until cache->fills is stable (see FILL macro below). */ \
127 prefills_ = fills_; \
128 (entry) = *(pce); \
129 } while ((fills_ = (cache)->fills) != prefills_); \
130 JS_END_MACRO
132 #define PCE_STORE(cache, pce, entry) \
133 JS_BEGIN_MACRO \
134 do { \
135 /* Store until no racing collider stores half or all of pce. */ \
136 *(pce) = (entry); \
137 } while (PCE_OBJECT(*pce) != PCE_OBJECT(entry) || \
138 PCE_PROPERTY(*pce) != PCE_PROPERTY(entry)); \
139 JS_END_MACRO
141 #endif /* !HAVE_ATOMIC_DWORD_ACCESS */
143 #else /* !JS_THREADSAFE */
145 #define PCE_LOAD(cache, pce, entry) ((entry) = *(pce))
146 #define PCE_STORE(cache, pce, entry) (*(pce) = (entry))
148 #endif /* !JS_THREADSAFE */
150 typedef union JSPropertyCacheEntry {
151 struct {
152 JSObject *object; /* weak link to object */
153 JSScopeProperty *property; /* weak link to property */
154 } s;
155 #ifdef HAVE_ATOMIC_DWORD_ACCESS
156 prdword align;
157 #endif
158 } JSPropertyCacheEntry;
160 /* These may be called in lvalue or rvalue position. */
161 #define PCE_OBJECT(entry) ((entry).s.object)
162 #define PCE_PROPERTY(entry) ((entry).s.property)
164 typedef struct JSPropertyCache {
165 JSPropertyCacheEntry table[PROPERTY_CACHE_SIZE];
166 JSBool empty;
167 JSBool disabled;
168 #ifdef JS_PROPERTY_CACHE_METERING
169 uint32 fills;
170 uint32 recycles;
171 uint32 tests;
172 uint32 misses;
173 uint32 flushes;
174 # define PCMETER(x) x
175 #else
176 # define PCMETER(x) /* nothing */
177 #endif
178 } JSPropertyCache;
180 #define PROPERTY_CACHE_FILL(cache, obj, id, sprop) \
181 JS_BEGIN_MACRO \
182 JSPropertyCache *cache_ = (cache); \
183 if (!cache_->disabled) { \
184 uintN hashIndex_ = (uintN) PROPERTY_CACHE_HASH(obj, id); \
185 JSPropertyCacheEntry *pce_ = &cache_->table[hashIndex_]; \
186 JSPropertyCacheEntry entry_; \
187 JSScopeProperty *pce_sprop_; \
188 PCE_LOAD(cache_, pce_, entry_); \
189 pce_sprop_ = PCE_PROPERTY(entry_); \
190 PCMETER(if (pce_sprop_ && pce_sprop_ != sprop) \
191 cache_->recycles++); \
192 PCE_OBJECT(entry_) = obj; \
193 PCE_PROPERTY(entry_) = sprop; \
194 cache_->empty = JS_FALSE; \
195 PCMETER(cache_->fills++); \
196 PCE_STORE(cache_, pce_, entry_); \
197 } \
198 JS_END_MACRO
200 #define PROPERTY_CACHE_TEST(cache, obj, id, sprop) \
201 JS_BEGIN_MACRO \
202 uintN hashIndex_ = (uintN) PROPERTY_CACHE_HASH(obj, id); \
203 JSPropertyCache *cache_ = (cache); \
204 JSPropertyCacheEntry *pce_ = &cache_->table[hashIndex_]; \
205 JSPropertyCacheEntry entry_; \
206 JSScopeProperty *pce_sprop_; \
207 PCE_LOAD(cache_, pce_, entry_); \
208 pce_sprop_ = PCE_PROPERTY(entry_); \
209 PCMETER(cache_->tests++); \
210 if (pce_sprop_ && \
211 PCE_OBJECT(entry_) == obj && \
212 pce_sprop_->id == id) { \
213 sprop = pce_sprop_; \
214 } else { \
215 PCMETER(cache_->misses++); \
216 sprop = NULL; \
217 } \
218 JS_END_MACRO
220 extern void
221 js_FlushPropertyCache(JSContext *cx);
223 extern void
224 js_DisablePropertyCache(JSContext *cx);
226 extern void
227 js_EnablePropertyCache(JSContext *cx);
229 extern JS_FRIEND_API(jsval *)
230 js_AllocStack(JSContext *cx, uintN nslots, void **markp);
232 extern JS_FRIEND_API(void)
233 js_FreeStack(JSContext *cx, void *mark);
235 extern JSBool
236 js_GetArgument(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
238 extern JSBool
239 js_SetArgument(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
241 extern JSBool
242 js_GetLocalVariable(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
244 extern JSBool
245 js_SetLocalVariable(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
247 /*
248 * NB: js_Invoke requires that cx is currently running JS (i.e., that cx->fp
249 * is non-null).
250 */
251 extern JS_FRIEND_API(JSBool)
252 js_Invoke(JSContext *cx, uintN argc, uintN flags);
254 /*
255 * Consolidated js_Invoke flags simply rename the low JSFRAME_* flags.
256 */
257 #define JSINVOKE_CONSTRUCT JSFRAME_CONSTRUCTING
258 #define JSINVOKE_INTERNAL JSFRAME_INTERNAL
259 #define JSINVOKE_SKIP_CALLER JSFRAME_SKIP_CALLER
261 /*
262 * "Internal" calls may come from C or C++ code using a JSContext on which no
263 * JS is running (!cx->fp), so they may need to push a dummy JSStackFrame.
264 */
265 #define js_InternalCall(cx,obj,fval,argc,argv,rval) \
266 js_InternalInvoke(cx, obj, fval, 0, argc, argv, rval)
268 #define js_InternalConstruct(cx,obj,fval,argc,argv,rval) \
269 js_InternalInvoke(cx, obj, fval, JSINVOKE_CONSTRUCT, argc, argv, rval)
271 extern JSBool
272 js_InternalInvoke(JSContext *cx, JSObject *obj, jsval fval, uintN flags,
273 uintN argc, jsval *argv, jsval *rval);
275 extern JSBool
276 js_InternalGetOrSet(JSContext *cx, JSObject *obj, jsid id, jsval fval,
277 JSAccessMode mode, uintN argc, jsval *argv, jsval *rval);
279 extern JSBool
280 js_Execute(JSContext *cx, JSObject *chain, JSScript *script,
281 JSStackFrame *down, uintN flags, jsval *result);
283 extern JSBool
284 js_CheckRedeclaration(JSContext *cx, JSObject *obj, jsid id, uintN attrs,
285 JSBool *foundp);
287 extern JSBool
288 js_Interpret(JSContext *cx, jsval *result);
290 JS_END_EXTERN_C
292 #endif /* jsinterp_h___ */