Truly independent web browser
LibWeb: Return the same key objects from IDBCursor key and primaryKey
Andreas Klingfb140168482b
diff --git a/Libraries/LibWeb/IndexedDB/IDBCursor.cpp b/Libraries/LibWeb/IndexedDB/IDBCursor.cppindex 7648aa900ce..58883148052 100644--- a/Libraries/LibWeb/IndexedDB/IDBCursor.cpp+++ b/Libraries/LibWeb/IndexedDB/IDBCursor.cpp@@ -22,17 +22,31 @@ JS::Value IDBCursor::key(JS::Realm& realm){// The key getter steps are to return the result of converting a key to a// value with the cursor's current key.- if (auto key = current_key())- return convert_a_key_to_a_value(realm, *key);-- return JS::js_undefined();+ // NOTE: If key returns an object (e.g. a Date or Array), it returns the same object instance every time it is+ // inspected, until the cursor's key is changed. This means that if the object is modified, those+ // modifications will be seen by anyone inspecting the value of the cursor. However modifying such an object+ // does not modify the contents of the database.+ auto key = current_key();+ if (!key)+ return JS::js_undefined();++ if (m_cached_key_value.key != key)+ m_cached_key_value = { key, convert_a_key_to_a_value(realm, *key) };+ return m_cached_key_value.value;}JS::Value IDBCursor::primary_key(JS::Realm& realm){// The primaryKey getter steps are to return the result of converting a key// to a value with the cursor's current effective key.- return convert_a_key_to_a_value(realm, effective_key());+ // NOTE: If primaryKey returns an object (e.g. a Date or Array), it returns the same object instance every time it+ // is inspected, until the cursor's effective key is changed. This means that if the object is modified,+ // those modifications will be seen by anyone inspecting the value of the cursor. However modifying such an+ // object does not modify the contents of the database.+ auto effective_key = this->effective_key();+ if (m_cached_primary_key_value.key.ptr() != effective_key.ptr())+ m_cached_primary_key_value = { effective_key, convert_a_key_to_a_value(realm, effective_key) };+ return m_cached_primary_key_value.value;}IDBCursor::IDBCursor(CursorSourceHandle source_handle, GC::Ptr<Key> position, CursorDirection direction, GotValue got_value, GC::Ptr<Key> key, JS::Value value, GC::Ref<IDBKeyRange> range, KeyOnly key_only)@@ -66,6 +80,10 @@ void IDBCursor::visit_edges(GC::Cell::Visitor& visitor)visitor.visit(m_request);visitor.visit(m_value);+ visitor.visit(m_cached_key_value.key);+ visitor.visit(m_cached_key_value.value);+ visitor.visit(m_cached_primary_key_value.key);+ visitor.visit(m_cached_primary_key_value.value);m_source_handle.visit([&](auto& source) {visitor.visit(source);diff --git a/Libraries/LibWeb/IndexedDB/IDBCursor.h b/Libraries/LibWeb/IndexedDB/IDBCursor.hindex ff4e3e58dcc..de8b7ebfa28 100644--- a/Libraries/LibWeb/IndexedDB/IDBCursor.h+++ b/Libraries/LibWeb/IndexedDB/IDBCursor.h@@ -108,6 +108,15 @@ private:// A cursor also has a key only flag, that indicates whether the cursor’s value is exposed via the API.bool m_key_only { false };++ // NB: The key and primaryKey getters return the same object instance every time they are inspected, until the+ // cursor's key or effective key changes. These cache the converted values along with the keys they came from.+ struct CachedKeyValue {+ GC::Ptr<Key> key;+ JS::Value value;+ };+ CachedKeyValue m_cached_key_value;+ CachedKeyValue m_cached_primary_key_value;};}diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.sharedworker.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.sharedworker.txtindex 594526ae399..d5bb05a508f 100644--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.sharedworker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.sharedworker.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-2 Pass-1 Fail+3 PassPass IDBCursor.keyPass IDBCursor.key 1-Fail IDBCursor.key 2\ No newline at end of file+Pass IDBCursor.key 2\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.txtindex 594526ae399..d5bb05a508f 100644--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-2 Pass-1 Fail+3 PassPass IDBCursor.keyPass IDBCursor.key 1-Fail IDBCursor.key 2\ No newline at end of file+Pass IDBCursor.key 2\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.worker.txtindex 594526ae399..d5bb05a508f 100644--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.worker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-key.any.worker.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-2 Pass-1 Fail+3 PassPass IDBCursor.keyPass IDBCursor.key 1-Fail IDBCursor.key 2\ No newline at end of file+Pass IDBCursor.key 2\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.sharedworker.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.sharedworker.txtindex 0d77e14bf58..235c2b082f8 100644--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.sharedworker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.sharedworker.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-2 Pass-1 Fail+3 PassPass IDBCursor.primaryKeyPass IDBCursor.primaryKey 1-Fail IDBCursor.primaryKey 2\ No newline at end of file+Pass IDBCursor.primaryKey 2\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.txtindex 0d77e14bf58..235c2b082f8 100644--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-2 Pass-1 Fail+3 PassPass IDBCursor.primaryKeyPass IDBCursor.primaryKey 1-Fail IDBCursor.primaryKey 2\ No newline at end of file+Pass IDBCursor.primaryKey 2\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.worker.txtindex 0d77e14bf58..235c2b082f8 100644--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.worker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbcursor-primarykey.any.worker.txt@@ -2,8 +2,7 @@ Harness status: OKFound 3 tests-2 Pass-1 Fail+3 PassPass IDBCursor.primaryKeyPass IDBCursor.primaryKey 1-Fail IDBCursor.primaryKey 2\ No newline at end of file+Pass IDBCursor.primaryKey 2\ No newline at end of file