File: bigint_value.htm

package info (click to toggle)
firefox-esr 78.15.0esr-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,301,296 kB
  • sloc: cpp: 5,665,905; javascript: 4,798,386; ansic: 2,878,233; python: 977,004; asm: 270,347; xml: 181,456; java: 111,756; sh: 72,926; makefile: 21,819; perl: 13,380; cs: 4,725; yacc: 4,565; objc: 3,026; pascal: 1,787; lex: 1,720; ada: 1,681; exp: 505; php: 436; lisp: 260; awk: 152; ruby: 103; csh: 80; sed: 53; sql: 45
file content (72 lines) | stat: -rw-r--r-- 2,447 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<meta charset="utf-8">
<title>IndexedDB: BigInt keys and values</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support.js"></script>

<script>
// BigInt and BigInt objects are supported in serialization, per
// https://github.com/whatwg/html/pull/3480
// This support allows them to be used as IndexedDB values.

function value_test(value, predicate, name) {
    async_test(t => {
        t.step(function() {
            assert_true(predicate(value),
                        "Predicate should return true for the initial value.");
        });

        createdb(t).onupgradeneeded = t.step_func(e => {
            e.target.result
                    .createObjectStore("store")
                    .add(value, 1);

            e.target.onsuccess = t.step_func(e => {
                e.target.result
                        .transaction("store")
                        .objectStore("store")
                        .get(1)
                        .onsuccess = t.step_func(e =>
                {
                    assert_true(predicate(e.target.result),
                                "Predicate should return true for the deserialized result.");
                    t.done();
                });
            });
        });
    }, "BigInts as values in IndexedDB - " + name);
}

value_test(1n,
           x => x === 1n,
           "primitive BigInt");
value_test(Object(1n),
           x => typeof x === 'object' &&
                x instanceof BigInt &&
                x.valueOf() === 1n,
           "BigInt object");
value_test({val: 1n},
           x => x.val === 1n,
           "primitive BigInt inside object");
value_test({val: Object(1n)},
           x => x.val.valueOf() === 1n &&
                x.val instanceof BigInt &&
                x.val.valueOf() === 1n,
           "BigInt object inside object");

// 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
// https://github.com/w3c/IndexedDB/pull/231

function invalidKey(key, name) {
    test(t => {
        assert_throws_dom("DataError", () => indexedDB.cmp(0, key));
    }, "BigInts as keys in IndexedDB - " + name);
}

invalidKey(1n, "primitive BigInt");
// Still an error even if the IndexedDB patch lands
invalidKey(Object(1n), "BigInt object");
</script>