Back to index

lightning-sunbird  0.9+nobinonly
jsscript.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * vim: set ts=8 sw=4 et tw=78:
00003  *
00004  * ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is Mozilla Communicator client code, released
00018  * March 31, 1998.
00019  *
00020  * The Initial Developer of the Original Code is
00021  * Netscape Communications Corporation.
00022  * Portions created by the Initial Developer are Copyright (C) 1998
00023  * the Initial Developer. All Rights Reserved.
00024  *
00025  * Contributor(s):
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #ifndef jsscript_h___
00042 #define jsscript_h___
00043 /*
00044  * JS script descriptor.
00045  */
00046 #include "jsatom.h"
00047 #include "jsprvtd.h"
00048 
00049 JS_BEGIN_EXTERN_C
00050 
00051 /*
00052  * Exception handling runtime information.
00053  *
00054  * All fields except length are code offsets relative to the main entry point
00055  * of the script.  If script->trynotes is not null, it points to a vector of
00056  * these structs terminated by one with catchStart == 0.
00057  */
00058 struct JSTryNote {
00059     ptrdiff_t    start;         /* start of try statement */
00060     ptrdiff_t    length;        /* count of try statement bytecodes */
00061     ptrdiff_t    catchStart;    /* start of catch block (0 if end) */
00062 };
00063 
00064 #define JSTRYNOTE_GRAIN         sizeof(ptrdiff_t)
00065 #define JSTRYNOTE_ALIGNMASK     (JSTRYNOTE_GRAIN - 1)
00066 
00067 struct JSScript {
00068     jsbytecode   *code;         /* bytecodes and their immediate operands */
00069     uint32       length;        /* length of code vector */
00070     jsbytecode   *main;         /* main entry point, after predef'ing prolog */
00071     uint16       version;       /* JS version under which script was compiled */
00072     uint16       numGlobalVars; /* declared global var/const/function count */
00073     JSAtomMap    atomMap;       /* maps immediate index to literal struct */
00074     const char   *filename;     /* source filename or null */
00075     uintN        lineno;        /* base line number of script */
00076     uintN        depth;         /* maximum stack depth in slots */
00077     JSTryNote    *trynotes;     /* exception table for this script */
00078     JSPrincipals *principals;   /* principals for this script */
00079     JSObject     *object;       /* optional Script-class object wrapper */
00080 };
00081 
00082 /* No need to store script->notes now that it is allocated right after code. */
00083 #define SCRIPT_NOTES(script)    ((jssrcnote*)((script)->code+(script)->length))
00084 
00085 #define SCRIPT_FIND_CATCH_START(script, pc, catchpc)                          \
00086     JS_BEGIN_MACRO                                                            \
00087         JSTryNote *tn_ = (script)->trynotes;                                  \
00088         jsbytecode *catchpc_ = NULL;                                          \
00089         if (tn_) {                                                            \
00090             ptrdiff_t off_ = PTRDIFF(pc, (script)->main, jsbytecode);         \
00091             if (off_ >= 0) {                                                  \
00092                 while ((jsuword)(off_ - tn_->start) >= (jsuword)tn_->length)  \
00093                     ++tn_;                                                    \
00094                 if (tn_->catchStart)                                          \
00095                     catchpc_ = (script)->main + tn_->catchStart;              \
00096             }                                                                 \
00097         }                                                                     \
00098         catchpc = catchpc_;                                                   \
00099     JS_END_MACRO
00100 
00101 /*
00102  * Find the innermost finally block that handles the given pc. This is a
00103  * version of SCRIPT_FIND_CATCH_START that ignore catch blocks and is used
00104  * to implement generator.close().
00105  */
00106 jsbytecode *
00107 js_FindFinallyHandler(JSScript *script, jsbytecode *pc);
00108 
00109 extern JS_FRIEND_DATA(JSClass) js_ScriptClass;
00110 
00111 extern JSObject *
00112 js_InitScriptClass(JSContext *cx, JSObject *obj);
00113 
00114 /*
00115  * On first new context in rt, initialize script runtime state, specifically
00116  * the script filename table and its lock.
00117  */
00118 extern JSBool
00119 js_InitRuntimeScriptState(JSRuntime *rt);
00120 
00121 /*
00122  * On last context destroy for rt, if script filenames are all GC'd, free the
00123  * script filename table and its lock.
00124  */
00125 extern void
00126 js_FinishRuntimeScriptState(JSRuntime *rt);
00127 
00128 /*
00129  * On JS_DestroyRuntime(rt), forcibly free script filename prefixes and any
00130  * script filename table entries that have not been GC'd, the latter using
00131  * js_FinishRuntimeScriptState.
00132  *
00133  * This allows script filename prefixes to outlive any context in rt.
00134  */
00135 extern void
00136 js_FreeRuntimeScriptState(JSRuntime *rt);
00137 
00138 extern const char *
00139 js_SaveScriptFilename(JSContext *cx, const char *filename);
00140 
00141 extern const char *
00142 js_SaveScriptFilenameRT(JSRuntime *rt, const char *filename, uint32 flags);
00143 
00144 extern uint32
00145 js_GetScriptFilenameFlags(const char *filename);
00146 
00147 extern void
00148 js_MarkScriptFilename(const char *filename);
00149 
00150 extern void
00151 js_MarkScriptFilenames(JSRuntime *rt, JSBool keepAtoms);
00152 
00153 extern void
00154 js_SweepScriptFilenames(JSRuntime *rt);
00155 
00156 /*
00157  * Two successively less primitive ways to make a new JSScript.  The first
00158  * does *not* call a non-null cx->runtime->newScriptHook -- only the second,
00159  * js_NewScriptFromCG, calls this optional debugger hook.
00160  *
00161  * The js_NewScript function can't know whether the script it creates belongs
00162  * to a function, or is top-level or eval code, but the debugger wants access
00163  * to the newly made script's function, if any -- so callers of js_NewScript
00164  * are responsible for notifying the debugger after successfully creating any
00165  * kind (function or other) of new JSScript.
00166  */
00167 extern JSScript *
00168 js_NewScript(JSContext *cx, uint32 length, uint32 snlength, uint32 tnlength);
00169 
00170 extern JS_FRIEND_API(JSScript *)
00171 js_NewScriptFromCG(JSContext *cx, JSCodeGenerator *cg, JSFunction *fun);
00172 
00173 /*
00174  * New-script-hook calling is factored from js_NewScriptFromCG so that it
00175  * and callers of js_XDRScript can share this code.  In the case of callers
00176  * of js_XDRScript, the hook should be invoked only after successful decode
00177  * of any owning function (the fun parameter) or script object (null fun).
00178  */
00179 extern JS_FRIEND_API(void)
00180 js_CallNewScriptHook(JSContext *cx, JSScript *script, JSFunction *fun);
00181 
00182 extern JS_FRIEND_API(void)
00183 js_CallDestroyScriptHook(JSContext *cx, JSScript *script);
00184 
00185 extern void
00186 js_DestroyScript(JSContext *cx, JSScript *script);
00187 
00188 extern void
00189 js_MarkScript(JSContext *cx, JSScript *script);
00190 
00191 /*
00192  * To perturb as little code as possible, we introduce a js_GetSrcNote lookup
00193  * cache without adding an explicit cx parameter.  Thus js_GetSrcNote becomes
00194  * a macro that uses cx from its calls' lexical environments.
00195  */
00196 #define js_GetSrcNote(script,pc) js_GetSrcNoteCached(cx, script, pc)
00197 
00198 extern jssrcnote *
00199 js_GetSrcNoteCached(JSContext *cx, JSScript *script, jsbytecode *pc);
00200 
00201 /* XXX need cx to lock function objects declared by prolog bytecodes. */
00202 extern uintN
00203 js_PCToLineNumber(JSContext *cx, JSScript *script, jsbytecode *pc);
00204 
00205 extern jsbytecode *
00206 js_LineNumberToPC(JSScript *script, uintN lineno);
00207 
00208 extern JS_FRIEND_API(uintN)
00209 js_GetScriptLineExtent(JSScript *script);
00210 
00211 /*
00212  * If magic is non-null, js_XDRScript succeeds on magic number mismatch but
00213  * returns false in *magic; it reflects a match via a true *magic out param.
00214  * If magic is null, js_XDRScript returns false on bad magic number errors,
00215  * which it reports.
00216  *
00217  * NB: callers must call js_CallNewScriptHook after successful JSXDR_DECODE
00218  * and subsequent set-up of owning function or script object, if any.
00219  */
00220 extern JSBool
00221 js_XDRScript(JSXDRState *xdr, JSScript **scriptp, JSBool *magic);
00222 
00223 JS_END_EXTERN_C
00224 
00225 #endif /* jsscript_h___ */