Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 47de9d3

Browse files
committed
Bug 864727 part 4. Pass a handle for the scope object to all the various Wrap*Object stuff in BindingUtils. r=ms2ger
Note: The JS::Rooted in CGWrapWithCacheMethod is just there until we start passing a handle to Wrap().
1 parent 73485c5 commit 47de9d3

4 files changed

Lines changed: 38 additions & 24 deletions

File tree

content/canvas/src/WebGLContextUtils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ WebGLContext::WebGLObjectAsJSValue(JSContext *cx, const WebGLObjectType *object,
2020
return JS::NullValue();
2121
}
2222
MOZ_ASSERT(this == object->Context());
23-
JS::Value v;
24-
JSObject* wrapper = GetWrapper();
23+
JS::Rooted<JS::Value> v(cx);
24+
JS::Rooted<JSObject*> wrapper(cx, GetWrapper());
2525
JSAutoCompartment ac(cx, wrapper);
26-
if (!dom::WrapNewBindingObject(cx, wrapper, const_cast<WebGLObjectType*>(object), &v)) {
26+
if (!dom::WrapNewBindingObject(cx, wrapper, const_cast<WebGLObjectType*>(object), v.address())) {
2727
rv.Throw(NS_ERROR_FAILURE);
2828
return JS::NullValue();
2929
}

dom/bindings/BindingUtils.h

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,8 @@ WrapNewBindingForSameCompartment(JSContext* cx, JSObject* obj,
550550
// having "value" inherit from nsWrapperCache.
551551
template <class T>
552552
MOZ_ALWAYS_INLINE bool
553-
WrapNewBindingObject(JSContext* cx, JSObject* scope, T* value, JS::Value* vp)
553+
WrapNewBindingObject(JSContext* cx, JS::Handle<JSObject*> scope, T* value,
554+
JS::Value* vp)
554555
{
555556
MOZ_ASSERT(value);
556557
JSObject* obj = value->GetWrapperPreserveColor();
@@ -612,8 +613,9 @@ WrapNewBindingObject(JSContext* cx, JSObject* scope, T* value, JS::Value* vp)
612613
// WrapObject() method taking a JSContext and a scope.
613614
template <class T>
614615
inline bool
615-
WrapNewBindingNonWrapperCachedObject(JSContext* cx, JSObject* scope, T* value,
616-
JS::Value* vp)
616+
WrapNewBindingNonWrapperCachedObject(JSContext* cx,
617+
JS::Handle<JSObject*> scopeArg,
618+
T* value, JS::Value* vp)
617619
{
618620
MOZ_ASSERT(value);
619621
// We try to wrap in the compartment of the underlying object of "scope"
@@ -622,6 +624,10 @@ WrapNewBindingNonWrapperCachedObject(JSContext* cx, JSObject* scope, T* value,
622624
// scope for the JSAutoCompartment so that we restore the compartment
623625
// before we call JS_WrapValue.
624626
Maybe<JSAutoCompartment> ac;
627+
// Maybe<Handle> doesn't so much work, and in any case, adding
628+
// more Maybe (one for a Rooted and one for a Handle) adds more
629+
// code (and branches!) than just adding a single rooted.
630+
JS::Rooted<JSObject*> scope(cx, scopeArg);
625631
if (js::IsWrapper(scope)) {
626632
scope = js::CheckedUnwrap(scope, /* stopAtOuter = */ false);
627633
if (!scope)
@@ -648,7 +654,8 @@ WrapNewBindingNonWrapperCachedObject(JSContext* cx, JSObject* scope, T* value,
648654
// is true if the JSObject took ownership
649655
template <class T>
650656
inline bool
651-
WrapNewBindingNonWrapperCachedOwnedObject(JSContext* cx, JSObject* scope,
657+
WrapNewBindingNonWrapperCachedOwnedObject(JSContext* cx,
658+
JS::Handle<JSObject*> scopeArg,
652659
nsAutoPtr<T>& value, JS::Value* vp)
653660
{
654661
// We do a runtime check on value, because otherwise we might in
@@ -662,6 +669,10 @@ WrapNewBindingNonWrapperCachedOwnedObject(JSContext* cx, JSObject* scope,
662669
// scope for the JSAutoCompartment so that we restore the compartment
663670
// before we call JS_WrapValue.
664671
Maybe<JSAutoCompartment> ac;
672+
// Maybe<Handle> doesn't so much work, and in any case, adding
673+
// more Maybe (one for a Rooted and one for a Handle) adds more
674+
// code (and branches!) than just adding a single rooted.
675+
JS::Rooted<JSObject*> scope(cx, scopeArg);
665676
if (js::IsWrapper(scope)) {
666677
scope = js::CheckedUnwrap(scope, /* stopAtOuter = */ false);
667678
if (!scope)
@@ -692,7 +703,7 @@ WrapNewBindingNonWrapperCachedOwnedObject(JSContext* cx, JSObject* scope,
692703
// Helper for smart pointers (nsAutoPtr/nsRefPtr/nsCOMPtr).
693704
template <template <typename> class SmartPtr, typename T>
694705
inline bool
695-
WrapNewBindingNonWrapperCachedObject(JSContext* cx, JSObject* scope,
706+
WrapNewBindingNonWrapperCachedObject(JSContext* cx, JS::Handle<JSObject*> scope,
696707
const SmartPtr<T>& value, JS::Value* vp)
697708
{
698709
return WrapNewBindingNonWrapperCachedObject(cx, scope, value.get(), vp);
@@ -1055,8 +1066,8 @@ struct WrapNativeParentFallback<T, true >
10551066
template<typename T, bool hasWrapObject=HasWrapObject<T>::Value >
10561067
struct WrapNativeParentHelper
10571068
{
1058-
static inline JSObject* Wrap(JSContext* cx, JSObject* scope, T* parent,
1059-
nsWrapperCache* cache)
1069+
static inline JSObject* Wrap(JSContext* cx, JS::Handle<JSObject*> scope,
1070+
T* parent, nsWrapperCache* cache)
10601071
{
10611072
MOZ_ASSERT(cache);
10621073

@@ -1081,8 +1092,8 @@ struct WrapNativeParentHelper
10811092
template<typename T>
10821093
struct WrapNativeParentHelper<T, false >
10831094
{
1084-
static inline JSObject* Wrap(JSContext* cx, JSObject* scope, T* parent,
1085-
nsWrapperCache* cache)
1095+
static inline JSObject* Wrap(JSContext* cx, JS::Handle<JSObject*> scope,
1096+
T* parent, nsWrapperCache* cache)
10861097
{
10871098
JSObject* obj;
10881099
if (cache && (obj = cache->GetWrapper())) {
@@ -1100,7 +1111,8 @@ struct WrapNativeParentHelper<T, false >
11001111
// Wrapping of our native parent.
11011112
template<typename T>
11021113
static inline JSObject*
1103-
WrapNativeParent(JSContext* cx, JSObject* scope, T* p, nsWrapperCache* cache)
1114+
WrapNativeParent(JSContext* cx, JS::Handle<JSObject*> scope, T* p,
1115+
nsWrapperCache* cache)
11041116
{
11051117
if (!p) {
11061118
return scope;
@@ -1113,7 +1125,7 @@ WrapNativeParent(JSContext* cx, JSObject* scope, T* p, nsWrapperCache* cache)
11131125
// things like the nsWrapperCache for it.
11141126
template<typename T>
11151127
static inline JSObject*
1116-
WrapNativeParent(JSContext* cx, JSObject* scope, const T& p)
1128+
WrapNativeParent(JSContext* cx, JS::Handle<JSObject*> scope, const T& p)
11171129
{
11181130
return WrapNativeParent(cx, scope, GetParentPointer(p), GetWrapperCache(p));
11191131
}
@@ -1123,7 +1135,7 @@ HAS_MEMBER(GetParentObject)
11231135
template<typename T, bool WrapperCached=HasGetParentObjectMember<T>::Value>
11241136
struct GetParentObject
11251137
{
1126-
static JSObject* Get(JSContext* cx, JSObject* obj)
1138+
static JSObject* Get(JSContext* cx, JS::Handle<JSObject*> obj)
11271139
{
11281140
T* native = UnwrapDOMObject<T>(obj);
11291141
return WrapNativeParent(cx, obj, native->GetParentObject());
@@ -1133,7 +1145,7 @@ struct GetParentObject
11331145
template<typename T>
11341146
struct GetParentObject<T, false>
11351147
{
1136-
static JSObject* Get(JSContext* cx, JSObject* obj)
1148+
static JSObject* Get(JSContext* cx, JS::Handle<JSObject*> obj)
11371149
{
11381150
MOZ_CRASH();
11391151
return nullptr;
@@ -1154,7 +1166,7 @@ JSObject* GetJSObjectFromCallback(void* noncallback)
11541166

11551167
template<typename T>
11561168
static inline JSObject*
1157-
WrapCallThisObject(JSContext* cx, JSObject* scope, const T& p)
1169+
WrapCallThisObject(JSContext* cx, JS::Handle<JSObject*> scope, const T& p)
11581170
{
11591171
// Callbacks are nsISupports, so WrapNativeParent will just happily wrap them
11601172
// up as an nsISupports XPCWrappedNative... which is not at all what we want.
@@ -1182,8 +1194,8 @@ WrapCallThisObject(JSContext* cx, JSObject* scope, const T& p)
11821194
template <class T, bool isSmartPtr=HasgetMember<T>::Value>
11831195
struct WrapNewBindingObjectHelper
11841196
{
1185-
static inline bool Wrap(JSContext* cx, JSObject* scope, const T& value,
1186-
JS::Value* vp)
1197+
static inline bool Wrap(JSContext* cx, JS::Handle<JSObject*> scope,
1198+
const T& value, JS::Value* vp)
11871199
{
11881200
return WrapNewBindingObject(cx, scope, value.get(), vp);
11891201
}
@@ -1192,7 +1204,7 @@ struct WrapNewBindingObjectHelper
11921204
template <class T>
11931205
struct WrapNewBindingObjectHelper<T, false>
11941206
{
1195-
static inline bool Wrap(JSContext* cx, JSObject* scope, T& value,
1207+
static inline bool Wrap(JSContext* cx, JS::Handle<JSObject*> scope, T& value,
11961208
JS::Value* vp)
11971209
{
11981210
return WrapNewBindingObject(cx, scope, &value, vp);
@@ -1201,7 +1213,7 @@ struct WrapNewBindingObjectHelper<T, false>
12011213

12021214
template<class T>
12031215
inline bool
1204-
WrapNewBindingObject(JSContext* cx, JSObject* scope, T& value,
1216+
WrapNewBindingObject(JSContext* cx, JS::Handle<JSObject*> scope, T& value,
12051217
JS::Value* vp)
12061218
{
12071219
return WrapNewBindingObjectHelper<T>::Wrap(cx, scope, value, vp);

dom/bindings/Codegen.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,7 @@ class CGWrapWithCacheMethod(CGAbstractMethod):
20232023
"""
20242024
def __init__(self, descriptor, properties):
20252025
assert descriptor.interface.hasInterfacePrototypeObject()
2026-
args = [Argument('JSContext*', 'aCx'), Argument('JSObject*', 'aScope'),
2026+
args = [Argument('JSContext*', 'aCx'), Argument('JSObject*', 'aScopeArg'),
20272027
Argument(descriptor.nativeType + '*', 'aObject'),
20282028
Argument('nsWrapperCache*', 'aCache')]
20292029
CGAbstractMethod.__init__(self, descriptor, 'Wrap', 'JSObject*', args)
@@ -2041,6 +2041,7 @@ def definition_body(self):
20412041
assertISupportsInheritance = ""
20422042
return """%s
20432043
%s
2044+
JS::Rooted<JSObject*> aScope(aCx, aScopeArg); // Temporary!
20442045
JSObject* parent = WrapNativeParent(aCx, aScope, aObject->GetParentObject());
20452046
if (!parent) {
20462047
return NULL;
@@ -8588,7 +8589,8 @@ def getMethodImpls(self, method):
85888589

85898590
bodyWithThis = string.Template(
85908591
setupCall+
8591-
"JSObject* thisObjJS = WrapCallThisObject(s.GetContext(), mCallback, thisObj);\n"
8592+
"JSObject* thisObjJS =\n"
8593+
" WrapCallThisObject(s.GetContext(), CallbackPreserveColor(), thisObj);\n"
85928594
"if (!thisObjJS) {\n"
85938595
" aRv.Throw(NS_ERROR_FAILURE);\n"
85948596
" return${errorReturn};\n"

dom/bindings/DOMJSClass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ enum DOMObjectType {
156156
eInterfacePrototype
157157
};
158158

159-
typedef JSObject* (*ParentGetter)(JSContext* aCx, JSObject* aObj);
159+
typedef JSObject* (*ParentGetter)(JSContext* aCx, JS::Handle<JSObject*> aObj);
160160
typedef JSObject* (*ProtoGetter)(JSContext* aCx, JSObject* aGlobal);
161161

162162
struct DOMClass

0 commit comments

Comments
 (0)