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