File: idbstore.c

package info (click to toggle)
emscripten 3.1.6~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 114,112 kB
  • sloc: ansic: 583,052; cpp: 391,943; javascript: 79,361; python: 54,180; sh: 49,997; pascal: 4,658; makefile: 3,426; asm: 2,191; lisp: 1,869; ruby: 488; cs: 142
file content (101 lines) | stat: -rw-r--r-- 2,413 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright 2015 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

#include <emscripten/emscripten.h>
#include <emscripten/html5.h>

#define DB "THE_DB"

long expected;
int result;

void doExit(void* userData) {
  emscripten_force_exit(0);
}

void ok(void* arg)
{
  assert(expected == (long)arg);
  emscripten_set_timeout(doExit, 0, 0);
}

void onerror(void* arg)
{
  assert(expected == (long)arg);
  assert(false);
}

void onload(void* arg, void* ptr, int num)
{
  assert(expected == (long)arg);
  printf("loaded %s\n", (char*)ptr);
  assert(num == strlen(SECRET)+1);
  assert(strcmp(ptr, SECRET) == 0);
  emscripten_set_timeout(doExit, 0, 0);
}

void onbadload(void* arg, void* ptr, int num)
{
  printf("load failed, surprising\n");
  assert(false);
}

void oncheck(void* arg, int exists)
{
  assert(expected == (long)arg);
  printf("exists? %d\n", exists);
  assert(exists);
  emscripten_set_timeout(doExit, 0, 0);
}

void onchecknope(void* arg, int exists)
{
  assert(expected == (long)arg);
  printf("exists (hopefully not)? %d\n", exists);
  assert(!exists);
  emscripten_set_timeout(doExit, 0, 0);
}

int main() {
  result = STAGE;
#if STAGE == 0
  expected = 12;
  printf("storing %s\n", SECRET);
  emscripten_idb_async_store(DB, "the_secret", SECRET, strlen(SECRET)+1, (void*)expected, ok, onerror);
#elif STAGE == 1
  expected = 31;
  printf("loading the_secret\n");
  emscripten_idb_async_load(DB, "the_secret", (void*)expected, onload, onerror);
#elif STAGE == 2
  expected = 44;
  printf("deleting the_secret\n");
  emscripten_idb_async_delete(DB, "the_secret", (void*)expected, ok, onerror);
#elif STAGE == 3
  expected = 55;
  printf("loading, should fail as we deleted\n");
  emscripten_idb_async_load(DB, "the_secret", (void*)expected, onbadload, ok);
#elif STAGE == 4
  expected = 66;
  emscripten_idb_async_exists(DB, "the_secret", (void*)expected, oncheck, onerror);
#elif STAGE == 5
  expected = 77;
  emscripten_idb_async_exists(DB, "the_secret", (void*)expected, onchecknope, onerror);
#else
  assert(0);
#endif

  emscripten_exit_with_live_runtime();
  __builtin_trap();
}