JSValue jsXMLHttpRequestResponseXML(ExecState* exec, const Identifier&, const PropertySlot& slot)
{
    JSXMLHttpRequest* castedThis = static_cast<JSXMLHttpRequest*>(asObject(slot.slotBase()));
    UNUSED_PARAM(exec);
    XMLHttpRequest* imp = static_cast<XMLHttpRequest*>(castedThis->impl());
    return toJS(exec, castedThis->globalObject(), WTF::getPtr(imp->responseXML()));
}
Exemple #2
0
void V8XMLHttpRequest::responseAttributeGetterCustom(
    const v8::FunctionCallbackInfo<v8::Value>& info) {
  XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toImpl(info.Holder());
  ExceptionState exceptionState(info.GetIsolate(),
                                ExceptionState::GetterContext, "XMLHttpRequest",
                                "response");

  switch (xmlHttpRequest->getResponseTypeCode()) {
    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::Local<v8::Value> json = fromJSONString(
          isolate, toCoreString(jsonSource.v8Value()), exceptionState);
      if (exceptionState.hadException()) {
        exceptionState.clearException();
        v8SetReturnValue(info, v8::Null(isolate));
      } else {
        v8SetReturnValue(info, json);
      }
      return;
    }

    case XMLHttpRequest::ResponseTypeDocument: {
      Document* document = xmlHttpRequest->responseXML(exceptionState);
      v8SetReturnValueFast(info, document, xmlHttpRequest);
      return;
    }

    case XMLHttpRequest::ResponseTypeBlob: {
      Blob* blob = xmlHttpRequest->responseBlob();
      v8SetReturnValueFast(info, blob, xmlHttpRequest);
      return;
    }

    case XMLHttpRequest::ResponseTypeArrayBuffer: {
      DOMArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer();
      v8SetReturnValueFast(info, arrayBuffer, xmlHttpRequest);
      return;
    }
  }
}
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;
        }
    }
}
JSValue* JSXMLHttpRequest::getValueProperty(ExecState* exec, int token) const
{
    switch (token) {
    case OnabortAttrNum: {
        return onabort(exec);
    }
    case OnerrorAttrNum: {
        return onerror(exec);
    }
    case OnloadAttrNum: {
        return onload(exec);
    }
    case OnloadstartAttrNum: {
        return onloadstart(exec);
    }
    case OnprogressAttrNum: {
        return onprogress(exec);
    }
    case OnreadystatechangeAttrNum: {
        return onreadystatechange(exec);
    }
    case ReadyStateAttrNum: {
        XMLHttpRequest* imp = static_cast<XMLHttpRequest*>(impl());
        return jsNumber(exec, imp->readyState());
    }
    case ResponseTextAttrNum: {
        XMLHttpRequest* imp = static_cast<XMLHttpRequest*>(impl());
        return jsStringOrNull(exec, imp->responseText());
    }
    case ResponseXMLAttrNum: {
        XMLHttpRequest* imp = static_cast<XMLHttpRequest*>(impl());
        return toJS(exec, WTF::getPtr(imp->responseXML()));
    }
    case StatusAttrNum: {
        ExceptionCode ec = 0;
        XMLHttpRequest* imp = static_cast<XMLHttpRequest*>(impl());
        KJS::JSValue* result = jsNumber(exec, imp->status(ec));
        setDOMException(exec, ec);
        return result;
    }
    case StatusTextAttrNum: {
        ExceptionCode ec = 0;
        XMLHttpRequest* imp = static_cast<XMLHttpRequest*>(impl());
        KJS::JSValue* result = jsString(exec, imp->statusText(ec));
        setDOMException(exec, ec);
        return result;
    }
    }
    return 0;
}