v8::Handle<v8::Value> V8XMLHttpRequest::responseAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info) { INC_STATS("DOM.XMLHttpRequest.response._get"); XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder()); switch (xmlHttpRequest->responseTypeCode()) { case XMLHttpRequest::ResponseTypeDefault: case XMLHttpRequest::ResponseTypeText: return responseTextAccessorGetter(name, info); case XMLHttpRequest::ResponseTypeDocument: { ExceptionCode ec = 0; Document* document = xmlHttpRequest->responseXML(ec); if (ec) { V8Proxy::setDOMException(ec); return v8::Undefined(); } return toV8(document); } case XMLHttpRequest::ResponseTypeBlob: #if ENABLE(XHR_RESPONSE_BLOB) { ExceptionCode ec = 0; Blob* blob = xmlHttpRequest->responseBlob(ec); if (ec) { V8Proxy::setDOMException(ec); return v8::Undefined(); } return toV8(blob); } #else return v8::Undefined(); #endif #if ENABLE(WEBGL) || ENABLE(BLOB) case XMLHttpRequest::ResponseTypeArrayBuffer: { ExceptionCode ec = 0; ArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer(ec); if (ec) { V8Proxy::setDOMException(ec); return v8::Undefined(); } return toV8(arrayBuffer); } #endif } return v8::Undefined(); }
v8::Handle<v8::Value> V8XMLHttpRequest::responseAttrGetterCustom(v8::Local<v8::String> name, const v8::AccessorInfo& info) { XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder()); switch (xmlHttpRequest->responseTypeCode()) { case XMLHttpRequest::ResponseTypeDefault: case XMLHttpRequest::ResponseTypeText: return responseTextAttrGetterCustom(name, info); case XMLHttpRequest::ResponseTypeDocument: { ExceptionCode ec = 0; Document* document = xmlHttpRequest->responseXML(ec); if (ec) return setDOMException(ec, info.GetIsolate()); return toV8Fast(document, info, xmlHttpRequest); } case XMLHttpRequest::ResponseTypeBlob: { ExceptionCode ec = 0; Blob* blob = xmlHttpRequest->responseBlob(ec); if (ec) return setDOMException(ec, info.GetIsolate()); return toV8Fast(blob, info, xmlHttpRequest); } case XMLHttpRequest::ResponseTypeArrayBuffer: { ExceptionCode ec = 0; ArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer(ec); if (ec) return setDOMException(ec, info.GetIsolate()); return toV8Fast(arrayBuffer, info, xmlHttpRequest); } } return v8::Undefined(); }
void V8XMLHttpRequest::responseAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info) { XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder()); switch (xmlHttpRequest->responseTypeCode()) { case XMLHttpRequest::ResponseTypeDefault: case XMLHttpRequest::ResponseTypeText: responseTextAttributeGetterCustom(info); return; case XMLHttpRequest::ResponseTypeJSON: { v8::Isolate* isolate = info.GetIsolate(); ScriptString jsonSource = xmlHttpRequest->responseJSONSource(); if (jsonSource.isEmpty()) { v8SetReturnValue(info, v8::Null(isolate)); return; } // Catch syntax error. v8::TryCatch exceptionCatcher; v8::Handle<v8::Value> json = v8::JSON::Parse(jsonSource.v8Value()); if (exceptionCatcher.HasCaught() || json.IsEmpty()) v8SetReturnValue(info, v8::Null(isolate)); else v8SetReturnValue(info, json); return; } case XMLHttpRequest::ResponseTypeDocument: { ExceptionState exceptionState(ExceptionState::GetterContext, "response", "XMLHttpRequest", info.Holder(), info.GetIsolate()); Document* document = xmlHttpRequest->responseXML(exceptionState); if (exceptionState.throwIfNeeded()) return; v8SetReturnValueFast(info, document, xmlHttpRequest); return; } case XMLHttpRequest::ResponseTypeBlob: { Blob* blob = xmlHttpRequest->responseBlob(); v8SetReturnValueFast(info, blob, xmlHttpRequest); return; } case XMLHttpRequest::ResponseTypeLegacyStream: { Stream* stream = xmlHttpRequest->responseLegacyStream(); v8SetReturnValueFast(info, stream, xmlHttpRequest); return; } case XMLHttpRequest::ResponseTypeStream: { ReadableStream* stream = xmlHttpRequest->responseStream(); v8SetReturnValueFast(info, stream, xmlHttpRequest); return; } case XMLHttpRequest::ResponseTypeArrayBuffer: { ArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer(); if (arrayBuffer) { arrayBuffer->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instanceTemplate()); } v8SetReturnValueFast(info, arrayBuffer, xmlHttpRequest); return; } } }
void V8XMLHttpRequest::responseAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info) { XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toImpl(info.Holder()); switch (xmlHttpRequest->responseTypeCode()) { case XMLHttpRequest::ResponseTypeDefault: case XMLHttpRequest::ResponseTypeText: responseTextAttributeGetterCustom(info); return; case XMLHttpRequest::ResponseTypeJSON: { v8::Isolate* isolate = info.GetIsolate(); ScriptString jsonSource = xmlHttpRequest->responseJSONSource(); if (jsonSource.isEmpty()) { v8SetReturnValue(info, v8::Null(isolate)); return; } // Catch syntax error. Swallows an exception (when thrown) as the // spec says. https://xhr.spec.whatwg.org/#response-body v8::TryCatch exceptionCatcher; v8::Local<v8::Value> json; if (v8Call(v8::JSON::Parse(isolate, jsonSource.v8Value()), json, exceptionCatcher)) v8SetReturnValue(info, json); else v8SetReturnValue(info, v8::Null(isolate)); return; } case XMLHttpRequest::ResponseTypeDocument: { ExceptionState exceptionState(ExceptionState::GetterContext, "response", "XMLHttpRequest", info.Holder(), info.GetIsolate()); Document* document = xmlHttpRequest->responseXML(exceptionState); if (exceptionState.throwIfNeeded()) return; v8SetReturnValueFast(info, document, xmlHttpRequest); return; } case XMLHttpRequest::ResponseTypeBlob: { Blob* blob = xmlHttpRequest->responseBlob(); v8SetReturnValueFast(info, blob, xmlHttpRequest); return; } case XMLHttpRequest::ResponseTypeLegacyStream: { Stream* stream = xmlHttpRequest->responseLegacyStream(); v8SetReturnValueFast(info, stream, xmlHttpRequest); return; } case XMLHttpRequest::ResponseTypeStream: { ReadableStream* stream = xmlHttpRequest->responseStream(); v8SetReturnValueFast(info, stream, xmlHttpRequest); return; } case XMLHttpRequest::ResponseTypeArrayBuffer: { DOMArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer(); v8SetReturnValueFast(info, arrayBuffer, xmlHttpRequest); return; } } }