Bug 743615 - Introduce the ImageData object/constructor in workers. r=bent

This commit is contained in:
Bobby Holley 2012-04-24 12:58:08 +02:00
parent 6de4e1495d
commit 4f1637c5e8
4 changed files with 246 additions and 0 deletions

202
dom/workers/ImageData.cpp Normal file
View File

@ -0,0 +1,202 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ImageData.h"
#include "jsfriendapi.h"
#include "nsTraceRefcnt.h"
#define PROPERTY_FLAGS \
(JSPROP_ENUMERATE | JSPROP_SHARED)
USING_WORKERS_NAMESPACE
namespace {
class ImageData
{
static JSClass sClass;
static JSPropertySpec sProperties[];
enum SLOT {
SLOT_width = 0,
SLOT_height,
SLOT_data,
SLOT_COUNT
};
public:
static JSObject*
InitClass(JSContext* aCx, JSObject* aObj)
{
return JS_InitClass(aCx, aObj, NULL, &sClass, Construct, 0, sProperties,
NULL, NULL, NULL);
}
static JSObject*
Create(JSContext* aCx, uint32_t aWidth, uint32_t aHeight, JSObject *aData)
{
MOZ_ASSERT(aData);
MOZ_ASSERT(JS_IsTypedArrayObject(aData, aCx));
MOZ_ASSERT(JS_IsUint8ClampedArray(aData, aCx));
JSObject* obj = JS_NewObject(aCx, &sClass, NULL, NULL);
if (!obj) {
return NULL;
}
JS_SetReservedSlot(obj, SLOT_width, UINT_TO_JSVAL(aWidth));
JS_SetReservedSlot(obj, SLOT_height, UINT_TO_JSVAL(aHeight));
JS_SetReservedSlot(obj, SLOT_data, OBJECT_TO_JSVAL(aData));
// This is an empty object. The point is just to differentiate instances
// from the interface object.
ImageData* priv = new ImageData();
JS_SetPrivate(obj, priv);
return obj;
}
static bool
IsInstance(JSObject* aObj)
{
return JS_GetClass(aObj) == &sClass;
}
static uint32_t
GetWidth(JSObject* aObj)
{
MOZ_ASSERT(IsInstance(aObj));
return JS_DoubleToUint32(JS_GetReservedSlot(aObj, SLOT_width).toNumber());
}
static uint32_t
GetHeight(JSObject* aObj)
{
MOZ_ASSERT(IsInstance(aObj));
return JS_DoubleToUint32(JS_GetReservedSlot(aObj, SLOT_height).toNumber());
}
static
JSObject* GetData(JSObject* aObj)
{
MOZ_ASSERT(IsInstance(aObj));
return &JS_GetReservedSlot(aObj, SLOT_data).toObject();
}
private:
ImageData()
{
MOZ_COUNT_CTOR(mozilla::dom::workers::ImageData);
}
~ImageData()
{
MOZ_COUNT_DTOR(mozilla::dom::workers::ImageData);
}
static JSBool
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
{
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
sClass.name);
return false;
}
static void
Finalize(JSFreeOp* aFop, JSObject* aObj)
{
MOZ_ASSERT(JS_GetClass(aObj) == &sClass);
delete static_cast<ImageData*>(JS_GetPrivate(aObj));
}
static JSBool
GetProperty(JSContext* aCx, JSObject* aObj, jsid aIdval, jsval* aVp)
{
JSClass* classPtr = JS_GetClass(aObj);
if (classPtr != &sClass) {
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
JSMSG_INCOMPATIBLE_PROTO, sClass.name, "GetProperty",
classPtr->name);
return false;
}
MOZ_ASSERT(JSID_IS_INT(aIdval));
MOZ_ASSERT(JSID_TO_INT(aIdval) >= 0 && JSID_TO_INT(aIdval) < SLOT_COUNT);
*aVp = JS_GetReservedSlot(aObj, JSID_TO_INT(aIdval));
return true;
}
};
JSClass ImageData::sClass = {
"ImageData",
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT),
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
};
JSPropertySpec ImageData::sProperties[] = {
// These properties are read-only per spec, which means that sets must throw
// in strict mode and silently fail otherwise. This is a problem for workers
// in general (because js_GetterOnlyPropertyStub throws unconditionally). The
// general plan for fixing this involves the new DOM bindings. But Peace
// Keeper breaks if we throw when setting these properties, so we need to do
// something about it in the mean time. So we use NULL, which defaults to the
// class setter (JS_StrictPropertyStub), which is always a silent no-op,
// regardless of strict mode. Not ideal, but good enough for now.
{ "width", SLOT_width, PROPERTY_FLAGS, GetProperty, NULL },
{ "height", SLOT_height, PROPERTY_FLAGS, GetProperty, NULL },
{ "data", SLOT_data, PROPERTY_FLAGS, GetProperty, NULL },
{ 0, 0, 0, NULL, NULL }
};
} // anonymous namespace
BEGIN_WORKERS_NAMESPACE
namespace imagedata {
bool
InitClass(JSContext* aCx, JSObject* aGlobal)
{
return !!ImageData::InitClass(aCx, aGlobal);
}
JSObject*
Create(JSContext* aCx, uint32_t aWidth, uint32_t aHeight, JSObject* aData)
{
return ImageData::Create(aCx, aWidth, aHeight, aData);
}
bool
IsImageData(JSObject* aObj)
{
return ImageData::IsInstance(aObj);
}
uint32_t
GetWidth(JSObject* aObj)
{
return ImageData::GetWidth(aObj);
}
uint32_t
GetHeight(JSObject* aObj)
{
return ImageData::GetHeight(aObj);
}
JSObject*
GetData(JSObject* aObj)
{
return ImageData::GetData(aObj);
}
} // namespace imagedata
END_WORKERS_NAMESPACE

41
dom/workers/ImageData.h Normal file
View File

@ -0,0 +1,41 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_workers_imagedata_h__
#define mozilla_dom_workers_imagedata_h__
#include "Workers.h"
BEGIN_WORKERS_NAMESPACE
namespace imagedata {
bool
InitClass(JSContext* aCx, JSObject* aGlobal);
JSObject*
Create(JSContext* aCx, uint32_t aWidth, uint32_t aHeight, JSObject* aData);
/*
* All data members live in private slots on the JS Object. Callers must
* first check IsImageData, after which they may call the data accessors.
*/
bool
IsImageData(JSObject* aObj);
uint32_t
GetWidth(JSObject* aObj);
uint32_t
GetHeight(JSObject* aObj);
JSObject*
GetData(JSObject* aObj);
} // namespace imagedata
END_WORKERS_NAMESPACE
#endif // mozilla_dom_workers_imagedata_h__

View File

@ -24,6 +24,7 @@ CPPSRCS = \
File.cpp \
FileReaderSync.cpp \
FileReaderSyncPrivate.cpp \
ImageData.cpp \
Location.cpp \
Navigator.cpp \
Principal.cpp \

View File

@ -62,6 +62,7 @@
#include "File.h"
#include "FileReaderSync.h"
#include "Location.h"
#include "ImageData.h"
#include "Navigator.h"
#include "Principal.h"
#include "ScriptLoader.h"
@ -996,6 +997,7 @@ CreateDedicatedWorkerGlobalScope(JSContext* aCx)
!filereadersync::InitClass(aCx, global) ||
!exceptions::InitClasses(aCx, global) ||
!location::InitClass(aCx, global) ||
!imagedata::InitClass(aCx, global) ||
!navigator::InitClass(aCx, global)) {
return NULL;
}