Open Bug 632422 Opened 13 years ago Updated 2 years ago

XPCOM objects retrieved from different scopes aren't strictly equal

Categories

(Core :: XPConnect, defect)

defect

Tracking

()

People

(Reporter: rain1, Unassigned)

Details

STR:

Components.utils.import("resource://gre/modules/Services.jsm");
var ct1 = Services.tm.currentThread;
var ct2 = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).currentThread;
ct1 === ct2

returns false. ct1 == ct2 returns true.
This is expected, no?  They're distinct objects; if you try to add expandos to one they won't show up on the other, for example.
And in particular, Services and the code above live in different scopes, so can't get a hit on the wrappednative cache, I would think.
But maybe the cache should be per-compartment, not per-scope?
/me likes WONTFIX for this.
I don't think so: we don't want one JSM to be able to pollute these objects for another JSM.

Unless of course we take my suggestion and don't allow expandos on XPCOM objects from chrome at all, in which case per-compartment would be fine.
From the perspective of an XPCOM user, I think it makes sense that Services.tm !== the service if you get it directly, but it doesn't intuitively make sense that something you get from the service differs based on which scope the service is in. In particular, it means that current getService code cannot be replaced wholesale with the Services equivalent, since stuff like Array.indexOf breaks.
I don't understand the last comment at all.
If I have code somewhere that looks like:

var thread = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager).currentThread;
if (arr.indexOf(thread) != -1) { ... }

I can't replace it with

var thread = Services.tm.currentThread;
if (arr.indexOf(thread) != -1) { ... }
(In reply to comment #5)
> I don't think so: we don't want one JSM to be able to pollute these objects for
> another JSM.

By default, XPConnect doesn't allow you to add expandos to an object unless it has a scriptable helper that explicitly sets the right flags (USE_JSSTUB_FOR_{SET,ADD}PROPERTY or WANT_{ADD,SET}PROPERTY). The only pollution really would be through Object.prototype. The reason being (as I understand it) is that we can't reflect expandos into C++, so unless your object explicitly says "I can deal with my reflections into different languages being different" we don't allow the reflections to differ.
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.