Skip to content

Commit c618397

Browse files
committed
Test BigInt as keys and values in IndexedDB
BigInt and BigInt wrappers are supported in serialization, per whatwg/html#3480 This support allows them to be used as IndexedDB values. However, BigInt is not supported as an IndexedDB key; support has been proposed in the following PR, but that change has not landed at the time this patch was written w3c/IndexedDB#231
1 parent 08dd5d8 commit c618397

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

IndexedDB/bigint_value.htm

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>Values</title>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script src="support.js"></script>
7+
8+
<script>
9+
// BigInt and BigInt wrappers are supported in serialization, per
10+
// https://github.com/whatwg/html/pull/3480
11+
// This support allows them to be used as IndexedDB values.
12+
13+
let i = 0;
14+
function value(value, test) {
15+
var t = async_test(document.title + " - " + (i++));
16+
t.step(function() {
17+
assert_true(test(value),
18+
"condition does not apply to initial value");
19+
});
20+
21+
createdb(t).onupgradeneeded = function(e) {
22+
e.target.result
23+
.createObjectStore("store")
24+
.add(value, 1);
25+
26+
e.target.onsuccess = t.step_func(function(e) {
27+
e.target.result
28+
.transaction("store")
29+
.objectStore("store")
30+
.get(1)
31+
.onsuccess = t.step_func(function(e)
32+
{
33+
assert_true(test(e.target.result),
34+
"condition does not apply to deserialized result")
35+
t.done();
36+
});
37+
});
38+
};
39+
}
40+
41+
value(1n, x => x === 1n);
42+
value(Object(1n), x => typeof x === 'object' && x instanceof BigInt
43+
&& x.valueOf() === 1n);
44+
45+
// However, BigInt is not supported as an IndexedDB key; support
46+
// has been proposed in the following PR, but that change has not
47+
// landed at the time this patch was written
48+
// https://github.com/w3c/IndexedDB/pull/231
49+
50+
function invalidKey(key) {
51+
var t = async_test(document.title + " - " + (i++));
52+
53+
createdb(t).onupgradeneeded = function(e) {
54+
let store = e.target.result.createObjectStore("store")
55+
assert_throws('DataError', () => store.add(1, key));
56+
t.done();
57+
};
58+
}
59+
60+
invalidKey(1n);
61+
invalidKey(Object(1n)); // Still an error even if the IndexedDB patch lands
62+
63+
</script>
64+
65+
<div id="log"></div>

0 commit comments

Comments
 (0)