Bug 156676 - [ES6] Use @isObject to check Object Type instead of using instanceof
Summary: [ES6] Use @isObject to check Object Type instead of using instanceof
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Yusuke Suzuki
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-04-17 08:54 PDT by Yusuke Suzuki
Modified: 2016-04-17 14:53 PDT (History)
8 users (show)

See Also:


Attachments
Patch (5.31 KB, patch)
2016-04-17 09:07 PDT, Yusuke Suzuki
darin: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Yusuke Suzuki 2016-04-17 08:54:14 PDT
[ES6] Use @isObject to check Object Type instead of using instanceof
Comment 1 Yusuke Suzuki 2016-04-17 09:07:09 PDT
Created attachment 276591 [details]
Patch
Comment 2 Yusuke Suzuki 2016-04-17 09:10:29 PDT
BTW, this is why Array.isArray is introduced despite there is `instanceof Array`.
Comment 3 Darin Adler 2016-04-17 09:33:25 PDT
Comment on attachment 276591 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=276591&action=review

I noticed other similar usage of instanceof elsewhere. For example, "instanceof @Number" in toLocaleString. Does that have a similar problem.

In WebCore I see "instanceof @Array" in one part, "instanceof Array" in another part, and "instanceof Function" . Do any of those have similar problems? When is it appropriate to use "Array" instead of "@Array"? I would like to understand more about correct ways to write JavaScript implementations both in the JavaScript engine and the DOM implementation.

> Source/JavaScriptCore/jsc.cpp:1783
> +    return JSValue::encode(GlobalObject::create(exec->vm(), GlobalObject::createStructure(exec->vm(), jsNull()), Vector<String>()));

Since exec->vm() does a number of memory accesses, I often wonder whether we should put it in a local variable in cases like this. Not important in the JSC tool I suppose, but important as an idiom elsewhere in WebKit.
Comment 4 Yusuke Suzuki 2016-04-17 14:47:24 PDT
(In reply to comment #3)
> Comment on attachment 276591 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=276591&action=review
> 
> I noticed other similar usage of instanceof elsewhere. For example,
> "instanceof @Number" in toLocaleString. Does that have a similar problem.

Yup. That's wrong and they also should be fixed.
For `instanceof @Number` case, we should add @thisNumberValue abstract operation, similar to @thisTimeValue for Date. I've opened the issue for that. https://bugs.webkit.org/show_bug.cgi?id=156680

> In WebCore I see "instanceof @Array" in one part, "instanceof Array" in
> another part, and "instanceof Function" . Do any of those have similar
> problems? When is it appropriate to use "Array" instead of "@Array"? I would
> like to understand more about correct ways to write JavaScript
> implementations both in the JavaScript engine and the DOM implementation.

I think `instanceof @Array` in fetch/FetchHeader.js is a problem. Since it is internal builtin JS, we should take care such edge cases.

But I'm not sure mediacontrols should consider these cases.
Mediacontrols are used to construct media UI, and it seems that the code is written as usual user-level JS code; it does not use builtins and it directly uses usual APIs (It means that user can replace the called APIs).

And for the WebRTC's addIceCandidate() case, I think `candidate instanceof @RTCIceCandidate` is ok; this is because addIceCandidate() does not have any step-by-step spec. The rough implementation for now seems acceptable.


> 
> > Source/JavaScriptCore/jsc.cpp:1783
> > +    return JSValue::encode(GlobalObject::create(exec->vm(), GlobalObject::createStructure(exec->vm(), jsNull()), Vector<String>()));
> 
> Since exec->vm() does a number of memory accesses, I often wonder whether we
> should put it in a local variable in cases like this. Not important in the
> JSC tool I suppose, but important as an idiom elsewhere in WebKit.

Thanks, fixed.
Comment 5 Yusuke Suzuki 2016-04-17 14:53:14 PDT
Committed r199647: <http://trac.webkit.org/changeset/199647>