<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>141115</bug_id>
          
          <creation_ts>2015-01-31 04:59:55 -0800</creation_ts>
          <short_desc>new Date(NaN).toJSON() must return null instead of throwing a TypeError</short_desc>
          <delta_ts>2015-08-27 18:53:12 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc>https://es5.github.io/#x15.9.5.44</bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>ES5, InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Claude Pache">claude.pache</reporter>
          <assigned_to name="Jordan Harband">ljharb</assigned_to>
          <cc>commit-queue</cc>
    
    <cc>darin</cc>
    
    <cc>fpizlo</cc>
    
    <cc>ljharb</cc>
    
    <cc>p.niemirski</cc>
    
    <cc>timothy</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>ysuzuki</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1065809</commentid>
    <comment_count>0</comment_count>
    <who name="Claude Pache">claude.pache</who>
    <bug_when>2015-01-31 04:59:55 -0800</bug_when>
    <thetext>When the toJSON method is applied to an invalid date, it must return null, and not throwing a TypeError.

Note that this behaviour is different from the toISOString method.

For reference, see [ES5.1 Date.prototype.toJSON], steps 2 and 3.

[ES5.1 Date.prototype.toJSON]: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.44</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110406</commentid>
    <comment_count>1</comment_count>
    <who name="Jordan Harband">ljharb</who>
    <bug_when>2015-07-18 17:29:11 -0700</bug_when>
    <thetext>It&apos;d be nice to fix this one - Safari/WebKit seem to be skipping step 3.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110518</commentid>
    <comment_count>2</comment_count>
      <attachid>257066</attachid>
    <who name="Jordan Harband">ljharb</who>
    <bug_when>2015-07-19 14:45:58 -0700</bug_when>
    <thetext>Created attachment 257066
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110530</commentid>
    <comment_count>3</comment_count>
      <attachid>257066</attachid>
    <who name="Yusuke Suzuki">ysuzuki</who>
    <bug_when>2015-07-19 15:20:19 -0700</bug_when>
    <thetext>Comment on attachment 257066
Patch

r=me</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110533</commentid>
    <comment_count>4</comment_count>
      <attachid>257066</attachid>
    <who name="Darin Adler">darin</who>
    <bug_when>2015-07-19 15:37:44 -0700</bug_when>
    <thetext>Comment on attachment 257066
Patch

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

&gt; Source/JavaScriptCore/runtime/DatePrototype.cpp:1076
&gt; +    JSValue timeValue = object-&gt;toPrimitive(exec, PreferNumber);

This patch seems wrong.

If &quot;this&quot; has a valueOf function, the code will call that function, but will ignore the value the function returns except for checking it to see if it’s infinity or NaN. I don’t think that’s correct behavior.

Is that really the behavior the specification calls for? If so, why?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110534</commentid>
    <comment_count>5</comment_count>
    <who name="Jordan Harband">ljharb</who>
    <bug_when>2015-07-19 15:40:05 -0700</bug_when>
    <thetext>Absolutely it is. It&apos;s specifically to catch the case that WebKit is buggy here on: when toISOString() would throw, toJSON should NOT throw, but should return `null`.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110535</commentid>
    <comment_count>6</comment_count>
    <who name="Jordan Harband">ljharb</who>
    <bug_when>2015-07-19 15:40:47 -0700</bug_when>
    <thetext>Put another way, serializing an object to JSON should ideally never cause it to throw, but you can&apos;t serialize an invalid date to JSON in WebKit - because it throws (incorrectly).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110536</commentid>
    <comment_count>7</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2015-07-19 15:44:01 -0700</bug_when>
    <thetext>OK, that sounds wrong.

This will work with the built in toISOString function.

But note that we intentionally call whatever toISOString function is present, which could be any arbitrary function. How can we predict what toISOString will do without calling it? It could even be a function that returns different values when you call it multiple times.

I’m pretty sure the right way to handle this is to detect that toISOString tried to throw an exception, and then clear the exception and return null. Not to try to preflight it, which can’t be done reliably.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110537</commentid>
    <comment_count>8</comment_count>
      <attachid>257066</attachid>
    <who name="Yusuke Suzuki">ysuzuki</who>
    <bug_when>2015-07-19 15:44:07 -0700</bug_when>
    <thetext>Comment on attachment 257066
Patch

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

&gt;&gt; Source/JavaScriptCore/runtime/DatePrototype.cpp:1076
&gt;&gt; +    JSValue timeValue = object-&gt;toPrimitive(exec, PreferNumber);
&gt; 
&gt; This patch seems wrong.
&gt; 
&gt; If &quot;this&quot; has a valueOf function, the code will call that function, but will ignore the value the function returns except for checking it to see if it’s infinity or NaN. I don’t think that’s correct behavior.
&gt; 
&gt; Is that really the behavior the specification calls for? If so, why?

This behavior is actually specified here.
http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.5.44

1. Let O be the result of calling ToObject, giving it the this value as its argument.
2. Let tv be ToPrimitive(O, hint Number).
3. If tv is a Number and is not finite, return null.
4. Let toISO be the result of calling the [[Get]] internal method of O with argument &quot;toISOString&quot;.
5. If IsCallable(toISO) is false, throw a TypeError exception.
6. Return the result of calling the [[Call]] internal method of toISO with O as the this value and an empty argument list.

And it is the same in the ES6 spec.
http://ecma-international.org/ecma-262/6.0/#sec-date.prototype.tojso

tv is just used to check that the toPrimitive result value is finite or not.

I think this is becacuse Date#toJSON is defined as the generic function. The spec notes that

&gt; NOTE 2 The toJSON function is intentionally generic; it does not require that its this value be a Date object. Therefore, it can be transferred to other kinds of objects for use as a method. However, it does require that any such object have a toISOString method.

So to check the held value, instead of extracting the value of Date&apos;s private field, it calls `toPrimitive` to extract it from the possibly date object.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110538</commentid>
    <comment_count>9</comment_count>
    <who name="Darin Adler">darin</who>
    <bug_when>2015-07-19 15:46:10 -0700</bug_when>
    <thetext>OK, that is a strange way to specify it, but I accept that it’s what the standard calls for.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110540</commentid>
    <comment_count>10</comment_count>
      <attachid>257066</attachid>
    <who name="WebKit Commit Bot">commit-queue</who>
    <bug_when>2015-07-19 16:11:25 -0700</bug_when>
    <thetext>Comment on attachment 257066
Patch

Clearing flags on attachment: 257066

Committed r187016: &lt;http://trac.webkit.org/changeset/187016&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1110541</commentid>
    <comment_count>11</comment_count>
    <who name="WebKit Commit Bot">commit-queue</who>
    <bug_when>2015-07-19 16:11:29 -0700</bug_when>
    <thetext>All reviewed patches have been landed.  Closing bug.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1121534</commentid>
    <comment_count>12</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2015-08-27 18:53:12 -0700</bug_when>
    <thetext>&lt;rdar://problem/22468732&gt;</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>257066</attachid>
            <date>2015-07-19 14:45:58 -0700</date>
            <delta_ts>2015-07-19 16:11:25 -0700</delta_ts>
            <desc>Patch</desc>
            <filename>bug-141115-20150719144446.patch</filename>
            <type>text/plain</type>
            <size>3775</size>
            <attacher name="Jordan Harband">ljharb</attacher>
            
              <data encoding="base64">U3VidmVyc2lvbiBSZXZpc2lvbjogMTg3MDA4CmRpZmYgLS1naXQgYS9Tb3VyY2UvSmF2YVNjcmlw
dENvcmUvQ2hhbmdlTG9nIGIvU291cmNlL0phdmFTY3JpcHRDb3JlL0NoYW5nZUxvZwppbmRleCBk
YzkxYzFiOGE1YzhjNmY0MGZiNWIxNzJjYTliMDBhZmEwZDI4NjNmLi44MDAzMmRiZDhkNjc2YjE1
OGUxOTg3NzBjNDE4OTI1MjJlMTAwNWY4IDEwMDY0NAotLS0gYS9Tb3VyY2UvSmF2YVNjcmlwdENv
cmUvQ2hhbmdlTG9nCisrKyBiL1NvdXJjZS9KYXZhU2NyaXB0Q29yZS9DaGFuZ2VMb2cKQEAgLTEs
MyArMSwxMyBAQAorMjAxNS0wNy0xOSAgSm9yZGFuIEhhcmJhbmQgIDxsamhhcmJAZ21haWwuY29t
PgorCisgICAgICAgIG5ldyBEYXRlKE5hTikudG9KU09OKCkgbXVzdCByZXR1cm4gbnVsbCBpbnN0
ZWFkIG9mIHRocm93aW5nIGEgVHlwZUVycm9yCisgICAgICAgIGh0dHBzOi8vYnVncy53ZWJraXQu
b3JnL3Nob3dfYnVnLmNnaT9pZD0xNDExMTUKKworICAgICAgICBSZXZpZXdlZCBieSBOT0JPRFkg
KE9PUFMhKS4KKworICAgICAgICAqIHJ1bnRpbWUvRGF0ZVByb3RvdHlwZS5jcHA6CisgICAgICAg
IChKU0M6OmRhdGVQcm90b0Z1bmNUb0pTT04pOgorCiAyMDE1LTA3LTE4ICBTYWFtIGJhcmF0aSAg
PHNhYW1iYXJhdGkxQGdtYWlsLmNvbT4KIAogICAgICAgICBsZXhpY2FsIHNjb3BpbmcgaXMgYnJv
a2VuIHdpdGggcmVzcGVjdCB0byAiYnJlYWsiIGFuZCAiY29udGludWUiCmRpZmYgLS1naXQgYS9T
b3VyY2U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