|
| 1 | +<!DOCTYPE html> |
| 2 | +<meta charset="utf-8"> |
| 3 | +<title>IndexedDB: BigInt keys and 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 objects 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 | +function value_test(value, predicate, name) { |
| 14 | + async_test(t => { |
| 15 | + t.step(function() { |
| 16 | + assert_true(predicate(value), |
| 17 | + "Predicate should return true for the initial value."); |
| 18 | + }); |
| 19 | + |
| 20 | + createdb(t).onupgradeneeded = t.step_func(e => { |
| 21 | + e.target.result |
| 22 | + .createObjectStore("store") |
| 23 | + .add(value, 1); |
| 24 | + |
| 25 | + e.target.onsuccess = t.step_func(e => { |
| 26 | + e.target.result |
| 27 | + .transaction("store") |
| 28 | + .objectStore("store") |
| 29 | + .get(1) |
| 30 | + .onsuccess = t.step_func(e => |
| 31 | + { |
| 32 | + assert_true(predicate(e.target.result), |
| 33 | + "Predicate should return true for the deserialized result."); |
| 34 | + t.done(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + }); |
| 38 | + }, "BigInts as values in IndexedDB - " + name); |
| 39 | +} |
| 40 | + |
| 41 | +value_test(1n, |
| 42 | + x => x === 1n, |
| 43 | + "primitive BigInt"); |
| 44 | +value_test(Object(1n), |
| 45 | + x => typeof x === 'object' && |
| 46 | + x instanceof BigInt && |
| 47 | + x.valueOf() === 1n, |
| 48 | + "BigInt object"); |
| 49 | +value_test({val: 1n}, |
| 50 | + x => x.val === 1n, |
| 51 | + "primitive BigInt inside object"); |
| 52 | +value_test({val: Object(1n)}, |
| 53 | + x => x.val.valueOf() === 1n && |
| 54 | + x.val instanceof BigInt && |
| 55 | + x.val.valueOf() === 1n, |
| 56 | + "BigInt object inside object"); |
| 57 | + |
| 58 | +// However, BigInt is not supported as an IndexedDB key; support |
| 59 | +// has been proposed in the following PR, but that change has not |
| 60 | +// landed at the time this patch was written |
| 61 | +// https://github.com/w3c/IndexedDB/pull/231 |
| 62 | + |
| 63 | +function invalidKey(key, name) { |
| 64 | + test(t => { |
| 65 | + assert_throws("DataError", () => indexedDB.cmp(0, key)); |
| 66 | + }, "BigInts as keys in IndexedDB - " + name); |
| 67 | +} |
| 68 | + |
| 69 | +invalidKey(1n, "primitive BigInt"); |
| 70 | +// Still an error even if the IndexedDB patch lands |
| 71 | +invalidKey(Object(1n), "BigInt object"); |
| 72 | +</script> |
0 commit comments