#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 toJS(ExecState*, JSDOMGlobalObject* globalObject, Blob* blob)
{
if (!blob)
return jsNull();
if (blob->isFile())
return wrap<JSFile>(globalObject, static_cast<File*>(blob));
return wrap<JSBlob>(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()) {
RefPtr<Blob> blob = Blob::create();
return JSValue::encode(CREATE_DOM_WRAPPER(jsConstructor->globalObject(), Blob, blob.get()));
}
unsigned blobPartsLength = 0;
JSObject* blobParts = toJSSequence(exec, exec->argument(0), blobPartsLength);
if (exec->hadException())
return JSValue::encode(jsUndefined());
ASSERT(blobParts);
String type;
String endings = ASCIILiteral("transparent");
if (exec->argumentCount() > 1) {
JSValue blobPropertyBagValue = exec->argument(1);
if (!blobPropertyBagValue.isObject())
return throwVMError(exec, createTypeError(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 throwVMError(exec, createTypeError(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 (RefPtr<ArrayBufferView> arrayBufferView = toArrayBufferView(item))
blobBuilder.append(arrayBufferView.release());
else if (Blob* blob = JSBlob::toWrapped(item))
blobBuilder.append(blob);
else {
String string = item.toString(exec)->value(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
blobBuilder.append(string, endings);
}
}
RefPtr<Blob> blob = Blob::create(blobBuilder.finalize(), Blob::normalizedContentType(type));
return JSValue::encode(CREATE_DOM_WRAPPER(jsConstructor->globalObject(), Blob, blob.get()));
}
}