forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImageData.cpp
202 lines (162 loc) · 4.99 KB
/
ImageData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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, JSHandleObject aObj, JSHandleId aIdval, JSMutableHandleValue 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_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, 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, 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