Skip to content

Commit

Permalink
LibWeb: Implement IDBFactory::cmp
Browse files Browse the repository at this point in the history
  • Loading branch information
stelar7 authored and awesomekling committed Nov 25, 2024
1 parent c43b93e commit 331f26a
Show file tree
Hide file tree
Showing 14 changed files with 655 additions and 1 deletion.
22 changes: 22 additions & 0 deletions Libraries/LibWeb/IndexedDB/IDBFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <LibWeb/IndexedDB/IDBDatabase.h>
#include <LibWeb/IndexedDB/IDBFactory.h>
#include <LibWeb/IndexedDB/Internal/Algorithms.h>
#include <LibWeb/IndexedDB/Internal/Key.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/StorageAPI/StorageKey.h>

Expand Down Expand Up @@ -93,4 +94,25 @@ WebIDL::ExceptionOr<GC::Ref<IDBOpenDBRequest>> IDBFactory::open(String const& na
return request;
}

// https://w3c.github.io/IndexedDB/#dom-idbfactory-cmp
WebIDL::ExceptionOr<i8> IDBFactory::cmp(JS::Value first, JS::Value second)
{
// 1. Let a be the result of converting a value to a key with first. Rethrow any exceptions.
auto a = convert_a_value_to_a_key(realm(), first);

// 2. If a is invalid, throw a "DataError" DOMException.
if (a.is_error())
return WebIDL::DataError::create(realm(), "Failed to convert a value to a key"_string);

// 3. Let b be the result of converting a value to a key with second. Rethrow any exceptions.
auto b = convert_a_value_to_a_key(realm(), second);

// 4. If b is invalid, throw a "DataError" DOMException.
if (b.is_error())
return WebIDL::DataError::create(realm(), "Failed to convert a value to a key"_string);

// 5. Return the results of comparing two keys with a and b.
return Key::compare_two_keys(a.release_value(), b.release_value());
}

}
1 change: 1 addition & 0 deletions Libraries/LibWeb/IndexedDB/IDBFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class IDBFactory : public Bindings::PlatformObject {
virtual ~IDBFactory() override;

WebIDL::ExceptionOr<GC::Ref<IDBOpenDBRequest>> open(String const& name, Optional<u64> version);
WebIDL::ExceptionOr<i8> cmp(JS::Value first, JS::Value second);

protected:
explicit IDBFactory(JS::Realm&);
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibWeb/IndexedDB/IDBFactory.idl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface IDBFactory {

[FIXME] Promise<sequence<IDBDatabaseInfo>> databases();

[FIXME] short cmp(any first, any second);
short cmp(any first, any second);
};

dictionary IDBDatabaseInfo {
Expand Down
11 changes: 11 additions & 0 deletions Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbfactory_cmp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Summary

Harness status: OK

Rerun

Found 1 tests

1 Pass
Details
Result Test Name MessagePass IDBFactory.cmp()
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Summary

Harness status: OK

Rerun

Found 3 tests

3 Pass
Details
Result Test Name MessagePass IDBFactory.cmp() - no argument
Pass IDBFactory.cmp() - null
Pass IDBFactory.cmp() - NaN
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Summary

Harness status: OK

Rerun

Found 4 tests

4 Pass
Details
Result Test Name MessagePass Array v.s. Binary
Pass Binary v.s. String
Pass String v.s. Date
Pass Date v.s. Number
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Summary

Harness status: OK

Rerun

Found 4 tests

4 Pass
Details
Result Test Name MessagePass Compare in unsigned octet values (in the range [0, 255])
Pass Compare values in then same length
Pass Compare values in different lengths
Pass Compare when the values in the range of their minimal length are the same
35 changes: 35 additions & 0 deletions Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keyorder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Summary

Harness status: OK

Rerun

Found 24 tests

12 Pass
12 Fail
Details
Result Test Name MessageFail Database readback sort - String < Array Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - String < Array
Fail Database readback sort - float < String Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - float < String
Fail Database readback sort - float < Date Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - float < Date
Fail Database readback sort - float < Date < String < Array Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - float < Date < String < Array
Fail Database readback sort - Date(1 sec ago) < Date(now) < Date(1 minute in future) Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - Date(1 sec ago) < Date(now) < Date(1 minute in future)
Fail Database readback sort - -1.1 < 1 < 1.01337 < 1.013373 < 2 Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - -1.1 < 1 < 1.01337 < 1.013373 < 2
Fail Database readback sort - -Infinity < -0.01 < 0 < Infinity Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - -Infinity < -0.01 < 0 < Infinity
Fail Database readback sort - "" < "a" < "ab" < "b" < "ba" Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - "" < "a" < "ab" < "b" < "ba"
Fail Database readback sort - Arrays Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - Arrays
Fail Database readback sort - Array.length: 10,000 < Array.length: 10,001 Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - Array.length: 10,000 < Array.length: 10,001
Fail Database readback sort - Infinity inside arrays Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - Infinity inside arrays
Fail Database readback sort - Test different stuff at once Cannot access property "transaction" on undefined object "db"
Pass IDBKey.cmp sorted - Test different stuff at once
21 changes: 21 additions & 0 deletions Tests/LibWeb/Text/input/wpt-import/IndexedDB/idbfactory_cmp.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>IDBFactory.cmp() - compared keys return correct value</title>
<link rel="author" title="Microsoft" href="http://www.microsoft.com">
<script src=../resources/testharness.js></script>
<script src=../resources/testharnessreport.js></script>
<script src=resources/support.js></script>

<script>
test(function() {
var greater = window.indexedDB.cmp(2, 1);
var equal = window.indexedDB.cmp(2, 2);
var less = window.indexedDB.cmp(1, 2);

assert_equals(greater, 1, "greater");
assert_equals(equal, 0, "equal");
assert_equals(less, -1, "less");
}, "IDBFactory.cmp()");
</script>

<div id=log></div>
41 changes: 41 additions & 0 deletions Tests/LibWeb/Text/input/wpt-import/IndexedDB/idbfactory_cmp2.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>IDBFactory.cmp() - invalid key</title>
<link rel="author" title="Intel" href="http://www.intel.com">
<link rel="help" href="https://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#widl-IDBFactory-cmp-short-any-first-any-second">
<script src=../resources/testharness.js></script>
<script src=../resources/testharnessreport.js></script>
<script src=resources/support.js></script>
<div id=log></div>
<script>
test( function() {
assert_throws_js(TypeError, function() {
indexedDB.cmp();
});
}, "IDBFactory.cmp() - no argument");

test( function() {
assert_throws_dom("DataError", function() {
indexedDB.cmp(null, null);
});
assert_throws_dom("DataError", function() {
indexedDB.cmp(1, null);
});
assert_throws_dom("DataError", function() {
indexedDB.cmp(null, 1);
});
}, "IDBFactory.cmp() - null");

test( function() {
assert_throws_dom("DataError", function() {
indexedDB.cmp(NaN, NaN);
});
assert_throws_dom("DataError", function() {
indexedDB.cmp(1, NaN);
});
assert_throws_dom("DataError", function() {
indexedDB.cmp(NaN, 1);
});
}, "IDBFactory.cmp() - NaN");
</script>

27 changes: 27 additions & 0 deletions Tests/LibWeb/Text/input/wpt-import/IndexedDB/idbfactory_cmp3.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>IDBFactory.cmp() - compared keys in different types</title>
<link rel="author" title="Mozilla" href="https://www.mozilla.org">
<link rel="help" href="http://w3c.github.io/IndexedDB/#key-construct">
<script src=../resources/testharness.js></script>
<script src=../resources/testharnessreport.js></script>

<script>
test(function() {
assert_equals(indexedDB.cmp([0], new Uint8Array([0])), 1, "Array > Binary");
}, "Array v.s. Binary");

test(function() {
assert_equals(indexedDB.cmp(new Uint8Array([0]), "0"), 1, "Binary > String");
}, "Binary v.s. String");

test(function() {
assert_equals(indexedDB.cmp("", new Date(0)), 1, "String > Date");
}, "String v.s. Date");

test(function() {
assert_equals(indexedDB.cmp(new Date(0), 0), 1, "Date > Number");
}, "Date v.s. Number");
</script>

<div id=log></div>
40 changes: 40 additions & 0 deletions Tests/LibWeb/Text/input/wpt-import/IndexedDB/idbfactory_cmp4.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>IDBFactory.cmp() - comparison of binary keys</title>
<link rel="author" title="Mozilla" href="https://www.mozilla.org">
<link rel="help" href="http://w3c.github.io/IndexedDB/#key-construct">
<script src=../resources/testharness.js></script>
<script src=../resources/testharnessreport.js></script>

<script>
test(function() {
assert_equals(indexedDB.cmp(new Int8Array([-1]), new Uint8Array([0])), 1,
"255(-1) shall be larger than 0");
}, "Compare in unsigned octet values (in the range [0, 255])");

test(function() {
assert_equals(indexedDB.cmp(
new Uint8Array([255, 254, 253]),
new Uint8Array([255, 253, 254])),
1,
"[255, 254, 253] shall be larger than [255, 253, 254]");
}, "Compare values in then same length");

test(function() {
assert_equals(indexedDB.cmp(
new Uint8Array([255, 254]),
new Uint8Array([255, 253, 254])),
1,
"[255, 254] shall be larger than [255, 253, 254]");
}, "Compare values in different lengths");

test(function() {
assert_equals(indexedDB.cmp(
new Uint8Array([255, 253, 254]),
new Uint8Array([255, 253])),
1,
"[255, 253, 254] shall be larger than [255, 253]");
}, "Compare when the values in the range of their minimal length are the same");
</script>

<div id=log></div>
Loading

0 comments on commit 331f26a

Please sign in to comment.