Bug 576148: Factor out js::UpvarCookie. (r=mrbkap, dvander)
[mozilla-central.git] / js / src / jsinterp.h
blob1749c734fee92f5f4397265abf684d4c151f07c0
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=4 sw=4 et tw=78:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
17 * The Original Code is Mozilla Communicator client code, released
18 * March 31, 1998.
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
25 * Contributor(s):
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.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef jsinterp_h___
42 #define jsinterp_h___
44 * JS interpreter interface.
46 #include "jsprvtd.h"
47 #include "jspubtd.h"
48 #include "jsfun.h"
49 #include "jsopcode.h"
50 #include "jsscript.h"
52 JS_BEGIN_EXTERN_C
54 typedef struct JSFrameRegs {
55 jsbytecode *pc; /* program counter */
56 jsval *sp; /* stack pointer */
57 } JSFrameRegs;
59 /* JS stack frame flags. */
60 enum JSFrameFlags {
61 JSFRAME_CONSTRUCTING = 0x01, /* frame is for a constructor invocation */
62 JSFRAME_COMPUTED_THIS = 0x02, /* frame.thisv was computed already and
63 JSVAL_IS_OBJECT(thisv) */
64 JSFRAME_ASSIGNING = 0x04, /* a complex (not simplex JOF_ASSIGNING) op
65 is currently assigning to a property */
66 JSFRAME_DEBUGGER = 0x08, /* frame for JS_EvaluateInStackFrame */
67 JSFRAME_EVAL = 0x10, /* frame for obj_eval */
68 JSFRAME_FLOATING_GENERATOR = 0x20, /* frame copy stored in a generator obj */
69 JSFRAME_YIELDING = 0x40, /* js_Interpret dispatched JSOP_YIELD */
70 JSFRAME_GENERATOR = 0x80, /* frame belongs to generator-iterator */
71 JSFRAME_OVERRIDE_ARGS = 0x100, /* overridden arguments local variable */
73 JSFRAME_SPECIAL = JSFRAME_DEBUGGER | JSFRAME_EVAL
77 * JS stack frame, may be allocated on the C stack by native callers. Always
78 * allocated on cx->stackPool for calls from the interpreter to an interpreted
79 * function.
81 * NB: This struct is manually initialized in jsinterp.c and jsiter.c. If you
82 * add new members, update both files.
84 struct JSStackFrame
86 jsbytecode *imacpc; /* null or interpreter macro call pc */
87 JSObject *callobj; /* lazily created Call object */
88 jsval argsobj; /* lazily created arguments object, must be
89 JSVAL_OBJECT */
90 JSScript *script; /* script being interpreted */
91 JSFunction *fun; /* function being called or null */
92 jsval thisv; /* "this" pointer if in method */
93 uintN argc; /* actual argument count */
94 jsval *argv; /* base of argument stack slots */
95 jsval rval; /* function return value */
96 void *annotation; /* used by Java security */
98 /* Maintained by StackSpace operations */
99 JSStackFrame *down; /* previous frame, part of
100 stack layout invariant */
101 jsbytecode *savedPC; /* only valid if cx->fp != this */
102 #ifdef DEBUG
103 static jsbytecode *const sInvalidPC;
104 #endif
107 * We can't determine in advance which local variables can live on
108 * the stack and be freed when their dynamic scope ends, and which
109 * will be closed over and need to live in the heap. So we place
110 * variables on the stack initially, note when they are closed
111 * over, and copy those that are out to the heap when we leave
112 * their dynamic scope.
114 * The bytecode compiler produces a tree of block objects
115 * accompanying each JSScript representing those lexical blocks in
116 * the script that have let-bound variables associated with them.
117 * These block objects are never modified, and never become part
118 * of any function's scope chain. Their parent slots point to the
119 * innermost block that encloses them, or are NULL in the
120 * outermost blocks within a function or in eval or global code.
122 * When we are in the static scope of such a block, blockChain
123 * points to its compiler-allocated block object; otherwise, it is
124 * NULL.
126 * scopeChain is the current scope chain, including 'call' and
127 * 'block' objects for those function calls and lexical blocks
128 * whose static scope we are currently executing in, and 'with'
129 * objects for with statements; the chain is typically terminated
130 * by a global object. However, as an optimization, the young end
131 * of the chain omits block objects we have not yet cloned. To
132 * create a closure, we clone the missing blocks from blockChain
133 * (which is always current), place them at the head of
134 * scopeChain, and use that for the closure's scope chain. If we
135 * never close over a lexical block, we never place a mutable
136 * clone of it on scopeChain.
138 * This lazy cloning is implemented in js_GetScopeChain, which is
139 * also used in some other cases --- entering 'with' blocks, for
140 * example.
142 union {
143 JSObject *scopeChain;
144 jsval scopeChainVal;
146 JSObject *blockChain;
148 uint32 flags; /* frame flags -- see below */
149 JSStackFrame *displaySave; /* previous value of display entry for
150 script->staticLevel */
152 /* Members only needed for inline calls. */
153 void *hookData; /* debugger call hook data */
154 JSVersion callerVersion; /* dynamic version of calling script */
156 void putActivationObjects(JSContext *cx) {
158 * The order of calls here is important as js_PutCallObject needs to
159 * access argsobj.
161 if (callobj) {
162 js_PutCallObject(cx, this);
163 JS_ASSERT(!argsobj);
164 } else if (argsobj) {
165 js_PutArgsObject(cx, this);
169 /* Get the frame's current bytecode, assuming |this| is in |cx|. */
170 jsbytecode *pc(JSContext *cx) const;
172 jsval *argEnd() const {
173 return (jsval *)this;
176 jsval *slots() const {
177 return (jsval *)(this + 1);
180 jsval calleeValue() {
181 JS_ASSERT(argv);
182 return argv[-2];
185 JSObject *calleeObject() {
186 JS_ASSERT(argv);
187 return JSVAL_TO_OBJECT(argv[-2]);
190 JSObject *callee() {
191 return argv ? JSVAL_TO_OBJECT(argv[-2]) : NULL;
195 * Get the object associated with the Execution Context's
196 * VariableEnvironment (ES5 10.3). The given CallStack must contain this
197 * stack frame.
199 JSObject *varobj(js::CallStack *cs) const;
201 /* Short for: varobj(cx->activeCallStack()). */
202 JSObject *varobj(JSContext *cx) const;
204 inline JSObject *getThisObject(JSContext *cx);
206 bool isGenerator() const { return flags & JSFRAME_GENERATOR; }
207 bool isFloatingGenerator() const {
208 if (flags & JSFRAME_FLOATING_GENERATOR) {
209 JS_ASSERT(isGenerator());
210 return true;
212 return false;
215 bool isDummyFrame() const { return !script && !fun; }
218 namespace js {
220 static const size_t VALUES_PER_STACK_FRAME = sizeof(JSStackFrame) / sizeof(jsval);
221 JS_STATIC_ASSERT(sizeof(JSStackFrame) % sizeof(jsval) == 0);
225 static JS_INLINE jsval *
226 StackBase(JSStackFrame *fp)
228 return fp->slots() + fp->script->nfixed;
231 static JS_INLINE uintN
232 GlobalVarCount(JSStackFrame *fp)
234 JS_ASSERT(!fp->fun);
235 return fp->script->nfixed;
239 * Refresh and return fp->scopeChain. It may be stale if block scopes are
240 * active but not yet reflected by objects in the scope chain. If a block
241 * scope contains a with, eval, XML filtering predicate, or similar such
242 * dynamically scoped construct, then compile-time block scope at fp->blocks
243 * must reflect at runtime.
245 extern JSObject *
246 js_GetScopeChain(JSContext *cx, JSStackFrame *fp);
249 * Given a context and a vector of [callee, this, args...] for a function that
250 * was specified with a JSFUN_THISP_PRIMITIVE flag, get the primitive value of
251 * |this| into *thisvp. In doing so, if |this| is an object, insist it is an
252 * instance of clasp and extract its private slot value to return via *thisvp.
254 * NB: this function loads and uses *vp before storing *thisvp, so the two may
255 * alias the same jsval.
257 extern JSBool
258 js_GetPrimitiveThis(JSContext *cx, jsval *vp, JSClass *clasp, jsval *thisvp);
261 * For a call with arguments argv including argv[-1] (nominal |this|) and
262 * argv[-2] (callee) replace null |this| with callee's parent, replace
263 * primitive values with the equivalent wrapper objects and censor activation
264 * objects as, per ECMA-262, they may not be referred to by |this|. argv[-1]
265 * must not be a JSVAL_VOID.
267 extern JSObject *
268 js_ComputeThis(JSContext *cx, jsval *argv);
270 extern const uint16 js_PrimitiveTestFlags[];
272 #define PRIMITIVE_THIS_TEST(fun,thisv) \
273 (JS_ASSERT(!JSVAL_IS_VOID(thisv)), \
274 JSFUN_THISP_TEST(JSFUN_THISP_FLAGS((fun)->flags), \
275 js_PrimitiveTestFlags[JSVAL_TAG(thisv) - 1]))
278 * The js::InvokeArgumentsGuard passed to js_Invoke must come from an
279 * immediately-enclosing successful call to js::StackSpace::pushInvokeArgs,
280 * i.e., there must have been no un-popped pushes to cx->stack(). Furthermore,
281 * |args.getvp()[0]| should be the callee, |args.getvp()[1]| should be |this|,
282 * and the range [args.getvp() + 2, args.getvp() + 2 + args.getArgc()) should
283 * be initialized actual arguments.
285 extern JS_REQUIRES_STACK JS_FRIEND_API(JSBool)
286 js_Invoke(JSContext *cx, const js::InvokeArgsGuard &args, uintN flags);
289 * Consolidated js_Invoke flags simply rename certain JSFRAME_* flags, so that
290 * we can share bits stored in JSStackFrame.flags and passed to:
292 * js_Invoke
293 * js_InternalInvoke
294 * js_ValueToFunction
295 * js_ValueToFunctionObject
296 * js_ValueToCallableObject
297 * js_ReportIsNotFunction
299 * See jsfun.h for the latter four and flag renaming macros.
301 #define JSINVOKE_CONSTRUCT JSFRAME_CONSTRUCTING
304 * Mask to isolate construct and iterator flags for use with jsfun.h functions.
306 #define JSINVOKE_FUNFLAGS JSINVOKE_CONSTRUCT
309 * "Internal" calls may come from C or C++ code using a JSContext on which no
310 * JS is running (!cx->fp), so they may need to push a dummy JSStackFrame.
312 #define js_InternalCall(cx,obj,fval,argc,argv,rval) \
313 js_InternalInvoke(cx, OBJECT_TO_JSVAL(obj), fval, 0, argc, argv, rval)
315 #define js_InternalConstruct(cx,obj,fval,argc,argv,rval) \
316 js_InternalInvoke(cx, OBJECT_TO_JSVAL(obj), fval, JSINVOKE_CONSTRUCT, argc, argv, rval)
318 extern JSBool
319 js_InternalInvoke(JSContext *cx, jsval thisv, jsval fval, uintN flags,
320 uintN argc, jsval *argv, jsval *rval);
322 extern JSBool
323 js_InternalGetOrSet(JSContext *cx, JSObject *obj, jsid id, jsval fval,
324 JSAccessMode mode, uintN argc, jsval *argv, jsval *rval);
326 extern JS_FORCES_STACK JSBool
327 js_Execute(JSContext *cx, JSObject *chain, JSScript *script,
328 JSStackFrame *down, uintN flags, jsval *result);
330 extern JS_REQUIRES_STACK JSBool
331 js_InvokeConstructor(JSContext *cx, const js::InvokeArgsGuard &args);
333 extern JS_REQUIRES_STACK JSBool
334 js_Interpret(JSContext *cx);
336 #define JSPROP_INITIALIZER 0x100 /* NB: Not a valid property attribute. */
338 extern JSBool
339 js_CheckRedeclaration(JSContext *cx, JSObject *obj, jsid id, uintN attrs,
340 JSObject **objp, JSProperty **propp);
342 extern JSBool
343 js_StrictlyEqual(JSContext *cx, jsval lval, jsval rval);
345 /* === except that NaN is the same as NaN and -0 is not the same as +0. */
346 extern JSBool
347 js_SameValue(jsval v1, jsval v2, JSContext *cx);
349 extern JSBool
350 js_InternNonIntElementId(JSContext *cx, JSObject *obj, jsval idval, jsid *idp);
353 * Given an active context, a static scope level, and an upvar cookie, return
354 * the value of the upvar.
356 extern jsval &
357 js_GetUpvar(JSContext *cx, uintN level, js::UpvarCookie cookie);
360 * JS_LONE_INTERPRET indicates that the compiler should see just the code for
361 * the js_Interpret function when compiling jsinterp.cpp. The rest of the code
362 * from the file should be visible only when compiling jsinvoke.cpp. It allows
363 * platform builds to optimize selectively js_Interpret when the granularity
364 * of the optimizations with the given compiler is a compilation unit.
366 * JS_STATIC_INTERPRET is the modifier for functions defined in jsinterp.cpp
367 * that only js_Interpret calls. When JS_LONE_INTERPRET is true all such
368 * functions are declared below.
370 #ifndef JS_LONE_INTERPRET
371 # ifdef _MSC_VER
372 # define JS_LONE_INTERPRET 0
373 # else
374 # define JS_LONE_INTERPRET 1
375 # endif
376 #endif
378 #define JS_MAX_INLINE_CALL_COUNT 3000
380 #if !JS_LONE_INTERPRET
381 # define JS_STATIC_INTERPRET static
382 #else
383 # define JS_STATIC_INTERPRET
386 * ECMA requires "the global object", but in embeddings such as the browser,
387 * which have multiple top-level objects (windows, frames, etc. in the DOM),
388 * we prefer fun's parent. An example that causes this code to run:
390 * // in window w1
391 * function f() { return this }
392 * function g() { return f }
394 * // in window w2
395 * var h = w1.g()
396 * alert(h() == w1)
398 * The alert should display "true".
400 extern JSObject *
401 js_ComputeGlobalThis(JSContext *cx, jsval *argv);
403 extern JS_REQUIRES_STACK JSBool
404 js_EnterWith(JSContext *cx, jsint stackIndex);
406 extern JS_REQUIRES_STACK void
407 js_LeaveWith(JSContext *cx);
409 extern JS_REQUIRES_STACK JSClass *
410 js_IsActiveWithOrBlock(JSContext *cx, JSObject *obj, int stackDepth);
413 * Unwind block and scope chains to match the given depth. The function sets
414 * fp->sp on return to stackDepth.
416 extern JS_REQUIRES_STACK JSBool
417 js_UnwindScope(JSContext *cx, jsint stackDepth, JSBool normalUnwind);
419 extern JSBool
420 js_OnUnknownMethod(JSContext *cx, jsval *vp);
423 * Find the results of incrementing or decrementing *vp. For pre-increments,
424 * both *vp and *vp2 will contain the result on return. For post-increments,
425 * vp will contain the original value converted to a number and vp2 will get
426 * the result. Both vp and vp2 must be roots.
428 extern JSBool
429 js_DoIncDec(JSContext *cx, const JSCodeSpec *cs, jsval *vp, jsval *vp2);
432 * Opcode tracing helper. When len is not 0, cx->fp->regs->pc[-len] gives the
433 * previous opcode.
435 extern JS_REQUIRES_STACK void
436 js_TraceOpcode(JSContext *cx);
439 * JS_OPMETER helper functions.
441 extern void
442 js_MeterOpcodePair(JSOp op1, JSOp op2);
444 extern void
445 js_MeterSlotOpcode(JSOp op, uint32 slot);
447 #endif /* JS_LONE_INTERPRET */
449 JS_END_EXTERN_C
451 inline JSObject *
452 JSStackFrame::getThisObject(JSContext *cx)
454 if (flags & JSFRAME_COMPUTED_THIS)
455 return JSVAL_TO_OBJECT(thisv); /* JSVAL_COMPUTED_THIS invariant */
456 JSObject* obj = js_ComputeThis(cx, argv);
457 if (!obj)
458 return NULL;
459 thisv = OBJECT_TO_JSVAL(obj);
460 flags |= JSFRAME_COMPUTED_THIS;
461 return obj;
464 #endif /* jsinterp_h___ */