Bug 105282 - Date.prototype.toJSON does not execute steps 1-2
Summary: Date.prototype.toJSON does not execute steps 1-2
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2012-12-18 05:55 PST by André Bargull
Modified: 2019-09-13 17:26 PDT (History)
14 users (show)

See Also:


Attachments
Patch (1.38 KB, patch)
2019-06-04 12:56 PDT, Alexey Shvayka
no flags Details | Formatted Diff | Diff
Patch (2.89 KB, patch)
2019-06-18 12:53 PDT, Alexey Shvayka
no flags Details | Formatted Diff | Diff
Patch (3.14 KB, patch)
2019-09-12 16:22 PDT, Alexey Shvayka
no flags Details | Formatted Diff | Diff
Patch (3.45 KB, patch)
2019-09-13 16:38 PDT, Alexey Shvayka
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description André Bargull 2012-12-18 05:55:45 PST
test case:
---
> this.toISOString = function(){print("global toISOString")}
function () {print("global toISOString");}

> Date.prototype.toJSON.call(null)
global toISOString

> Date.prototype.toJSON.call(void 0)
global toISOString

> Date.prototype.toJSON.call({ valueOf: function(){print("ToPrimitive(valueOf) called"); return 0}, toISOString: function(){print("toISOString called")} })
toISOString called
---

[ES5.1 - 15.9.5.44 Date.prototype.toJSON] steps 1-2 aren't executed properly:
- the first two calls should raise a TypeError because ToObject() is called with an incompatible argument (step 1 of 15.9.5.44)
- the last call should invoke the supplied valueOf() function (step 2 of 15.9.5.44)
Comment 1 Alexey Shvayka 2019-06-04 12:53:48 PDT
As of build 246014, ToPrimitive is called with proper hint, but `this` value is still resolved using sloppy mode semantics, resulting in ToObject being called on global object.

Test262: https://github.com/tc39/test262/pull/2190
Comment 2 Alexey Shvayka 2019-06-04 12:56:38 PDT
Created attachment 371320 [details]
Patch
Comment 3 Alexey Shvayka 2019-06-18 12:53:53 PDT
Created attachment 372364 [details]
Patch

With test262 imported, this patch now fixes a test.
Comment 4 Oliver Hunt 2019-06-18 12:57:22 PDT
Comment on attachment 372364 [details]
Patch

lgtm (I wonder if the spec changed at some point?)
Comment 5 Alexey Shvayka 2019-06-18 13:22:00 PDT
Comment on attachment 372364 [details]
Patch

Yes, but in a subtle way: ES5 introduced strict mode for built-in functions.
Comment 6 EWS 2019-06-18 13:22:30 PDT
Comment on attachment 372364 [details]
Patch

Rejecting attachment 372364 [details] from review queue.

oliver@nerget.com does not have reviewer permissions according to https://trac.webkit.org/browser/trunk/Tools/Scripts/webkitpy/common/config/contributors.json.

- If you do not have reviewer rights please read http://webkit.org/coding/contributing.html for instructions on how to use bugzilla flags.

- If you have reviewer rights please correct the error in Tools/Scripts/webkitpy/common/config/contributors.json by adding yourself to the file (no review needed).  The commit-queue restarts itself every 2 hours.  After restart the commit-queue will correctly respect your reviewer rights.
Comment 7 EWS 2019-06-18 13:23:05 PDT
Comment on attachment 372364 [details]
Patch

Rejecting attachment 372364 [details] from commit-queue.

shvaikalesh@gmail.com does not have committer permissions according to https://trac.webkit.org/browser/trunk/Tools/Scripts/webkitpy/common/config/contributors.json.

- If you do not have committer rights please read http://webkit.org/coding/contributing.html for instructions on how to use bugzilla flags.

- If you have committer rights please correct the error in Tools/Scripts/webkitpy/common/config/contributors.json by adding yourself to the file (no review needed).  The commit-queue restarts itself every 2 hours.  After restart the commit-queue will correctly respect your committer rights.
Comment 8 Oliver Hunt 2019-06-18 13:47:56 PDT
(In reply to ews-feeder from comment #6)
> Comment on attachment 372364 [details]
> Patch
> 
> Rejecting attachment 372364 [details] from review queue.
> 
> oliver@nerget.com does not have reviewer permissions according to
> https://trac.webkit.org/browser/trunk/Tools/Scripts/webkitpy/common/config/
> contributors.json.
> 

Oh boo, I guess that list never got updated.
Comment 9 Alexey Shvayka 2019-06-18 13:55:48 PDT
(In reply to Oliver Hunt from comment #8)
> (In reply to ews-feeder from comment #6)
> > Comment on attachment 372364 [details]
> > Patch
> > 
> > Rejecting attachment 372364 [details] from review queue.
> > 
> > oliver@nerget.com does not have reviewer permissions according to
> > https://trac.webkit.org/browser/trunk/Tools/Scripts/webkitpy/common/config/
> > contributors.json.
> > 
> 
> Oh boo, I guess that list never got updated.

Completely my fault: I attempted commit-queue+ and miserably failed.
I guess re-review would help.
Comment 10 Ross Kirsling 2019-09-12 12:39:58 PDT
Comment on attachment 372364 [details]
Patch

Just noticed this now. r=me but I think you'll need to rebase expectations.yaml.
Comment 11 Alexey Shvayka 2019-09-12 16:22:27 PDT
Created attachment 378688 [details]
Patch

Rebase patch and simplify std::isfinite check.
Comment 12 Alexey Shvayka 2019-09-12 16:23:14 PDT
Thank you for review.

I took the liberty of tweaking time value finiteness check (step 3 of https://tc39.es/ecma262/#sec-date.prototype.tojson).
While it removes weird isInt32() call, it doesn't justifies making a separate patch IMO.
Comment 13 Ross Kirsling 2019-09-12 17:25:36 PDT
(In reply to Alexey Shvayka from comment #12)
> Thank you for review.
> 
> I took the liberty of tweaking time value finiteness check (step 3 of
> https://tc39.es/ecma262/#sec-date.prototype.tojson).
> While it removes weird isInt32() call, it doesn't justifies making a
> separate patch IMO.

Yeah, that seems fine -- guess it's trying to avoid calling std::isfinite unnecessarily, but that should just amount to `return true` for integral types anyway.
Comment 14 Mark Lam 2019-09-12 17:46:15 PDT
Comment on attachment 378688 [details]
Patch

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

> Source/JavaScriptCore/ChangeLog:8
> +        Resolve `this` value using strict mode semantics.

Can you add a link to the relevant piece of the spec for this change?
Comment 15 Mark Lam 2019-09-12 18:48:46 PDT
Comment on attachment 378688 [details]
Patch

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

>> Source/JavaScriptCore/ChangeLog:8
>> +        Resolve `this` value using strict mode semantics.
> 
> Can you add a link to the relevant piece of the spec for this change?

nit: Please also describe what the issue is and why the fix works (if not obvious) here in the ChangeLog.  I think the description in https://bugs.webkit.org/show_bug.cgi?id=105282#c0 covers this quite well.  We prefer to make the ChangeLog as descriptive as possible so that if we need to investigate why a change was made, it provides the intent of the author.  Granted, this info may also be present in the bugzilla, but often, the thinking about an issue changes over the period when the issue is being investigated.  Capturing the final understanding on the nature of bug and its fix in the ChangeLog makes it more digestible than having to splunk through the bugzilla comment trail.

Secondly, in my personal experience, when I have to write a descriptive ChangeLog, it forces me to make sure that I really understand what I'm changing, and serves as a sanity check to prevent bugs from creeping in.

So, please add a descriptive ChangeLog, plus a link to the relevant section of the spec for reference.  Thanks.
Comment 16 Alexey Shvayka 2019-09-13 16:38:37 PDT
Created attachment 378761 [details]
Patch

Set reviewer and add descriptive ChangeLog.
Comment 17 Mark Lam 2019-09-13 16:40:30 PDT
Comment on attachment 378761 [details]
Patch

Thanks.
Comment 18 WebKit Commit Bot 2019-09-13 17:24:30 PDT
Comment on attachment 378761 [details]
Patch

Clearing flags on attachment: 378761

Committed r249861: <https://trac.webkit.org/changeset/249861>
Comment 19 WebKit Commit Bot 2019-09-13 17:24:32 PDT
All reviewed patches have been landed.  Closing bug.
Comment 20 Radar WebKit Bug Importer 2019-09-13 17:25:21 PDT
<rdar://problem/55358552>
Comment 21 Alexey Shvayka 2019-09-13 17:26:16 PDT
(In reply to Ross Kirsling from comment #13)
> 
> Yeah, that seems fine -- guess it's trying to avoid calling std::isfinite
> unnecessarily, but that should just amount to `return true` for integral
> types anyway.

Thanks, it didn't occur to me that std::isfinite might be slow.
I've benchmarked code path that is largely dependent on finiteness check performance with:

```
const date = new Date(NaN);
for (let i = 0; i < 1e6; ++i)
  date.toJSON();
```

Proposed tweak and trunk did perform equally (within 1%).

(In reply to Mark Lam from comment #15)
> 
> Secondly, in my personal experience, when I have to write a descriptive
> ChangeLog, it forces me to make sure that I really understand what I'm
> changing, and serves as a sanity check to prevent bugs from creeping in.

Thank you for detailed explanation. As outside contributor, I find ChangeLogs extremely useful as sources of motivation for some classes like JSCell or CustomGetterSetter. Also, descriptive ChangeLog for this patch in particular is highly important, because first comment and bug title are quite different from what the patch fixes.