1 // META: global=window,worker
2 // META: title=IndexedDB: keys and values
3 // META: script=resources/support.js
4 // @author Odin Hørthe Omdal <mailto:odinho@opera.com>
8 function setOnUpgradeNeeded(t, predicate, _instanceof, value) {
9 createdb(t).onupgradeneeded = t.step_func(e => {
10 const db = e.target.result;
11 const store = db.createObjectStore("store");
14 e.target.onsuccess = t.step_func(e => {
15 const transaction = db.transaction("store", "readonly", { durability: "relaxed" });
16 const objectStore = transaction.objectStore("store");
17 objectStore.get(1).onsuccess = t.step_func(e => {
19 assert_true(predicate(e.target.result),
20 "Predicate should return true for the deserialized result.");
21 } else if (_instanceof) {
22 assert_true(e.target.result instanceof _instanceof, "instanceof");
30 // BigInt and BigInt objects are supported in serialization, per
31 // https://github.com/whatwg/html/pull/3480
32 // This support allows them to be used as IndexedDB values.
34 function value_test(value, predicate, name) {
37 assert_true(predicate(value),
38 "Predicate should return true for the initial value.");
41 setOnUpgradeNeeded(t, predicate, null, value);
42 }, "BigInts as values in IndexedDB - " + name);
48 value_test(Object(1n),
49 x => typeof x === 'object' &&
50 x instanceof BigInt &&
53 value_test({ val: 1n },
55 "primitive BigInt inside object");
56 value_test({ val: Object(1n) },
57 x => x.val.valueOf() === 1n &&
58 x.val instanceof BigInt &&
59 x.val.valueOf() === 1n,
60 "BigInt object inside object");
62 // However, BigInt is not supported as an IndexedDB key; support
63 // has been proposed in the following PR, but that change has not
64 // landed at the time this patch was written
65 // https://github.com/w3c/IndexedDB/pull/231
67 function invalidKey(key, name) {
69 assert_throws_dom("DataError", () => indexedDB.cmp(0, key));
70 }, "BigInts as keys in IndexedDB - " + name);
73 invalidKey(1n, "primitive BigInt");
74 // Still an error even if the IndexedDB patch lands
75 invalidKey(Object(1n), "BigInt object");
77 function value(value, _instanceof) {
80 assert_true(value instanceof _instanceof, "TEST ERROR, instanceof");
83 setOnUpgradeNeeded(t, null, _instanceof, value);
84 }, "Values - " + _instanceof.name);
87 value(new Date(), Date);
88 value(new Array(), Array);