LibWeb: Use Infra AO for JSON parsing in XMLHttpRequest::response()

This commit is contained in:
Linus Groh 2022-10-04 18:05:26 +01:00
parent d2e9faf2da
commit 183462acfd
Notes: sideshowbarker 2024-07-17 06:22:16 +09:00

View file

@ -11,7 +11,6 @@
#include <AK/ByteBuffer.h>
#include <AK/GenericLexer.h>
#include <AK/QuickSort.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibJS/Runtime/GlobalObject.h>
@ -30,6 +29,7 @@
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Origin.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Infra/JSON.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/WebIDL/DOMException.h>
@ -97,14 +97,16 @@ WebIDL::ExceptionOr<String> XMLHttpRequest::response_text() const
// https://xhr.spec.whatwg.org/#response
WebIDL::ExceptionOr<JS::Value> XMLHttpRequest::response()
{
auto& vm = this->vm();
// 1. If thiss response type is the empty string or "text", then:
if (m_response_type == Bindings::XMLHttpRequestResponseType::Empty || m_response_type == Bindings::XMLHttpRequestResponseType::Text) {
// 1. If thiss state is not loading or done, then return the empty string.
if (m_ready_state != ReadyState::Loading && m_ready_state != ReadyState::Done)
return JS::Value(JS::js_string(vm(), ""));
return JS::Value(JS::js_string(vm, ""));
// 2. Return the result of getting a text response for this.
return JS::Value(JS::js_string(vm(), get_text_response()));
return JS::Value(JS::js_string(vm, get_text_response()));
}
// 2. If thiss state is not done, then return null.
if (m_ready_state != ReadyState::Done)
@ -153,9 +155,7 @@ WebIDL::ExceptionOr<JS::Value> XMLHttpRequest::response()
return JS::Value(JS::js_null());
// 3. Let jsonObject be the result of running parse JSON from bytes on thiss received bytes. If that threw an exception, then return null.
TextCodec::UTF8Decoder decoder;
auto json_object_result = JS::call(vm(), realm().intrinsics().json_parse_function(), JS::js_undefined(), JS::js_string(vm(), decoder.to_utf8({ m_received_bytes.data(), m_received_bytes.size() })));
auto json_object_result = Infra::parse_json_bytes_to_javascript_value(vm, m_received_bytes);
if (json_object_result.is_error())
return JS::Value(JS::js_null());