#include "config.h"
#include "JSBlob.h"
#include "Blob.h"
#include "ExceptionCode.h"
#include "ExceptionCodePlaceholder.h"
#include "JSDOMBinding.h"
#include "JSDictionary.h"
#include "JSFile.h"
#include "WebKitBlobBuilder.h"
#include <runtime/Error.h>
#include <runtime/JSArray.h>
#include <runtime/JSArrayBuffer.h>
#include <runtime/JSArrayBufferView.h>
#include <wtf/Assertions.h>
using namespace JSC;
namespace WebCore {
JSValue toJSNewlyCreated(JSC::ExecState*, JSDOMGlobalObject* globalObject, Ref<Blob>&& blob)
{
if (is<File>(blob))
return CREATE_DOM_WRAPPER(globalObject, File, WTFMove(blob));
return createWrapper<JSBlob>(globalObject, WTFMove(blob));
}
JSValue toJS(ExecState* state, JSDOMGlobalObject* globalObject, Blob& blob)
{
return wrap(state, globalObject, blob);
}
EncodedJSValue JSC_HOST_CALL constructJSBlob(ExecState& exec)
{
DOMConstructorObject* jsConstructor = jsCast<DOMConstructorObject*>(exec.callee());
ScriptExecutionContext* context = jsConstructor->scriptExecutionContext();
if (!context)
return throwVMError(&exec, createReferenceError(&exec, "Blob constructor associated document is unavailable"));
if (!exec.argumentCount()) {
return JSValue::encode(CREATE_DOM_WRAPPER(jsConstructor->globalObject(), Blob, Blob::create()));
}
unsigned blobPartsLength = 0;
JSObject* blobParts = toJSSequence(exec, exec.uncheckedArgument(0), blobPartsLength);
if (exec.hadException())
return JSValue::encode(jsUndefined());
ASSERT(blobParts);
String type;
String endings = ASCIILiteral("transparent");
if (exec.argumentCount() > 1) {
JSValue blobPropertyBagValue = exec.uncheckedArgument(1);
if (!blobPropertyBagValue.isObject())
return throwVMTypeError(&exec, "Second argument of the constructor is not of type Object");
JSObject* blobPropertyBagObject = blobPropertyBagValue.toObject(&exec);
JSDictionary dictionary(&exec, blobPropertyBagObject);
bool containsEndings = dictionary.get("endings", endings);
if (exec.hadException())
return JSValue::encode(jsUndefined());
if (containsEndings) {
if (endings != "transparent" && endings != "native")
return throwVMTypeError(&exec, "The endings property must be either \"transparent\" or \"native\"");
}
dictionary.get("type", type);
if (exec.hadException())
return JSValue::encode(jsUndefined());
}
ASSERT(endings == "transparent" || endings == "native");
BlobBuilder blobBuilder;
for (unsigned i = 0; i < blobPartsLength; ++i) {
JSValue item = blobParts->get(&exec, i);
if (exec.hadException())
return JSValue::encode(jsUndefined());
if (ArrayBuffer* arrayBuffer = toArrayBuffer(item))
blobBuilder.append(arrayBuffer);
else if (auto arrayBufferView = toArrayBufferView(item))
blobBuilder.append(WTFMove(arrayBufferView));
else if (Blob* blob = JSBlob::toWrapped(item))
blobBuilder.append(blob);
else {
String string = item.toWTFString(&exec);
if (exec.hadException())
return JSValue::encode(jsUndefined());
blobBuilder.append(string, endings);
}
}
auto blob = Blob::create(blobBuilder.finalize(), Blob::normalizedContentType(type));
return JSValue::encode(CREATE_DOM_WRAPPER(jsConstructor->globalObject(), Blob, WTFMove(blob)));
}
}