1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jsscript_h___
41 #define jsscript_h___
42 /*
43 * JS script descriptor.
44 */
45 #include "jsatom.h"
46 #include "jsprvtd.h"
48 JS_BEGIN_EXTERN_C
50 /*
51 * Exception handling runtime information.
52 *
53 * All fields except length are code offsets relative to the main entry point
54 * of the script. If script->trynotes is not null, it points to a vector of
55 * these structs terminated by one with catchStart == 0.
56 */
57 struct JSTryNote {
58 ptrdiff_t start; /* start of try statement */
59 ptrdiff_t length; /* count of try statement bytecodes */
60 ptrdiff_t catchStart; /* start of catch block (0 if end) */
61 };
63 #define JSTRYNOTE_GRAIN sizeof(ptrdiff_t)
64 #define JSTRYNOTE_ALIGNMASK (JSTRYNOTE_GRAIN - 1)
66 struct JSScript {
67 jsbytecode *code; /* bytecodes and their immediate operands */
68 uint32 length; /* length of code vector */
69 jsbytecode *main; /* main entry point, after predef'ing prolog */
70 uint16 version; /* JS version under which script was compiled */
71 uint16 numGlobalVars; /* declared global var/const/function count */
72 JSAtomMap atomMap; /* maps immediate index to literal struct */
73 const char *filename; /* source filename or null */
74 uintN lineno; /* base line number of script */
75 uintN depth; /* maximum stack depth in slots */
76 JSTryNote *trynotes; /* exception table for this script */
77 JSPrincipals *principals; /* principals for this script */
78 JSObject *object; /* optional Script-class object wrapper */
79 };
81 /* No need to store script->notes now that it is allocated right after code. */
82 #define SCRIPT_NOTES(script) ((jssrcnote*)((script)->code+(script)->length))
84 #define SCRIPT_FIND_CATCH_START(script, pc, catchpc) \
85 JS_BEGIN_MACRO \
86 JSTryNote *tn_ = (script)->trynotes; \
87 jsbytecode *catchpc_ = NULL; \
88 if (tn_) { \
89 ptrdiff_t off_ = PTRDIFF(pc, (script)->main, jsbytecode); \
90 if (off_ >= 0) { \
91 while ((jsuword)(off_ - tn_->start) >= (jsuword)tn_->length) \
92 ++tn_; \
93 if (tn_->catchStart) \
94 catchpc_ = (script)->main + tn_->catchStart; \
95 } \
96 } \
97 catchpc = catchpc_; \
98 JS_END_MACRO
100 extern JS_FRIEND_DATA(JSClass) js_ScriptClass;
102 extern JSObject *
103 js_InitScriptClass(JSContext *cx, JSObject *obj);
105 /*
106 * On first new context in rt, initialize script runtime state, specifically
107 * the script filename table and its lock.
108 */
109 extern JSBool
110 js_InitRuntimeScriptState(JSRuntime *rt);
112 /*
113 * On last context destroy for rt, if script filenames are all GC'd, free the
114 * script filename table and its lock.
115 */
116 extern void
117 js_FinishRuntimeScriptState(JSRuntime *rt);
119 /*
120 * On JS_DestroyRuntime(rt), forcibly free script filename prefixes and any
121 * script filename table entries that have not been GC'd, the latter using
122 * js_FinishRuntimeScriptState.
123 *
124 * This allows script filename prefixes to outlive any context in rt.
125 */
126 extern void
127 js_FreeRuntimeScriptState(JSRuntime *rt);
129 extern const char *
130 js_SaveScriptFilename(JSContext *cx, const char *filename);
132 extern const char *
133 js_SaveScriptFilenameRT(JSRuntime *rt, const char *filename, uint32 flags);
135 extern uint32
136 js_GetScriptFilenameFlags(const char *filename);
138 extern void
139 js_MarkScriptFilename(const char *filename);
141 extern void
142 js_MarkScriptFilenames(JSRuntime *rt, uintN gcflags);
144 extern void
145 js_SweepScriptFilenames(JSRuntime *rt);
147 /*
148 * Two successively less primitive ways to make a new JSScript. The first
149 * does *not* call a non-null cx->runtime->newScriptHook -- only the second,
150 * js_NewScriptFromCG, calls this optional debugger hook.
151 *
152 * The js_NewScript function can't know whether the script it creates belongs
153 * to a function, or is top-level or eval code, but the debugger wants access
154 * to the newly made script's function, if any -- so callers of js_NewScript
155 * are responsible for notifying the debugger after successfully creating any
156 * kind (function or other) of new JSScript.
157 */
158 extern JSScript *
159 js_NewScript(JSContext *cx, uint32 length, uint32 snlength, uint32 tnlength);
161 extern JS_FRIEND_API(JSScript *)
162 js_NewScriptFromCG(JSContext *cx, JSCodeGenerator *cg, JSFunction *fun);
164 /*
165 * New-script-hook calling is factored from js_NewScriptFromCG so that it
166 * and callers of js_XDRScript can share this code. In the case of callers
167 * of js_XDRScript, the hook should be invoked only after successful decode
168 * of any owning function (the fun parameter) or script object (null fun).
169 */
170 extern JS_FRIEND_API(void)
171 js_CallNewScriptHook(JSContext *cx, JSScript *script, JSFunction *fun);
173 extern JS_FRIEND_API(void)
174 js_CallDestroyScriptHook(JSContext *cx, JSScript *script);
176 extern void
177 js_DestroyScript(JSContext *cx, JSScript *script);
179 extern void
180 js_MarkScript(JSContext *cx, JSScript *script, void *arg);
182 extern jssrcnote *
183 js_GetSrcNote(JSScript *script, jsbytecode *pc);
185 /* XXX need cx to lock function objects declared by prolog bytecodes. */
186 extern uintN
187 js_PCToLineNumber(JSContext *cx, JSScript *script, jsbytecode *pc);
189 extern jsbytecode *
190 js_LineNumberToPC(JSScript *script, uintN lineno);
192 extern JS_FRIEND_API(uintN)
193 js_GetScriptLineExtent(JSScript *script);
195 /*
196 * If magic is non-null, js_XDRScript succeeds on magic number mismatch but
197 * returns false in *magic; it reflects a match via a true *magic out param.
198 * If magic is null, js_XDRScript returns false on bad magic number errors,
199 * which it reports.
200 *
201 * NB: callers must call js_CallNewScriptHook after successful JSXDR_DECODE
202 * and subsequent set-up of owning function or script object, if any.
203 */
204 extern JSBool
205 js_XDRScript(JSXDRState *xdr, JSScript **scriptp, JSBool *magic);
207 JS_END_EXTERN_C
209 #endif /* jsscript_h___ */