2011-07-17 19:09:13 +00:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2011-08-16 03:40:38 +00:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2011-10-21 10:16:47 +00:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
#include "Events.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
2011-10-04 14:06:54 +00:00
|
|
|
#include "jsfriendapi.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
#include "nsTraceRefcnt.h"
|
|
|
|
|
|
|
|
#include "WorkerInlines.h"
|
2011-08-16 03:40:38 +00:00
|
|
|
#include "WorkerPrivate.h"
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
#define PROPERTY_FLAGS \
|
2012-02-13 08:02:21 +00:00
|
|
|
(JSPROP_ENUMERATE | JSPROP_SHARED)
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
#define FUNCTION_FLAGS \
|
|
|
|
JSPROP_ENUMERATE
|
|
|
|
|
|
|
|
#define CONSTANT_FLAGS \
|
|
|
|
JSPROP_ENUMERATE | JSPROP_SHARED | JSPROP_PERMANENT | JSPROP_READONLY
|
|
|
|
|
2011-10-21 10:16:47 +00:00
|
|
|
using namespace mozilla;
|
2011-07-17 19:09:13 +00:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Event : public PrivatizableBase
|
|
|
|
{
|
|
|
|
static JSClass sClass;
|
|
|
|
static JSClass sMainRuntimeClass;
|
|
|
|
|
|
|
|
static JSPropertySpec sProperties[];
|
|
|
|
static JSFunctionSpec sFunctions[];
|
|
|
|
static JSPropertySpec sStaticProperties[];
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool mStopPropagationCalled;
|
2011-11-05 21:41:16 +00:00
|
|
|
bool mStopImmediatePropagationCalled;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static bool
|
|
|
|
IsThisClass(JSClass* aClass)
|
|
|
|
{
|
|
|
|
return aClass == &sClass || aClass == &sMainRuntimeClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj, bool aMainRuntime)
|
|
|
|
{
|
|
|
|
JSObject* parentProto = NULL;
|
|
|
|
|
|
|
|
if (aMainRuntime) {
|
|
|
|
jsval windowPropVal;
|
|
|
|
if (!JS_GetProperty(aCx, aObj, sClass.name, &windowPropVal)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JSVAL_IS_PRIMITIVE(windowPropVal)) {
|
|
|
|
jsval protoVal;
|
|
|
|
if (!JS_GetProperty(aCx, JSVAL_TO_OBJECT(windowPropVal), "prototype",
|
|
|
|
&protoVal)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JSVAL_IS_PRIMITIVE(protoVal)) {
|
|
|
|
parentProto = JSVAL_TO_OBJECT(protoVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSClass* clasp = parentProto ? &sMainRuntimeClass : &sClass;
|
|
|
|
|
|
|
|
JSObject* proto = JS_InitClass(aCx, aObj, parentProto, clasp, Construct, 0,
|
|
|
|
sProperties, sFunctions, sStaticProperties,
|
|
|
|
NULL);
|
|
|
|
if (proto && !JS_DefineProperties(aCx, proto, sStaticProperties)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
Create(JSContext* aCx, JSObject* aParent, JSString* aType, bool aBubbles,
|
|
|
|
bool aCancelable, bool aMainRuntime)
|
|
|
|
{
|
|
|
|
JSClass* clasp = aMainRuntime ? &sMainRuntimeClass : &sClass;
|
|
|
|
|
|
|
|
JSObject* obj = JS_NewObject(aCx, clasp, NULL, aParent);
|
|
|
|
if (obj) {
|
|
|
|
Event* priv = new Event();
|
2012-02-05 20:07:23 +00:00
|
|
|
SetJSPrivateSafeish(obj, priv);
|
|
|
|
InitEventCommon(obj, priv, aType, aBubbles, aCancelable, true);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-02-05 20:07:23 +00:00
|
|
|
IsSupportedClass(JSObject* aEvent)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
return !!GetPrivate(aEvent);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
static void
|
|
|
|
SetTarget(JSObject* aEvent, JSObject* aTarget)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
JS_ASSERT(IsSupportedClass(aEvent));
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
jsval target = OBJECT_TO_JSVAL(aTarget);
|
2012-02-05 20:07:23 +00:00
|
|
|
JS_SetReservedSlot(aEvent, SLOT_target, target);
|
|
|
|
JS_SetReservedSlot(aEvent, SLOT_currentTarget, target);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2012-02-05 20:07:23 +00:00
|
|
|
WasCanceled(JSObject* aEvent)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
JS_ASSERT(IsSupportedClass(aEvent));
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
jsval canceled = JS_GetReservedSlot(aEvent, SLOT_defaultPrevented);
|
2011-07-17 19:09:13 +00:00
|
|
|
return JSVAL_TO_BOOLEAN(canceled);
|
|
|
|
}
|
|
|
|
|
2011-11-05 21:41:16 +00:00
|
|
|
static bool
|
2012-02-05 20:07:23 +00:00
|
|
|
ImmediatePropagationStopped(JSObject* aEvent)
|
2011-11-05 21:41:16 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
Event* event = GetPrivate(aEvent);
|
2011-11-05 21:41:16 +00:00
|
|
|
return event ? event->mStopImmediatePropagationCalled : false;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
protected:
|
|
|
|
Event()
|
2011-11-05 21:41:16 +00:00
|
|
|
: mStopPropagationCalled(false), mStopImmediatePropagationCalled(false)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::Event);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~Event()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::Event);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
CAPTURING_PHASE = 1,
|
|
|
|
AT_TARGET = 2,
|
|
|
|
BUBBLING_PHASE = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
enum SLOT {
|
|
|
|
SLOT_type = 0,
|
|
|
|
SLOT_target,
|
|
|
|
SLOT_currentTarget,
|
|
|
|
SLOT_eventPhase,
|
|
|
|
SLOT_bubbles,
|
|
|
|
SLOT_cancelable,
|
|
|
|
SLOT_timeStamp,
|
|
|
|
SLOT_defaultPrevented,
|
|
|
|
SLOT_isTrusted,
|
|
|
|
|
|
|
|
SLOT_COUNT,
|
|
|
|
SLOT_FIRST = SLOT_type
|
|
|
|
};
|
|
|
|
|
|
|
|
static Event*
|
2012-02-05 20:07:23 +00:00
|
|
|
GetPrivate(JSObject* aEvent);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
static Event*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
|
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
Event* priv = GetPrivate(aObj);
|
2012-02-03 18:00:08 +00:00
|
|
|
if (priv) {
|
|
|
|
return priv;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-04 00:54:57 +00:00
|
|
|
JS_GetClass(aObj)->name);
|
2011-07-17 19:09:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
static void
|
|
|
|
InitEventCommon(JSObject* aObj, Event* aEvent, JSString* aType,
|
|
|
|
JSBool aBubbles, JSBool aCancelable, bool aIsTrusted)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
aEvent->mStopPropagationCalled = false;
|
2011-11-05 21:41:16 +00:00
|
|
|
aEvent->mStopImmediatePropagationCalled = false;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
JS_SetReservedSlot(aObj, SLOT_type, STRING_TO_JSVAL(aType));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_target, JSVAL_NULL);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_currentTarget, JSVAL_NULL);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_eventPhase, INT_TO_JSVAL(CAPTURING_PHASE));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_bubbles,
|
|
|
|
aBubbles ? JSVAL_TRUE : JSVAL_FALSE);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_cancelable,
|
|
|
|
aCancelable ? JSVAL_TRUE : JSVAL_FALSE);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_timeStamp, JS::NumberValue(double(JS_Now())));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_defaultPrevented, JSVAL_FALSE);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_isTrusted,
|
|
|
|
aIsTrusted ? JSVAL_TRUE : JSVAL_FALSE);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
|
|
|
|
sClass.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 14:34:55 +00:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-04 00:54:57 +00:00
|
|
|
JS_ASSERT(IsThisClass(JS_GetClass(aObj)));
|
2012-02-05 20:07:23 +00:00
|
|
|
delete GetJSPrivateSafeish<Event>(aObj);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-07-30 11:19:09 +00:00
|
|
|
GetProperty(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSMutableHandleValue aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(aIdval));
|
|
|
|
|
2012-10-11 23:38:04 +00:00
|
|
|
int32_t slot = JSID_TO_INT(aIdval);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
const char*& name = sProperties[slot - SLOT_FIRST].name;
|
|
|
|
if (!GetInstancePrivate(aCx, aObj, name)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-30 11:19:09 +00:00
|
|
|
aVp.set(JS_GetReservedSlot(aObj, slot));
|
2012-02-05 20:07:23 +00:00
|
|
|
return true;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-07-30 11:19:09 +00:00
|
|
|
GetConstant(JSContext* aCx, JSHandleObject aObj, JSHandleId idval, JSMutableHandleValue aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(idval));
|
|
|
|
JS_ASSERT(JSID_TO_INT(idval) >= CAPTURING_PHASE &&
|
|
|
|
JSID_TO_INT(idval) <= BUBBLING_PHASE);
|
|
|
|
|
2012-07-30 11:19:09 +00:00
|
|
|
aVp.set(INT_TO_JSVAL(JSID_TO_INT(idval)));
|
2011-07-17 19:09:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
StopPropagation(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 21:30:28 +00:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
Event* event = GetInstancePrivate(aCx, obj, sFunctions[0].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->mStopPropagationCalled = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-05 21:41:16 +00:00
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
StopImmediatePropagation(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-11-05 21:41:16 +00:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 21:30:28 +00:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-05 21:41:16 +00:00
|
|
|
|
|
|
|
Event* event = GetInstancePrivate(aCx, obj, sFunctions[3].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->mStopImmediatePropagationCalled = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
PreventDefault(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 21:30:28 +00:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
Event* event = GetInstancePrivate(aCx, obj, sFunctions[1].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
jsval cancelableVal = JS_GetReservedSlot(obj, SLOT_cancelable);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
if (JSVAL_TO_BOOLEAN(cancelableVal))
|
|
|
|
JS_SetReservedSlot(obj, SLOT_defaultPrevented, cancelableVal);
|
|
|
|
return true;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
InitEvent(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 21:30:28 +00:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
Event* event = GetInstancePrivate(aCx, obj, sFunctions[2].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString* type;
|
|
|
|
JSBool bubbles, cancelable;
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "Sbb", &type,
|
|
|
|
&bubbles, &cancelable)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
InitEventCommon(obj, event, type, bubbles, cancelable, false);
|
|
|
|
return true;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECL_EVENT_CLASS(_varname, _name) \
|
|
|
|
JSClass _varname = { \
|
|
|
|
_name, \
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT), \
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, \
|
2012-03-19 14:27:58 +00:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize \
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DECL_EVENT_CLASS(Event::sClass, "Event")
|
|
|
|
DECL_EVENT_CLASS(Event::sMainRuntimeClass, "WorkerEvent")
|
|
|
|
|
|
|
|
#undef DECL_EVENT_CLASS
|
|
|
|
|
|
|
|
JSPropertySpec Event::sProperties[] = {
|
2012-08-08 05:26:18 +00:00
|
|
|
{ "type", SLOT_type, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "target", SLOT_target, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "currentTarget", SLOT_currentTarget, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "eventPhase", SLOT_eventPhase, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "bubbles", SLOT_bubbles, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "cancelable", SLOT_cancelable, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "timeStamp", SLOT_timeStamp, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "defaultPrevented", SLOT_defaultPrevented, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "isTrusted", SLOT_isTrusted, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
JSFunctionSpec Event::sFunctions[] = {
|
|
|
|
JS_FN("stopPropagation", StopPropagation, 0, FUNCTION_FLAGS),
|
|
|
|
JS_FN("preventDefault", PreventDefault, 0, FUNCTION_FLAGS),
|
|
|
|
JS_FN("initEvent", InitEvent, 3, FUNCTION_FLAGS),
|
2011-11-05 21:41:16 +00:00
|
|
|
JS_FN("stopImmediatePropagation", StopImmediatePropagation, 0, FUNCTION_FLAGS),
|
2011-07-17 19:09:13 +00:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
JSPropertySpec Event::sStaticProperties[] = {
|
2012-08-08 05:26:18 +00:00
|
|
|
{ "CAPTURING_PHASE", CAPTURING_PHASE, CONSTANT_FLAGS,
|
|
|
|
JSOP_WRAPPER(GetConstant), JSOP_NULLWRAPPER },
|
|
|
|
{ "AT_TARGET", AT_TARGET, CONSTANT_FLAGS, JSOP_WRAPPER(GetConstant), JSOP_NULLWRAPPER },
|
|
|
|
{ "BUBBLING_PHASE", BUBBLING_PHASE, CONSTANT_FLAGS,
|
|
|
|
JSOP_WRAPPER(GetConstant), JSOP_NULLWRAPPER },
|
|
|
|
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class MessageEvent : public Event
|
|
|
|
{
|
|
|
|
static JSClass sClass;
|
|
|
|
static JSClass sMainRuntimeClass;
|
|
|
|
|
|
|
|
static JSPropertySpec sProperties[];
|
|
|
|
static JSFunctionSpec sFunctions[];
|
|
|
|
|
|
|
|
protected:
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 03:54:10 +00:00
|
|
|
uint64_t* mData;
|
2011-07-17 19:09:13 +00:00
|
|
|
size_t mDataByteCount;
|
2011-08-16 03:40:38 +00:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mClonedObjects;
|
|
|
|
bool mMainRuntime;
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static bool
|
|
|
|
IsThisClass(JSClass* aClass)
|
|
|
|
{
|
|
|
|
return aClass == &sClass || aClass == &sMainRuntimeClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj, JSObject* aParentProto,
|
|
|
|
bool aMainRuntime)
|
|
|
|
{
|
|
|
|
JSClass* clasp = aMainRuntime ? &sMainRuntimeClass : &sClass;
|
|
|
|
|
|
|
|
return JS_InitClass(aCx, aObj, aParentProto, clasp, Construct, 0,
|
|
|
|
sProperties, sFunctions, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
Create(JSContext* aCx, JSObject* aParent, JSAutoStructuredCloneBuffer& aData,
|
2011-08-16 03:40:38 +00:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aClonedObjects, bool aMainRuntime)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JSString* type = JS_InternString(aCx, "message");
|
|
|
|
if (!type) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSClass* clasp = aMainRuntime ? &sMainRuntimeClass : &sClass;
|
|
|
|
|
|
|
|
JSObject* obj = JS_NewObject(aCx, clasp, NULL, aParent);
|
|
|
|
if (!obj) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-16 03:40:38 +00:00
|
|
|
MessageEvent* priv = new MessageEvent(aMainRuntime);
|
2012-02-05 20:07:23 +00:00
|
|
|
SetJSPrivateSafeish(obj, priv);
|
|
|
|
InitMessageEventCommon(aCx, obj, priv, type, false, false, NULL, NULL, NULL,
|
|
|
|
true);
|
2011-07-17 19:09:13 +00:00
|
|
|
aData.steal(&priv->mData, &priv->mDataByteCount);
|
2011-08-16 03:40:38 +00:00
|
|
|
priv->mClonedObjects.SwapElements(aClonedObjects);
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2011-08-16 03:40:38 +00:00
|
|
|
MessageEvent(bool aMainRuntime)
|
|
|
|
: mData(NULL), mDataByteCount(0), mMainRuntime(aMainRuntime)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::MessageEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~MessageEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::MessageEvent);
|
|
|
|
JS_ASSERT(!mData);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum SLOT {
|
|
|
|
SLOT_data = Event::SLOT_COUNT,
|
|
|
|
SLOT_origin,
|
|
|
|
SLOT_source,
|
|
|
|
|
|
|
|
SLOT_COUNT,
|
|
|
|
SLOT_FIRST = SLOT_data
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
static MessageEvent*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
|
|
|
|
{
|
2012-02-04 00:54:57 +00:00
|
|
|
JSClass* classPtr = JS_GetClass(aObj);
|
2012-02-03 18:00:08 +00:00
|
|
|
if (IsThisClass(classPtr)) {
|
2012-02-05 20:07:23 +00:00
|
|
|
return GetJSPrivateSafeish<MessageEvent>(aObj);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-03 18:00:08 +00:00
|
|
|
classPtr->name);
|
2011-07-17 19:09:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
static void
|
2011-07-17 19:09:13 +00:00
|
|
|
InitMessageEventCommon(JSContext* aCx, JSObject* aObj, Event* aEvent,
|
|
|
|
JSString* aType, JSBool aBubbles, JSBool aCancelable,
|
|
|
|
JSString* aData, JSString* aOrigin, JSObject* aSource,
|
|
|
|
bool aIsTrusted)
|
|
|
|
{
|
|
|
|
jsval emptyString = JS_GetEmptyStringValue(aCx);
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
Event::InitEventCommon(aObj, aEvent, aType, aBubbles, aCancelable,
|
|
|
|
aIsTrusted);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_data,
|
|
|
|
aData ? STRING_TO_JSVAL(aData) : emptyString);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_origin,
|
|
|
|
aOrigin ? STRING_TO_JSVAL(aOrigin) : emptyString);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_source, OBJECT_TO_JSVAL(aSource));
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
|
|
|
|
sClass.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 14:34:55 +00:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-04 00:54:57 +00:00
|
|
|
JS_ASSERT(IsThisClass(JS_GetClass(aObj)));
|
2012-02-05 20:07:23 +00:00
|
|
|
MessageEvent* priv = GetJSPrivateSafeish<MessageEvent>(aObj);
|
2011-07-17 19:09:13 +00:00
|
|
|
if (priv) {
|
2012-03-19 14:34:55 +00:00
|
|
|
JS_freeop(aFop, priv->mData);
|
2011-07-17 19:09:13 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
priv->mData = NULL;
|
|
|
|
#endif
|
|
|
|
delete priv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-07-30 11:19:09 +00:00
|
|
|
GetProperty(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSMutableHandleValue aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(aIdval));
|
|
|
|
|
2012-10-11 23:38:04 +00:00
|
|
|
int32_t slot = JSID_TO_INT(aIdval);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
JS_ASSERT(slot >= SLOT_data && slot < SLOT_COUNT);
|
|
|
|
|
|
|
|
const char*& name = sProperties[slot - SLOT_FIRST].name;
|
|
|
|
MessageEvent* event = GetInstancePrivate(aCx, aObj, name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize and save the data value if we can.
|
|
|
|
if (slot == SLOT_data && event->mData) {
|
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
2011-07-12 17:42:24 +00:00
|
|
|
buffer.adopt(event->mData, event->mDataByteCount);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
event->mData = NULL;
|
|
|
|
event->mDataByteCount = 0;
|
|
|
|
|
2011-08-16 03:40:38 +00:00
|
|
|
// Release reference to objects that were AddRef'd for
|
|
|
|
// cloning into worker when array goes out of scope.
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > clonedObjects;
|
|
|
|
clonedObjects.SwapElements(event->mClonedObjects);
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
jsval data;
|
2011-08-16 03:40:38 +00:00
|
|
|
if (!buffer.read(aCx, &data,
|
2012-02-05 20:07:23 +00:00
|
|
|
WorkerStructuredCloneCallbacks(event->mMainRuntime))) {
|
2011-07-17 19:09:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-05 20:07:23 +00:00
|
|
|
JS_SetReservedSlot(aObj, slot, data);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
2012-07-30 11:19:09 +00:00
|
|
|
aVp.set(data);
|
2011-07-17 19:09:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-30 11:19:09 +00:00
|
|
|
aVp.set(JS_GetReservedSlot(aObj, slot));
|
2012-02-05 20:07:23 +00:00
|
|
|
return true;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
InitMessageEvent(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 21:30:28 +00:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
MessageEvent* event = GetInstancePrivate(aCx, obj, sFunctions[0].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString* type, *data, *origin;
|
|
|
|
JSBool bubbles, cancelable;
|
|
|
|
JSObject* source;
|
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "SbbSSo", &type,
|
|
|
|
&bubbles, &cancelable, &data, &origin, &source)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
InitMessageEventCommon(aCx, obj, event, type, bubbles, cancelable,
|
|
|
|
data, origin, source, false);
|
|
|
|
return true;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECL_MESSAGEEVENT_CLASS(_varname, _name) \
|
|
|
|
JSClass _varname = { \
|
|
|
|
_name, \
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT), \
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, \
|
2012-03-19 14:27:58 +00:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize \
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DECL_MESSAGEEVENT_CLASS(MessageEvent::sClass, "MessageEvent")
|
|
|
|
DECL_MESSAGEEVENT_CLASS(MessageEvent::sMainRuntimeClass, "WorkerMessageEvent")
|
|
|
|
|
|
|
|
#undef DECL_MESSAGEEVENT_CLASS
|
|
|
|
|
|
|
|
JSPropertySpec MessageEvent::sProperties[] = {
|
2012-08-08 05:26:18 +00:00
|
|
|
{ "data", SLOT_data, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "origin", SLOT_origin, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "source", SLOT_source, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
JSFunctionSpec MessageEvent::sFunctions[] = {
|
|
|
|
JS_FN("initMessageEvent", InitMessageEvent, 6, FUNCTION_FLAGS),
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
class ErrorEvent : public Event
|
|
|
|
{
|
|
|
|
static JSClass sClass;
|
|
|
|
static JSClass sMainRuntimeClass;
|
|
|
|
|
|
|
|
static JSPropertySpec sProperties[];
|
|
|
|
static JSFunctionSpec sFunctions[];
|
|
|
|
|
|
|
|
public:
|
|
|
|
static bool
|
|
|
|
IsThisClass(JSClass* aClass)
|
|
|
|
{
|
|
|
|
return aClass == &sClass || aClass == &sMainRuntimeClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj, JSObject* aParentProto,
|
|
|
|
bool aMainRuntime)
|
|
|
|
{
|
|
|
|
JSClass* clasp = aMainRuntime ? &sMainRuntimeClass : &sClass;
|
|
|
|
|
|
|
|
return JS_InitClass(aCx, aObj, aParentProto, clasp, Construct, 0,
|
|
|
|
sProperties, sFunctions, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
Create(JSContext* aCx, JSObject* aParent, JSString* aMessage,
|
2012-10-11 23:38:04 +00:00
|
|
|
JSString* aFilename, uint32_t aLineNumber, bool aMainRuntime)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JSString* type = JS_InternString(aCx, "error");
|
|
|
|
if (!type) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSClass* clasp = aMainRuntime ? &sMainRuntimeClass : &sClass;
|
|
|
|
|
|
|
|
JSObject* obj = JS_NewObject(aCx, clasp, NULL, aParent);
|
|
|
|
if (!obj) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorEvent* priv = new ErrorEvent();
|
2012-02-05 20:07:23 +00:00
|
|
|
SetJSPrivateSafeish(obj, priv);
|
|
|
|
InitErrorEventCommon(obj, priv, type, false, true, aMessage, aFilename,
|
|
|
|
aLineNumber, true);
|
2011-07-17 19:09:13 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ErrorEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::ErrorEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ErrorEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::ErrorEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum SLOT {
|
|
|
|
SLOT_message = Event::SLOT_COUNT,
|
|
|
|
SLOT_filename,
|
|
|
|
SLOT_lineno,
|
|
|
|
|
|
|
|
SLOT_COUNT,
|
|
|
|
SLOT_FIRST = SLOT_message
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
static ErrorEvent*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
|
|
|
|
{
|
2012-02-04 00:54:57 +00:00
|
|
|
JSClass* classPtr = JS_GetClass(aObj);
|
2012-02-03 18:00:08 +00:00
|
|
|
if (IsThisClass(classPtr)) {
|
2012-02-05 20:07:23 +00:00
|
|
|
return GetJSPrivateSafeish<ErrorEvent>(aObj);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-03 18:00:08 +00:00
|
|
|
classPtr->name);
|
2011-07-17 19:09:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
static void
|
|
|
|
InitErrorEventCommon(JSObject* aObj, Event* aEvent, JSString* aType,
|
|
|
|
JSBool aBubbles, JSBool aCancelable,
|
2011-07-17 19:09:13 +00:00
|
|
|
JSString* aMessage, JSString* aFilename,
|
2012-10-11 23:38:04 +00:00
|
|
|
uint32_t aLineNumber, bool aIsTrusted)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
Event::InitEventCommon(aObj, aEvent, aType, aBubbles, aCancelable,
|
|
|
|
aIsTrusted);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_message, STRING_TO_JSVAL(aMessage));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_filename, STRING_TO_JSVAL(aFilename));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_lineno, INT_TO_JSVAL(aLineNumber));
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
|
|
|
|
sClass.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 14:34:55 +00:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-04 00:54:57 +00:00
|
|
|
JS_ASSERT(IsThisClass(JS_GetClass(aObj)));
|
2012-02-05 20:07:23 +00:00
|
|
|
delete GetJSPrivateSafeish<ErrorEvent>(aObj);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-07-30 11:19:09 +00:00
|
|
|
GetProperty(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSMutableHandleValue aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(aIdval));
|
|
|
|
|
2012-10-11 23:38:04 +00:00
|
|
|
int32_t slot = JSID_TO_INT(aIdval);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
JS_ASSERT(slot >= SLOT_message && slot < SLOT_COUNT);
|
|
|
|
|
|
|
|
const char*& name = sProperties[slot - SLOT_FIRST].name;
|
|
|
|
ErrorEvent* event = GetInstancePrivate(aCx, aObj, name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-30 11:19:09 +00:00
|
|
|
aVp.set(JS_GetReservedSlot(aObj, slot));
|
2012-02-05 20:07:23 +00:00
|
|
|
return true;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
InitErrorEvent(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 21:30:28 +00:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
ErrorEvent* event = GetInstancePrivate(aCx, obj, sFunctions[0].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString* type, *message, *filename;
|
|
|
|
JSBool bubbles, cancelable;
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-09 03:54:10 +00:00
|
|
|
uint32_t lineNumber;
|
2011-07-17 19:09:13 +00:00
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "SbbSSu", &type,
|
|
|
|
&bubbles, &cancelable, &message, &filename,
|
|
|
|
&lineNumber)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
InitErrorEventCommon(obj, event, type, bubbles, cancelable, message,
|
|
|
|
filename, lineNumber, false);
|
|
|
|
return true;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DECL_ERROREVENT_CLASS(_varname, _name) \
|
|
|
|
JSClass _varname = { \
|
|
|
|
_name, \
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT), \
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, \
|
2012-03-19 14:27:58 +00:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize \
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DECL_ERROREVENT_CLASS(ErrorEvent::sClass, "ErrorEvent")
|
|
|
|
DECL_ERROREVENT_CLASS(ErrorEvent::sMainRuntimeClass, "WorkerErrorEvent")
|
|
|
|
|
|
|
|
#undef DECL_ERROREVENT_CLASS
|
|
|
|
|
|
|
|
JSPropertySpec ErrorEvent::sProperties[] = {
|
2012-08-08 05:26:18 +00:00
|
|
|
{ "message", SLOT_message, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "filename", SLOT_filename, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "lineno", SLOT_lineno, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
JSFunctionSpec ErrorEvent::sFunctions[] = {
|
|
|
|
JS_FN("initErrorEvent", InitErrorEvent, 6, FUNCTION_FLAGS),
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProgressEvent : public Event
|
|
|
|
{
|
|
|
|
static JSClass sClass;
|
|
|
|
static JSPropertySpec sProperties[];
|
|
|
|
static JSFunctionSpec sFunctions[];
|
|
|
|
|
|
|
|
public:
|
|
|
|
static JSClass*
|
|
|
|
Class()
|
|
|
|
{
|
|
|
|
return &sClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj, JSObject* aParentProto)
|
|
|
|
{
|
|
|
|
return JS_InitClass(aCx, aObj, aParentProto, &sClass, Construct, 0,
|
|
|
|
sProperties, sFunctions, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
Create(JSContext* aCx, JSObject* aParent, JSString* aType,
|
2012-02-24 22:19:52 +00:00
|
|
|
bool aLengthComputable, double aLoaded, double aTotal)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JSString* type = JS_InternJSString(aCx, aType);
|
|
|
|
if (!type) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* obj = JS_NewObject(aCx, &sClass, NULL, aParent);
|
|
|
|
if (!obj) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProgressEvent* priv = new ProgressEvent();
|
2012-02-05 20:07:23 +00:00
|
|
|
SetJSPrivateSafeish(obj, priv);
|
|
|
|
InitProgressEventCommon(obj, priv, type, false, false, aLengthComputable,
|
|
|
|
aLoaded, aTotal, true);
|
2011-07-17 19:09:13 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ProgressEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::ProgressEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ProgressEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::ProgressEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum SLOT {
|
|
|
|
SLOT_lengthComputable = Event::SLOT_COUNT,
|
|
|
|
SLOT_loaded,
|
|
|
|
SLOT_total,
|
|
|
|
|
|
|
|
SLOT_COUNT,
|
|
|
|
SLOT_FIRST = SLOT_lengthComputable
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
static ProgressEvent*
|
|
|
|
GetInstancePrivate(JSContext* aCx, JSObject* aObj, const char* aFunctionName)
|
|
|
|
{
|
2012-02-04 00:54:57 +00:00
|
|
|
JSClass* classPtr = JS_GetClass(aObj);
|
2012-02-03 18:00:08 +00:00
|
|
|
if (classPtr == &sClass) {
|
2012-02-05 20:07:23 +00:00
|
|
|
return GetJSPrivateSafeish<ProgressEvent>(aObj);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, aFunctionName,
|
2012-02-03 18:00:08 +00:00
|
|
|
classPtr->name);
|
2011-07-17 19:09:13 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
static void
|
|
|
|
InitProgressEventCommon(JSObject* aObj, Event* aEvent, JSString* aType,
|
|
|
|
JSBool aBubbles, JSBool aCancelable,
|
2012-02-24 22:19:52 +00:00
|
|
|
JSBool aLengthComputable, double aLoaded,
|
|
|
|
double aTotal, bool aIsTrusted)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
Event::InitEventCommon(aObj, aEvent, aType, aBubbles, aCancelable,
|
|
|
|
aIsTrusted);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_lengthComputable,
|
|
|
|
aLengthComputable ? JSVAL_TRUE : JSVAL_FALSE);
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_loaded, DOUBLE_TO_JSVAL(aLoaded));
|
|
|
|
JS_SetReservedSlot(aObj, SLOT_total, DOUBLE_TO_JSVAL(aTotal));
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
|
|
|
|
sClass.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 14:34:55 +00:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-04 00:54:57 +00:00
|
|
|
JS_ASSERT(JS_GetClass(aObj) == &sClass);
|
2012-02-05 20:07:23 +00:00
|
|
|
delete GetJSPrivateSafeish<ProgressEvent>(aObj);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-07-30 11:19:09 +00:00
|
|
|
GetProperty(JSContext* aCx, JSHandleObject aObj, JSHandleId aIdval, JSMutableHandleValue aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(aIdval));
|
|
|
|
|
2012-10-11 23:38:04 +00:00
|
|
|
int32_t slot = JSID_TO_INT(aIdval);
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
JS_ASSERT(slot >= SLOT_lengthComputable && slot < SLOT_COUNT);
|
|
|
|
|
|
|
|
const char*& name = sProperties[slot - SLOT_FIRST].name;
|
|
|
|
ProgressEvent* event = GetInstancePrivate(aCx, aObj, name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-30 11:19:09 +00:00
|
|
|
aVp.set(JS_GetReservedSlot(aObj, slot));
|
2012-02-05 20:07:23 +00:00
|
|
|
return true;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 23:11:11 +00:00
|
|
|
InitProgressEvent(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
|
2011-12-01 21:30:28 +00:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
|
|
|
|
ProgressEvent* event = GetInstancePrivate(aCx, obj, sFunctions[0].name);
|
|
|
|
if (!event) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSString* type;
|
|
|
|
JSBool bubbles, cancelable, lengthComputable;
|
2012-02-24 22:19:52 +00:00
|
|
|
double loaded, total;
|
2011-07-17 19:09:13 +00:00
|
|
|
if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "Sbbbdd", &type,
|
|
|
|
&bubbles, &cancelable, &lengthComputable, &loaded,
|
|
|
|
&total)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
InitProgressEventCommon(obj, event, type, bubbles, cancelable,
|
|
|
|
lengthComputable, loaded, total, false);
|
|
|
|
return true;
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSClass ProgressEvent::sClass = {
|
|
|
|
"ProgressEvent",
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT),
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
2012-03-19 14:27:58 +00:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
JSPropertySpec ProgressEvent::sProperties[] = {
|
2012-08-08 05:26:18 +00:00
|
|
|
{ "lengthComputable", SLOT_lengthComputable, PROPERTY_FLAGS,
|
|
|
|
JSOP_WRAPPER(GetProperty), JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "loaded", SLOT_loaded, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "total", SLOT_total, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
|
2011-07-17 19:09:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
JSFunctionSpec ProgressEvent::sFunctions[] = {
|
|
|
|
JS_FN("initProgressEvent", InitProgressEvent, 6, FUNCTION_FLAGS),
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
Event*
|
2012-02-05 20:07:23 +00:00
|
|
|
Event::GetPrivate(JSObject* aObj)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
|
|
|
if (aObj) {
|
2012-02-04 00:54:57 +00:00
|
|
|
JSClass* classPtr = JS_GetClass(aObj);
|
2011-07-17 19:09:13 +00:00
|
|
|
if (IsThisClass(classPtr) ||
|
|
|
|
MessageEvent::IsThisClass(classPtr) ||
|
|
|
|
ErrorEvent::IsThisClass(classPtr) ||
|
|
|
|
classPtr == ProgressEvent::Class()) {
|
2012-02-05 20:07:23 +00:00
|
|
|
return GetJSPrivateSafeish<Event>(aObj);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* anonymous namespace */
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace events {
|
|
|
|
|
|
|
|
bool
|
|
|
|
InitClasses(JSContext* aCx, JSObject* aGlobal, bool aMainRuntime)
|
|
|
|
{
|
|
|
|
JSObject* eventProto = Event::InitClass(aCx, aGlobal, aMainRuntime);
|
|
|
|
if (!eventProto) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MessageEvent::InitClass(aCx, aGlobal, eventProto, aMainRuntime) &&
|
|
|
|
ErrorEvent::InitClass(aCx, aGlobal, eventProto, aMainRuntime) &&
|
|
|
|
ProgressEvent::InitClass(aCx, aGlobal, eventProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
CreateGenericEvent(JSContext* aCx, JSString* aType, bool aBubbles,
|
|
|
|
bool aCancelable, bool aMainRuntime)
|
|
|
|
{
|
2011-07-27 23:15:50 +00:00
|
|
|
JSObject* global = JS_GetGlobalForScopeChain(aCx);
|
2011-07-17 19:09:13 +00:00
|
|
|
return Event::Create(aCx, global, aType, aBubbles, aCancelable, aMainRuntime);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
CreateMessageEvent(JSContext* aCx, JSAutoStructuredCloneBuffer& aData,
|
2011-08-16 03:40:38 +00:00
|
|
|
nsTArray<nsCOMPtr<nsISupports> >& aClonedObjects,
|
2011-07-17 19:09:13 +00:00
|
|
|
bool aMainRuntime)
|
|
|
|
{
|
2011-07-27 23:15:50 +00:00
|
|
|
JSObject* global = JS_GetGlobalForScopeChain(aCx);
|
2011-08-16 03:40:38 +00:00
|
|
|
return MessageEvent::Create(aCx, global, aData, aClonedObjects, aMainRuntime);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
CreateErrorEvent(JSContext* aCx, JSString* aMessage, JSString* aFilename,
|
2012-10-11 23:38:04 +00:00
|
|
|
uint32_t aLineNumber, bool aMainRuntime)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2011-07-27 23:15:50 +00:00
|
|
|
JSObject* global = JS_GetGlobalForScopeChain(aCx);
|
2011-07-17 19:09:13 +00:00
|
|
|
return ErrorEvent::Create(aCx, global, aMessage, aFilename, aLineNumber,
|
|
|
|
aMainRuntime);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
CreateProgressEvent(JSContext* aCx, JSString* aType, bool aLengthComputable,
|
2012-02-24 22:19:52 +00:00
|
|
|
double aLoaded, double aTotal)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2011-07-27 23:15:50 +00:00
|
|
|
JSObject* global = JS_GetGlobalForScopeChain(aCx);
|
2011-07-17 19:09:13 +00:00
|
|
|
return ProgressEvent::Create(aCx, global, aType, aLengthComputable, aLoaded,
|
|
|
|
aTotal);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-02-05 20:07:23 +00:00
|
|
|
IsSupportedEventClass(JSObject* aEvent)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
return Event::IsSupportedClass(aEvent);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2012-02-05 20:07:23 +00:00
|
|
|
void
|
|
|
|
SetEventTarget(JSObject* aEvent, JSObject* aTarget)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
Event::SetTarget(aEvent, aTarget);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2012-02-05 20:07:23 +00:00
|
|
|
EventWasCanceled(JSObject* aEvent)
|
2011-07-17 19:09:13 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
return Event::WasCanceled(aEvent);
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
2011-11-05 21:41:16 +00:00
|
|
|
bool
|
2012-02-05 20:07:23 +00:00
|
|
|
EventImmediatePropagationStopped(JSObject* aEvent)
|
2011-11-05 21:41:16 +00:00
|
|
|
{
|
2012-02-05 20:07:23 +00:00
|
|
|
return Event::ImmediatePropagationStopped(aEvent);
|
2011-11-05 21:41:16 +00:00
|
|
|
}
|
|
|
|
|
2011-07-17 19:09:13 +00:00
|
|
|
bool
|
|
|
|
DispatchEventToTarget(JSContext* aCx, JSObject* aTarget, JSObject* aEvent,
|
|
|
|
bool* aPreventDefaultCalled)
|
|
|
|
{
|
2011-10-14 15:58:37 +00:00
|
|
|
static const char kFunctionName[] = "dispatchEvent";
|
|
|
|
JSBool hasProperty;
|
|
|
|
if (!JS_HasProperty(aCx, aTarget, kFunctionName, &hasProperty)) {
|
2011-07-17 19:09:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-14 15:58:37 +00:00
|
|
|
JSBool preventDefaultCalled = false;
|
|
|
|
if (hasProperty) {
|
|
|
|
jsval argv[] = { OBJECT_TO_JSVAL(aEvent) };
|
|
|
|
jsval rval = JSVAL_VOID;
|
2011-10-21 10:16:47 +00:00
|
|
|
if (!JS_CallFunctionName(aCx, aTarget, kFunctionName, ArrayLength(argv),
|
2011-10-14 15:58:37 +00:00
|
|
|
argv, &rval) ||
|
|
|
|
!JS_ValueToBoolean(aCx, rval, &preventDefaultCalled)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 19:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*aPreventDefaultCalled = !!preventDefaultCalled;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace events
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|