1 // Regression: web/idb's key-value stores work end to end in a browser.
2 //
3 // The stdlib package declares 17 idb_* bridge imports and the host provided 14
4 // until this round: a module linking web/idb could not instantiate at all,
5 // because a wasm module imports every symbol of a package it links. Grepping the
6 // two lists against each other catches that class of bug, but only running it
7 // proves the entries are really wired: this opens the database, stores a value,
8 // reads it back through the document-less worker context the module runs in, and
9 // walks the store with get-all.
10 //
11 // The assertions run in callbacks, so the module is passive after main returns
12 // and a callback that never arrives leaves the expected output short - which the
13 // gate compares against tests/wasm/expected/web_idb_kv.txt.
14 //:wasmtest browser-only
15 package main
16 17 import (
18 "web/idb"
19 )
20 21 func assert(ok bool, what string) {
22 if !ok {
23 panic("idb round trip: " | what)
24 }
25 println("ok:", what)
26 }
27 28 func main() {
29 // Termination: a batch program must still be stoppable, so the entry
30 // handles the channel the runtime closes on SIGINT/SIGTERM.
31 select {
32 case <-shutdown:
33 return
34 default:
35 }
36 37 idb.Open(onOpen)
38 }
39 40 func onOpen() {
41 println("ok: idb opened")
42 idb.KVPut("settings", "greeting", "hello kv")
43 idb.KVGet("settings", "greeting", onGet)
44 }
45 46 func onGet(v string) {
47 assert(v == "hello kv", "kv get returns what kv put stored")
48 idb.KVPut("settings", "second", "two")
49 idb.KVGetAll("settings", onPair, onListed)
50 }
51 52 // The each callback prints every pair the store walks; if it never runs, the
53 // expected output is missing those lines and the gate says so.
54 func onPair(k, v string) {
55 println("ok: kv pair", k, "=", v)
56 }
57 58 func onListed() {
59 idb.KVGet("settings", "second", onSecond)
60 }
61 62 func onSecond(v string) {
63 assert(v == "two", "second key survived the walk")
64 println("PASS: stdlib idb kv round trip")
65 }
66