Re: How to check if a custom class has a private object set?

Till Schneidereit <[email protected]> Fri, 16 Dec 2016 12:40:28 +0100
Newsgroups gmane.comp.mozilla.devel.jseng
Message-ID <CACRHVk9FXUnb+Amo5Hzkb7bCrw2qXEs+vk8kZ1KShtVetHPLqg@mail.gmail.com>
On Fri, Dec 16, 2016 at 9:09 AM, Mihai Dobrescu <[email protected]>
wrote:

> Well, I have a case, a custom object. The API it manages has static and
> instance functions. Sometimes, I need to check if there is an instance or
> not present in the custom class instance. That's because if I call
> JS_GetPrivate on a class with JSCLASS_HAS_PRIVATE that has not an instance
> set (for calling static functions), an assertion fails and breaks the
> execution in debug, in NativeObject.h:
>
>     inline void*& privateRef(uint32_t nfixed) const { /* XXX should be
> private, not protected! */
>         /*
>          * The private pointer of an object can hold any word sized value.
>          * Private pointers are stored immediately after the last fixed
> slot of
>          * the object.
>          */
>         MOZ_ASSERT(nfixed == numFixedSlots());
>         MOZ_ASSERT(hasPrivate()); << FAILS HERE
>

That assert should only fail of the JSObject's class doesn't have
JSCLASS_HAS_PRIVATE. `hasPrivate`checks that, not whether a private value
was stored on the object. So it looks like somewhere in your code you're
accidentally calling JS_GetPrivate on an object that you think has the
right JSClass, but really doesn't.