mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 996831. Add a ToJSValue overload for nsresult, to allow rejecting promises from C++ more easily. r=bholley
This commit is contained in:
parent
557d9e0d10
commit
c6a5fb4be3
@ -131,28 +131,7 @@ Throw(JSContext* aCx, nsresult aRv, const char* aMessage)
|
||||
}
|
||||
}
|
||||
|
||||
nsRefPtr<Exception> finalException;
|
||||
|
||||
// Do we use DOM exceptions for this error code?
|
||||
switch (NS_ERROR_GET_MODULE(aRv)) {
|
||||
case NS_ERROR_MODULE_DOM:
|
||||
case NS_ERROR_MODULE_SVG:
|
||||
case NS_ERROR_MODULE_DOM_XPATH:
|
||||
case NS_ERROR_MODULE_DOM_INDEXEDDB:
|
||||
case NS_ERROR_MODULE_DOM_FILEHANDLE:
|
||||
finalException = DOMException::Create(aRv);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// If not, use the default.
|
||||
if (!finalException) {
|
||||
// aMessage can be null.
|
||||
finalException = new Exception(nsCString(aMessage), aRv,
|
||||
EmptyCString(), nullptr, nullptr);
|
||||
}
|
||||
nsRefPtr<Exception> finalException = CreateException(aCx, aRv, aMessage);
|
||||
|
||||
MOZ_ASSERT(finalException);
|
||||
if (!ThrowExceptionObject(aCx, finalException)) {
|
||||
@ -164,6 +143,29 @@ Throw(JSContext* aCx, nsresult aRv, const char* aMessage)
|
||||
return false;
|
||||
}
|
||||
|
||||
already_AddRefed<Exception>
|
||||
CreateException(JSContext* aCx, nsresult aRv, const char* aMessage)
|
||||
{
|
||||
// Do we use DOM exceptions for this error code?
|
||||
switch (NS_ERROR_GET_MODULE(aRv)) {
|
||||
case NS_ERROR_MODULE_DOM:
|
||||
case NS_ERROR_MODULE_SVG:
|
||||
case NS_ERROR_MODULE_DOM_XPATH:
|
||||
case NS_ERROR_MODULE_DOM_INDEXEDDB:
|
||||
case NS_ERROR_MODULE_DOM_FILEHANDLE:
|
||||
return DOMException::Create(aRv);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// If not, use the default.
|
||||
// aMessage can be null, so we can't use nsDependentCString on it.
|
||||
nsRefPtr<Exception> exception =
|
||||
new Exception(nsCString(aMessage), aRv,
|
||||
EmptyCString(), nullptr, nullptr);
|
||||
return exception.forget();
|
||||
}
|
||||
|
||||
already_AddRefed<nsIStackFrame>
|
||||
GetCurrentJSStack()
|
||||
{
|
||||
|
@ -30,6 +30,11 @@ ThrowExceptionObject(JSContext* aCx, Exception* aException);
|
||||
bool
|
||||
ThrowExceptionObject(JSContext* aCx, nsIException* aException);
|
||||
|
||||
// Create an exception object for the given nsresult and message but
|
||||
// don't set it pending on aCx. This never returns null.
|
||||
already_AddRefed<Exception>
|
||||
CreateException(JSContext* aCx, nsresult aRv, const char* aMessage = nullptr);
|
||||
|
||||
already_AddRefed<nsIStackFrame>
|
||||
GetCurrentJSStack();
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "mozilla/dom/ToJSValue.h"
|
||||
#include "mozilla/dom/DOMException.h"
|
||||
#include "mozilla/dom/Exceptions.h"
|
||||
#include "nsAString.h"
|
||||
#include "nsContentUtils.h"
|
||||
#include "nsStringBuffer.h"
|
||||
@ -49,5 +51,14 @@ ISupportsToJSValue(JSContext* aCx,
|
||||
|
||||
} // namespace tojsvalue_detail
|
||||
|
||||
bool
|
||||
ToJSValue(JSContext* aCx,
|
||||
nsresult aArgument,
|
||||
JS::MutableHandle<JS::Value> aValue)
|
||||
{
|
||||
nsRefPtr<Exception> exception = CreateException(aCx, aArgument);
|
||||
return ToJSValue(aCx, exception, aValue);
|
||||
}
|
||||
|
||||
} // namespace dom
|
||||
} // namespace mozilla
|
||||
|
@ -54,6 +54,14 @@ ToJSValue(JSContext* aCx,
|
||||
return true;
|
||||
}
|
||||
|
||||
// The uint32_t version is disabled for now because on the super-old b2g
|
||||
// compiler nsresult and uint32_t are the same type. If someone needs this at
|
||||
// some point we'll need to figure out how to make it work (e.g. by switching to
|
||||
// traits structs and using the trick IPC's ParamTraits uses, where a traits
|
||||
// struct templated on the type inherits from a base traits struct of some sort,
|
||||
// templated on the same type, or something). Maybe b2g will update to a modern
|
||||
// compiler before that happens....
|
||||
#if 0
|
||||
inline bool
|
||||
ToJSValue(JSContext* aCx,
|
||||
uint32_t aArgument,
|
||||
@ -65,6 +73,7 @@ ToJSValue(JSContext* aCx,
|
||||
aValue.setNumber(aArgument);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
inline bool
|
||||
ToJSValue(JSContext* aCx,
|
||||
@ -212,6 +221,13 @@ ToJSValue(JSContext* aCx, JS::Handle<JS::Value> aArgument,
|
||||
return MaybeWrapValue(aCx, aValue);
|
||||
}
|
||||
|
||||
// Accept nsresult, for use in rejections, and create an XPCOM
|
||||
// exception object representing that nsresult.
|
||||
bool
|
||||
ToJSValue(JSContext* aCx,
|
||||
nsresult aArgument,
|
||||
JS::MutableHandle<JS::Value> aValue);
|
||||
|
||||
// Accept arrays of other things we accept
|
||||
template <typename T>
|
||||
bool
|
||||
|
@ -78,15 +78,17 @@ public:
|
||||
JS::Handle<JS::Value> aValue);
|
||||
|
||||
// Helpers for using Promise from C++.
|
||||
// Most DOM objects are handled already. To add a new type T, such as ints,
|
||||
// or dictionaries, add a ToJSValue overload in ToJSValue.h.
|
||||
// Most DOM objects are handled already. To add a new type T, add a
|
||||
// ToJSValue overload in ToJSValue.h.
|
||||
// aArg is a const reference so we can pass rvalues like integer constants
|
||||
template <typename T>
|
||||
void MaybeResolve(T& aArg) {
|
||||
void MaybeResolve(const T& aArg) {
|
||||
MaybeSomething(aArg, &Promise::MaybeResolve);
|
||||
}
|
||||
|
||||
// aArg is a const reference so we can pass rvalues like NS_ERROR_*
|
||||
template <typename T>
|
||||
void MaybeReject(T& aArg) {
|
||||
void MaybeReject(const T& aArg) {
|
||||
MaybeSomething(aArg, &Promise::MaybeReject);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user