@@ -183,6 +183,89 @@ bool parse_and_validate_key(JSContext *cx, const char *key, size_t len) {
183
183
184
184
} // namespace
185
185
186
+ bool KVStore::has_pending_delete_handle (JSObject *self) {
187
+ MOZ_ASSERT (KVStore::is_instance (self));
188
+
189
+ JS::Value handle_val =
190
+ JS::GetReservedSlot (self, static_cast <uint32_t >(Slots::PendingDeleteHandle));
191
+ return handle_val.isInt32 () &&
192
+ handle_val.toInt32 () != host_api::ObjectStorePendingDelete::invalid;
193
+ }
194
+
195
+ host_api::ObjectStorePendingDelete KVStore::pending_delete_handle (JSObject *self) {
196
+ MOZ_ASSERT (KVStore::is_instance (self));
197
+ host_api::ObjectStorePendingDelete res;
198
+
199
+ JS::Value handle_val =
200
+ JS::GetReservedSlot (self, static_cast <uint32_t >(Slots::PendingDeleteHandle));
201
+ if (handle_val.isInt32 ()) {
202
+ res = host_api::ObjectStorePendingDelete (handle_val.toInt32 ());
203
+ }
204
+
205
+ return res;
206
+ }
207
+
208
+ bool KVStore::process_pending_kv_store_delete (JSContext *cx, JS::HandleObject self) {
209
+ MOZ_ASSERT (KVStore::is_instance (self));
210
+
211
+ auto pending_promise_value =
212
+ JS::GetReservedSlot (self, static_cast <uint32_t >(Slots::PendingDeletePromise));
213
+ MOZ_ASSERT (pending_promise_value.isObject ());
214
+ JS::RootedObject result_promise (cx, &pending_promise_value.toObject ());
215
+
216
+ auto res = builtins::KVStore::pending_delete_handle (self).wait ();
217
+
218
+ if (auto *err = res.to_err ()) {
219
+ HANDLE_ERROR (cx, *err);
220
+ return RejectPromiseWithPendingError (cx, result_promise);
221
+ }
222
+
223
+ JS::ResolvePromise (cx, result_promise, JS::UndefinedHandleValue);
224
+
225
+ return true ;
226
+ }
227
+
228
+ bool KVStore::delete_ (JSContext *cx, unsigned argc, JS::Value *vp) {
229
+ METHOD_HEADER_WITH_NAME (1 , " delete" );
230
+
231
+ JS::RootedObject result_promise (cx, JS::NewPromiseObject (cx, nullptr ));
232
+ if (!result_promise) {
233
+ return ReturnPromiseRejectedWithPendingError (cx, args);
234
+ }
235
+
236
+ JS::RootedValue key (cx, args.get (0 ));
237
+
238
+ // Convert the key argument into a String following https://tc39.es/ecma262/#sec-tostring
239
+ auto key_chars = core::encode (cx, key);
240
+ if (!key_chars) {
241
+ return ReturnPromiseRejectedWithPendingError (cx, args);
242
+ }
243
+
244
+ if (!parse_and_validate_key (cx, key_chars.begin (), key_chars.len )) {
245
+ return ReturnPromiseRejectedWithPendingError (cx, args);
246
+ }
247
+
248
+ auto res = kv_store_handle (self).delete_async (key_chars);
249
+
250
+ if (auto *err = res.to_err ()) {
251
+ HANDLE_ERROR (cx, *err);
252
+ return ReturnPromiseRejectedWithPendingError (cx, args);
253
+ }
254
+ auto ret = res.unwrap ();
255
+
256
+ JS::SetReservedSlot (self, static_cast <uint32_t >(Slots::PendingDeleteHandle),
257
+ JS::Int32Value (ret.handle ));
258
+ JS::SetReservedSlot (self, static_cast <uint32_t >(Slots::PendingDeletePromise),
259
+ JS::ObjectValue (*result_promise));
260
+
261
+ if (!core::EventLoop::queue_async_task (self)) {
262
+ return ReturnPromiseRejectedWithPendingError (cx, args);
263
+ }
264
+
265
+ args.rval ().setObject (*result_promise);
266
+ return true ;
267
+ }
268
+
186
269
host_api::ObjectStorePendingLookup KVStore::pending_lookup_handle (JSObject *self) {
187
270
MOZ_ASSERT (KVStore::is_instance (self));
188
271
host_api::ObjectStorePendingLookup res;
@@ -399,6 +482,7 @@ const JSPropertySpec KVStore::static_properties[] = {
399
482
};
400
483
401
484
const JSFunctionSpec KVStore::methods[] = {
485
+ JS_FN (" delete" , delete_, 1 , JSPROP_ENUMERATE),
402
486
JS_FN (" get" , get, 1 , JSPROP_ENUMERATE),
403
487
JS_FN (" put" , put, 1 , JSPROP_ENUMERATE),
404
488
JS_FS_END,
0 commit comments