Bug 845545: Part 5 - Port ImageData to workers. r=mccr8,peterv,bent

This commit is contained in:
Kyle Huey 2013-08-03 16:55:40 -07:00
parent b0e1508e38
commit d8a5e42d26
10 changed files with 37 additions and 284 deletions

View File

@ -605,9 +605,10 @@ DOMInterfaces = {
'workers': True,
}],
'ImageData': {
'wrapperCache': False
},
'ImageData': [
{
'wrapperCache': False,
}],
'InputStream': [
{

View File

@ -2148,7 +2148,7 @@ class CGWrapWithCacheMethod(CGAbstractMethod):
self.properties = properties
def definition_body(self):
if self.descriptor.workers:
if self.descriptor.nativeOwnership == 'worker':
return """ return aObject->GetJSObject();"""
assertISupportsInheritance = (

View File

@ -358,11 +358,7 @@ class Descriptor(DescriptorProvider):
iface = iface.parent
self.operations = operations
if self.workers:
if desc.get('nativeOwnership', 'worker') != 'worker':
raise TypeError("Worker descriptor for %s should have 'worker' "
"as value for nativeOwnership" %
self.interface.identifier.name)
if self.workers and desc.get('nativeOwnership', 'worker') == 'worker':
self.nativeOwnership = "worker"
else:
self.nativeOwnership = desc.get('nativeOwnership', 'refcounted')
@ -373,7 +369,7 @@ class Descriptor(DescriptorProvider):
self.customTrace = desc.get('customTrace', self.workers)
self.customFinalize = desc.get('customFinalize', self.workers)
self.wrapperCache = (not self.interface.isCallback() and
(self.workers or
(self.nativeOwnership == 'worker' or
(self.nativeOwnership != 'owned' and
desc.get('wrapperCache', True))))

View File

@ -1,205 +0,0 @@
/* 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 const 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, JS::Handle<JSObject*> aData)
{
MOZ_ASSERT(aData);
MOZ_ASSERT(JS_IsTypedArrayObject(aData));
MOZ_ASSERT(JS_IsUint8ClampedArray(aData));
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, JS::Handle<JSObject*> aObj, JS::Handle<jsid> aIdval,
JS::MutableHandle<JS::Value> 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.set(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_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
};
const 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, JSOP_WRAPPER(GetProperty), JSOP_NULLWRAPPER },
{ "height", SLOT_height, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty), JSOP_NULLWRAPPER },
{ "data", SLOT_data, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty), JSOP_NULLWRAPPER },
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
};
} // 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, JS::Handle<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

View File

@ -1,42 +0,0 @@
/* 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, JS::Handle<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

@ -825,7 +825,8 @@ public:
// call to JS_SetGCParameter inside CreateJSContextForWorker.
WorkerJSRuntime(WorkerPrivate* aWorkerPrivate)
: CycleCollectedJSRuntime(WORKER_DEFAULT_RUNTIME_HEAPSIZE,
JS_NO_HELPER_THREADS)
JS_NO_HELPER_THREADS,
false)
{
// We need to ensure that a JSContext outlives the cycle collector, and
// that the internal JSContext created by ctypes is not the last JSContext

View File

@ -36,6 +36,9 @@
#include "js/MemoryMetrics.h"
#include "mozilla/Attributes.h"
#include "mozilla/Likely.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/ImageData.h"
#include "mozilla/dom/ImageDataBinding.h"
#include "nsAlgorithm.h"
#include "nsContentUtils.h"
#include "nsCxPusher.h"
@ -57,7 +60,6 @@
#include "Events.h"
#include "Exceptions.h"
#include "File.h"
#include "ImageData.h"
#include "Principal.h"
#include "RuntimeService.h"
#include "ScriptLoader.h"
@ -204,9 +206,14 @@ struct WorkerStructuredCloneCallbacks
MOZ_ASSERT(dataArray.isObject());
// Construct the ImageData.
JS::Rooted<JSObject*> dataObj(aCx, &dataArray.toObject());
JSObject* obj = imagedata::Create(aCx, width, height, dataObj);
return obj;
nsRefPtr<ImageData> imageData = new ImageData(width, height,
dataArray.toObject());
// Wrap it in a JS::Value.
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
if (!global) {
return nullptr;
}
return imageData->WrapObject(aCx, global);
}
Error(aCx, 0);
@ -250,16 +257,20 @@ struct WorkerStructuredCloneCallbacks
}
// See if this is an ImageData object.
if (imagedata::IsImageData(aObj)) {
// Pull the properties off the object.
uint32_t width = imagedata::GetWidth(aObj);
uint32_t height = imagedata::GetHeight(aObj);
JSObject* data = imagedata::GetData(aObj);
{
ImageData* imageData = nullptr;
if (NS_SUCCEEDED(UnwrapObject<ImageData>(aCx, aObj, imageData))) {
// Prepare the ImageData internals.
uint32_t width = imageData->Width();
uint32_t height = imageData->Height();
JS::Rooted<JSObject*> dataArray(aCx, imageData->GetDataObject());
// Write the structured clone.
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_IMAGEDATA, 0) &&
JS_WriteUint32Pair(aWriter, width, height) &&
JS_WriteTypedArray(aWriter, OBJECT_TO_JSVAL(data));
// Write the internals to the stream.
JSAutoCompartment ac(aCx, dataArray);
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_IMAGEDATA, 0) &&
JS_WriteUint32Pair(aWriter, width, height) &&
JS_WriteTypedArray(aWriter, JS::ObjectValue(*dataArray));
}
}
Error(aCx, 0);

View File

@ -13,6 +13,8 @@
#include "mozilla/dom/EventTargetBinding.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/FileReaderSyncBinding.h"
#include "mozilla/dom/ImageData.h"
#include "mozilla/dom/ImageDataBinding.h"
#include "mozilla/dom/TextDecoderBinding.h"
#include "mozilla/dom/TextEncoderBinding.h"
#include "mozilla/dom/XMLHttpRequestBinding.h"
@ -36,7 +38,6 @@
#include "File.h"
#include "FileReaderSync.h"
#include "Location.h"
#include "ImageData.h"
#include "Navigator.h"
#include "Principal.h"
#include "ScriptLoader.h"
@ -1012,13 +1013,13 @@ CreateDedicatedWorkerGlobalScope(JSContext* aCx)
// Init other classes we care about.
if (!events::InitClasses(aCx, global, false) ||
!file::InitClasses(aCx, global) ||
!exceptions::InitClasses(aCx, global) ||
!imagedata::InitClass(aCx, global)) {
!exceptions::InitClasses(aCx, global)) {
return NULL;
}
// Init other paris-bindings.
if (!FileReaderSyncBinding_workers::GetConstructorObject(aCx, global) ||
!ImageDataBinding::GetConstructorObject(aCx, global) ||
!TextDecoderBinding_workers::GetConstructorObject(aCx, global) ||
!TextEncoderBinding_workers::GetConstructorObject(aCx, global) ||
!XMLHttpRequestBinding_workers::GetConstructorObject(aCx, global) ||

View File

@ -39,7 +39,6 @@ CPP_SOURCES += [
'Exceptions.cpp',
'File.cpp',
'FileReaderSync.cpp',
'ImageData.cpp',
'Location.cpp',
'Navigator.cpp',
'Principal.cpp',

View File

@ -65,7 +65,6 @@
#include "nsCycleCollectionParticipant.h"
#include "nsCycleCollector.h"
#include "nsDOMJSUtils.h"
#include "nsLayoutStatics.h"
#include "xpcpublic.h"
using namespace mozilla;
@ -850,11 +849,7 @@ CycleCollectedJSRuntime::TraceNativeGrayRoots(JSTracer* aTracer)
void
CycleCollectedJSRuntime::AddJSHolder(void* aHolder, nsScriptObjectTracer* aTracer)
{
bool wasEmpty = mJSHolders.Count() == 0;
mJSHolders.Put(aHolder, aTracer);
if (wasEmpty && mJSHolders.Count() == 1) {
nsLayoutStatics::AddRef();
}
}
void
@ -869,11 +864,7 @@ CycleCollectedJSRuntime::RemoveJSHolder(void* aHolder)
AssertNoObjectsToTrace(aHolder);
}
#endif
bool hadOne = mJSHolders.Count() == 1;
mJSHolders.Remove(aHolder);
if (hadOne && mJSHolders.Count() == 0) {
nsLayoutStatics::Release();
}
}
#ifdef DEBUG