espruino / Espruino

The Espruino JavaScript interpreter - Official Repo
http://www.espruino.com/
Other
2.73k stars 741 forks source link

Constructor comparisons don't work #2451

Closed RaphiSpoerri closed 5 months ago

RaphiSpoerri commented 5 months ago

Regarding primitive types, comparisons like <T literal>.constructor == <T>.prototype.constructor do not always give consistent results. For example

const cmp = () => "blah".constructor == String.prototype.constructor;
console.log(
    cmp() == cmp()
);

prints

>false


(I'm using the web IDE's Bangle.js 2 emulator, and version 2v18.1)

gfwilliams commented 5 months ago

Thanks - interestingly, "blah".constructor seems to return undefined initially, but if you call String.prototype.constructor it's fine afterwards - so it seems to be having trouble creating the constructor.

So if you change the order of comparison, it appears to work reliably for me? Is that the same for you?

const cmp = () => String.prototype.constructor == "blah".constructor;
console.log(
    cmp() == cmp()
);
gfwilliams commented 5 months ago

I've got a fix for this, but it appears to break some other stuff so I'm going to hold off committing it.

I believe the 'real prototype chain' branch would fix this too.