JSWebGLArrayBufferConstructor.h [plain text]
#ifndef JSWebGLArrayBufferConstructor_h
#define JSWebGLArrayBufferConstructor_h
#include "JSDOMBinding.h"
#include "JSDocument.h"
#include "JSWebGLArrayBuffer.h"
#include <runtime/Error.h>
#include "WebGLArrayBuffer.h"
namespace WebCore {
class WebGLArray;
template<class C, typename T>
PassRefPtr<WebGLArray> construct(JSC::ExecState* exec, const JSC::ArgList& args)
{
RefPtr<C> arrayObject;
if (args.size() < 1)
return C::create(0, 0, 0);
if (args.size() > 1 && !args.at(0).isObject())
return 0;
if (args.at(0).isObject()) {
RefPtr<WebGLArrayBuffer> buffer = toWebGLArrayBuffer(args.at(0));
if (buffer) {
unsigned offset = (args.size() > 1) ? args.at(1).toUInt32(exec) : 0;
unsigned int length = (buffer->byteLength() - offset) / sizeof(T);
if (args.size() > 2)
length = args.at(2).toUInt32(exec);
return C::create(buffer, offset, length);
}
JSC::JSObject* array = asObject(args.at(0));
unsigned length = array->get(exec, JSC::Identifier(exec, "length")).toUInt32(exec);
void* tempValues;
if (!tryFastCalloc(length, sizeof(T)).getValue(tempValues)) {
throwError(exec, JSC::GeneralError);
return 0;
}
OwnFastMallocPtr<T> values(static_cast<T*>(tempValues));
for (unsigned i = 0; i < length; ++i) {
JSC::JSValue v = array->get(exec, i);
if (exec->hadException())
return 0;
values.get()[i] = static_cast<T>(v.toNumber(exec));
}
return C::create(values.get(), length);
}
unsigned size = args.at(0).toUInt32(exec);
return C::create(size);
}
class JSWebGLArrayBufferConstructor : public DOMConstructorObject {
public:
JSWebGLArrayBufferConstructor(JSC::ExecState*, JSDOMGlobalObject*);
static const JSC::ClassInfo s_info;
private:
virtual JSC::ConstructType getConstructData(JSC::ConstructData&);
virtual const JSC::ClassInfo* classInfo() const { return &s_info; }
};
}
#endif // JSWebGLArrayBufferConstructor_h