#include "config.h"
#include "JSBlob.h"
#include "Blob.h"
#include "ExceptionCode.h"
#include "ExceptionCodePlaceholder.h"
#include "JSArrayBuffer.h"
#include "JSArrayBufferView.h"
#include "JSDOMBinding.h"
#include "JSDictionary.h"
#include "JSFile.h"
#include "WebKitBlobBuilder.h"
#include <runtime/Error.h>
#include <runtime/JSArray.h>
#include <wtf/Assertions.h>
using namespace JSC;
namespace WebCore {
JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, Blob* blob)
{
if (!blob)
return jsNull();
if (blob->isFile())
return wrap<JSFile>(exec, globalObject, static_cast<File*>(blob));
return wrap<JSBlob>(exec, globalObject, blob);
}
EncodedJSValue JSC_HOST_CALL JSBlobConstructor::constructJSBlob(ExecState* exec)
{
JSBlobConstructor* jsConstructor = jsCast<JSBlobConstructor*>(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(exec, jsConstructor->globalObject(), Blob, blob.get()));
}
JSValue firstArg = exec->argument(0);
if (!isJSArray(firstArg))
return throwVMError(exec, createTypeError(exec, "First argument of the constructor is not of type Array"));
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;
JSArray* array = asArray(firstArg);
unsigned length = array->length();
for (unsigned i = 0; i < length; ++i) {
JSValue item = array->getIndex(exec, i);
#if ENABLE(BLOB)
if (item.inherits(&JSArrayBuffer::s_info))
blobBuilder.append(toArrayBuffer(item));
else if (item.inherits(&JSArrayBufferView::s_info))
blobBuilder.append(toArrayBufferView(item));
else
#endif
if (item.inherits(&JSBlob::s_info))
blobBuilder.append(toBlob(item));
else {
String string = item.toString(exec)->value(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
blobBuilder.append(string, endings);
}
}
RefPtr<Blob> blob = blobBuilder.getBlob(type);
return JSValue::encode(CREATE_DOM_WRAPPER(exec, jsConstructor->globalObject(), Blob, blob.get()));
}
}