Changeset 44931 in webkit


Ignore:
Timestamp:
Jun 21, 2009 11:35:51 PM (15 years ago)
Author:
oliver@apple.com
Message:

Bug 26592: Support standard toJSON functions
<https://bugs.webkit.org/show_bug.cgi?id=26592>

Reviewed by Darin Adler

Add support for the standard Date.toJSON function.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r44929 r44931  
     12009-06-21  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Darin Adler.
     4
     5        Bug 26592: Support standard toJSON functions
     6        <https://bugs.webkit.org/show_bug.cgi?id=26592>
     7
     8        Add support for the standard Date.toJSON function.
     9
     10        * runtime/DatePrototype.cpp:
     11        (JSC::dateProtoFuncToJSON):
     12
    1132009-06-21  Oliver Hunt  <oliver@apple.com>
    214
  • trunk/JavaScriptCore/runtime/CommonIdentifiers.h

    r44813 r44931  
    6060    macro(toExponential) \
    6161    macro(toFixed) \
     62    macro(toISOString) \
    6263    macro(toJSON) \
    6364    macro(toLocaleString) \
  • trunk/JavaScriptCore/runtime/DatePrototype.cpp

    r44929 r44931  
    115115static JSValue JSC_HOST_CALL dateProtoFuncToUTCString(ExecState*, JSObject*, JSValue, const ArgList&);
    116116static JSValue JSC_HOST_CALL dateProtoFuncToISOString(ExecState*, JSObject*, JSValue, const ArgList&);
     117
     118static JSValue JSC_HOST_CALL dateProtoFuncToJSON(ExecState*, JSObject*, JSValue, const ArgList&);
    117119
    118120}
     
    388390  setYear               dateProtoFuncSetYear                 DontEnum|Function       1
    389391  getYear               dateProtoFuncGetYear                 DontEnum|Function       0
     392  toJSON                dateProtoFuncToJSON                  DontEnum|Function       0
    390393@end
    391394*/
     
    10771080}
    10781081
     1082JSValue JSC_HOST_CALL dateProtoFuncToJSON(ExecState* exec, JSObject*, JSValue thisValue, const ArgList&)
     1083{
     1084    JSObject* object = thisValue.toThisObject(exec);
     1085    if (exec->hadException())
     1086        return jsNull();
     1087   
     1088    JSValue toISOValue = object->get(exec, exec->globalData().propertyNames->toISOString);
     1089    if (exec->hadException())
     1090        return jsNull();
     1091
     1092    CallData callData;
     1093    CallType callType = toISOValue.getCallData(callData);
     1094    if (callType == CallTypeNone)
     1095        return throwError(exec, TypeError, "toISOString is not a function");
     1096
     1097    JSValue result = call(exec, asObject(toISOValue), callType, callData, object, exec->emptyList());
     1098    if (exec->hadException())
     1099        return jsNull();
     1100    if (result.isObject())
     1101        return throwError(exec, TypeError, "toISOString did not return a primitive value");
     1102    return result;
     1103}
     1104
    10791105} // namespace JSC
  • trunk/LayoutTests/ChangeLog

    r44929 r44931  
     12009-06-21  Oliver Hunt  <oliver@apple.com>
     2
     3        Reviewed by Darin Adler.
     4
     5        Bug 26592: Support standard toJSON functions
     6
     7        Add tests of Date.toJSON.
     8
     9        * fast/js/JSON-stringify-expected.txt:
     10        * fast/js/resources/JSON-stringify.js:
     11        (createTests.result):
     12        (createTests.result.push.):
     13
    1142009-06-21  Oliver Hunt  <oliver@apple.com>
    215
  • trunk/LayoutTests/fast/js/JSON-stringify-expected.txt

    r44813 r44931  
     1function (jsonObject) {
     2        return jsonObject.stringify(new Date(0));
     3    }
     4PASS tests[i](nativeJSON) is tests[i](JSON)
     5function (jsonObject) {
     6        return jsonObject.stringify({toJSON: Date.prototype.toJSON});
     7    }
     8PASS tests[i](nativeJSON) threw exception TypeError: toISOString is not a function.
     9function (jsonObject) {
     10        return jsonObject.stringify({toJSON: Date.prototype.toJSON, toISOString: function(){ return "custom toISOString"; }});
     11    }
     12PASS tests[i](nativeJSON) is tests[i](JSON)
    113function (jsonObject) {
    214        return jsonObject.stringify({get Foo() { return "bar"; }});
  • trunk/LayoutTests/fast/js/resources/JSON-stringify.js

    r44813 r44931  
    66    var complexObject = {a:"1", b:"2", c:"3", d:undefined, e:null, "":12, get f(){ return simpleArray; }, array: complexArray};
    77    var result = [];
     8    result.push(function(jsonObject){
     9        return jsonObject.stringify(new Date(0));
     10    });
     11    result.push(function(jsonObject){
     12        return jsonObject.stringify({toJSON: Date.prototype.toJSON});
     13    });
     14    result[result.length - 1].throws = true;
     15    result.push(function(jsonObject){
     16        return jsonObject.stringify({toJSON: Date.prototype.toJSON, toISOString: function(){ return "custom toISOString"; }});
     17    });
     18    result.push(function(jsonObject){
     19        return jsonObject.stringify({toJSON: Date.prototype.toJSON, toISOString: function(){ return {}; }});
     20    });
     21    result[result.length - 1].throws = true;
     22    result.push(function(jsonObject){
     23        return jsonObject.stringify({toJSON: Date.prototype.toJSON, toISOString: function(){ throw "An exception"; }});
     24    });
     25    result[result.length - 1].throws = true;
     26    result.push(function(jsonObject){
     27        var d = new Date(0);
     28        d.toISOString = null;
     29        return jsonObject.stringify(d);
     30    });
     31    result[result.length - 1].throws = true;
     32    result.push(function(jsonObject){
     33        var d = new Date(0);
     34        d.toJSON = undefined;
     35        return jsonObject.stringify(d);
     36    });
    837    result.push(function(jsonObject){
    938        return jsonObject.stringify({get Foo() { return "bar"; }});
Note: See TracChangeset for help on using the changeset viewer.