[dawn][emscripten] Add device lost future getter.

- Adds a non-standardized (yet?) getter for the device lost
  future in order to support testing in Emscripten.

Bug: 374803367
Change-Id: I659281c88709dd3d1aa566985998267c54ea3a7d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/211938
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Loko Kung <lokokung@google.com>
diff --git a/src/dawn/dawn.json b/src/dawn/dawn.json
index 2b2cb28..dafb1cc 100644
--- a/src/dawn/dawn.json
+++ b/src/dawn/dawn.json
@@ -1547,6 +1547,11 @@
                 ]
             },
             {
+                "name": "get lost future",
+                "returns": "future",
+                "tags": ["emscripten"]
+            },
+            {
                 "name": "has feature",
                 "returns": "bool",
                 "args": [
diff --git a/third_party/emdawnwebgpu/webgpu.cpp b/third_party/emdawnwebgpu/webgpu.cpp
index aa3be19..5bd625e1 100644
--- a/third_party/emdawnwebgpu/webgpu.cpp
+++ b/third_party/emdawnwebgpu/webgpu.cpp
@@ -566,6 +566,7 @@
 
   template <typename Event, typename... ReadyArgs>
   void SetFutureReady(FutureID futureId, ReadyArgs&&... readyArgs) {
+    assert(futureId != kNullFutureId);
     std::unique_ptr<TrackedEvent> spontaneousEvent;
     {
       std::unique_lock<std::mutex> lock(mMutex);
@@ -681,9 +682,8 @@
 
   void Destroy();
   WGPUQueue GetQueue() const;
-  WGPUFuture GetDeviceLostFuture() const;
+  WGPUFuture GetLostFuture() const;
 
-  void OnDeviceLost(WGPUDeviceLostReason reason, const char* message);
   void OnUncapturedError(WGPUErrorType type, char const* message);
 
  private:
@@ -1228,10 +1228,11 @@
                                                          device, message);
   } else {
     // If the request failed, we need to resolve the DeviceLostEvent.
-    device->OnDeviceLost(WGPUDeviceLostReason_FailedCreation,
-                         "Device failed at creation.");
     GetEventManager().SetFutureReady<RequestDeviceEvent>(futureId, status,
                                                          nullptr, message);
+    GetEventManager().SetFutureReady<DeviceLostEvent>(
+        device->GetLostFuture().id, WGPUDeviceLostReason_FailedCreation,
+        "Device failed at creation.");
   }
 }
 void emwgpuOnWorkDoneCompleted(double futureId,
@@ -1382,8 +1383,6 @@
 
 void WGPUDeviceImpl::Destroy() {
   emwgpuDeviceDestroy(this);
-  // TODO(374803367): Remove this when we can get the device lost future.
-  OnDeviceLost(WGPUDeviceLostReason_Destroyed, "Device was destroyed.");
 }
 
 WGPUQueue WGPUDeviceImpl::GetQueue() const {
@@ -1391,19 +1390,10 @@
   return ReturnToAPI(std::move(queue));
 }
 
-WGPUFuture WGPUDeviceImpl::GetDeviceLostFuture() const {
+WGPUFuture WGPUDeviceImpl::GetLostFuture() const {
   return WGPUFuture{mDeviceLostFutureId};
 }
 
-void WGPUDeviceImpl::OnDeviceLost(WGPUDeviceLostReason reason,
-                                  const char* message) {
-  if (mDeviceLostFutureId != kNullFutureId) {
-    GetEventManager().SetFutureReady<DeviceLostEvent>(mDeviceLostFutureId,
-                                                      reason, message);
-  }
-  mDeviceLostFutureId = kNullFutureId;
-}
-
 void WGPUDeviceImpl::OnUncapturedError(WGPUErrorType type,
                                        char const* message) {
   if (mUncapturedErrorCallbackInfo.callback) {
@@ -1576,7 +1566,7 @@
   // Device is also immediately associated with the DeviceLostEvent.
   WGPUQueue queue = new WGPUQueueImpl(adapter);
   WGPUDevice device = new WGPUDeviceImpl(adapter, descriptor, queue);
-  auto deviceLostFutureId = device->GetDeviceLostFuture().id;
+  auto deviceLostFutureId = device->GetLostFuture().id;
 
   emwgpuAdapterRequestDevice(adapter, futureId, deviceLostFutureId, device,
                              queue, descriptor);
@@ -1733,6 +1723,10 @@
   device->Destroy();
 }
 
+WGPUFuture wgpuDeviceGetLostFuture(WGPUDevice device) {
+  return device->GetLostFuture();
+}
+
 WGPUQueue wgpuDeviceGetQueue(WGPUDevice device) {
   return device->GetQueue();
 }