Bug 1270746 part 1 - Remove hasInstance hook from CType objects. r=iain

We can instead rely on the default behavior for `instanceof` in `JS::OrdinaryHasInstance`.

This works because these objects are callable (since the JSClass has a call-hook) and
they have a `.prototype` property that returns the value in the proto slot.

Differential Revision: https://phabricator.services.mozilla.com/D141342
This commit is contained in:
Jan de Mooij 2022-03-20 11:28:02 +00:00
parent cbcd1dc469
commit 6dc57e36ea
2 changed files with 5 additions and 33 deletions

View File

@ -155,8 +155,6 @@ bool PtrGetter(JSContext* cx, const JS::CallArgs& args);
static bool CreateArray(JSContext* cx, unsigned argc, Value* vp);
static bool ToString(JSContext* cx, unsigned argc, Value* vp);
static bool ToSource(JSContext* cx, unsigned argc, Value* vp);
static bool HasInstance(JSContext* cx, HandleObject obj, MutableHandleValue v,
bool* bp);
/*
* Get the global "ctypes" object.
@ -487,7 +485,7 @@ static const JSClassOps sCTypeClassOps = {
nullptr, // mayResolve
CType::Finalize, // finalize
CType::ConstructData, // call
CType::HasInstance, // hasInstance
nullptr, // hasInstance
CType::ConstructData, // construct
CType::Trace, // trace
};
@ -4929,36 +4927,6 @@ bool CType::ToSource(JSContext* cx, unsigned argc, Value* vp) {
return true;
}
bool CType::HasInstance(JSContext* cx, HandleObject obj, MutableHandleValue v,
bool* bp) {
MOZ_ASSERT(CType::IsCType(obj));
Value slot = JS::GetReservedSlot(obj, SLOT_PROTO);
JS::Rooted<JSObject*> prototype(cx, &slot.toObject());
MOZ_ASSERT(prototype);
MOZ_ASSERT(CData::IsCDataProto(prototype));
*bp = false;
if (v.isPrimitive()) {
return true;
}
RootedObject proto(cx, &v.toObject());
for (;;) {
if (!JS_GetPrototype(cx, proto, &proto)) {
return false;
}
if (!proto) {
break;
}
if (proto == prototype) {
*bp = true;
break;
}
}
return true;
}
static JSObject* CType::GetGlobalCTypes(JSContext* cx, JSObject* objArg) {
MOZ_ASSERT(CType::IsCType(objArg));

View File

@ -2168,6 +2168,10 @@ function run_type_ctor_class_tests(
Assert.ok(d.__proto__ === t.prototype);
Assert.ok(d instanceof t);
Assert.ok(d.constructor === t);
// Other objects that are not instances of 't'.
Assert.equal({} instanceof t, false);
Assert.equal(t.__proto__ instanceof t, false);
Assert.equal(t.prototype instanceof t, false);
}
}