Bug 1752155 - Refactor WebGPU's ValidationError to make it constructable from JS. r=gfx-reviewers,lsalzman,webidl,edgar

Differential Revision: https://phabricator.services.mozilla.com/D144322
This commit is contained in:
Andrew Osmond 2022-04-23 18:15:51 +00:00
parent 44139eec4c
commit 6504eb38b3
5 changed files with 41 additions and 23 deletions

View File

@ -360,8 +360,8 @@ already_AddRefed<dom::Promise> Device::PopErrorScope(ErrorResult& aRv) {
if (aMaybeError->validationMessage.IsEmpty()) {
error.SetAsGPUOutOfMemoryError();
} else {
error.SetAsGPUValidationError() =
new ValidationError(self, aMaybeError->validationMessage);
error.SetAsGPUValidationError() = new ValidationError(
self->GetParentObject(), aMaybeError->validationMessage);
}
promise->MaybeResolve(std::move(error));
}

View File

@ -4,28 +4,38 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ValidationError.h"
#include "Device.h"
#include "mozilla/dom/WebGPUBinding.h"
#include "mozilla/ErrorResult.h"
#include "nsIGlobalObject.h"
#include "nsReadableUtils.h"
namespace mozilla {
namespace webgpu {
namespace mozilla::webgpu {
GPU_IMPL_CYCLE_COLLECTION(ValidationError, mParent)
GPU_IMPL_CYCLE_COLLECTION(ValidationError, mGlobal)
GPU_IMPL_JS_WRAP(ValidationError)
ValidationError::ValidationError(Device* aParent, const nsACString& aMessage)
: ChildOf(aParent), mMessage(aMessage) {}
ValidationError::ValidationError(nsIGlobalObject* aGlobal,
const nsACString& aMessage)
: mGlobal(aGlobal) {
CopyUTF8toUTF16(aMessage, mMessage);
}
ValidationError::ValidationError(nsIGlobalObject* aGlobal,
const nsAString& aMessage)
: mGlobal(aGlobal), mMessage(aMessage) {}
ValidationError::~ValidationError() = default;
already_AddRefed<ValidationError> ValidationError::Constructor(
const dom::GlobalObject& aGlobal, const nsAString& aString) {
MOZ_CRASH("TODO");
const dom::GlobalObject& aGlobal, const nsAString& aString,
ErrorResult& aRv) {
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
if (!global) {
aRv.ThrowInvalidStateError("aGlobal is not nsIGlobalObject");
return nullptr;
}
return MakeAndAddRef<ValidationError>(global, aString);
}
void ValidationError::GetMessage(nsAString& aMessage) const {
CopyUTF8toUTF16(mMessage, aMessage);
}
} // namespace webgpu
} // namespace mozilla
} // namespace mozilla::webgpu

View File

@ -6,23 +6,28 @@
#ifndef GPU_ValidationError_H_
#define GPU_ValidationError_H_
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsWrapperCache.h"
#include "ObjectModel.h"
namespace mozilla {
class ErrorResult;
namespace dom {
class GlobalObject;
} // namespace dom
namespace webgpu {
class Device;
class ValidationError final : public nsWrapperCache, public ChildOf<Device> {
nsCString mMessage;
class ValidationError final : public nsWrapperCache {
nsCOMPtr<nsIGlobalObject> mGlobal;
nsString mMessage;
public:
GPU_DECL_CYCLE_COLLECTION(ValidationError)
GPU_DECL_JS_WRAP(ValidationError)
ValidationError(Device* aParent, const nsACString& aMessage);
ValidationError(nsIGlobalObject* aGlobal, const nsACString& aMessage);
ValidationError(nsIGlobalObject* aGlobal, const nsAString& aMessage);
private:
virtual ~ValidationError();
@ -30,8 +35,10 @@ class ValidationError final : public nsWrapperCache, public ChildOf<Device> {
public:
static already_AddRefed<ValidationError> Constructor(
const dom::GlobalObject& aGlobal, const nsAString& aString);
void GetMessage(nsAString& aMessage) const;
const dom::GlobalObject& aGlobal, const nsAString& aString,
ErrorResult& aRv);
void GetMessage(nsAString& aMessage) const { aMessage = mMessage; }
nsIGlobalObject* GetParentObject() const { return mGlobal; }
};
} // namespace webgpu

View File

@ -998,7 +998,7 @@ ipc::IPCResult WebGPUChild::RecvDeviceUncapturedError(
dom::GPUUncapturedErrorEventInit init;
init.mError.SetAsGPUValidationError() =
new ValidationError(target, aMessage);
new ValidationError(target->GetParentObject(), aMessage);
RefPtr<mozilla::dom::GPUUncapturedErrorEvent> event =
dom::GPUUncapturedErrorEvent::Constructor(
target, u"uncapturederror"_ns, init);

View File

@ -208,6 +208,7 @@ interface GPUOutOfMemoryError {
[Pref="dom.webgpu.enabled",
Exposed=(Window,DedicatedWorker)]
interface GPUValidationError {
[Throws]
constructor(DOMString message);
readonly attribute DOMString message;
};