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 beginning of
54 * 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 extern JSBool
106 js_InitRuntimeScriptState(JSContext *cx);
108 extern void
109 js_FinishRuntimeScriptState(JSContext *cx);
111 extern const char *
112 js_SaveScriptFilename(JSContext *cx, const char *filename);
114 extern void
115 js_MarkScriptFilename(const char *filename);
117 extern void
118 js_SweepScriptFilenames(JSRuntime *rt);
120 /*
121 * Two successively less primitive ways to make a new JSScript. The first
122 * does *not* call a non-null cx->runtime->newScriptHook -- only the second,
123 * js_NewScriptFromCG, calls this optional debugger hook.
124 *
125 * The js_NewScript function can't know whether the script it creates belongs
126 * to a function, or is top-level or eval code, but the debugger wants access
127 * to the newly made script's function, if any -- so callers of js_NewScript
128 * are responsible for notifying the debugger after successfully creating any
129 * kind (function or other) of new JSScript.
130 */
131 extern JSScript *
132 js_NewScript(JSContext *cx, uint32 length, uint32 snlength, uint32 tnlength);
134 extern JS_FRIEND_API(JSScript *)
135 js_NewScriptFromCG(JSContext *cx, JSCodeGenerator *cg, JSFunction *fun);
137 /*
138 * New-script-hook calling is factored from js_NewScriptFromCG so that it
139 * and callers of js_XDRScript can share this code. In the case of callers
140 * of js_XDRScript, the hook should be invoked only after successful decode
141 * of any owning function (the fun parameter) or script object (null fun).
142 */
143 extern JS_FRIEND_API(void)
144 js_CallNewScriptHook(JSContext *cx, JSScript *script, JSFunction *fun);
146 extern void
147 js_DestroyScript(JSContext *cx, JSScript *script);
149 extern void
150 js_MarkScript(JSContext *cx, JSScript *script, void *arg);
152 extern jssrcnote *
153 js_GetSrcNote(JSScript *script, jsbytecode *pc);
155 /* XXX need cx to lock function objects declared by prolog bytecodes. */
156 extern uintN
157 js_PCToLineNumber(JSContext *cx, JSScript *script, jsbytecode *pc);
159 extern jsbytecode *
160 js_LineNumberToPC(JSScript *script, uintN lineno);
162 extern uintN
163 js_GetScriptLineExtent(JSScript *script);
165 /*
166 * If magic is non-null, js_XDRScript succeeds on magic number mismatch but
167 * returns false in *magic; it reflects a match via a true *magic out param.
168 * If magic is null, js_XDRScript returns false on bad magic number errors,
169 * which it reports.
170 *
171 * NB: callers must call js_CallNewScriptHook after successful JSXDR_DECODE
172 * and subsequent set-up of owning function or script object, if any.
173 */
174 extern JSBool
175 js_XDRScript(JSXDRState *xdr, JSScript **scriptp, JSBool *magic);
177 JS_END_EXTERN_C
179 #endif /* jsscript_h___ */