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 };
77 typedef struct JSInlineFrame {
78 JSStackFrame frame; /* base struct */
79 void *mark; /* mark before inline frame */
80 void *hookData; /* debugger call hook data */
81 JSVersion callerVersion; /* dynamic version of calling script */
82 } JSInlineFrame;
84 /* JS stack frame flags. */
85 #define JSFRAME_CONSTRUCTING 0x01 /* frame is for a constructor invocation */
86 #define JSFRAME_INTERNAL 0x02 /* internal call, not invoked by a script */
87 #define JSFRAME_SKIP_CALLER 0x04 /* skip one link when evaluating f.caller
88 for this invocation of f */
89 #define JSFRAME_ASSIGNING 0x08 /* a complex (not simplex JOF_ASSIGNING) op
90 is currently assigning to a property */
91 #define JSFRAME_DEBUGGER 0x10 /* frame for JS_EvaluateInStackFrame */
92 #define JSFRAME_EVAL 0x20 /* frame for obj_eval */
93 #define JSFRAME_SPECIAL 0x30 /* special evaluation frame flags */
94 #define JSFRAME_COMPILING 0x40 /* frame is being used by compiler */
95 #define JSFRAME_COMPILE_N_GO 0x80 /* compiler-and-go mode, can optimize name
96 references based on scope chain */
98 #define JSFRAME_OVERRIDE_SHIFT 24 /* override bit-set params; see jsfun.c */
99 #define JSFRAME_OVERRIDE_BITS 8
101 /*
102 * Property cache for quickened get/set property opcodes.
103 */
104 #define PROPERTY_CACHE_LOG2 10
105 #define PROPERTY_CACHE_SIZE JS_BIT(PROPERTY_CACHE_LOG2)
106 #define PROPERTY_CACHE_MASK JS_BITMASK(PROPERTY_CACHE_LOG2)
108 #define PROPERTY_CACHE_HASH(obj, id) \
109 ((((jsuword)(obj) >> JSVAL_TAGBITS) ^ (jsuword)(id)) & PROPERTY_CACHE_MASK)
111 #ifdef JS_THREADSAFE
113 #if HAVE_ATOMIC_DWORD_ACCESS
115 #define PCE_LOAD(cache, pce, entry) JS_ATOMIC_DWORD_LOAD(pce, entry)
116 #define PCE_STORE(cache, pce, entry) JS_ATOMIC_DWORD_STORE(pce, entry)
118 #else /* !HAVE_ATOMIC_DWORD_ACCESS */
120 #define JS_PROPERTY_CACHE_METERING 1
122 #define PCE_LOAD(cache, pce, entry) \
123 JS_BEGIN_MACRO \
124 uint32 prefills_; \
125 uint32 fills_ = (cache)->fills; \
126 do { \
127 /* Load until cache->fills is stable (see FILL macro below). */ \
128 prefills_ = fills_; \
129 (entry) = *(pce); \
130 } while ((fills_ = (cache)->fills) != prefills_); \
131 JS_END_MACRO
133 #define PCE_STORE(cache, pce, entry) \
134 JS_BEGIN_MACRO \
135 do { \
136 /* Store until no racing collider stores half or all of pce. */ \
137 *(pce) = (entry); \
138 } while (PCE_OBJECT(*pce) != PCE_OBJECT(entry) || \
139 PCE_PROPERTY(*pce) != PCE_PROPERTY(entry)); \
140 JS_END_MACRO
142 #endif /* !HAVE_ATOMIC_DWORD_ACCESS */
144 #else /* !JS_THREADSAFE */
146 #define PCE_LOAD(cache, pce, entry) ((entry) = *(pce))
147 #define PCE_STORE(cache, pce, entry) (*(pce) = (entry))
149 #endif /* !JS_THREADSAFE */
151 typedef union JSPropertyCacheEntry {
152 struct {
153 JSObject *object; /* weak link to object */
154 JSScopeProperty *property; /* weak link to property */
155 } s;
156 #ifdef HAVE_ATOMIC_DWORD_ACCESS
157 prdword align;
158 #endif
159 } JSPropertyCacheEntry;
161 /* These may be called in lvalue or rvalue position. */
162 #define PCE_OBJECT(entry) ((entry).s.object)
163 #define PCE_PROPERTY(entry) ((entry).s.property)
165 typedef struct JSPropertyCache {
166 JSPropertyCacheEntry table[PROPERTY_CACHE_SIZE];
167 JSBool empty;
168 JSBool disabled;
169 #ifdef JS_PROPERTY_CACHE_METERING
170 uint32 fills;
171 uint32 recycles;
172 uint32 tests;
173 uint32 misses;
174 uint32 flushes;
175 # define PCMETER(x) x
176 #else
177 # define PCMETER(x) /* nothing */
178 #endif
179 } JSPropertyCache;
181 #define PROPERTY_CACHE_FILL(cache, obj, id, sprop) \
182 JS_BEGIN_MACRO \
183 JSPropertyCache *cache_ = (cache); \
184 if (!cache_->disabled) { \
185 uintN hashIndex_ = (uintN) PROPERTY_CACHE_HASH(obj, id); \
186 JSPropertyCacheEntry *pce_ = &cache_->table[hashIndex_]; \
187 JSPropertyCacheEntry entry_; \
188 JSScopeProperty *pce_sprop_; \
189 PCE_LOAD(cache_, pce_, entry_); \
190 pce_sprop_ = PCE_PROPERTY(entry_); \
191 PCMETER(if (pce_sprop_ && pce_sprop_ != sprop) \
192 cache_->recycles++); \
193 PCE_OBJECT(entry_) = obj; \
194 PCE_PROPERTY(entry_) = sprop; \
195 cache_->empty = JS_FALSE; \
196 PCMETER(cache_->fills++); \
197 PCE_STORE(cache_, pce_, entry_); \
198 } \
199 JS_END_MACRO
201 #define PROPERTY_CACHE_TEST(cache, obj, id, sprop) \
202 JS_BEGIN_MACRO \
203 uintN hashIndex_ = (uintN) PROPERTY_CACHE_HASH(obj, id); \
204 JSPropertyCache *cache_ = (cache); \
205 JSPropertyCacheEntry *pce_ = &cache_->table[hashIndex_]; \
206 JSPropertyCacheEntry entry_; \
207 JSScopeProperty *pce_sprop_; \
208 PCE_LOAD(cache_, pce_, entry_); \
209 pce_sprop_ = PCE_PROPERTY(entry_); \
210 PCMETER(cache_->tests++); \
211 if (pce_sprop_ && \
212 PCE_OBJECT(entry_) == obj && \
213 pce_sprop_->id == id) { \
214 sprop = pce_sprop_; \
215 } else { \
216 PCMETER(cache_->misses++); \
217 sprop = NULL; \
218 } \
219 JS_END_MACRO
221 extern void
222 js_FlushPropertyCache(JSContext *cx);
224 extern void
225 js_DisablePropertyCache(JSContext *cx);
227 extern void
228 js_EnablePropertyCache(JSContext *cx);
230 extern JS_FRIEND_API(jsval *)
231 js_AllocStack(JSContext *cx, uintN nslots, void **markp);
233 extern JS_FRIEND_API(void)
234 js_FreeStack(JSContext *cx, void *mark);
236 extern JSBool
237 js_GetArgument(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
239 extern JSBool
240 js_SetArgument(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
242 extern JSBool
243 js_GetLocalVariable(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
245 extern JSBool
246 js_SetLocalVariable(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
248 #ifdef DUMP_CALL_TABLE
249 # define JSOPTION_LOGCALL_TOSOURCE JS_BIT(15)
251 extern JSHashTable *js_CallTable;
252 extern size_t js_LogCallToSourceLimit;
254 extern void js_DumpCallTable(JSContext *cx);
255 #endif
257 /*
258 * NB: js_Invoke requires that cx is currently running JS (i.e., that cx->fp
259 * is non-null).
260 */
261 extern JS_FRIEND_API(JSBool)
262 js_Invoke(JSContext *cx, uintN argc, uintN flags);
264 /*
265 * Consolidated js_Invoke flags simply rename the low JSFRAME_* flags.
266 */
267 #define JSINVOKE_CONSTRUCT JSFRAME_CONSTRUCTING
268 #define JSINVOKE_INTERNAL JSFRAME_INTERNAL
269 #define JSINVOKE_SKIP_CALLER JSFRAME_SKIP_CALLER
271 /*
272 * "Internal" calls may come from C or C++ code using a JSContext on which no
273 * JS is running (!cx->fp), so they may need to push a dummy JSStackFrame.
274 */
275 #define js_InternalCall(cx,obj,fval,argc,argv,rval) \
276 js_InternalInvoke(cx, obj, fval, 0, argc, argv, rval)
278 #define js_InternalConstruct(cx,obj,fval,argc,argv,rval) \
279 js_InternalInvoke(cx, obj, fval, JSINVOKE_CONSTRUCT, argc, argv, rval)
281 extern JSBool
282 js_InternalInvoke(JSContext *cx, JSObject *obj, jsval fval, uintN flags,
283 uintN argc, jsval *argv, jsval *rval);
285 extern JSBool
286 js_InternalGetOrSet(JSContext *cx, JSObject *obj, jsid id, jsval fval,
287 JSAccessMode mode, uintN argc, jsval *argv, jsval *rval);
289 extern JSBool
290 js_Execute(JSContext *cx, JSObject *chain, JSScript *script,
291 JSStackFrame *down, uintN flags, jsval *result);
293 extern JSBool
294 js_CheckRedeclaration(JSContext *cx, JSObject *obj, jsid id, uintN attrs,
295 JSObject **objp, JSProperty **propp);
297 extern JSBool
298 js_Interpret(JSContext *cx, jsval *result);
300 JS_END_EXTERN_C
302 #endif /* jsinterp_h___ */