JSGenericTypedArrayViewPrototypeInlines.h [plain text]
#ifndef JSGenericTypedArrayViewPrototypeInlines_h
#define JSGenericTypedArrayViewPrototypeInlines_h
#include "Error.h"
#include "ExceptionHelpers.h"
#include "JSFunction.h"
#include "JSGenericTypedArrayViewPrototype.h"
#include <wtf/StdLibExtras.h>
namespace JSC {
template<typename ViewClass>
EncodedJSValue JSC_HOST_CALL genericTypedArrayViewProtoFuncSet(ExecState* exec)
{
ViewClass* thisObject = jsDynamicCast<ViewClass*>(exec->thisValue());
if (!thisObject)
return throwVMError(exec, createTypeError(exec, "Receiver should be a typed array view"));
if (!exec->argumentCount())
return throwVMError(exec, createTypeError(exec, "Expected at least one argument"));
JSObject* sourceArray = jsDynamicCast<JSObject*>(exec->uncheckedArgument(0));
if (!sourceArray)
return throwVMError(exec, createTypeError(exec, "First argument should be an object"));
unsigned offset;
if (exec->argumentCount() >= 2) {
offset = exec->uncheckedArgument(1).toUInt32(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
} else
offset = 0;
unsigned length = sourceArray->get(exec, exec->vm().propertyNames->length).toUInt32(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
thisObject->set(exec, sourceArray, offset, length);
return JSValue::encode(jsUndefined());
}
template<typename ViewClass>
EncodedJSValue JSC_HOST_CALL genericTypedArrayViewProtoFuncSubarray(ExecState* exec)
{
JSFunction* callee = jsCast<JSFunction*>(exec->callee());
ViewClass* thisObject = jsDynamicCast<ViewClass*>(exec->thisValue());
if (!thisObject)
return throwVMError(exec, createTypeError(exec, "Receiver should be a typed array view"));
if (!exec->argumentCount())
return throwVMError(exec, createTypeError(exec, "Expected at least one argument"));
int32_t begin = exec->uncheckedArgument(0).toInt32(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
int32_t end;
if (exec->argumentCount() >= 2) {
end = exec->uncheckedArgument(1).toInt32(exec);
if (exec->hadException())
return JSValue::encode(jsUndefined());
} else
end = thisObject->length();
unsigned thisLength = thisObject->length();
if (begin < 0)
begin = std::max(static_cast<int>(thisLength + begin), 0);
if (end < 0)
end = std::max(static_cast<int>(thisLength + end), 0);
ASSERT(begin >= 0);
ASSERT(end >= 0);
begin = std::min(begin, static_cast<int32_t>(thisLength));
end = std::min(end, static_cast<int32_t>(thisLength));
end = std::max(begin, end);
ASSERT(end >= begin);
unsigned offset = begin;
unsigned length = end - begin;
RefPtr<ArrayBuffer> arrayBuffer = thisObject->buffer();
RELEASE_ASSERT(thisLength == thisObject->length());
Structure* structure =
callee->globalObject()->typedArrayStructure(ViewClass::TypedArrayStorageType);
ViewClass* result = ViewClass::create(
exec, structure, arrayBuffer,
thisObject->byteOffset() + offset * ViewClass::elementSize,
length);
return JSValue::encode(result);
}
template<typename ViewClass>
JSGenericTypedArrayViewPrototype<ViewClass>::JSGenericTypedArrayViewPrototype(VM& vm, Structure* structure)
: Base(vm, structure)
{
}
template<typename ViewClass>
void JSGenericTypedArrayViewPrototype<ViewClass>::finishCreation(
VM& vm, JSGlobalObject* globalObject)
{
Base::finishCreation(vm);
ASSERT(inherits(info()));
JSC_NATIVE_FUNCTION(vm.propertyNames->set, genericTypedArrayViewProtoFuncSet<ViewClass>, DontEnum, 2);
JSC_NATIVE_FUNCTION(vm.propertyNames->subarray, genericTypedArrayViewProtoFuncSubarray<ViewClass>, DontEnum, 2);
putDirect(vm, vm.propertyNames->BYTES_PER_ELEMENT, jsNumber(ViewClass::elementSize), DontEnum | ReadOnly | DontDelete);
}
template<typename ViewClass>
JSGenericTypedArrayViewPrototype<ViewClass>*
JSGenericTypedArrayViewPrototype<ViewClass>::create(
VM& vm, JSGlobalObject* globalObject, Structure* structure)
{
JSGenericTypedArrayViewPrototype* prototype =
new (NotNull, allocateCell<JSGenericTypedArrayViewPrototype>(vm.heap))
JSGenericTypedArrayViewPrototype(vm, structure);
prototype->finishCreation(vm, globalObject);
return prototype;
}
template<typename ViewClass>
Structure* JSGenericTypedArrayViewPrototype<ViewClass>::createStructure(
VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(
vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
}
#endif // JSGenericTypedArrayViewPrototypeInlines_h