Web IDL hasInstance behavior

18 views
Skip to first unread message

Domenic Denicola

unread,
Jun 7, 2016, 3:33:02 PM6/7/16
to blink-revie...@chromium.org
Apparently for a long time Web IDL has specified special behavior for instanceof on platform objects.

Boris is interested in updating the spec for ES6 which means using `Symbol.hasInstance`. This is a similar change to the recent changes to use `Symbol.toStringTag`.

However, I am unsure we want to support having special hasInstance behavior at all. Nobody currently implements this besides Firefox. The motivation seems to be to provide some kind of branding mechanism, but in my opinion overriding instanceof is probably not the right way to do that.

It sounds like the decision was made before my time though, in threads that are old enough that Boris doesn't really remember them. So maybe it would be bad to walk back on that consensus? But I am unsure whether there was really ever consensus. It looks to me like the discussion was had at https://lists.w3.org/Archives/Public/public-script-coord/2013JanMar/0001.html and did not have any involvement from Chrome engineers.

I thought it would be worth getting the binding team's thoughts. I think this behavior is a mistake but I am not prepared to fight against it on my own. If the binding team is OK with implementing it, or plans to not implement it, it would be good to get one of you to weigh in on https://github.com/heycam/webidl/issues/129.

Kentaro Hara

unread,
Jun 8, 2016, 1:30:40 AM6/8/16
to Domenic Denicola, Toon Verwaest, Jochen Eisinger, blink-revie...@chromium.org
+verwast +jochen


--
Kentaro Hara, Tokyo, Japan

Jochen Eisinger

unread,
Jun 8, 2016, 2:02:33 AM6/8/16
to Kentaro Hara, Domenic Denicola, Toon Verwaest, Adam Klein, blink-revie...@chromium.org
+adamk

Toon Verwaest

unread,
Jun 8, 2016, 3:18:48 AM6/8/16
to Jochen Eisinger, Kentaro Hara, Domenic Denicola, Toon Verwaest, Adam Klein, Benedikt Meurer, Michael Stanton, blink-revie...@chromium.org
+bmeurer, +mvstanton who implemented @@hasInstance in v8 (keeping instanceof fast exactly by assuming that nobody uses @@hasInstance...)

Adam Klein

unread,
Jun 8, 2016, 4:25:06 PM6/8/16
to Toon Verwaest, Jochen Eisinger, Kentaro Hara, Domenic Denicola, Benedikt Meurer, Michael Stanton, blink-revie...@chromium.org
As Toon points out, this would have significant performance implications for instanceof, at least in v8's current implementation.

What would you propose as an alternative branding mechanism? I don't doubt that this is a problem programmers have.


Domenic Denicola

unread,
Jun 8, 2016, 4:29:57 PM6/8/16
to Adam Klein, Toon Verwaest, Jochen Eisinger, Kentaro Hara, Benedikt Meurer, Michael Stanton, blink-revie...@chromium.org
I guess I could see a few possibilities worth discussing:
  • isPlatformObject(obj, "TypeName"), e.g. isPlatformObject(obj, "Node")
  • TypeName.isTypeName(obj), e.g. Node.isNode(obj)
  • TypeName.isInstance(obj), e.g. Node.isInstance(obj)

Yuki Shiino

unread,
Jun 9, 2016, 4:04:32 AM6/9/16
to Domenic Denicola, Adam Klein, Toon Verwaest, Jochen Eisinger, Kentaro Hara, Benedikt Meurer, Michael Stanton, blink-revie...@chromium.org
Let me ask a silly question; what's good with Symbol.hasInstance?

If Symbol.hasInstance were supported, it seems that user JavaScript can pretend to be an instance of an interface/class that is not the actual type.  What benefits are there?

Simply I don't understand why we'd like to introduce Symbol.hasInstance.

Cheers,
Yuki Shiino

Adam Klein

unread,
Jun 9, 2016, 4:41:27 AM6/9/16
to Yuki Shiino, Domenic Denicola, Toon Verwaest, Jochen Eisinger, Kentaro Hara, Benedikt Meurer, Michael Stanton, blink-revie...@chromium.org
While those are all reasonable questions, V8 does support Symbol.hasInstance, as part of our implementation of ES6 in general.

So this thread is really about whether WebIDL should utilize Symbol.hasInstance.

Note that even without Symbol.hasInstance, "instanceof" in JS is a pretty "weak" relationship: it basically asserts a few things about the prototype chain.

Toon Verwaest

unread,
Jun 9, 2016, 8:55:18 AM6/9/16
to Adam Klein, Yuki Shiino, Domenic Denicola, Toon Verwaest, Jochen Eisinger, Kentaro Hara, Benedikt Meurer, Michael Stanton, blink-revie...@chromium.org
If hasInstance becomes a broadly used feature, main instanceof performance will suffer and we'll need a different strategy to make sure that users of @@hasInstance aren't on the slow path by default.

esp...@google.com

unread,
Jun 25, 2016, 12:27:22 AM6/25/16
to blink-reviews-bindings, ad...@chromium.org, yukis...@chromium.org, dom...@google.com, verw...@chromium.org, joc...@chromium.org, har...@chromium.org, bme...@google.com, mvst...@google.com
I'd really rather we didn't do this and just removed it from the spec, it's been years and no browser except Firefox does this, and they have a radically different binding system which makes it perhaps easier.

It's also of questionable value as you note. It's very strange to me that doing (e instanceof HTMLDivElement) might be true, but if you had modified HTMLDivElement.prototype to have new methods they'd be missing.

I'm fine providing some kind of Platform.isInstance like facility if authors actually want that, though I've never heard anyone ask for it (literally the only compliant I've ever heard was for Array, and Array.isArray fixed that.)

Reply all
Reply to author
Forward
0 new messages