Bug 26592 - Support standard toJSON functions
: Support standard toJSON functions
Status: RESOLVED FIXED
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore
: 528+ (Nightly build)
: Macintosh Mac OS X 10.5
: P2 Normal
Assigned To: Oliver Hunt
:
Depends on: 26594
Blocks: 20031
  Show dependency treegraph
 
Reported: 2009-06-21 16:16 PDT by Oliver Hunt
Modified: 2009-06-21 23:36 PDT (History)
1 user (show)

See Also:


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

Note You need to log in before you can comment on or make changes to this bug.
Description Oliver Hunt 2009-06-21 16:16:45 PDT
ES5 defines toJSON functions on a number of standard objects.  For complete JSON support we need to implement these.
Comment 1 Erik Arvidsson 2009-06-21 17:49:38 PDT
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 Oliver Hunt 2009-06-21 22:41:49 PDT
Created attachment 31634 [details]
Support Date.toJSON
Comment 3 Darin Adler 2009-06-21 22:45:37 PDT
Comment on attachment 31634 [details]
Support Date.toJSON

> +    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 Oliver Hunt 2009-06-21 23:36:10 PDT
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