Bug 26592 - Support standard toJSON functions
: Support standard toJSON functions
Status: RESOLVED FIXED
: WebKit
JavaScriptCore
: 528+ (Nightly build)
: Macintosh Mac OS X 10.5
: P2 Normal
Assigned To:
:
:
: 26594
: 20031
  Show dependency treegraph
 
Reported: 2009-06-21 16:16 PST by
Modified: 2009-06-21 23:36 PST (History)


Attachments
Support Date.toJSON (6.20 KB, patch)
2009-06-21 22:41 PST, Oliver Hunt
darin: review+
Review Patch | Details | Formatted Diff | Diff


Note

You need to log in before you can comment on or make changes to this bug.


Description From 2009-06-21 16:16:45 PST
ES5 defines toJSON functions on a number of standard objects.  For complete JSON support we need to implement these.
------- Comment #1 From 2009-06-21 17:49:38 PST -------
Note that Boolean.prototype.toJSON, Number.prototype.toJSON and String.prototype.toJSON have been deleted from the spec. See section "15.12.3 JSON.stringify" at http://wiki.ecmascript.org/lib/exe/fetch.php?id=es3.1%3Aes3.1_proposal_working_draft&cache=cache&media=es3.1:es5_candidate_errata_june_17.pdf
------- Comment #2 From 2009-06-21 22:41:49 PST -------
Created an attachment (id=31634) [details]
Support Date.toJSON
------- Comment #3 From 2009-06-21 22:45:37 PST -------
(From update of attachment 31634 [details])
> +    JSValue toISOValue = object->get(exec, Identifier(exec, "toISOString"));

Should we make toISOString a common identifier?

> +    if (!toISOValue.isObject())
> +        return throwError(exec, TypeError, "toISOString is not a function");
> +    
> +    JSObject* toISO = asObject(toISOValue);
> +    CallData callData;
> +    CallType callType = toISO->getCallData(callData);
> +    if (callType == CallTypeNone)
> +        return throwError(exec, TypeError, "toISOString is not a function");

You should just use the JSValue version of getCallData, which does the same thing as above. You could do the asObject() cast in the call.

You should add a test case that returns a string wrapper, since that's an easy case to get wrong.

r=me
------- Comment #4 From 2009-06-21 23:36:10 PST -------
Committing to http://svn.webkit.org/repository/webkit/trunk ...
    M    JavaScriptCore/ChangeLog
    M    JavaScriptCore/runtime/CommonIdentifiers.h
    M    JavaScriptCore/runtime/DatePrototype.cpp
    M    LayoutTests/ChangeLog
    M    LayoutTests/fast/js/JSON-stringify-expected.txt
    M    LayoutTests/fast/js/resources/JSON-stringify.js
Committed r44931