Open Bug 1820521 Opened 1 year ago Updated 1 month ago

Instantiated subclass of Xray-wrapped DOM constructor is not an instance of the subclass, but the superclass.

Categories

(Core :: XPConnect, defect)

defect

Tracking

()

People

(Reporter: robwu, Unassigned)

References

(Blocks 3 open bugs)

Details

Attachments

(2 files)

Given a XrayWrapper of a DOM constructor, when a class subclasses that, and an instance of that subclass is created, the instance is unexpectedly not an instance of the subclass, but of the native DOM constructor.

STR:

  1. Load the attached extension (relevant script pasted below) at about:debugging.
  2. Visit example.com
  3. Look at the console.

Expected logs:

  • "constructor called"
  • "instanceof Sub =" true
  • "myMethod() called"

Actual logs:

  • "constructor called"
  • "instanceof Sub =" false
  • TypeError: sub.myMethod is not a function
// In an extension content script, CustomEvent is a XrayWrapper of the native constructor.
class Sub extends CustomEvent {
  constructor(name) {
    super(name);
    console.log("constructor called");
  }
  myMethod() {
    console.log("myMethod() called");
  }
}

let sub = new Sub("x"); // logs constructor called
console.log(sub instanceof Sub); // false
sub.myMethod(); // TypeError: sub.myMethod is not a function
See Also: → 1771291
Component: General → XPConnect
Product: WebExtensions → Core
Severity: -- → S3
Duplicate of this bug: 1842565
Blocks: 1871628

I think that the issue here is that in step 3.2 in https://webidl.spec.whatwg.org/#internally-create-a-new-object-implementing-the-interface prototype in this case is a prototype from within the sandbox, so we create the CustomEvent object and use a CCW as its proto (because we're creating the CustomEvent outside of the sandbox and then wrap it inside it). But the Xray for WebIDL objects ignores that prototype chain and simply returns CustomEvent.prototype.

Duplicate of this bug: 1896267
Blocks: 1896267
No longer duplicate of this bug: 1896267
Blocks: 1473306

Peter, I see that you've attached a WIP patch but without further activity. What is blocking the patch?

Flags: needinfo?(peterv)

Figuring out if it's safe. It's moving away from how Xrays traditionally work, so it's a bit scary.

Flags: needinfo?(peterv)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: