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 |
|
135 |
#define JSSF_NO_SCRIPT_RVAL 0x01 /* no need for result value of last |
136 |
expression statement */ |
137 |
#define JSSF_SAVED_CALLER_FUN 0x02 /* object 0 is caller function */ |
138 |
|
139 |
static JS_INLINE uintN |
140 |
StackDepth(JSScript *script) |
141 |
{ |
142 |
return script->nslots - script->nfixed; |
143 |
} |
144 |
|
145 |
/* No need to store script->notes now that it is allocated right after code. */ |
146 |
#define SCRIPT_NOTES(script) ((jssrcnote*)((script)->code+(script)->length)) |
147 |
|
148 |
#define JS_SCRIPT_OBJECTS(script) \ |
149 |
(JS_ASSERT((script)->objectsOffset != 0), \ |
150 |
(JSObjectArray *)((uint8 *)(script) + (script)->objectsOffset)) |
151 |
|
152 |
#define JS_SCRIPT_UPVARS(script) \ |
153 |
(JS_ASSERT((script)->upvarsOffset != 0), \ |
154 |
(JSUpvarArray *)((uint8 *)(script) + (script)->upvarsOffset)) |
155 |
|
156 |
#define JS_SCRIPT_REGEXPS(script) \ |
157 |
(JS_ASSERT((script)->regexpsOffset != 0), \ |
158 |
(JSObjectArray *)((uint8 *)(script) + (script)->regexpsOffset)) |
159 |
|
160 |
#define JS_SCRIPT_TRYNOTES(script) \ |
161 |
(JS_ASSERT((script)->trynotesOffset != 0), \ |
162 |
(JSTryNoteArray *)((uint8 *)(script) + (script)->trynotesOffset)) |
163 |
|
164 |
#define JS_GET_SCRIPT_ATOM(script_, index, atom) \ |
165 |
JS_BEGIN_MACRO \ |
166 |
JSStackFrame *fp_ = js_GetTopStackFrame(cx); \ |
167 |
if (fp_ && fp_->imacpc && fp_->script == script_) { \ |
168 |
JS_ASSERT((size_t)(index) < js_common_atom_count); \ |
169 |
(atom) = COMMON_ATOMS_START(&cx->runtime->atomState)[index]; \ |
170 |
} else { \ |
171 |
JSAtomMap *atoms_ = &(script_)->atomMap; \ |
172 |
JS_ASSERT((uint32)(index) < atoms_->length); \ |
173 |
(atom) = atoms_->vector[index]; \ |
174 |
} \ |
175 |
JS_END_MACRO |
176 |
|
177 |
#define JS_GET_SCRIPT_OBJECT(script, index, obj) \ |
178 |
JS_BEGIN_MACRO \ |
179 |
JSObjectArray *objects_ = JS_SCRIPT_OBJECTS(script); \ |
180 |
JS_ASSERT((uint32)(index) < objects_->length); \ |
181 |
(obj) = objects_->vector[index]; \ |
182 |
JS_END_MACRO |
183 |
|
184 |
#define JS_GET_SCRIPT_FUNCTION(script, index, fun) \ |
185 |
JS_BEGIN_MACRO \ |
186 |
JSObject *funobj_; \ |
187 |
\ |
188 |
JS_GET_SCRIPT_OBJECT(script, index, funobj_); \ |
189 |
JS_ASSERT(HAS_FUNCTION_CLASS(funobj_)); \ |
190 |
JS_ASSERT(funobj_ == (JSObject *) STOBJ_GET_PRIVATE(funobj_)); \ |
191 |
(fun) = (JSFunction *) funobj_; \ |
192 |
JS_ASSERT(FUN_INTERPRETED(fun)); \ |
193 |
JS_END_MACRO |
194 |
|
195 |
#define JS_GET_SCRIPT_REGEXP(script, index, obj) \ |
196 |
JS_BEGIN_MACRO \ |
197 |
JSObjectArray *regexps_ = JS_SCRIPT_REGEXPS(script); \ |
198 |
JS_ASSERT((uint32)(index) < regexps_->length); \ |
199 |
(obj) = regexps_->vector[index]; \ |
200 |
JS_ASSERT(STOBJ_GET_CLASS(obj) == &js_RegExpClass); \ |
201 |
JS_END_MACRO |
202 |
|
203 |
/* |
204 |
* Check if pc is inside a try block that has finally code. GC calls this to |
205 |
* check if it is necessary to schedule generator.close() invocation for an |
206 |
* unreachable generator. |
207 |
*/ |
208 |
JSBool |
209 |
js_IsInsideTryWithFinally(JSScript *script, jsbytecode *pc); |
210 |
|
211 |
extern JS_FRIEND_DATA(JSClass) js_ScriptClass; |
212 |
|
213 |
extern JSObject * |
214 |
js_InitScriptClass(JSContext *cx, JSObject *obj); |
215 |
|
216 |
/* |
217 |
* On first new context in rt, initialize script runtime state, specifically |
218 |
* the script filename table and its lock. |
219 |
*/ |
220 |
extern JSBool |
221 |
js_InitRuntimeScriptState(JSRuntime *rt); |
222 |
|
223 |
/* |
224 |
* On last context destroy for rt, if script filenames are all GC'd, free the |
225 |
* script filename table and its lock. |
226 |
*/ |
227 |
extern void |
228 |
js_FinishRuntimeScriptState(JSRuntime *rt); |
229 |
|
230 |
/* |
231 |
* On JS_DestroyRuntime(rt), forcibly free script filename prefixes and any |
232 |
* script filename table entries that have not been GC'd, the latter using |
233 |
* js_FinishRuntimeScriptState. |
234 |
* |
235 |
* This allows script filename prefixes to outlive any context in rt. |
236 |
*/ |
237 |
extern void |
238 |
js_FreeRuntimeScriptState(JSRuntime *rt); |
239 |
|
240 |
extern const char * |
241 |
js_SaveScriptFilename(JSContext *cx, const char *filename); |
242 |
|
243 |
extern const char * |
244 |
js_SaveScriptFilenameRT(JSRuntime *rt, const char *filename, uint32 flags); |
245 |
|
246 |
extern uint32 |
247 |
js_GetScriptFilenameFlags(const char *filename); |
248 |
|
249 |
extern void |
250 |
js_MarkScriptFilename(const char *filename); |
251 |
|
252 |
extern void |
253 |
js_MarkScriptFilenames(JSRuntime *rt, JSBool keepAtoms); |
254 |
|
255 |
extern void |
256 |
js_SweepScriptFilenames(JSRuntime *rt); |
257 |
|
258 |
/* |
259 |
* Two successively less primitive ways to make a new JSScript. The first |
260 |
* does *not* call a non-null cx->runtime->newScriptHook -- only the second, |
261 |
* js_NewScriptFromCG, calls this optional debugger hook. |
262 |
* |
263 |
* The js_NewScript function can't know whether the script it creates belongs |
264 |
* to a function, or is top-level or eval code, but the debugger wants access |
265 |
* to the newly made script's function, if any -- so callers of js_NewScript |
266 |
* are responsible for notifying the debugger after successfully creating any |
267 |
* kind (function or other) of new JSScript. |
268 |
*/ |
269 |
extern JSScript * |
270 |
js_NewScript(JSContext *cx, uint32 length, uint32 nsrcnotes, uint32 natoms, |
271 |
uint32 nobjects, uint32 nupvars, uint32 nregexps, |
272 |
uint32 ntrynotes); |
273 |
|
274 |
extern JSScript * |
275 |
js_NewScriptFromCG(JSContext *cx, JSCodeGenerator *cg); |
276 |
|
277 |
/* |
278 |
* New-script-hook calling is factored from js_NewScriptFromCG so that it |
279 |
* and callers of js_XDRScript can share this code. In the case of callers |
280 |
* of js_XDRScript, the hook should be invoked only after successful decode |
281 |
* of any owning function (the fun parameter) or script object (null fun). |
282 |
*/ |
283 |
extern JS_FRIEND_API(void) |
284 |
js_CallNewScriptHook(JSContext *cx, JSScript *script, JSFunction *fun); |
285 |
|
286 |
extern JS_FRIEND_API(void) |
287 |
js_CallDestroyScriptHook(JSContext *cx, JSScript *script); |
288 |
|
289 |
extern void |
290 |
js_DestroyScript(JSContext *cx, JSScript *script); |
291 |
|
292 |
extern void |
293 |
js_TraceScript(JSTracer *trc, JSScript *script); |
294 |
|
295 |
/* |
296 |
* To perturb as little code as possible, we introduce a js_GetSrcNote lookup |
297 |
* cache without adding an explicit cx parameter. Thus js_GetSrcNote becomes |
298 |
* a macro that uses cx from its calls' lexical environments. |
299 |
*/ |
300 |
#define js_GetSrcNote(script,pc) js_GetSrcNoteCached(cx, script, pc) |
301 |
|
302 |
extern jssrcnote * |
303 |
js_GetSrcNoteCached(JSContext *cx, JSScript *script, jsbytecode *pc); |
304 |
|
305 |
/* |
306 |
* NOTE: use js_FramePCToLineNumber(cx, fp) when you have an active fp, in |
307 |
* preference to js_PCToLineNumber (cx, fp->script fp->regs->pc), because |
308 |
* fp->imacpc may be non-null, indicating an active imacro. |
309 |
*/ |
310 |
extern uintN |
311 |
js_FramePCToLineNumber(JSContext *cx, JSStackFrame *fp); |
312 |
|
313 |
extern uintN |
314 |
js_PCToLineNumber(JSContext *cx, JSScript *script, jsbytecode *pc); |
315 |
|
316 |
extern jsbytecode * |
317 |
js_LineNumberToPC(JSScript *script, uintN lineno); |
318 |
|
319 |
extern JS_FRIEND_API(uintN) |
320 |
js_GetScriptLineExtent(JSScript *script); |
321 |
|
322 |
static JS_INLINE JSOp |
323 |
js_GetOpcode(JSContext *cx, JSScript *script, jsbytecode *pc) |
324 |
{ |
325 |
JSOp op = (JSOp) *pc; |
326 |
if (op == JSOP_TRAP) |
327 |
op = JS_GetTrapOpcode(cx, script, pc); |
328 |
return op; |
329 |
} |
330 |
|
331 |
/* |
332 |
* If magic is non-null, js_XDRScript succeeds on magic number mismatch but |
333 |
* returns false in *magic; it reflects a match via a true *magic out param. |
334 |
* If magic is null, js_XDRScript returns false on bad magic number errors, |
335 |
* which it reports. |
336 |
* |
337 |
* NB: callers must call js_CallNewScriptHook after successful JSXDR_DECODE |
338 |
* and subsequent set-up of owning function or script object, if any. |
339 |
*/ |
340 |
extern JSBool |
341 |
js_XDRScript(JSXDRState *xdr, JSScript **scriptp, JSBool *magic); |
342 |
|
343 |
JS_END_EXTERN_C |
344 |
|
345 |
#endif /* jsscript_h___ */ |