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 JSObject *xmlNamespace; /* null or default xml namespace in E4X */
76 };
78 typedef struct JSInlineFrame {
79 JSStackFrame frame; /* base struct */
80 void *mark; /* mark before inline frame */
81 void *hookData; /* debugger call hook data */
82 JSVersion callerVersion; /* dynamic version of calling script */
83 } JSInlineFrame;
85 /* JS stack frame flags. */
86 #define JSFRAME_CONSTRUCTING 0x01 /* frame is for a constructor invocation */
87 #define JSFRAME_INTERNAL 0x02 /* internal call, not invoked by a script */
88 #define JSFRAME_SKIP_CALLER 0x04 /* skip one link when evaluating f.caller
89 for this invocation of f */
90 #define JSFRAME_ASSIGNING 0x08 /* a complex (not simplex JOF_ASSIGNING) op
91 is currently assigning to a property */
92 #define JSFRAME_DEBUGGER 0x10 /* frame for JS_EvaluateInStackFrame */
93 #define JSFRAME_EVAL 0x20 /* frame for obj_eval */
94 #define JSFRAME_SPECIAL 0x30 /* special evaluation frame flags */
95 #define JSFRAME_COMPILING 0x40 /* frame is being used by compiler */
96 #define JSFRAME_COMPILE_N_GO 0x80 /* compiler-and-go mode, can optimize name
97 references based on scope chain */
98 #define JSFRAME_SCRIPT_OBJECT 0x100 /* compiling source for a Script object */
100 #define JSFRAME_OVERRIDE_SHIFT 24 /* override bit-set params; see jsfun.c */
101 #define JSFRAME_OVERRIDE_BITS 8
103 /*
104 * Property cache for quickened get/set property opcodes.
105 */
106 #define PROPERTY_CACHE_LOG2 10
107 #define PROPERTY_CACHE_SIZE JS_BIT(PROPERTY_CACHE_LOG2)
108 #define PROPERTY_CACHE_MASK JS_BITMASK(PROPERTY_CACHE_LOG2)
110 #define PROPERTY_CACHE_HASH(obj, id) \
111 ((((jsuword)(obj) >> JSVAL_TAGBITS) ^ (jsuword)(id)) & PROPERTY_CACHE_MASK)
113 #ifdef JS_THREADSAFE
115 #if HAVE_ATOMIC_DWORD_ACCESS
117 #define PCE_LOAD(cache, pce, entry) JS_ATOMIC_DWORD_LOAD(pce, entry)
118 #define PCE_STORE(cache, pce, entry) JS_ATOMIC_DWORD_STORE(pce, entry)
120 #else /* !HAVE_ATOMIC_DWORD_ACCESS */
122 #define JS_PROPERTY_CACHE_METERING 1
124 #define PCE_LOAD(cache, pce, entry) \
125 JS_BEGIN_MACRO \
126 uint32 prefills_; \
127 uint32 fills_ = (cache)->fills; \
128 do { \
129 /* Load until cache->fills is stable (see FILL macro below). */ \
130 prefills_ = fills_; \
131 (entry) = *(pce); \
132 } while ((fills_ = (cache)->fills) != prefills_); \
133 JS_END_MACRO
135 #define PCE_STORE(cache, pce, entry) \
136 JS_BEGIN_MACRO \
137 do { \
138 /* Store until no racing collider stores half or all of pce. */ \
139 *(pce) = (entry); \
140 } while (PCE_OBJECT(*pce) != PCE_OBJECT(entry) || \
141 PCE_PROPERTY(*pce) != PCE_PROPERTY(entry)); \
142 JS_END_MACRO
144 #endif /* !HAVE_ATOMIC_DWORD_ACCESS */
146 #else /* !JS_THREADSAFE */
148 #define PCE_LOAD(cache, pce, entry) ((entry) = *(pce))
149 #define PCE_STORE(cache, pce, entry) (*(pce) = (entry))
151 #endif /* !JS_THREADSAFE */
153 typedef union JSPropertyCacheEntry {
154 struct {
155 JSObject *object; /* weak link to object */
156 JSScopeProperty *property; /* weak link to property */
157 } s;
158 #ifdef HAVE_ATOMIC_DWORD_ACCESS
159 prdword align;
160 #endif
161 } JSPropertyCacheEntry;
163 /* These may be called in lvalue or rvalue position. */
164 #define PCE_OBJECT(entry) ((entry).s.object)
165 #define PCE_PROPERTY(entry) ((entry).s.property)
167 typedef struct JSPropertyCache {
168 JSPropertyCacheEntry table[PROPERTY_CACHE_SIZE];
169 JSBool empty;
170 JSBool disabled;
171 #ifdef JS_PROPERTY_CACHE_METERING
172 uint32 fills;
173 uint32 recycles;
174 uint32 tests;
175 uint32 misses;
176 uint32 flushes;
177 # define PCMETER(x) x
178 #else
179 # define PCMETER(x) /* nothing */
180 #endif
181 } JSPropertyCache;
183 #define PROPERTY_CACHE_FILL(cache, obj, id, sprop) \
184 JS_BEGIN_MACRO \
185 JSPropertyCache *cache_ = (cache); \
186 if (!cache_->disabled) { \
187 uintN hashIndex_ = (uintN) PROPERTY_CACHE_HASH(obj, id); \
188 JSPropertyCacheEntry *pce_ = &cache_->table[hashIndex_]; \
189 JSPropertyCacheEntry entry_; \
190 JSScopeProperty *pce_sprop_; \
191 PCE_LOAD(cache_, pce_, entry_); \
192 pce_sprop_ = PCE_PROPERTY(entry_); \
193 PCMETER(if (pce_sprop_ && pce_sprop_ != sprop) \
194 cache_->recycles++); \
195 PCE_OBJECT(entry_) = obj; \
196 PCE_PROPERTY(entry_) = sprop; \
197 cache_->empty = JS_FALSE; \
198 PCMETER(cache_->fills++); \
199 PCE_STORE(cache_, pce_, entry_); \
200 } \
201 JS_END_MACRO
203 #define PROPERTY_CACHE_TEST(cache, obj, id, sprop) \
204 JS_BEGIN_MACRO \
205 uintN hashIndex_ = (uintN) PROPERTY_CACHE_HASH(obj, id); \
206 JSPropertyCache *cache_ = (cache); \
207 JSPropertyCacheEntry *pce_ = &cache_->table[hashIndex_]; \
208 JSPropertyCacheEntry entry_; \
209 JSScopeProperty *pce_sprop_; \
210 PCE_LOAD(cache_, pce_, entry_); \
211 pce_sprop_ = PCE_PROPERTY(entry_); \
212 PCMETER(cache_->tests++); \
213 if (pce_sprop_ && \
214 PCE_OBJECT(entry_) == obj && \
215 pce_sprop_->id == id) { \
216 sprop = pce_sprop_; \
217 } else { \
218 PCMETER(cache_->misses++); \
219 sprop = NULL; \
220 } \
221 JS_END_MACRO
223 extern void
224 js_FlushPropertyCache(JSContext *cx);
226 extern void
227 js_DisablePropertyCache(JSContext *cx);
229 extern void
230 js_EnablePropertyCache(JSContext *cx);
232 extern JS_FRIEND_API(jsval *)
233 js_AllocStack(JSContext *cx, uintN nslots, void **markp);
235 extern JS_FRIEND_API(void)
236 js_FreeStack(JSContext *cx, void *mark);
238 extern JSBool
239 js_GetArgument(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
241 extern JSBool
242 js_SetArgument(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
244 extern JSBool
245 js_GetLocalVariable(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
247 extern JSBool
248 js_SetLocalVariable(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
250 #ifdef DUMP_CALL_TABLE
251 # define JSOPTION_LOGCALL_TOSOURCE JS_BIT(15)
253 extern JSHashTable *js_CallTable;
254 extern size_t js_LogCallToSourceLimit;
256 extern void js_DumpCallTable(JSContext *cx);
257 #endif
259 /*
260 * Compute the 'this' parameter and store it in frame as frame.thisp.
261 * Activation objects ("Call" objects not created with "new Call()", i.e.,
262 * "Call" objects that have private data) may not be referred to by 'this',
263 * as dictated by ECMA.
264 *
265 * N.B.: fp->argv must be set, fp->argv[-1] the nominal 'this' paramter as
266 * a jsval, and fp->argv[-2] must be the callee object reference, usually a
267 * function object. Also, fp->flags must contain JSFRAME_CONSTRUCTING if we
268 * are preparing for a constructor call.
269 */
270 extern JSBool
271 js_ComputeThis(JSContext *cx, JSObject *thisp, JSStackFrame *fp);
273 /*
274 * NB: js_Invoke requires that cx is currently running JS (i.e., that cx->fp
275 * is non-null), and that the callee, |this| parameter, and actual arguments
276 * are already pushed on the stack under cx->fp->sp.
277 */
278 extern JS_FRIEND_API(JSBool)
279 js_Invoke(JSContext *cx, uintN argc, uintN flags);
281 /*
282 * Consolidated js_Invoke flags simply rename the low JSFRAME_* flags.
283 */
284 #define JSINVOKE_CONSTRUCT JSFRAME_CONSTRUCTING
285 #define JSINVOKE_INTERNAL JSFRAME_INTERNAL
286 #define JSINVOKE_SKIP_CALLER JSFRAME_SKIP_CALLER
288 /*
289 * "Internal" calls may come from C or C++ code using a JSContext on which no
290 * JS is running (!cx->fp), so they may need to push a dummy JSStackFrame.
291 */
292 #define js_InternalCall(cx,obj,fval,argc,argv,rval) \
293 js_InternalInvoke(cx, obj, fval, 0, argc, argv, rval)
295 #define js_InternalConstruct(cx,obj,fval,argc,argv,rval) \
296 js_InternalInvoke(cx, obj, fval, JSINVOKE_CONSTRUCT, argc, argv, rval)
298 extern JSBool
299 js_InternalInvoke(JSContext *cx, JSObject *obj, jsval fval, uintN flags,
300 uintN argc, jsval *argv, jsval *rval);
302 extern JSBool
303 js_InternalGetOrSet(JSContext *cx, JSObject *obj, jsid id, jsval fval,
304 JSAccessMode mode, uintN argc, jsval *argv, jsval *rval);
306 extern JSBool
307 js_Execute(JSContext *cx, JSObject *chain, JSScript *script,
308 JSStackFrame *down, uintN flags, jsval *result);
310 extern JSBool
311 js_CheckRedeclaration(JSContext *cx, JSObject *obj, jsid id, uintN attrs,
312 JSObject **objp, JSProperty **propp);
314 extern JSBool
315 js_StrictlyEqual(jsval lval, jsval rval);
317 extern JSBool
318 js_Interpret(JSContext *cx, jsbytecode *pc, jsval *result);
320 JS_END_EXTERN_C
322 #endif /* jsinterp_h___ */