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 |
JSTN_CATCH, |
57 |
JSTN_FINALLY, |
58 |
JSTN_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 |
JSAtomMap *atoms_ = &(script)->atomMap; \ |
162 |
JS_ASSERT((uint32)(index) < atoms_->length); \ |
163 |
(atom) = atoms_->vector[(index)]; \ |
164 |
JS_END_MACRO |
165 |
|
166 |
#define JS_GET_SCRIPT_OBJECT(script, index, obj) \ |
167 |
JS_BEGIN_MACRO \ |
168 |
JSObjectArray *objects_ = JS_SCRIPT_OBJECTS(script); \ |
169 |
JS_ASSERT((uint32)(index) < objects_->length); \ |
170 |
(obj) = objects_->vector[(index)]; \ |
171 |
JS_END_MACRO |
172 |
|
173 |
#define JS_GET_SCRIPT_FUNCTION(script, index, fun) \ |
174 |
JS_BEGIN_MACRO \ |
175 |
JSObject *funobj_; \ |
176 |
\ |
177 |
JS_GET_SCRIPT_OBJECT(script, index, funobj_); \ |
178 |
JS_ASSERT(HAS_FUNCTION_CLASS(funobj_)); \ |
179 |
JS_ASSERT(funobj_ == (JSObject *) STOBJ_GET_PRIVATE(funobj_)); \ |
180 |
(fun) = (JSFunction *) funobj_; \ |
181 |
JS_ASSERT(FUN_INTERPRETED(fun)); \ |
182 |
JS_END_MACRO |
183 |
|
184 |
#define JS_GET_SCRIPT_REGEXP(script, index, obj) \ |
185 |
JS_BEGIN_MACRO \ |
186 |
JSObjectArray *regexps_ = JS_SCRIPT_REGEXPS(script); \ |
187 |
JS_ASSERT((uint32)(index) < regexps_->length); \ |
188 |
(obj) = regexps_->vector[(index)]; \ |
189 |
JS_ASSERT(STOBJ_GET_CLASS(obj) == &js_RegExpClass); \ |
190 |
JS_END_MACRO |
191 |
|
192 |
/* |
193 |
* Check if pc is inside a try block that has finally code. GC calls this to |
194 |
* check if it is necessary to schedule generator.close() invocation for an |
195 |
* unreachable generator. |
196 |
*/ |
197 |
JSBool |
198 |
js_IsInsideTryWithFinally(JSScript *script, jsbytecode *pc); |
199 |
|
200 |
extern JS_FRIEND_DATA(JSClass) js_ScriptClass; |
201 |
|
202 |
extern JSObject * |
203 |
js_InitScriptClass(JSContext *cx, JSObject *obj); |
204 |
|
205 |
/* |
206 |
* On first new context in rt, initialize script runtime state, specifically |
207 |
* the script filename table and its lock. |
208 |
*/ |
209 |
extern JSBool |
210 |
js_InitRuntimeScriptState(JSRuntime *rt); |
211 |
|
212 |
/* |
213 |
* On last context destroy for rt, if script filenames are all GC'd, free the |
214 |
* script filename table and its lock. |
215 |
*/ |
216 |
extern void |
217 |
js_FinishRuntimeScriptState(JSRuntime *rt); |
218 |
|
219 |
/* |
220 |
* On JS_DestroyRuntime(rt), forcibly free script filename prefixes and any |
221 |
* script filename table entries that have not been GC'd, the latter using |
222 |
* js_FinishRuntimeScriptState. |
223 |
* |
224 |
* This allows script filename prefixes to outlive any context in rt. |
225 |
*/ |
226 |
extern void |
227 |
js_FreeRuntimeScriptState(JSRuntime *rt); |
228 |
|
229 |
extern const char * |
230 |
js_SaveScriptFilename(JSContext *cx, const char *filename); |
231 |
|
232 |
extern const char * |
233 |
js_SaveScriptFilenameRT(JSRuntime *rt, const char *filename, uint32 flags); |
234 |
|
235 |
extern uint32 |
236 |
js_GetScriptFilenameFlags(const char *filename); |
237 |
|
238 |
extern void |
239 |
js_MarkScriptFilename(const char *filename); |
240 |
|
241 |
extern void |
242 |
js_MarkScriptFilenames(JSRuntime *rt, JSBool keepAtoms); |
243 |
|
244 |
extern void |
245 |
js_SweepScriptFilenames(JSRuntime *rt); |
246 |
|
247 |
/* |
248 |
* Two successively less primitive ways to make a new JSScript. The first |
249 |
* does *not* call a non-null cx->runtime->newScriptHook -- only the second, |
250 |
* js_NewScriptFromCG, calls this optional debugger hook. |
251 |
* |
252 |
* The js_NewScript function can't know whether the script it creates belongs |
253 |
* to a function, or is top-level or eval code, but the debugger wants access |
254 |
* to the newly made script's function, if any -- so callers of js_NewScript |
255 |
* are responsible for notifying the debugger after successfully creating any |
256 |
* kind (function or other) of new JSScript. |
257 |
*/ |
258 |
extern JSScript * |
259 |
js_NewScript(JSContext *cx, uint32 length, uint32 nsrcnotes, uint32 natoms, |
260 |
uint32 nobjects, uint32 nupvars, uint32 nregexps, |
261 |
uint32 ntrynotes); |
262 |
|
263 |
extern JSScript * |
264 |
js_NewScriptFromCG(JSContext *cx, JSCodeGenerator *cg); |
265 |
|
266 |
/* |
267 |
* New-script-hook calling is factored from js_NewScriptFromCG so that it |
268 |
* and callers of js_XDRScript can share this code. In the case of callers |
269 |
* of js_XDRScript, the hook should be invoked only after successful decode |
270 |
* of any owning function (the fun parameter) or script object (null fun). |
271 |
*/ |
272 |
extern JS_FRIEND_API(void) |
273 |
js_CallNewScriptHook(JSContext *cx, JSScript *script, JSFunction *fun); |
274 |
|
275 |
extern JS_FRIEND_API(void) |
276 |
js_CallDestroyScriptHook(JSContext *cx, JSScript *script); |
277 |
|
278 |
extern void |
279 |
js_DestroyScript(JSContext *cx, JSScript *script); |
280 |
|
281 |
extern void |
282 |
js_TraceScript(JSTracer *trc, JSScript *script); |
283 |
|
284 |
/* |
285 |
* To perturb as little code as possible, we introduce a js_GetSrcNote lookup |
286 |
* cache without adding an explicit cx parameter. Thus js_GetSrcNote becomes |
287 |
* a macro that uses cx from its calls' lexical environments. |
288 |
*/ |
289 |
#define js_GetSrcNote(script,pc) js_GetSrcNoteCached(cx, script, pc) |
290 |
|
291 |
extern jssrcnote * |
292 |
js_GetSrcNoteCached(JSContext *cx, JSScript *script, jsbytecode *pc); |
293 |
|
294 |
/* XXX need cx to lock function objects declared by prolog bytecodes. */ |
295 |
extern uintN |
296 |
js_PCToLineNumber(JSContext *cx, JSScript *script, jsbytecode *pc); |
297 |
|
298 |
extern jsbytecode * |
299 |
js_LineNumberToPC(JSScript *script, uintN lineno); |
300 |
|
301 |
extern JS_FRIEND_API(uintN) |
302 |
js_GetScriptLineExtent(JSScript *script); |
303 |
|
304 |
/* |
305 |
* If magic is non-null, js_XDRScript succeeds on magic number mismatch but |
306 |
* returns false in *magic; it reflects a match via a true *magic out param. |
307 |
* If magic is null, js_XDRScript returns false on bad magic number errors, |
308 |
* which it reports. |
309 |
* |
310 |
* NB: callers must call js_CallNewScriptHook after successful JSXDR_DECODE |
311 |
* and subsequent set-up of owning function or script object, if any. |
312 |
*/ |
313 |
extern JSBool |
314 |
js_XDRScript(JSXDRState *xdr, JSScript **scriptp, JSBool *magic); |
315 |
|
316 |
JS_END_EXTERN_C |
317 |
|
318 |
#endif /* jsscript_h___ */ |