Fix register allocation bug in return values (bug 604996, r=dmandelin).
[mozilla-central.git] / dom / base / nsDOMJSUtils.h
blob9fb31cc2a9dcf0466646c3cf8515e81eee54841a
2 #ifndef nsDOMJSUtils_h__
3 #define nsDOMJSUtils_h__
5 #include "jsapi.h"
6 #include "nsIScriptContext.h"
8 // seems like overkill for just this 1 function - but let's see what else
9 // falls out first.
10 inline nsIScriptContext *
11 GetScriptContextFromJSContext(JSContext *cx)
13 if (!(::JS_GetOptions(cx) & JSOPTION_PRIVATE_IS_NSISUPPORTS)) {
14 return nsnull;
17 nsCOMPtr<nsIScriptContext> scx =
18 do_QueryInterface(static_cast<nsISupports *>
19 (::JS_GetContextPrivate(cx)));
21 // This will return a pointer to something that's about to be
22 // released, but that's ok here.
23 return scx;
26 inline nsIScriptContextPrincipal*
27 GetScriptContextPrincipalFromJSContext(JSContext *cx)
29 if (!(::JS_GetOptions(cx) & JSOPTION_PRIVATE_IS_NSISUPPORTS)) {
30 return nsnull;
33 nsCOMPtr<nsIScriptContextPrincipal> scx =
34 do_QueryInterface(static_cast<nsISupports *>
35 (::JS_GetContextPrivate(cx)));
37 // This will return a pointer to something that's about to be
38 // released, but that's ok here.
39 return scx;
42 // A factory function for turning a jsval argv into an nsIArray
43 // but also supports an effecient way of extracting the original argv.
44 // Bug 312003 describes why this must be "void *", but argv will be cast to
45 // jsval* and the args are found at:
46 // ((jsval*)aArgv)[0], ..., ((jsval*)aArgv)[aArgc - 1]
47 // The resulting object will take a copy of the array, and ensure each
48 // element is rooted.
49 // Optionally, aArgv may be NULL, in which case the array is allocated and
50 // rooted, but all items remain NULL. This presumably means the caller will
51 // then QI us for nsIJSArgArray, and set our array elements.
52 nsresult NS_CreateJSArgv(JSContext *aContext, PRUint32 aArgc, void *aArgv,
53 nsIArray **aArray);
55 #endif // nsDOMJSUtils_h__