Fix register allocation bug in return values (bug 604996, r=dmandelin).
[mozilla-central.git] / dom / base / nsJSUtils.cpp
blob1d386f39b090c7b22c201edfc0db3a900cad1a4d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=2 et tw=78: */
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):
24 * Vidur Apparao <vidur@netscape.com>
25 * L. David Baron <dbaron@mozillafoundation.org>
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.
39 * ***** END LICENSE BLOCK ***** */
41 /**
42 * This is not a generated file. It contains common utility functions
43 * invoked from the JavaScript code generated from IDL interfaces.
44 * The goal of the utility functions is to cut down on the size of
45 * the generated code itself.
48 #include "nsJSUtils.h"
49 #include "jsapi.h"
50 #include "jsdbgapi.h"
51 #include "prprf.h"
52 #include "nsIScriptContext.h"
53 #include "nsIScriptObjectOwner.h"
54 #include "nsIScriptGlobalObject.h"
55 #include "nsIServiceManager.h"
56 #include "nsIXPConnect.h"
57 #include "nsCOMPtr.h"
58 #include "nsContentUtils.h"
59 #include "nsIScriptSecurityManager.h"
61 #include "nsDOMJSUtils.h" // for GetScriptContextFromJSContext
63 JSBool
64 nsJSUtils::GetCallingLocation(JSContext* aContext, const char* *aFilename,
65 PRUint32* aLineno, nsIPrincipal* aPrincipal)
67 // Get the current filename and line number
68 JSStackFrame* frame = nsnull;
69 JSScript* script = nsnull;
70 do {
71 frame = ::JS_FrameIterator(aContext, &frame);
73 if (frame) {
74 script = ::JS_GetFrameScript(aContext, frame);
76 } while (frame && !script);
78 if (script) {
79 // If aPrincipals is non-null then our caller is asking us to ensure
80 // that the filename we return does not have elevated privileges.
81 if (aPrincipal) {
82 uint32 flags = JS_GetScriptFilenameFlags(script);
84 // Use the principal for the filename if it shouldn't be receiving
85 // implicit XPCNativeWrappers.
86 PRBool system;
87 if (flags & JSFILENAME_PROTECTED) {
88 nsIScriptSecurityManager *ssm = nsContentUtils::GetSecurityManager();
90 if (NS_FAILED(ssm->IsSystemPrincipal(aPrincipal, &system)) || !system) {
91 JSPrincipals* jsprins;
92 aPrincipal->GetJSPrincipals(aContext, &jsprins);
94 *aFilename = jsprins->codebase;
95 *aLineno = 0;
96 JSPRINCIPALS_DROP(aContext, jsprins);
97 return JS_TRUE;
102 const char* filename = ::JS_GetScriptFilename(aContext, script);
104 if (filename) {
105 PRUint32 lineno = 0;
106 jsbytecode* bytecode = ::JS_GetFramePC(aContext, frame);
108 if (bytecode) {
109 lineno = ::JS_PCToLineNumber(aContext, script, bytecode);
112 *aFilename = filename;
113 *aLineno = lineno;
114 return JS_TRUE;
118 return JS_FALSE;
121 nsIScriptGlobalObject *
122 nsJSUtils::GetStaticScriptGlobal(JSContext* aContext, JSObject* aObj)
124 nsISupports* supports;
125 JSClass* clazz;
126 JSObject* parent;
127 JSObject* glob = aObj; // starting point for search
129 if (!glob)
130 return nsnull;
132 while ((parent = ::JS_GetParent(aContext, glob)))
133 glob = parent;
135 clazz = JS_GET_CLASS(aContext, glob);
137 if (!clazz ||
138 !(clazz->flags & JSCLASS_HAS_PRIVATE) ||
139 !(clazz->flags & JSCLASS_PRIVATE_IS_NSISUPPORTS) ||
140 !(supports = (nsISupports*)::JS_GetPrivate(aContext, glob))) {
141 return nsnull;
144 nsCOMPtr<nsIXPConnectWrappedNative> wrapper(do_QueryInterface(supports));
145 NS_ENSURE_TRUE(wrapper, nsnull);
147 nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryWrappedNative(wrapper));
149 // We're returning a pointer to something that's about to be
150 // released, but that's ok here.
151 return sgo;
154 nsIScriptContext *
155 nsJSUtils::GetStaticScriptContext(JSContext* aContext, JSObject* aObj)
157 nsIScriptGlobalObject *nativeGlobal = GetStaticScriptGlobal(aContext, aObj);
158 if (!nativeGlobal)
159 return nsnull;
161 return nativeGlobal->GetScriptContext(nsIProgrammingLanguage::JAVASCRIPT);
164 nsIScriptGlobalObject *
165 nsJSUtils::GetDynamicScriptGlobal(JSContext* aContext)
167 nsIScriptContext *scriptCX = GetDynamicScriptContext(aContext);
168 if (!scriptCX)
169 return nsnull;
170 return scriptCX->GetGlobalObject();
173 nsIScriptContext *
174 nsJSUtils::GetDynamicScriptContext(JSContext *aContext)
176 return GetScriptContextFromJSContext(aContext);