diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp index bcc208cac5e..9eeed50e519 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -790,4 +791,28 @@ void commit_a_transaction(JS::Realm& realm, GC::Ref transaction) })); } +// https://w3c.github.io/IndexedDB/#clone +WebIDL::ExceptionOr clone_in_realm(JS::Realm& target_realm, JS::Value value, GC::Ref transaction) +{ + auto& vm = target_realm.vm(); + + // 1. Assert: transaction’s state is active. + VERIFY(transaction->state() == IDBTransaction::TransactionState::Active); + + // 2. Set transaction’s state to inactive. + transaction->set_state(IDBTransaction::TransactionState::Inactive); + + // 3. Let serialized be ? StructuredSerializeForStorage(value). + auto serialized = TRY(HTML::structured_serialize_for_storage(vm, value)); + + // 4. Let clone be ? StructuredDeserialize(serialized, targetRealm). + auto clone = TRY(HTML::structured_deserialize(vm, serialized, target_realm)); + + // 5. Set transaction’s state to active. + transaction->set_state(IDBTransaction::TransactionState::Active); + + // 6. Return clone. + return clone; +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h index 7d7540e5fe7..f3f899e8e19 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h @@ -28,5 +28,6 @@ JS::Value convert_a_key_to_a_value(JS::Realm&, GC::Ref); bool is_valid_key_path(KeyPath const&); GC::Ref create_a_sorted_name_list(JS::Realm&, Vector); void commit_a_transaction(JS::Realm&, GC::Ref); +WebIDL::ExceptionOr clone_in_realm(JS::Realm&, JS::Value, GC::Ref); }