JSArrayBufferViewHelper.h [plain text]
#ifndef JSArrayBufferViewHelper_h
#define JSArrayBufferViewHelper_h
#include "ExceptionCode.h"
#include "JSArrayBuffer.h"
#include "JSDOMBinding.h"
#include <interpreter/CallFrame.h>
#include <runtime/ArgList.h>
#include <runtime/Error.h>
#include <runtime/JSObject.h>
#include <runtime/JSValue.h>
#include <wtf/ArrayBufferView.h>
namespace WebCore {
template <class T>
JSC::JSValue setWebGLArrayHelper(JSC::ExecState* exec, T* impl, T* (*conversionFunc)(JSC::JSValue))
{
if (exec->argumentCount() < 1)
return JSC::throwSyntaxError(exec);
T* array = (*conversionFunc)(exec->argument(0));
if (array) {
unsigned offset = 0;
if (exec->argumentCount() == 2)
offset = exec->argument(1).toInt32(exec);
if (!impl->set(array, offset))
setDOMException(exec, INDEX_SIZE_ERR);
return JSC::jsUndefined();
}
if (exec->argument(0).isObject()) {
JSC::JSObject* array = JSC::asObject(exec->argument(0));
uint32_t offset = 0;
if (exec->argumentCount() == 2)
offset = exec->argument(1).toInt32(exec);
uint32_t length = array->get(exec, JSC::Identifier(exec, "length")).toInt32(exec);
if (offset > impl->length()
|| offset + length > impl->length()
|| offset + length < offset)
setDOMException(exec, INDEX_SIZE_ERR);
else {
for (uint32_t i = 0; i < length; i++) {
JSC::JSValue v = array->get(exec, i);
if (exec->hadException())
return JSC::jsUndefined();
impl->set(i + offset, v.toNumber(exec));
}
}
return JSC::jsUndefined();
}
return JSC::throwSyntaxError(exec);
}
template<class C, typename T>
PassRefPtr<C> constructArrayBufferViewWithArrayBufferArgument(JSC::ExecState* exec)
{
RefPtr<ArrayBuffer> buffer = toArrayBuffer(exec->argument(0));
if (!buffer)
return 0;
unsigned offset = (exec->argumentCount() > 1) ? exec->argument(1).toUInt32(exec) : 0;
unsigned int length = 0;
if (exec->argumentCount() > 2)
length = exec->argument(2).toUInt32(exec);
else {
if ((buffer->byteLength() - offset) % sizeof(T)) {
throwError(exec, createRangeError(exec, "ArrayBuffer length minus the byteOffset is not a multiple of the element size."));
return 0;
}
length = (buffer->byteLength() - offset) / sizeof(T);
}
RefPtr<C> array = C::create(buffer, offset, length);
if (!array)
setDOMException(exec, INDEX_SIZE_ERR);
return array;
}
template<class C, typename T>
PassRefPtr<C> constructArrayBufferView(JSC::ExecState* exec)
{
if (exec->argumentCount() < 1)
return C::create(0);
if (exec->argument(0).isNull()) {
throwTypeError(exec);
return 0;
}
if (exec->argument(0).isObject()) {
RefPtr<C> view = constructArrayBufferViewWithArrayBufferArgument<C, T>(exec);
if (view)
return view;
JSC::JSObject* srcArray = asObject(exec->argument(0));
uint32_t length = srcArray->get(exec, JSC::Identifier(exec, "length")).toUInt32(exec);
RefPtr<C> array = C::create(length);
if (!array) {
setDOMException(exec, INDEX_SIZE_ERR);
return array;
}
for (unsigned i = 0; i < length; ++i) {
JSC::JSValue v = srcArray->get(exec, i);
array->set(i, v.toNumber(exec));
}
return array;
}
int length = exec->argument(0).toInt32(exec);
RefPtr<C> result;
if (length >= 0)
result = C::create(static_cast<unsigned>(length));
if (!result)
throwError(exec, createRangeError(exec, "ArrayBufferView size is not a small enough positive integer."));
return result;
}
template <typename JSType, typename WebCoreType>
static JSC::JSValue toJSArrayBufferView(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, WebCoreType* object)
{
if (!object)
return JSC::jsNull();
if (JSDOMWrapper* wrapper = getCachedWrapper(currentWorld(exec), object))
return wrapper;
exec->heap()->reportExtraMemoryCost(object->byteLength());
return createWrapper<JSType>(exec, globalObject, object);
}
}
#endif // JSArrayBufferViewHelper_h