2011-07-17 12:09:13 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2011-08-15 20:40:38 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07: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 12:09:13 -07:00
|
|
|
|
|
|
|
#include "Exceptions.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
2011-10-04 07:06:54 -07:00
|
|
|
#include "jsfriendapi.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "jsprf.h"
|
2012-03-30 21:42:20 -07:00
|
|
|
#include "mozilla/Util.h"
|
2012-04-24 16:50:00 -07:00
|
|
|
#include "nsDOMException.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
#include "nsTraceRefcnt.h"
|
|
|
|
|
|
|
|
#include "WorkerInlines.h"
|
|
|
|
|
|
|
|
#define PROPERTY_FLAGS \
|
2012-02-13 00:02:21 -08:00
|
|
|
(JSPROP_ENUMERATE | JSPROP_SHARED)
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
#define CONSTANT_FLAGS \
|
|
|
|
JSPROP_ENUMERATE | JSPROP_SHARED | JSPROP_PERMANENT | JSPROP_READONLY
|
|
|
|
|
2011-10-21 03:16:47 -07:00
|
|
|
using namespace mozilla;
|
2011-07-17 12:09:13 -07:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class DOMException : public PrivatizableBase
|
|
|
|
{
|
|
|
|
static JSClass sClass;
|
2013-04-22 14:15:36 -07:00
|
|
|
static const JSPropertySpec sProperties[];
|
2013-04-22 14:15:49 -07:00
|
|
|
static const JSFunctionSpec sFunctions[];
|
2013-04-22 14:15:36 -07:00
|
|
|
static const JSPropertySpec sStaticProperties[];
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
enum SLOT {
|
|
|
|
SLOT_code = 0,
|
|
|
|
SLOT_name,
|
2012-04-24 16:50:00 -07:00
|
|
|
SLOT_message,
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
SLOT_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
static JSObject*
|
|
|
|
InitClass(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JSObject*> proto(aCx, JS_InitClass(aCx, aObj, nullptr, &sClass, Construct, 0,
|
|
|
|
sProperties, sFunctions, sStaticProperties,
|
|
|
|
nullptr));
|
2011-07-17 12:09:13 -07:00
|
|
|
if (proto && !JS_DefineProperties(aCx, proto, sStaticProperties)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject*
|
2012-04-24 16:50:00 -07:00
|
|
|
Create(JSContext* aCx, nsresult aNSResult);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
DOMException()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(mozilla::dom::workers::exceptions::DOMException);
|
|
|
|
}
|
|
|
|
|
|
|
|
~DOMException()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(mozilla::dom::workers::exceptions::DOMException);
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
|
|
|
|
sClass.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-19 07:34:55 -07:00
|
|
|
Finalize(JSFreeOp* aFop, JSObject* aObj)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-02-03 16:54:57 -08:00
|
|
|
JS_ASSERT(JS_GetClass(aObj) == &sClass);
|
2012-02-05 12:07:23 -08:00
|
|
|
delete GetJSPrivateSafeish<DOMException>(aObj);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-02 00:41:57 -07:00
|
|
|
static bool
|
2012-02-28 15:11:11 -08:00
|
|
|
ToString(JSContext* aCx, unsigned aArgc, jsval* aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_THIS_OBJECT(aCx, aVp));
|
2011-12-01 13:30:28 -08:00
|
|
|
if (!obj) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-02-03 16:54:57 -08:00
|
|
|
JSClass* classPtr = JS_GetClass(obj);
|
2012-02-03 10:00:08 -08:00
|
|
|
if (classPtr != &sClass) {
|
2011-07-17 12:09:13 -07:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name, "toString",
|
2012-02-03 10:00:08 -08:00
|
|
|
classPtr->name);
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JS::Value> name(aCx, JS_GetReservedSlot(obj, SLOT_name));
|
2012-04-24 16:50:00 -07:00
|
|
|
JS_ASSERT(name.isString());
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-04-24 16:50:00 -07:00
|
|
|
JSString *colon = JS_NewStringCopyN(aCx, ": ", 2);
|
|
|
|
if (!colon){
|
2012-04-24 16:50:00 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-24 16:50:00 -07:00
|
|
|
JSString* out = JS_ConcatStrings(aCx, name.toString(), colon);
|
|
|
|
if (!out) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval message = JS_GetReservedSlot(obj, SLOT_message);
|
|
|
|
JS_ASSERT(message.isString());
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-04-24 16:50:00 -07:00
|
|
|
out = JS_ConcatStrings(aCx, out, message.toString());
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!out) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(aCx, aVp, STRING_TO_JSVAL(out));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
GetProperty(JSContext* aCx, JS::Handle<JSObject*> aObj, JS::Handle<jsid> aIdval,
|
|
|
|
JS::MutableHandle<JS::Value> aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(aIdval));
|
|
|
|
|
2012-10-11 16:38:04 -07:00
|
|
|
int32_t slot = JSID_TO_INT(aIdval);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-02-03 16:54:57 -08:00
|
|
|
JSClass* classPtr = JS_GetClass(aObj);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2012-02-05 12:07:23 -08:00
|
|
|
if (classPtr != &sClass || !GetJSPrivateSafeish<DOMException>(aObj)) {
|
2011-07-17 12:09:13 -07:00
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_INCOMPATIBLE_PROTO, sClass.name,
|
2012-02-03 10:00:08 -08:00
|
|
|
sProperties[slot].name, classPtr->name);
|
2011-07-17 12:09:13 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-30 04:19:09 -07:00
|
|
|
aVp.set(JS_GetReservedSlot(aObj, slot));
|
2012-02-05 12:07:23 -08:00
|
|
|
return true;
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool
|
2013-06-21 06:12:46 -07:00
|
|
|
GetConstant(JSContext* aCx, JS::Handle<JSObject*> aObj, JS::Handle<jsid> idval,
|
|
|
|
JS::MutableHandle<JS::Value> aVp)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSID_IS_INT(idval));
|
2012-07-30 04:19:09 -07:00
|
|
|
aVp.set(INT_TO_JSVAL(JSID_TO_INT(idval)));
|
2011-07-17 12:09:13 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
JSClass DOMException::sClass = {
|
|
|
|
"DOMException",
|
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT),
|
2013-04-05 21:22:55 -07:00
|
|
|
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
|
2012-03-19 07:27:58 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
2013-04-22 14:15:36 -07:00
|
|
|
const JSPropertySpec DOMException::sProperties[] = {
|
2012-08-07 22:26:18 -07:00
|
|
|
{ "code", SLOT_code, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "name", SLOT_name, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ "message", SLOT_message, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
|
|
|
|
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
|
|
|
|
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
2013-04-22 14:15:49 -07:00
|
|
|
const JSFunctionSpec DOMException::sFunctions[] = {
|
2011-07-17 12:09:13 -07:00
|
|
|
JS_FN("toString", ToString, 0, 0),
|
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
2013-04-22 14:15:36 -07:00
|
|
|
const JSPropertySpec DOMException::sStaticProperties[] = {
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
#define EXCEPTION_ENTRY(_name) \
|
2012-08-07 22:26:18 -07:00
|
|
|
{ #_name, _name, CONSTANT_FLAGS, JSOP_WRAPPER(GetConstant), JSOP_NULLWRAPPER },
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
EXCEPTION_ENTRY(INDEX_SIZE_ERR)
|
|
|
|
EXCEPTION_ENTRY(DOMSTRING_SIZE_ERR)
|
|
|
|
EXCEPTION_ENTRY(HIERARCHY_REQUEST_ERR)
|
|
|
|
EXCEPTION_ENTRY(WRONG_DOCUMENT_ERR)
|
|
|
|
EXCEPTION_ENTRY(INVALID_CHARACTER_ERR)
|
|
|
|
EXCEPTION_ENTRY(NO_DATA_ALLOWED_ERR)
|
|
|
|
EXCEPTION_ENTRY(NO_MODIFICATION_ALLOWED_ERR)
|
|
|
|
EXCEPTION_ENTRY(NOT_FOUND_ERR)
|
|
|
|
EXCEPTION_ENTRY(NOT_SUPPORTED_ERR)
|
|
|
|
EXCEPTION_ENTRY(INUSE_ATTRIBUTE_ERR)
|
|
|
|
EXCEPTION_ENTRY(INVALID_STATE_ERR)
|
|
|
|
EXCEPTION_ENTRY(SYNTAX_ERR)
|
|
|
|
EXCEPTION_ENTRY(INVALID_MODIFICATION_ERR)
|
|
|
|
EXCEPTION_ENTRY(NAMESPACE_ERR)
|
|
|
|
EXCEPTION_ENTRY(INVALID_ACCESS_ERR)
|
|
|
|
EXCEPTION_ENTRY(VALIDATION_ERR)
|
|
|
|
EXCEPTION_ENTRY(TYPE_MISMATCH_ERR)
|
|
|
|
EXCEPTION_ENTRY(SECURITY_ERR)
|
|
|
|
EXCEPTION_ENTRY(NETWORK_ERR)
|
|
|
|
EXCEPTION_ENTRY(ABORT_ERR)
|
|
|
|
EXCEPTION_ENTRY(URL_MISMATCH_ERR)
|
|
|
|
EXCEPTION_ENTRY(QUOTA_EXCEEDED_ERR)
|
|
|
|
EXCEPTION_ENTRY(TIMEOUT_ERR)
|
|
|
|
EXCEPTION_ENTRY(INVALID_NODE_TYPE_ERR)
|
|
|
|
EXCEPTION_ENTRY(DATA_CLONE_ERR)
|
|
|
|
|
|
|
|
#undef EXCEPTION_ENTRY
|
|
|
|
|
2012-08-07 22:26:18 -07:00
|
|
|
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
|
2011-07-17 12:09:13 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// static
|
|
|
|
JSObject*
|
2012-04-24 16:50:00 -07:00
|
|
|
DOMException::Create(JSContext* aCx, nsresult aNSResult)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-05-04 00:52:57 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_NewObject(aCx, &sClass, NULL, NULL));
|
2011-07-17 12:09:13 -07:00
|
|
|
if (!obj) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-24 16:50:00 -07:00
|
|
|
const char* name;
|
|
|
|
const char* message;
|
|
|
|
uint16_t code;
|
|
|
|
if (NS_FAILED(NS_GetNameAndMessageForDOMNSResult(aNSResult, &name, &message,
|
|
|
|
&code))) {
|
|
|
|
JS_ReportError(aCx, "Exception thrown (nsresult = 0x%x).", aNSResult);
|
2011-07-17 12:09:13 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-10 19:39:45 -07:00
|
|
|
JS::Rooted<JSString*> jsname(aCx, JS_NewStringCopyZ(aCx, name));
|
2012-04-24 16:50:00 -07:00
|
|
|
if (!jsname) {
|
2011-08-15 20:40:38 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-10 19:39:45 -07:00
|
|
|
JS::Rooted<JSString*> jsmessage(aCx, JS_NewStringCopyZ(aCx, message));
|
2012-04-24 16:50:00 -07:00
|
|
|
if (!jsmessage) {
|
2011-08-15 20:40:38 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-24 16:50:00 -07:00
|
|
|
JS_SetReservedSlot(obj, SLOT_code, INT_TO_JSVAL(code));
|
|
|
|
JS_SetReservedSlot(obj, SLOT_name, STRING_TO_JSVAL(jsname));
|
|
|
|
JS_SetReservedSlot(obj, SLOT_message, STRING_TO_JSVAL(jsmessage));
|
2011-08-15 20:40:38 -07:00
|
|
|
|
2012-04-24 16:50:00 -07:00
|
|
|
DOMException* priv = new DOMException();
|
2012-02-05 12:07:23 -08:00
|
|
|
SetJSPrivateSafeish(obj, priv);
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace exceptions {
|
|
|
|
|
|
|
|
bool
|
|
|
|
InitClasses(JSContext* aCx, JSObject* aGlobal)
|
|
|
|
{
|
2012-04-24 16:50:00 -07:00
|
|
|
return DOMException::InitClass(aCx, aGlobal);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-04-24 16:50:00 -07:00
|
|
|
ThrowDOMExceptionForNSResult(JSContext* aCx, nsresult aNSResult)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2012-04-24 16:50:00 -07:00
|
|
|
JSObject* exception = DOMException::Create(aCx, aNSResult);
|
|
|
|
if (!exception) {
|
|
|
|
return;
|
|
|
|
}
|
2011-08-15 20:40:38 -07:00
|
|
|
|
|
|
|
JS_SetPendingException(aCx, OBJECT_TO_JSVAL(exception));
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
} // namespace exceptions
|
|
|
|
|
|
|
|
END_WORKERS_NAMESPACE
|