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: |
3 |
* |
4 |
* ***** BEGIN LICENSE BLOCK ***** |
5 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
6 |
* |
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/ |
11 |
* |
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. |
16 |
* |
17 |
* The Original Code is Mozilla Communicator client code, released |
18 |
* March 31, 1998. |
19 |
* |
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. |
24 |
* |
25 |
* Contributor(s): |
26 |
* |
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. |
38 |
* |
39 |
* ***** END LICENSE BLOCK ***** */ |
40 |
|
41 |
#ifndef jsscript_h___ |
42 |
#define jsscript_h___ |
43 |
/* |
44 |
* JS script descriptor. |
45 |
*/ |
46 |
#include "jsatom.h" |
47 |
#include "jsprvtd.h" |
48 |
#include "jsdbgapi.h" |
49 |
|
50 |
JS_BEGIN_EXTERN_C |
51 |
|
52 |
/* |
53 |
* Type of try note associated with each catch or finally block, and also with |
54 |
* for-in loops. |
55 |
*/ |
56 |
typedef enum JSTryNoteKind { |
57 |
JSTRY_CATCH, |
58 |
JSTRY_FINALLY, |
59 |
JSTRY_ITER |
60 |
} JSTryNoteKind; |
61 |
|
62 |
/* |
63 |
* Exception handling record. |
64 |
*/ |
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 */ |
72 |
}; |
73 |
|
74 |
typedef struct JSTryNoteArray { |
75 |
JSTryNote *vector; /* array of indexed try notes */ |
76 |
uint32 length; /* count of indexed try notes */ |
77 |
} JSTryNoteArray; |
78 |
|
79 |
typedef struct JSObjectArray { |
80 |
JSObject **vector; /* array of indexed objects */ |
81 |
uint32 length; /* count of indexed objects */ |
82 |
} JSObjectArray; |
83 |
|
84 |
typedef struct JSUpvarArray { |
85 |
uint32 *vector; /* array of indexed upvar cookies */ |
86 |
uint32 length; /* count of indexed upvar cookies */ |
87 |
} JSUpvarArray; |
88 |
|
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)) |
95 |
|
96 |
#define JS_OBJECT_ARRAY_SIZE(length) \ |
97 |
(offsetof(JSObjectArray, vector) + sizeof(JSObject *) * (length)) |
98 |
|
99 |
#if defined DEBUG && defined JS_THREADSAFE |
100 |
# define CHECK_SCRIPT_OWNER 1 |
101 |
#endif |
102 |
|
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 |
uint8 flags; /* see below */ |
119 |
jsbytecode *main; /* main entry point, after predef'ing prolog */ |
120 |
JSAtomMap atomMap; /* maps immediate index to literal struct */ |
121 |
const char *filename; /* source filename or null */ |
122 |
uint32 lineno; /* base line number of script */ |
123 |
uint16 nslots; /* vars plus maximum stack depth */ |
124 |
uint16 staticLevel;/* static level for display maintenance */ |
125 |
JSPrincipals *principals;/* principals for this script */ |
126 |
union { |
127 |
JSObject *object; /* optional Script-class object wrapper */ |
128 |
JSScript *nextToGC; /* next to GC in rt->scriptsToGC list */ |
129 |
} u; |
130 |
#ifdef CHECK_SCRIPT_OWNER |
131 |
JSThread *owner; /* for thread-safe life-cycle assertions */ |
132 |
#endif |
133 |
|
134 |
#ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */ |
135 |
|
136 |
/* Script notes are allocated right after the code. */ |
137 |
jssrcnote *notes() { return (jssrcnote *)(code + length); } |
138 |
|
139 |
JSObjectArray *objects() { |
140 |
JS_ASSERT(objectsOffset != 0); |
141 |
return (JSObjectArray *)((uint8 *) this + objectsOffset); |
142 |
} |
143 |
|
144 |
JSUpvarArray *upvars() { |
145 |
JS_ASSERT(upvarsOffset != 0); |
146 |
return (JSUpvarArray *) ((uint8 *) this + upvarsOffset); |
147 |
} |
148 |
|
149 |
JSObjectArray *regexps() { |
150 |
JS_ASSERT(regexpsOffset != 0); |
151 |
return (JSObjectArray *) ((uint8 *) this + regexpsOffset); |
152 |
} |
153 |
|
154 |
JSTryNoteArray *trynotes() { |
155 |
JS_ASSERT(trynotesOffset != 0); |
156 |
return (JSTryNoteArray *) ((uint8 *) this + trynotesOffset); |
157 |
} |
158 |
|
159 |
JSAtom *getAtom(size_t index) { |
160 |
JS_ASSERT(index < atomMap.length); |
161 |
return atomMap.vector[index]; |
162 |
} |
163 |
|
164 |
JSObject *getObject(size_t index) { |
165 |
JSObjectArray *arr = objects(); |
166 |
JS_ASSERT(index < arr->length); |
167 |
return arr->vector[index]; |
168 |
} |
169 |
|
170 |
inline JSFunction *getFunction(size_t index); |
171 |
|
172 |
inline JSObject *getRegExp(size_t index); |
173 |
|
174 |
#endif /* __cplusplus */ |
175 |
}; |
176 |
|
177 |
#define JSSF_NO_SCRIPT_RVAL 0x01 /* no need for result value of last |
178 |
expression statement */ |
179 |
#define JSSF_SAVED_CALLER_FUN 0x02 /* object 0 is caller function */ |
180 |
|
181 |
static JS_INLINE uintN |
182 |
StackDepth(JSScript *script) |
183 |
{ |
184 |
return script->nslots - script->nfixed; |
185 |
} |
186 |
|
187 |
/* |
188 |
* If pc_ does not point within script_'s bytecode, then it must point into an |
189 |
* imacro body, so we use cx->runtime common atoms instead of script_'s atoms. |
190 |
* This macro uses cx from its callers' environments in the pc-in-imacro case. |
191 |
*/ |
192 |
#define JS_GET_SCRIPT_ATOM(script_, pc_, index, atom) \ |
193 |
JS_BEGIN_MACRO \ |
194 |
if ((pc_) < (script_)->code || \ |
195 |
(script_)->code + (script_)->length <= (pc_)) { \ |
196 |
JS_ASSERT((size_t)(index) < js_common_atom_count); \ |
197 |
(atom) = COMMON_ATOMS_START(&cx->runtime->atomState)[index]; \ |
198 |
} else { \ |
199 |
(atom) = script_->getAtom(index); \ |
200 |
} \ |
201 |
JS_END_MACRO |
202 |
|
203 |
extern JS_FRIEND_DATA(JSClass) js_ScriptClass; |
204 |
|
205 |
extern JSObject * |
206 |
js_InitScriptClass(JSContext *cx, JSObject *obj); |
207 |
|
208 |
/* |
209 |
* On first new context in rt, initialize script runtime state, specifically |
210 |
* the script filename table and its lock. |
211 |
*/ |
212 |
extern JSBool |
213 |
js_InitRuntimeScriptState(JSRuntime *rt); |
214 |
|
215 |
/* |
216 |
* On JS_DestroyRuntime(rt), forcibly free script filename prefixes and any |
217 |
* script filename table entries that have not been GC'd. |
218 |
* |
219 |
* This allows script filename prefixes to outlive any context in rt. |
220 |
*/ |
221 |
extern void |
222 |
js_FreeRuntimeScriptState(JSRuntime *rt); |
223 |
|
224 |
extern const char * |
225 |
js_SaveScriptFilename(JSContext *cx, const char *filename); |
226 |
|
227 |
extern const char * |
228 |
js_SaveScriptFilenameRT(JSRuntime *rt, const char *filename, uint32 flags); |
229 |
|
230 |
extern uint32 |
231 |
js_GetScriptFilenameFlags(const char *filename); |
232 |
|
233 |
extern void |
234 |
js_MarkScriptFilename(const char *filename); |
235 |
|
236 |
extern void |
237 |
js_MarkScriptFilenames(JSRuntime *rt, JSBool keepAtoms); |
238 |
|
239 |
extern void |
240 |
js_SweepScriptFilenames(JSRuntime *rt); |
241 |
|
242 |
/* |
243 |
* Two successively less primitive ways to make a new JSScript. The first |
244 |
* does *not* call a non-null cx->runtime->newScriptHook -- only the second, |
245 |
* js_NewScriptFromCG, calls this optional debugger hook. |
246 |
* |
247 |
* The js_NewScript function can't know whether the script it creates belongs |
248 |
* to a function, or is top-level or eval code, but the debugger wants access |
249 |
* to the newly made script's function, if any -- so callers of js_NewScript |
250 |
* are responsible for notifying the debugger after successfully creating any |
251 |
* kind (function or other) of new JSScript. |
252 |
*/ |
253 |
extern JSScript * |
254 |
js_NewScript(JSContext *cx, uint32 length, uint32 nsrcnotes, uint32 natoms, |
255 |
uint32 nobjects, uint32 nupvars, uint32 nregexps, |
256 |
uint32 ntrynotes); |
257 |
|
258 |
extern JSScript * |
259 |
js_NewScriptFromCG(JSContext *cx, JSCodeGenerator *cg); |
260 |
|
261 |
/* |
262 |
* New-script-hook calling is factored from js_NewScriptFromCG so that it |
263 |
* and callers of js_XDRScript can share this code. In the case of callers |
264 |
* of js_XDRScript, the hook should be invoked only after successful decode |
265 |
* of any owning function (the fun parameter) or script object (null fun). |
266 |
*/ |
267 |
extern JS_FRIEND_API(void) |
268 |
js_CallNewScriptHook(JSContext *cx, JSScript *script, JSFunction *fun); |
269 |
|
270 |
extern JS_FRIEND_API(void) |
271 |
js_CallDestroyScriptHook(JSContext *cx, JSScript *script); |
272 |
|
273 |
extern void |
274 |
js_DestroyScript(JSContext *cx, JSScript *script); |
275 |
|
276 |
extern void |
277 |
js_TraceScript(JSTracer *trc, JSScript *script); |
278 |
|
279 |
/* |
280 |
* To perturb as little code as possible, we introduce a js_GetSrcNote lookup |
281 |
* cache without adding an explicit cx parameter. Thus js_GetSrcNote becomes |
282 |
* a macro that uses cx from its calls' lexical environments. |
283 |
*/ |
284 |
#define js_GetSrcNote(script,pc) js_GetSrcNoteCached(cx, script, pc) |
285 |
|
286 |
extern jssrcnote * |
287 |
js_GetSrcNoteCached(JSContext *cx, JSScript *script, jsbytecode *pc); |
288 |
|
289 |
/* |
290 |
* NOTE: use js_FramePCToLineNumber(cx, fp) when you have an active fp, in |
291 |
* preference to js_PCToLineNumber (cx, fp->script fp->regs->pc), because |
292 |
* fp->imacpc may be non-null, indicating an active imacro. |
293 |
*/ |
294 |
extern uintN |
295 |
js_FramePCToLineNumber(JSContext *cx, JSStackFrame *fp); |
296 |
|
297 |
extern uintN |
298 |
js_PCToLineNumber(JSContext *cx, JSScript *script, jsbytecode *pc); |
299 |
|
300 |
extern jsbytecode * |
301 |
js_LineNumberToPC(JSScript *script, uintN lineno); |
302 |
|
303 |
extern JS_FRIEND_API(uintN) |
304 |
js_GetScriptLineExtent(JSScript *script); |
305 |
|
306 |
static JS_INLINE JSOp |
307 |
js_GetOpcode(JSContext *cx, JSScript *script, jsbytecode *pc) |
308 |
{ |
309 |
JSOp op = (JSOp) *pc; |
310 |
if (op == JSOP_TRAP) |
311 |
op = JS_GetTrapOpcode(cx, script, pc); |
312 |
return op; |
313 |
} |
314 |
|
315 |
/* |
316 |
* If magic is non-null, js_XDRScript succeeds on magic number mismatch but |
317 |
* returns false in *magic; it reflects a match via a true *magic out param. |
318 |
* If magic is null, js_XDRScript returns false on bad magic number errors, |
319 |
* which it reports. |
320 |
* |
321 |
* NB: callers must call js_CallNewScriptHook after successful JSXDR_DECODE |
322 |
* and subsequent set-up of owning function or script object, if any. |
323 |
*/ |
324 |
extern JSBool |
325 |
js_XDRScript(JSXDRState *xdr, JSScript **scriptp, JSBool *magic); |
326 |
|
327 |
JS_END_EXTERN_C |
328 |
|
329 |
#endif /* jsscript_h___ */ |