Ignore:
Timestamp:
Sep 3, 2013, 11:45:51 AM (12 years ago)
Author:
[email protected]
Message:

Support the "json" responseType and JSON response entity in XHR
https://bugs.webkit.org/show_bug.cgi?id=73648

Reviewed by Oliver Hunt.

Source/JavaScriptCore:

Based on the patch written by Jarred Nicholls.

Add JSC::JSONParse. This function will be used in XMLHttpRequest.response of type 'json'.

(JSC::JSONParse):

  • runtime/JSONObject.h:

Source/WebCore:

Based on the patch written by Jarred Nicholls.

Implement 'json' type for XMLHttpRequest.response. We cache the result on JSC side as a cached attribute
unlike other response types like 'document' and 'blob' for which the parsed response object is cached
in XMLHttpRequest itself. In the long run, we should do the same for other types of response types.

Also refactored the various code to share the code.

Tests: fast/xmlhttprequest/xmlhttprequest-responsetype-json-invalid.html

fast/xmlhttprequest/xmlhttprequest-responsetype-json-utf16.html
fast/xmlhttprequest/xmlhttprequest-responsetype-json-valid.html

  • ForwardingHeaders/runtime/JSONObject.h: Added.
  • bindings/js/JSXMLHttpRequestCustom.cpp:

(WebCore::JSXMLHttpRequest::visitChildren):
(WebCore::JSXMLHttpRequest::response): Use JSONParse to parse the response text and cache the result.
Call didCacheResponseJSON to set the cache status and clear the original response buffer.

  • xml/XMLHttpRequest.cpp:

(WebCore::XMLHttpRequest::XMLHttpRequest): Added m_responseCacheIsValid to invalidate the cache of
a json response.
(WebCore::XMLHttpRequest::responseText):
(WebCore::XMLHttpRequest::didCacheResponseJSON): Added; Updates m_responseCacheIsValid and clears the
response buffer to save memory.
(WebCore::XMLHttpRequest::responseXML):
(WebCore::XMLHttpRequest::setResponseType):
(WebCore::XMLHttpRequest::responseType):
(WebCore::XMLHttpRequest::clearResponseBuffers):
(WebCore::XMLHttpRequest::didReceiveData):

  • xml/XMLHttpRequest.h:

(WebCore::XMLHttpRequest::doneWithoutErrors): Extracted from responseXML.
(WebCore::XMLHttpRequest::responseTextIgnoringResponseType): Extracted from responseText.
(WebCore::XMLHttpRequest::responseCacheIsValid): Added.
(WebCore::XMLHttpRequest::shouldDecodeResponse): Extracted from didReceiveData.
Also modified to decode when the response type is ResponseTypeJSON.

  • xml/XMLHttpRequest.idl: Added CachedAttribute IDL extention on response property. This cache is

used when the response type is 'json'.

LayoutTests:

Add regression tests for XMLHttpRequest.response of type 'json'.

Two of these tests (valid & invalid) come from Jarred Nicholls's original patch.

  • fast/xmlhttprequest/resources/xmlhttprequest-responsetype-json-utf-16.json: Added.
  • fast/xmlhttprequest/resources/xmlhttprequest-responsetype-json.json: Added.
  • fast/xmlhttprequest/xmlhttprequest-responsetype-json-invalid-expected.txt: Added.
  • fast/xmlhttprequest/xmlhttprequest-responsetype-json-invalid.html: Added.
  • fast/xmlhttprequest/xmlhttprequest-responsetype-json-utf16-expected.txt: Added.
  • fast/xmlhttprequest/xmlhttprequest-responsetype-json-utf16.html: Added.
  • fast/xmlhttprequest/xmlhttprequest-responsetype-json-valid-expected.txt: Added.
  • fast/xmlhttprequest/xmlhttprequest-responsetype-json-valid.html: Added.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/runtime/JSONObject.cpp

    r154797 r154992  
    820820}
    821821
     822JSValue JSONParse(ExecState* exec, const String& json)
     823{
     824    LocalScope scope(exec->vm());
     825
     826    if (json.is8Bit()) {
     827        LiteralParser<LChar> jsonParser(exec, json.characters8(), json.length(), StrictJSON);
     828        return jsonParser.tryLiteralParse();
     829    }
     830
     831    LiteralParser<UChar> jsonParser(exec, json.characters16(), json.length(), StrictJSON);
     832    return jsonParser.tryLiteralParse();
     833}
     834
    822835String JSONStringify(ExecState* exec, JSValue value, unsigned indent)
    823836{
Note: See TracChangeset for help on using the changeset viewer.