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 |
|
49 |
JS_BEGIN_EXTERN_C |
50 |
|
51 |
/* |
52 |
* Type of try note associated with each catch or finally block, and also with |
53 |
* for-in loops. |
54 |
*/ |
55 |
typedef enum JSTryNoteKind { |
56 |
JSTRY_CATCH, |
57 |
JSTRY_FINALLY, |
58 |
JSTRY_ITER |
59 |
} JSTryNoteKind; |
60 |
|
61 |
/* |
62 |
* Exception handling record. |
63 |
*/ |
64 |
struct JSTryNote { |
65 |
uint8 kind; /* one of JSTryNoteKind */ |
66 |
uint8 padding; /* explicit padding on uint16 boundary */ |
67 |
uint16 stackDepth; /* stack depth upon exception handler entry */ |
68 |
uint32 start; /* start of the try statement or for-in loop |
69 |
relative to script->main */ |
70 |
uint32 length; /* length of the try statement or for-in loop */ |
71 |
}; |
72 |
|
73 |
typedef struct JSTryNoteArray { |
74 |
JSTryNote *vector; /* array of indexed try notes */ |
75 |
uint32 length; /* count of indexed try notes */ |
76 |
} JSTryNoteArray; |
77 |
|
78 |
typedef struct JSObjectArray { |
79 |
JSObject **vector; /* array of indexed objects */ |
80 |
uint32 length; /* count of indexed objects */ |
81 |
} JSObjectArray; |
82 |
|
83 |
typedef struct JSUpvarArray { |
84 |
uint32 *vector; /* array of indexed upvar cookies */ |
85 |
uint32 length; /* count of indexed upvar cookies */ |
86 |
} JSUpvarArray; |
87 |
|
88 |
#define MAKE_UPVAR_COOKIE(skip,slot) ((skip) << 16 | (slot)) |
89 |
#define UPVAR_FRAME_SKIP(cookie) ((uint32)(cookie) >> 16) |
90 |
#define UPVAR_FRAME_SLOT(cookie) ((uint16)(cookie)) |
91 |
|
92 |
#define JS_OBJECT_ARRAY_SIZE(length) \ |
93 |
(offsetof(JSObjectArray, vector) + sizeof(JSObject *) * (length)) |
94 |
|
95 |
#if defined DEBUG && defined JS_THREADSAFE |
96 |
# define CHECK_SCRIPT_OWNER 1 |
97 |
#endif |
98 |
|
99 |
struct JSScript { |
100 |
jsbytecode *code; /* bytecodes and their immediate operands */ |
101 |
uint32 length; /* length of code vector */ |
102 |
uint16 version; /* JS version under which script was compiled */ |
103 |
uint16 nfixed; /* number of slots besides stack operands in |
104 |
slot array */ |
105 |
uint8 objectsOffset; /* offset to the array of nested function, |
106 |
block, scope, xml and one-time regexps |
107 |
objects or 0 if none */ |
108 |
uint8 upvarsOffset; /* offset of the array of display ("up") |
109 |
closure vars or 0 if none */ |
110 |
uint8 regexpsOffset; /* offset to the array of to-be-cloned |
111 |
regexps or 0 if none. */ |
112 |
uint8 trynotesOffset; /* offset to the array of try notes or |
113 |
0 if none */ |
114 |
uint8 flags; /* see below */ |
115 |
jsbytecode *main; /* main entry point, after predef'ing prolog */ |
116 |
JSAtomMap atomMap; /* maps immediate index to literal struct */ |
117 |
const char *filename; /* source filename or null */ |
118 |
uint32 lineno; /* base line number of script */ |
119 |
uint16 nslots; /* vars plus maximum stack depth */ |
120 |
uint16 staticDepth;/* static depth for display maintenance */ |
121 |
JSPrincipals *principals;/* principals for this script */ |
122 |
union { |
123 |
JSObject *object; /* optional Script-class object wrapper */ |
124 |
JSScript *nextToGC; /* next to GC in rt->scriptsToGC list */ |
125 |
} u; |
126 |
#ifdef CHECK_SCRIPT_OWNER |
127 |
JSThread *owner; /* for thread-safe life-cycle assertions */ |
128 |
#endif |
129 |
}; |
130 |
|
131 |
#define JSSF_NO_SCRIPT_RVAL 0x01 /* no need for result value of last |
132 |
expression statement */ |
133 |
|
134 |
static JS_INLINE uintN |
135 |
StackDepth(JSScript *script) |
136 |
{ |
137 |
return script->nslots - script->nfixed; |
138 |
} |
139 |
|
140 |
/* No need to store script->notes now that it is allocated right after code. */ |
141 |
#define SCRIPT_NOTES(script) ((jssrcnote*)((script)->code+(script)->length)) |
142 |
|
143 |
#define JS_SCRIPT_OBJECTS(script) \ |
144 |
(JS_ASSERT((script)->objectsOffset != 0), \ |
145 |
(JSObjectArray *)((uint8 *)(script) + (script)->objectsOffset)) |
146 |
|
147 |
#define JS_SCRIPT_UPVARS(script) \ |
148 |
(JS_ASSERT((script)->upvarsOffset != 0), \ |
149 |
(JSUpvarArray *)((uint8 *)(script) + (script)->upvarsOffset)) |
150 |
|
151 |
#define JS_SCRIPT_REGEXPS(script) \ |
152 |
(JS_ASSERT((script)->regexpsOffset != 0), \ |
153 |
(JSObjectArray *)((uint8 *)(script) + (script)->regexpsOffset)) |
154 |
|
155 |
#define JS_SCRIPT_TRYNOTES(script) \ |
156 |
(JS_ASSERT((script)->trynotesOffset != 0), \ |
157 |
(JSTryNoteArray *)((uint8 *)(script) + (script)->trynotesOffset)) |
158 |
|
159 |
#define JS_GET_SCRIPT_ATOM(script_, index, atom) \ |
160 |
JS_BEGIN_MACRO \ |
161 |
if (cx->fp && cx->fp->imacpc && cx->fp->script == script_) { \ |
162 |
JS_ASSERT((size_t)(index) < js_common_atom_count); \ |
163 |
(atom) = COMMON_ATOMS_START(&cx->runtime->atomState)[index]; \ |
164 |
} else { \ |
165 |
JSAtomMap *atoms_ = &(script_)->atomMap; \ |
166 |
JS_ASSERT((uint32)(index) < atoms_->length); \ |
167 |
(atom) = atoms_->vector[index]; \ |
168 |
} \ |
169 |
JS_END_MACRO |
170 |
|
171 |
#define JS_GET_SCRIPT_OBJECT(script, index, obj) \ |
172 |
JS_BEGIN_MACRO \ |
173 |
JSObjectArray *objects_ = JS_SCRIPT_OBJECTS(script); \ |
174 |
JS_ASSERT((uint32)(index) < objects_->length); \ |
175 |
(obj) = objects_->vector[index]; \ |
176 |
JS_END_MACRO |
177 |
|
178 |
#define JS_GET_SCRIPT_FUNCTION(script, index, fun) \ |
179 |
JS_BEGIN_MACRO \ |
180 |
JSObject *funobj_; \ |
181 |
\ |
182 |
JS_GET_SCRIPT_OBJECT(script, index, funobj_); \ |
183 |
JS_ASSERT(HAS_FUNCTION_CLASS(funobj_)); \ |
184 |
JS_ASSERT(funobj_ == (JSObject *) STOBJ_GET_PRIVATE(funobj_)); \ |
185 |
(fun) = (JSFunction *) funobj_; \ |
186 |
JS_ASSERT(FUN_INTERPRETED(fun)); \ |
187 |
JS_END_MACRO |
188 |
|
189 |
#define JS_GET_SCRIPT_REGEXP(script, index, obj) \ |
190 |
JS_BEGIN_MACRO \ |
191 |
JSObjectArray *regexps_ = JS_SCRIPT_REGEXPS(script); \ |
192 |
JS_ASSERT((uint32)(index) < regexps_->length); \ |
193 |
(obj) = regexps_->vector[index]; \ |
194 |
JS_ASSERT(STOBJ_GET_CLASS(obj) == &js_RegExpClass); \ |
195 |
JS_END_MACRO |
196 |
|
197 |
/* |
198 |
* Check if pc is inside a try block that has finally code. GC calls this to |
199 |
* check if it is necessary to schedule generator.close() invocation for an |
200 |
* unreachable generator. |
201 |
*/ |
202 |
JSBool |
203 |
js_IsInsideTryWithFinally(JSScript *script, jsbytecode *pc); |
204 |
|
205 |
extern JS_FRIEND_DATA(JSClass) js_ScriptClass; |
206 |
|
207 |
extern JSObject * |
208 |
js_InitScriptClass(JSContext *cx, JSObject *obj); |
209 |
|
210 |
/* |
211 |
* On first new context in rt, initialize script runtime state, specifically |
212 |
* the script filename table and its lock. |
213 |
*/ |
214 |
extern JSBool |
215 |
js_InitRuntimeScriptState(JSRuntime *rt); |
216 |
|
217 |
/* |
218 |
* On last context destroy for rt, if script filenames are all GC'd, free the |
219 |
* script filename table and its lock. |
220 |
*/ |
221 |
extern void |
222 |
js_FinishRuntimeScriptState(JSRuntime *rt); |
223 |
|
224 |
/* |
225 |
* On JS_DestroyRuntime(rt), forcibly free script filename prefixes and any |
226 |
* script filename table entries that have not been GC'd, the latter using |
227 |
* js_FinishRuntimeScriptState. |
228 |
* |
229 |
* This allows script filename prefixes to outlive any context in rt. |
230 |
*/ |
231 |
extern void |
232 |
js_FreeRuntimeScriptState(JSRuntime *rt); |
233 |
|
234 |
extern const char * |
235 |
js_SaveScriptFilename(JSContext *cx, const char *filename); |
236 |
|
237 |
extern const char * |
238 |
js_SaveScriptFilenameRT(JSRuntime *rt, const char *filename, uint32 flags); |
239 |
|
240 |
extern uint32 |
241 |
js_GetScriptFilenameFlags(const char *filename); |
242 |
|
243 |
extern void |
244 |
js_MarkScriptFilename(const char *filename); |
245 |
|
246 |
extern void |
247 |
js_MarkScriptFilenames(JSRuntime *rt, JSBool keepAtoms); |
248 |
|
249 |
extern void |
250 |
js_SweepScriptFilenames(JSRuntime *rt); |
251 |
|
252 |
/* |
253 |
* Two successively less primitive ways to make a new JSScript. The first |
254 |
* does *not* call a non-null cx->runtime->newScriptHook -- only the second, |
255 |
* js_NewScriptFromCG, calls this optional debugger hook. |
256 |
* |
257 |
* The js_NewScript function can't know whether the script it creates belongs |
258 |
* to a function, or is top-level or eval code, but the debugger wants access |
259 |
* to the newly made script's function, if any -- so callers of js_NewScript |
260 |
* are responsible for notifying the debugger after successfully creating any |
261 |
* kind (function or other) of new JSScript. |
262 |
*/ |
263 |
extern JSScript * |
264 |
js_NewScript(JSContext *cx, uint32 length, uint32 nsrcnotes, uint32 natoms, |
265 |
uint32 nobjects, uint32 nupvars, uint32 nregexps, |
266 |
uint32 ntrynotes); |
267 |
|
268 |
extern JSScript * |
269 |
js_NewScriptFromCG(JSContext *cx, JSCodeGenerator *cg); |
270 |
|
271 |
/* |
272 |
* New-script-hook calling is factored from js_NewScriptFromCG so that it |
273 |
* and callers of js_XDRScript can share this code. In the case of callers |
274 |
* of js_XDRScript, the hook should be invoked only after successful decode |
275 |
* of any owning function (the fun parameter) or script object (null fun). |
276 |
*/ |
277 |
extern JS_FRIEND_API(void) |
278 |
js_CallNewScriptHook(JSContext *cx, JSScript *script, JSFunction *fun); |
279 |
|
280 |
extern JS_FRIEND_API(void) |
281 |
js_CallDestroyScriptHook(JSContext *cx, JSScript *script); |
282 |
|
283 |
extern void |
284 |
js_DestroyScript(JSContext *cx, JSScript *script); |
285 |
|
286 |
extern void |
287 |
js_TraceScript(JSTracer *trc, JSScript *script); |
288 |
|
289 |
/* |
290 |
* To perturb as little code as possible, we introduce a js_GetSrcNote lookup |
291 |
* cache without adding an explicit cx parameter. Thus js_GetSrcNote becomes |
292 |
* a macro that uses cx from its calls' lexical environments. |
293 |
*/ |
294 |
#define js_GetSrcNote(script,pc) js_GetSrcNoteCached(cx, script, pc) |
295 |
|
296 |
extern jssrcnote * |
297 |
js_GetSrcNoteCached(JSContext *cx, JSScript *script, jsbytecode *pc); |
298 |
|
299 |
/* |
300 |
* NOTE: use js_FramePCToLineNumber(cx, fp) when you have an active fp, in |
301 |
* preference to js_PCToLineNumber (cx, fp->script fp->regs->pc), because |
302 |
* fp->imacpc may be non-null, indicating an active imacro. |
303 |
*/ |
304 |
extern uintN |
305 |
js_FramePCToLineNumber(JSContext *cx, JSStackFrame *fp); |
306 |
|
307 |
extern uintN |
308 |
js_PCToLineNumber(JSContext *cx, JSScript *script, jsbytecode *pc); |
309 |
|
310 |
extern jsbytecode * |
311 |
js_LineNumberToPC(JSScript *script, uintN lineno); |
312 |
|
313 |
extern JS_FRIEND_API(uintN) |
314 |
js_GetScriptLineExtent(JSScript *script); |
315 |
|
316 |
/* |
317 |
* If magic is non-null, js_XDRScript succeeds on magic number mismatch but |
318 |
* returns false in *magic; it reflects a match via a true *magic out param. |
319 |
* If magic is null, js_XDRScript returns false on bad magic number errors, |
320 |
* which it reports. |
321 |
* |
322 |
* NB: callers must call js_CallNewScriptHook after successful JSXDR_DECODE |
323 |
* and subsequent set-up of owning function or script object, if any. |
324 |
*/ |
325 |
extern JSBool |
326 |
js_XDRScript(JSXDRState *xdr, JSScript **scriptp, JSBool *magic); |
327 |
|
328 |
JS_END_EXTERN_C |
329 |
|
330 |
#endif /* jsscript_h___ */ |