Quantcast

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

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Mihai Dobrescu
Provided JS_GetPrivate returns some known-typed private object and JS_ObjectIsFunction checks if some specific class instance is there, how could I check if simply some object instance is present, regardless the class type?
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Boris Zbarsky
On 12/16/16 2:02 AM, Mihai Dobrescu wrote:
> Provided JS_GetPrivate returns some known-typed private object and JS_ObjectIsFunction checks if some specific class instance is there, how could I check if simply some object instance is present, regardless the class type?

JS_GetPrivate returns a void*.  It can be used (with JS_SetPrivate) to
store some non-spidermonkey-related data on your object.  So I'm not
sure where JS_ObjectIsFunction comes into this.

In general, if JS_GetPrivate returned something nonzero, that means
something was stored with JS_SetPrivate.  I'm not sure whether that
answers the question you're trying to ask, though, because I'm not sure
what that question is.

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Mihai Dobrescu
On Friday, December 16, 2016 at 9:38:38 AM UTC+2, Boris Zbarsky wrote:

> On 12/16/16 2:02 AM, Mihai Dobrescu wrote:
> > Provided JS_GetPrivate returns some known-typed private object and JS_ObjectIsFunction checks if some specific class instance is there, how could I check if simply some object instance is present, regardless the class type?
>
> JS_GetPrivate returns a void*.  It can be used (with JS_SetPrivate) to
> store some non-spidermonkey-related data on your object.  So I'm not
> sure where JS_ObjectIsFunction comes into this.
>
> In general, if JS_GetPrivate returned something nonzero, that means
> something was stored with JS_SetPrivate.  I'm not sure whether that
> answers the question you're trying to ask, though, because I'm not sure
> what that question is.
>
> -Boris

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
        HeapSlot* end = &fixedSlots()[nfixed];
        return *reinterpret_cast<void**>(end);
    }

I have expected JS_GetPrivate to return as you've said, it probably does in release version, but in debug it fails.

What can I do?
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Till Schneidereit-2
On Fri, Dec 16, 2016 at 9:09 AM, Mihai Dobrescu <[hidden email]>
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.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Mihai Dobrescu
In reply to this post by Mihai Dobrescu
I don't get it.
I have defined it as such.
The class has two kinds of functions, instance functions and static functions.

Instance functions work fine.

Only in case of static functions call it throws that assertion failure.

Why is that? Does it ignore JSCLASS_HAS_PRIVATE when static functions are called somehow?
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Till Schneidereit-2
Are you talking about static functions in JS, i.e. methods called on the
constructor? If so, are you trying to use JS_GetPrivate on the function's
receiver (i.e., `this` value)? That'd be the constructor function, not an
instance of your custom class.

If that's not it, then it'd help to get a more complete example of what
you're trying to do.

On Fri, Dec 16, 2016 at 1:16 PM, Mihai Dobrescu <[hidden email]>
wrote:

> I don't get it.
> I have defined it as such.
> The class has two kinds of functions, instance functions and static
> functions.
>
> Instance functions work fine.
>
> Only in case of static functions call it throws that assertion failure.
>
> Why is that? Does it ignore JSCLASS_HAS_PRIVATE when static functions are
> called somehow?
> _______________________________________________
> dev-tech-js-engine mailing list
> [hidden email]
> https://lists.mozilla.org/listinfo/dev-tech-js-engine
>
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Mihai Dobrescu
In reply to this post by Mihai Dobrescu
I have a class C with instance function F() and static function S().

I would call:

var c = new C();

c.F();

C.S(); << here crashes as explained

What I might do wrong, considering I try to extract an instance if there is one or not - when not a nullptr should be returned? I know this might be misleading, so the idea here is that I use generic template functions, even though I don't really use the object instance if any. But, anyway, when I need an instance, I should be able to check if there is something <> nullptr or not.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Mihai Dobrescu
I thonk the question could be rephrased like this:

When a function is executed, being static, what is the meaning of the code below?

JS::CallArgs args = CallArgsFromVp(argc, vp);

JSObject* obj = &args.thisv().toObject();

What is the content of obj?

What is the outcome of JS_GetPrivate(obj)?

That obj won't have the required flag, but how could I tell that before?
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Mihai Dobrescu
In reply to this post by Mihai Dobrescu
On Friday, December 16, 2016 at 3:13:47 PM UTC+2, Mihai Dobrescu wrote:

> I have a class C with instance function F() and static function S().
>
> I would call:
>
> var c = new C();
>
> c.F();
>
> C.S(); << here crashes as explained
>
> What I might do wrong, considering I try to extract an instance if there is one or not - when not a nullptr should be returned? I know this might be misleading, so the idea here is that I use generic template functions, even though I don't really use the object instance if any. But, anyway, when I need an instance, I should be able to check if there is something <> nullptr or not.

I think the question could be rephrased like this:

When a function is executed, being static, what is the meaning of the code below?

JS::CallArgs args = CallArgsFromVp(argc, vp);

JSObject* obj = &args.thisv().toObject();

What is the content of obj?

What is the outcome of JS_GetPrivate(obj)?

That obj won't have the required flag, but how could I tell that before?
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Till Schneidereit-2
In reply to this post by Mihai Dobrescu
On Fri, Dec 16, 2016 at 2:22 PM, Mihai Dobrescu <[hidden email]>
wrote:

> I thonk the question could be rephrased like this:
>
> When a function is executed, being static, what is the meaning of the code
> below?
>
> JS::CallArgs args = CallArgsFromVp(argc, vp);
>
> JSObject* obj = &args.thisv().toObject();
>
> What is the content of obj?
>

Ok, that's what I thought was going on. `obj` in this case is the
constructor function for your class, not an instance of it.


>
> What is the outcome of JS_GetPrivate(obj)?
>

The assert you get in debug builds, or some random memory content in opt
builds because functions don't have a private slot.


>
> That obj won't have the required flag, but how could I tell that before?
>

By using JS_GetClass(obj) and ensuring that that matches your class. You
should do that regardless to ensure that everything works as expected.
Otherwise this will cause crashes:

// with your class C with instance function F() and static function S().
let someObj = {F: C.prototype.F};
someObj.F(); // Tries to use JS_GetPrivate on someObj, which isn't an
instance of C.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Loading...