|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef nsDOMJSUtils_h__ |
|
6 #define nsDOMJSUtils_h__ |
|
7 |
|
8 #include "nsIScriptContext.h" |
|
9 #include "jsapi.h" |
|
10 |
|
11 class nsIJSArgArray; |
|
12 |
|
13 // seems like overkill for just this 1 function - but let's see what else |
|
14 // falls out first. |
|
15 inline nsIScriptContext * |
|
16 GetScriptContextFromJSContext(JSContext *cx) |
|
17 { |
|
18 if (!(JS::ContextOptionsRef(cx).privateIsNSISupports())) { |
|
19 return nullptr; |
|
20 } |
|
21 |
|
22 nsCOMPtr<nsIScriptContext> scx = |
|
23 do_QueryInterface(static_cast<nsISupports *> |
|
24 (::JS_GetContextPrivate(cx))); |
|
25 |
|
26 // This will return a pointer to something that's about to be |
|
27 // released, but that's ok here. |
|
28 return scx; |
|
29 } |
|
30 |
|
31 JSObject* GetDefaultScopeFromJSContext(JSContext *cx); |
|
32 |
|
33 // A factory function for turning a JS::Value argv into an nsIArray |
|
34 // but also supports an effecient way of extracting the original argv. |
|
35 // Bug 312003 describes why this must be "void *", but argv will be cast to |
|
36 // JS::Value* and the args are found at: |
|
37 // ((JS::Value*)aArgv)[0], ..., ((JS::Value*)aArgv)[aArgc - 1] |
|
38 // The resulting object will take a copy of the array, and ensure each |
|
39 // element is rooted. |
|
40 // Optionally, aArgv may be nullptr, in which case the array is allocated and |
|
41 // rooted, but all items remain nullptr. This presumably means the caller |
|
42 // will then QI us for nsIJSArgArray, and set our array elements. |
|
43 nsresult NS_CreateJSArgv(JSContext *aContext, uint32_t aArgc, void *aArgv, |
|
44 nsIJSArgArray **aArray); |
|
45 |
|
46 #endif // nsDOMJSUtils_h__ |