V8WebGLArrayCustom.h [plain text]
#include "config.h"
#if ENABLE(3D_CANVAS)
#include "WebGLArrayBuffer.h"
#include "V8Binding.h"
#include "V8WebGLArrayBuffer.h"
#include "V8Proxy.h"
namespace WebCore {
template<class ArrayClass, class ElementType>
v8::Handle<v8::Value> constructWebGLArray(const v8::Arguments& args, WrapperTypeInfo* type, v8::ExternalArrayType arrayType)
{
if (!args.IsConstructCall())
return throwError("DOM object constructor cannot be called as a function.");
int argLen = args.Length();
if (argLen == 0) {
return args.Holder();
}
if (V8WebGLArrayBuffer::HasInstance(args[0])) {
WebGLArrayBuffer* buf = V8WebGLArrayBuffer::toNative(args[0]->ToObject());
if (!buf)
return throwError("Could not convert argument 0 to a WebGLArrayBuffer");
bool ok;
uint32_t offset = 0;
if (argLen > 1) {
offset = toUInt32(args[1], ok);
if (!ok)
return throwError("Could not convert argument 1 to a number");
}
uint32_t length = (buf->byteLength() - offset) / sizeof(ElementType);
if (argLen > 2) {
length = toUInt32(args[2], ok);
if (!ok)
return throwError("Could not convert argument 2 to a number");
}
RefPtr<ArrayClass> array = ArrayClass::create(buf, offset, length);
if (!array)
return throwError("Out-of-range offset and/or length");
V8DOMWrapper::setDOMWrapper(args.Holder(), type, array.get());
args.Holder()->SetIndexedPropertiesToExternalArrayData(array.get()->baseAddress(), arrayType, array.get()->length());
return toV8(array.release(), args.Holder());
}
uint32_t len = 0;
v8::Handle<v8::Object> srcArray;
if (args[0]->IsInt32()) {
len = toUInt32(args[0]);
} else if (args[0]->IsObject()) {
srcArray = args[0]->ToObject();
if (srcArray.IsEmpty())
return throwError("Could not convert argument 0 to an array");
len = toUInt32(srcArray->Get(v8::String::New("length")));
} else
return throwError("Could not convert argument 0 to either a number or an array");
RefPtr<ArrayClass> array = ArrayClass::create(len);
if (!array.get()) {
V8Proxy::setDOMException(INDEX_SIZE_ERR);
return v8::Undefined();
}
if (!srcArray.IsEmpty()) {
for (unsigned i = 0; i < len; i++) {
v8::Local<v8::Value> val = srcArray->Get(v8::Integer::NewFromUnsigned(i));
array->set(i, val->NumberValue());
}
}
V8DOMWrapper::setDOMWrapper(args.Holder(), type, array.get());
args.Holder()->SetIndexedPropertiesToExternalArrayData(array.get()->baseAddress(), arrayType, array.get()->length());
return toV8(array.release(), args.Holder());
}
template <class T>
v8::Handle<v8::Value> setWebGLArrayFromArray(T* webGLArray, const v8::Arguments& args)
{
if (args[0]->IsObject()) {
v8::Local<v8::Object> array = args[0]->ToObject();
uint32_t offset = 0;
if (args.Length() == 2)
offset = toUInt32(args[1]);
uint32_t length = toUInt32(array->Get(v8::String::New("length")));
if (offset > webGLArray->length() ||
offset + length > webGLArray->length() ||
offset + length < offset)
V8Proxy::setDOMException(INDEX_SIZE_ERR);
else
for (uint32_t i = 0; i < length; i++)
webGLArray->set(offset + i, array->Get(v8::Integer::NewFromUnsigned(i))->NumberValue());
}
return v8::Undefined();
}
template <class CPlusPlusArrayType, class JavaScriptWrapperArrayType>
v8::Handle<v8::Value> setWebGLArray(const v8::Arguments& args)
{
if (args.Length() < 1 || args.Length() > 2) {
V8Proxy::setDOMException(SYNTAX_ERR);
return notHandledByInterceptor();
}
CPlusPlusArrayType* array = JavaScriptWrapperArrayType::toNative(args.Holder());
if (args.Length() == 2 && args[0]->IsInt32()) {
uint32_t index = toUInt32(args[0]);
array->set(index, args[1]->NumberValue());
return v8::Undefined();
}
if (JavaScriptWrapperArrayType::HasInstance(args[0])) {
CPlusPlusArrayType* src = JavaScriptWrapperArrayType::toNative(args[0]->ToObject());
uint32_t offset = 0;
if (args.Length() == 2)
offset = toUInt32(args[1]);
ExceptionCode ec = 0;
array->set(src, offset, ec);
V8Proxy::setDOMException(ec);
return v8::Undefined();
}
return setWebGLArrayFromArray(array, args);
}
}
#endif // ENABLE(3D_CANVAS)