Bug 550784 - [OOPP] Flash deadlocks during script evals that trigger focus related...
[mozilla-central.git] / js / src / jsscript.h
blob4028ebba55a04ff38a7d8b887028c35e1e6a4769
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=79 ft=cpp:
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 jsscript_h___
42 #define jsscript_h___
44 * JS script descriptor.
46 #include "jsatom.h"
47 #include "jsprvtd.h"
48 #include "jsdbgapi.h"
50 JS_BEGIN_EXTERN_C
53 * Type of try note associated with each catch or finally block, and also with
54 * for-in loops.
56 typedef enum JSTryNoteKind {
57 JSTRY_CATCH,
58 JSTRY_FINALLY,
59 JSTRY_ITER
60 } JSTryNoteKind;
63 * Exception handling record.
65 struct JSTryNote {
66 uint8 kind; /* one of JSTryNoteKind */
67 uint8 padding; /* explicit padding on uint16 boundary */
68 uint16 stackDepth; /* stack depth upon exception handler entry */
69 uint32 start; /* start of the try statement or for-in loop
70 relative to script->main */
71 uint32 length; /* length of the try statement or for-in loop */
74 typedef struct JSTryNoteArray {
75 JSTryNote *vector; /* array of indexed try notes */
76 uint32 length; /* count of indexed try notes */
77 } JSTryNoteArray;
79 typedef struct JSObjectArray {
80 JSObject **vector; /* array of indexed objects */
81 uint32 length; /* count of indexed objects */
82 } JSObjectArray;
84 typedef struct JSUpvarArray {
85 uint32 *vector; /* array of indexed upvar cookies */
86 uint32 length; /* count of indexed upvar cookies */
87 } JSUpvarArray;
89 #define CALLEE_UPVAR_SLOT 0xffff
90 #define FREE_STATIC_LEVEL 0x3fff
91 #define FREE_UPVAR_COOKIE 0xffffffff
92 #define MAKE_UPVAR_COOKIE(skip,slot) ((skip) << 16 | (slot))
93 #define UPVAR_FRAME_SKIP(cookie) ((uint32)(cookie) >> 16)
94 #define UPVAR_FRAME_SLOT(cookie) ((uint16)(cookie))
96 #define JS_OBJECT_ARRAY_SIZE(length) \
97 (offsetof(JSObjectArray, vector) + sizeof(JSObject *) * (length))
99 #if defined DEBUG && defined JS_THREADSAFE
100 # define CHECK_SCRIPT_OWNER 1
101 #endif
103 struct JSScript {
104 jsbytecode *code; /* bytecodes and their immediate operands */
105 uint32 length; /* length of code vector */
106 uint16 version; /* JS version under which script was compiled */
107 uint16 nfixed; /* number of slots besides stack operands in
108 slot array */
109 uint8 objectsOffset; /* offset to the array of nested function,
110 block, scope, xml and one-time regexps
111 objects or 0 if none */
112 uint8 upvarsOffset; /* offset of the array of display ("up")
113 closure vars or 0 if none */
114 uint8 regexpsOffset; /* offset to the array of to-be-cloned
115 regexps or 0 if none. */
116 uint8 trynotesOffset; /* offset to the array of try notes or
117 0 if none */
118 bool noScriptRval:1; /* no need for result value of last
119 expression statement */
120 bool savedCallerFun:1; /* object 0 is caller function */
121 bool hasSharps:1; /* script uses sharp variables */
122 bool strictModeCode:1; /* code is in strict mode */
124 jsbytecode *main; /* main entry point, after predef'ing prolog */
125 JSAtomMap atomMap; /* maps immediate index to literal struct */
126 const char *filename; /* source filename or null */
127 uint32 lineno; /* base line number of script */
128 uint16 nslots; /* vars plus maximum stack depth */
129 uint16 staticLevel;/* static level for display maintenance */
130 JSPrincipals *principals;/* principals for this script */
131 union {
132 JSObject *object; /* optional Script-class object wrapper */
133 JSScript *nextToGC; /* next to GC in rt->scriptsToGC list */
134 } u;
135 #ifdef CHECK_SCRIPT_OWNER
136 JSThread *owner; /* for thread-safe life-cycle assertions */
137 #endif
139 /* Script notes are allocated right after the code. */
140 jssrcnote *notes() { return (jssrcnote *)(code + length); }
142 JSObjectArray *objects() {
143 JS_ASSERT(objectsOffset != 0);
144 return (JSObjectArray *)((uint8 *) this + objectsOffset);
147 JSUpvarArray *upvars() {
148 JS_ASSERT(upvarsOffset != 0);
149 return (JSUpvarArray *) ((uint8 *) this + upvarsOffset);
152 JSObjectArray *regexps() {
153 JS_ASSERT(regexpsOffset != 0);
154 return (JSObjectArray *) ((uint8 *) this + regexpsOffset);
157 JSTryNoteArray *trynotes() {
158 JS_ASSERT(trynotesOffset != 0);
159 return (JSTryNoteArray *) ((uint8 *) this + trynotesOffset);
162 JSAtom *getAtom(size_t index) {
163 JS_ASSERT(index < atomMap.length);
164 return atomMap.vector[index];
167 JSObject *getObject(size_t index) {
168 JSObjectArray *arr = objects();
169 JS_ASSERT(index < arr->length);
170 return arr->vector[index];
173 inline JSFunction *getFunction(size_t index);
175 inline JSObject *getRegExp(size_t index);
178 * The isEmpty method tells whether this script has code that computes any
179 * result (not return value, result AKA normal completion value) other than
180 * JSVAL_VOID, or any other effects. It has a fast path for the case where
181 * |this| is the emptyScript singleton, but it also checks this->length and
182 * this->code, to handle debugger-generated mutable empty scripts.
184 inline bool isEmpty() const;
187 * Accessor for the emptyScriptConst singleton, to consolidate const_cast.
188 * See the private member declaration.
190 static JSScript *emptyScript() {
191 return const_cast<JSScript *>(&emptyScriptConst);
194 private:
196 * Use const to put this in read-only memory if possible. We are stuck with
197 * non-const JSScript * and jsbytecode * by legacy code (back in the 1990s,
198 * const wasn't supported correctly on all target platforms). The debugger
199 * does mutate bytecode, and script->u.object may be set after construction
200 * in some cases, so making JSScript pointers const will be "hard".
202 static const JSScript emptyScriptConst;
205 #define SHARP_NSLOTS 2 /* [#array, #depth] slots if the script
206 uses sharp variables */
208 static JS_INLINE uintN
209 StackDepth(JSScript *script)
211 return script->nslots - script->nfixed;
215 * If pc_ does not point within script_'s bytecode, then it must point into an
216 * imacro body, so we use cx->runtime common atoms instead of script_'s atoms.
217 * This macro uses cx from its callers' environments in the pc-in-imacro case.
219 #define JS_GET_SCRIPT_ATOM(script_, pc_, index, atom) \
220 JS_BEGIN_MACRO \
221 if ((pc_) < (script_)->code || \
222 (script_)->code + (script_)->length <= (pc_)) { \
223 JS_ASSERT((size_t)(index) < js_common_atom_count); \
224 (atom) = COMMON_ATOMS_START(&cx->runtime->atomState)[index]; \
225 } else { \
226 (atom) = script_->getAtom(index); \
228 JS_END_MACRO
230 extern JS_FRIEND_DATA(JSClass) js_ScriptClass;
232 extern JSObject *
233 js_InitScriptClass(JSContext *cx, JSObject *obj);
236 * On first new context in rt, initialize script runtime state, specifically
237 * the script filename table and its lock.
239 extern JSBool
240 js_InitRuntimeScriptState(JSRuntime *rt);
243 * On JS_DestroyRuntime(rt), forcibly free script filename prefixes and any
244 * script filename table entries that have not been GC'd.
246 * This allows script filename prefixes to outlive any context in rt.
248 extern void
249 js_FreeRuntimeScriptState(JSRuntime *rt);
251 extern const char *
252 js_SaveScriptFilename(JSContext *cx, const char *filename);
254 extern const char *
255 js_SaveScriptFilenameRT(JSRuntime *rt, const char *filename, uint32 flags);
257 extern uint32
258 js_GetScriptFilenameFlags(const char *filename);
260 extern void
261 js_MarkScriptFilename(const char *filename);
263 extern void
264 js_MarkScriptFilenames(JSRuntime *rt, JSBool keepAtoms);
266 extern void
267 js_SweepScriptFilenames(JSRuntime *rt);
270 * Two successively less primitive ways to make a new JSScript. The first
271 * does *not* call a non-null cx->runtime->newScriptHook -- only the second,
272 * js_NewScriptFromCG, calls this optional debugger hook.
274 * The js_NewScript function can't know whether the script it creates belongs
275 * to a function, or is top-level or eval code, but the debugger wants access
276 * to the newly made script's function, if any -- so callers of js_NewScript
277 * are responsible for notifying the debugger after successfully creating any
278 * kind (function or other) of new JSScript.
280 * NB: js_NewScript always creates a new script; it never returns the empty
281 * script singleton (JSScript::emptyScript()). Callers who know they can use
282 * that read-only singleton are responsible for choosing it instead of calling
283 * js_NewScript with length and nsrcnotes equal to 1 and other parameters save
284 * cx all zero.
286 extern JSScript *
287 js_NewScript(JSContext *cx, uint32 length, uint32 nsrcnotes, uint32 natoms,
288 uint32 nobjects, uint32 nupvars, uint32 nregexps,
289 uint32 ntrynotes);
291 extern JSScript *
292 js_NewScriptFromCG(JSContext *cx, JSCodeGenerator *cg);
295 * New-script-hook calling is factored from js_NewScriptFromCG so that it
296 * and callers of js_XDRScript can share this code. In the case of callers
297 * of js_XDRScript, the hook should be invoked only after successful decode
298 * of any owning function (the fun parameter) or script object (null fun).
300 extern JS_FRIEND_API(void)
301 js_CallNewScriptHook(JSContext *cx, JSScript *script, JSFunction *fun);
303 extern JS_FRIEND_API(void)
304 js_CallDestroyScriptHook(JSContext *cx, JSScript *script);
306 extern void
307 js_DestroyScript(JSContext *cx, JSScript *script);
309 extern void
310 js_TraceScript(JSTracer *trc, JSScript *script);
313 * To perturb as little code as possible, we introduce a js_GetSrcNote lookup
314 * cache without adding an explicit cx parameter. Thus js_GetSrcNote becomes
315 * a macro that uses cx from its calls' lexical environments.
317 #define js_GetSrcNote(script,pc) js_GetSrcNoteCached(cx, script, pc)
319 extern jssrcnote *
320 js_GetSrcNoteCached(JSContext *cx, JSScript *script, jsbytecode *pc);
323 * NOTE: use js_FramePCToLineNumber(cx, fp) when you have an active fp, in
324 * preference to js_PCToLineNumber (cx, fp->script fp->regs->pc), because
325 * fp->imacpc may be non-null, indicating an active imacro.
327 extern uintN
328 js_FramePCToLineNumber(JSContext *cx, JSStackFrame *fp);
330 extern uintN
331 js_PCToLineNumber(JSContext *cx, JSScript *script, jsbytecode *pc);
333 extern jsbytecode *
334 js_LineNumberToPC(JSScript *script, uintN lineno);
336 extern JS_FRIEND_API(uintN)
337 js_GetScriptLineExtent(JSScript *script);
339 static JS_INLINE JSOp
340 js_GetOpcode(JSContext *cx, JSScript *script, jsbytecode *pc)
342 JSOp op = (JSOp) *pc;
343 if (op == JSOP_TRAP)
344 op = JS_GetTrapOpcode(cx, script, pc);
345 return op;
349 * If magic is non-null, js_XDRScript succeeds on magic number mismatch but
350 * returns false in *magic; it reflects a match via a true *magic out param.
351 * If magic is null, js_XDRScript returns false on bad magic number errors,
352 * which it reports.
354 * NB: after a successful JSXDR_DECODE, and provided that *scriptp is not the
355 * JSScript::emptyScript() immutable singleton, js_XDRScript callers must do
356 * any required subsequent set-up of owning function or script object and then
357 * call js_CallNewScriptHook.
359 * If the caller requires a mutable empty script (for debugging or u.object
360 * ownership setting), pass true for needMutableScript. Otherwise pass false.
361 * Call js_CallNewScriptHook only with a mutable script, i.e. never with the
362 * JSScript::emptyScript() singleton.
364 extern JSBool
365 js_XDRScript(JSXDRState *xdr, JSScript **scriptp, bool needMutableScript,
366 JSBool *hasMagic);
368 JS_END_EXTERN_C
370 #endif /* jsscript_h___ */