Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / dom / base / nsIScriptGlobalObject.h
blobe9063592faf9049bf65d393f23836abf80cedf08
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=80: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsIScriptGlobalObject_h__
40 #define nsIScriptGlobalObject_h__
42 #include "nsISupports.h"
43 #include "nsEvent.h"
44 #include "nsIProgrammingLanguage.h"
46 class nsIScriptContext;
47 class nsIDOMDocument;
48 class nsIDOMEvent;
49 class nsIScriptGlobalObjectOwner;
50 class nsIArray;
51 class nsScriptErrorEvent;
52 class nsIScriptGlobalObject;
53 struct JSObject; // until we finally remove GetGlobalJSObject...
55 // Some helpers for working with integer "script type IDs", and specifically
56 // for working with arrays of such objects. For example, it is common for
57 // implementations supporting multiple script languages to keep each
58 // language's nsIScriptContext in an array indexed by the language ID.
60 // Implementation note: We always ignore nsIProgrammingLanguage::UNKNOWN and
61 // nsIProgrammingLanguage::CPLUSPLUS - this gives javascript slot 0. An
62 // attempted micro-optimization tried to avoid us going all the way to
63 // nsIProgrammingLanguage::MAX; however:
64 // * Someone is reportedly working on a PHP impl - that has value 9
65 // * nsGenericElement therefore allows 4 bits for the value.
66 // So there is no good reason for us to be more restrictive again...
68 #define NS_STID_FIRST nsIProgrammingLanguage::JAVASCRIPT
69 // like nsGenericElement, only 4 bits worth is valid...
70 #define NS_STID_LAST (nsIProgrammingLanguage::MAX > 0x000FU ? \
71 0x000FU : nsIProgrammingLanguage::MAX)
73 // Use to declare the array size
74 #define NS_STID_ARRAY_UBOUND (NS_STID_LAST-NS_STID_FIRST+1)
76 // Is a language ID valid?
77 #define NS_STID_VALID(langID) (langID >= NS_STID_FIRST && langID <= NS_STID_LAST)
79 // Return an index for a given ID.
80 #define NS_STID_INDEX(langID) (langID-NS_STID_FIRST)
82 // Create a 'for' loop iterating over all possible language IDs (*not* indexes)
83 #define NS_STID_FOR_ID(varName) \
84 for (varName=NS_STID_FIRST;varName<=NS_STID_LAST;varName++)
86 // Create a 'for' loop iterating over all indexes (when you don't need to know
87 // what language it is)
88 #define NS_STID_FOR_INDEX(varName) \
89 for (varName=0;varName<=NS_STID_INDEX(NS_STID_LAST);varName++)
91 // A helper function for nsIScriptGlobalObject implementations to use
92 // when handling a script error. Generally called by the global when a context
93 // notifies it of an error via nsIScriptGlobalObject::HandleScriptError.
94 // Returns PR_TRUE if HandleDOMEvent was actually called, in which case
95 // aStatus will be filled in with the status.
96 PRBool
97 NS_HandleScriptError(nsIScriptGlobalObject *aScriptGlobal,
98 nsScriptErrorEvent *aErrorEvent,
99 nsEventStatus *aStatus);
102 #define NS_ISCRIPTGLOBALOBJECT_IID \
103 { 0xe9f3f2c1, 0x2d94, 0x4722, \
104 { 0xbb, 0xd4, 0x2b, 0xf6, 0xfd, 0xf4, 0x2f, 0x48 } }
107 * The global object which keeps a script context for each supported script
108 * language. This often used to store per-window global state.
111 class nsIScriptGlobalObject : public nsISupports
113 public:
114 NS_DECLARE_STATIC_IID_ACCESSOR(NS_ISCRIPTGLOBALOBJECT_IID)
117 * Ensure that the script global object is initialized for working with the
118 * specified script language ID. This will set up the nsIScriptContext
119 * and 'script global' for that language, allowing these to be fetched
120 * and manipulated.
121 * @return NS_OK if successful; error conditions include that the language
122 * has not been registered, as well as 'normal' errors, such as
123 * out-of-memory
125 virtual nsresult EnsureScriptEnvironment(PRUint32 aLangID) = 0;
127 * Get a script context (WITHOUT added reference) for the specified language.
129 virtual nsIScriptContext *GetScriptContext(PRUint32 lang) = 0;
132 * Get the opaque "global" object for the specified lang.
134 virtual void *GetScriptGlobal(PRUint32 lang) = 0;
136 // Set/GetContext deprecated methods - use GetScriptContext/Global
137 virtual JSObject *GetGlobalJSObject() {
138 return (JSObject *)GetScriptGlobal(nsIProgrammingLanguage::JAVASCRIPT);
141 virtual nsIScriptContext *GetContext() {
142 return GetScriptContext(nsIProgrammingLanguage::JAVASCRIPT);
146 * Set a new language context for this global. The native global for the
147 * context is created by the context's GetNativeGlobal() method.
150 virtual nsresult SetScriptContext(PRUint32 lang, nsIScriptContext *aContext) = 0;
153 * Called when the global script for a language is finalized, typically as
154 * part of its GC process. By the time this call is made, the
155 * nsIScriptContext for the language has probably already been removed.
156 * After this call, the passed object is dead - which should generally be the
157 * same object the global is using for a global for that language.
160 virtual void OnFinalize(PRUint32 aLangID, void *aScriptGlobal) = 0;
163 * Called to enable/disable scripts.
165 virtual void SetScriptsEnabled(PRBool aEnabled, PRBool aFireTimeouts) = 0;
168 * Handle a script error. Generally called by a script context.
170 virtual nsresult HandleScriptError(nsScriptErrorEvent *aErrorEvent,
171 nsEventStatus *aEventStatus) {
172 return NS_HandleScriptError(this, aErrorEvent, aEventStatus);
176 NS_DEFINE_STATIC_IID_ACCESSOR(nsIScriptGlobalObject,
177 NS_ISCRIPTGLOBALOBJECT_IID)
179 #endif