Overwriting == and === operators for

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Overwriting == and === operators for

Benjamin Kircher
Hi there,

I'm looking for a way to "overload" the comparison operator == and ===. Is it possible to execute native code when two JS objects are compared in an expression? Those two JS objects would be created from a JSClass.

There was a JSEqualityOp that I could specify when using JSExtendedClass, but that has been long gone from the API.

Kind regards,

BK
_______________________________________________
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
|

Re: Overwriting == and === operators for

Jan de Mooij-3
Hi Benjamin,

Unfortunately that's no longer possible. Comparing two objects with the ==
or === operators is implemented as a simple pointer comparison inside the
engine.

What's your use case? If it's just for some local testing you could hack
EqualGivenSameType, but note that the JITs have similar code...

Jan

On Mon, Feb 15, 2016 at 2:13 PM, Benjamin Kircher <
[hidden email]> wrote:

> Hi there,
>
> I'm looking for a way to "overload" the comparison operator == and ===. Is
> it possible to execute native code when two JS objects are compared in an
> expression? Those two JS objects would be created from a JSClass.
>
> There was a JSEqualityOp that I could specify when using JSExtendedClass,
> but that has been long gone from the API.
>
> Kind regards,
>
> BK
> _______________________________________________
> 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
|

Re: Overwriting == and === operators for native Objects

Benjamin Kircher
Hey Jan, thanks for the quick reply!

> On 15 Feb 2016, at 15:44, Jan de Mooij <[hidden email]> wrote:
>
> Unfortunately that's no longer possible. Comparing two objects with the == or === operators is implemented as a simple pointer comparison inside the engine.

The decision for this is probably performance reasons, right?


> What's your use case?

We have some database objects that have a unique ID. Those objects are exposed via JSClass. A comparison based on that ID would make much more sense in a lot of scenarios here.

A different approach would be to somehow ensure that only one JS object for a given database object is constructed at any time in a JSContext. Subsequent requests for a new object would return a reference to an already constructed one. But that approach sounds much more difficult to implement if at all possible.

BK
_______________________________________________
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
|

Re: Overwriting == and === operators for native Objects

Jan de Mooij-3
On Mon, Feb 15, 2016 at 5:45 PM, Benjamin Kircher <
[hidden email]> wrote:

> > Unfortunately that's no longer possible. Comparing two objects with the
> == or === operators is implemented as a simple pointer comparison inside
> the engine.
>
> The decision for this is probably performance reasons, right?
>

We try to remove any Class hooks and stuff we don't need, it makes it
easier to maintain and optimize the engine.

But also, a Class hook for this is a bit complicated. What do we do when we
have |o1 === o2| and o1 and o2 have different hooks? Do we look at the LHS?
That means |o1 === o2| is not necessarily the same as |o2 === o1|.

A different approach would be to somehow ensure that only one JS object for
> a given database object is constructed at any time in a JSContext.
> Subsequent requests for a new object would return a reference to an already
> constructed one. But that approach sounds much more difficult to implement
> if at all possible.
>

You could use a Map, but that will leak memory. There's a proposal to add
weak references to the language - that would solve your problem but it will
take a while until that's standardized/implemented (assuming it will
happen):

https://github.com/tc39/proposal-weakrefs

Jan
_______________________________________________
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
|

Re: Overwriting == and === operators for native Objects

Benjamin Kircher

> On 16 Feb 2016, at 13:55, Jan de Mooij <[hidden email]> wrote:
>
> On Mon, Feb 15, 2016 at 5:45 PM, Benjamin Kircher <[hidden email]> wrote:
> > Unfortunately that's no longer possible. Comparing two objects with the == or === operators is implemented as a simple pointer comparison inside the engine.
>
> The decision for this is probably performance reasons, right?
>
> We try to remove any Class hooks and stuff we don't need, it makes it easier to maintain and optimize the engine.
>
> But also, a Class hook for this is a bit complicated. What do we do when we have |o1 === o2| and o1 and o2 have different hooks? Do we look at the LHS? That means |o1 === o2| is not necessarily the same as |o2 === o1|.
>
> A different approach would be to somehow ensure that only one JS object for a given database object is constructed at any time in a JSContext. Subsequent requests for a new object would return a reference to an already constructed one. But that approach sounds much more difficult to implement if at all possible.
>
> You could use a Map, but that will leak memory. There's a proposal to add weak references to the language - that would solve your problem but it will take a while until that's standardized/implemented (assuming it will happen):
>
> https://github.com/tc39/proposal-weakrefs

Highly informative! Thanks, Jan.
_______________________________________________
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
|

Re: Overwriting == and === operators for native Objects

Mihai Dobrescu
In reply to this post by Jan de Mooij-3
Although I understand the reasons, it would be highly useful for embedders to have such operators overloading.
I've seen the latest notes on SpiderMonkey release :

"Future Direction

...insert details on future plans...

SpiderMonkey embedders should be aware that

    Mozilla has no plans to keep the JSAPI, nor the JSDBGAPI, stable for embedders. We have chosen to concentrate on performance and correctness as primary concerns instead."

Although I agree to sacrifice stability of the API in therms of classes definitions, as embedder hurts to suddenly miss some features.
I've chosen SpiderMonkey over V8 due to the fact it was friendlier to me, but now I fear the future.
_______________________________________________
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
|

Re: Overwriting == and === operators for native Objects

Kent Williams
The fact of the matter is that if you're going to embed SpiderMonkey,
you need to pick a version of SpiderMonkey and stick with it during
development.

If there's a compelling reason to update to a newer version of
SpiderMonkey, then you have to figure out what changed that affects your
code.  But first, choose a version and get your code working. Trying to
chase the API is a shortcut to madness.

Where I work, it's taking me a long time to drag them into a relatively
modern version of SpiderMonkey, because 1.8.5 works in the current
context, and going to the new version means exhaustive testing so that
updating doesn't break dozens of client sites.

Frankly I think that if Mozilla actually wants people to embed
SpiderMonkey, that should figure out a public API for embedders that
doesn't change all the time.  This would involve thinking about what an
ideal API would be, and then publish it and implement it.

As has been stated, this isn't a priority for the core SpiderMonkey
devs.  If it was, it wouldn't be a mishmash of C-style function APIs and
object APIs.

All that (mildly critical comments) being said, I've looked at V8 too,
and I'm even less thrilled about the Google V8 API.  I could probably
re-write our code to work with it, but it would be a long painful
process, with no discernable benefit.

On 02/22/2016 03:11 AM, [hidden email] wrote:

> Although I understand the reasons, it would be highly useful for embedders to have such operators overloading.
> I've seen the latest notes on SpiderMonkey release :
>
> "Future Direction
>
> ...insert details on future plans...
>
> SpiderMonkey embedders should be aware that
>
>      Mozilla has no plans to keep the JSAPI, nor the JSDBGAPI, stable for embedders. We have chosen to concentrate on performance and correctness as primary concerns instead."
>
> Although I agree to sacrifice stability of the API in therms of classes definitions, as embedder hurts to suddenly miss some features.
> I've chosen SpiderMonkey over V8 due to the fact it was friendlier to me, but now I fear the future.
> _______________________________________________
> 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
|

Re: Overwriting == and === operators for native Objects

Martin McDonough
In reply to this post by Mihai Dobrescu
I would actually consider the SM API reasonably stable.

Compare it to the utter madness of the Google V8 API. In comparison, SM is stable as a rock. After years of chasing the V8 API, I for one find the SM API to quite easy to continuously adjust to. Mozilla even provides documentation about when certain features become deprecated.

I would recommend that, if you find the SM API a bit unstable for your tastes, don't even consider V8.
_______________________________________________
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
|

Re: Overwriting == and === operators for native Objects

Mihai Dobrescu
In my experience, building V8 under Visual Studio is a pain.
I've tried V8 for the sake of comparison and experience. It took hours to get all the setup working and did not compile.
SpiderMonkey took a couple of hours to have it running the first script.
But it did not have all the sources so I had to take some third party archive containing NSPR, here https://people.mozilla.org/~sstangl/mozjs-38.2.1.rc0.tar.bz2. So unpleasant. But works and the documentation is acceptable enough to do some job, but the help from the community is the main source of information. Thank you.
_______________________________________________
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
|

Re: Overwriting == and === operators for native Objects

Steve Fink-4
On 02/23/2016 11:08 PM, [hidden email] wrote:
> In my experience, building V8 under Visual Studio is a pain.
> I've tried V8 for the sake of comparison and experience. It took hours to get all the setup working and did not compile.
> SpiderMonkey took a couple of hours to have it running the first script.
> But it did not have all the sources so I had to take some third party archive containing NSPR, here https://people.mozilla.org/~sstangl/mozjs-38.2.1.rc0.tar.bz2. So unpleasant. But works and the documentation is acceptable enough to do some job, but the help from the community is the main source of information. Thank you.

FWIW, I've added in the nspr sources (and I think there was something
else missing) in the latest package. Or perhaps just the script that
generates the packages; we probably don't have an official-ish updated
package yet.

you can check it out at
http://people.mozilla.org/~sfink/data/mozjs-nightly-45.0a1.0.tar.bz2

_______________________________________________
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
|

Re: Overwriting == and === operators for native Objects

Mihai Dobrescu
In reply to this post by Mihai Dobrescu
Thanks!
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine