Skip to content

Commit 5b962fe

Browse files
authored
Update library_idbstore.js to use runtimeKeepalivePush/Pop. NFC (#19940)
1 parent 505f49b commit 5b962fe

File tree

3 files changed

+63
-56
lines changed

3 files changed

+63
-56
lines changed

src/library_idbstore.js

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,73 @@
77
#include "IDBStore.js"
88

99
var LibraryIDBStore = {
10-
// A simple IDB-backed storage mechanism. Suitable for saving and loading large files asynchronously. This does
11-
// *NOT* use the emscripten filesystem, intentionally, to avoid overhead. It lets you application define whatever
12-
// filesystem-like layer you want, with the overhead 100% controlled by you. At the extremes, you could either
13-
// just store large files, with almost no extra code; or you could implement a file b-tree using posix-compliant
10+
// A simple IDB-backed storage mechanism. Suitable for saving and loading
11+
// large files asynchronously. This does *NOT* use the emscripten filesystem,
12+
// intentionally, to avoid overhead. It lets you application define whatever
13+
// filesystem-like layer you want, with the overhead 100% controlled by you.
14+
// At the extremes, you could either just store large files, with almost no
15+
// extra code; or you could implement a file b-tree using posix-compliant
1416
// filesystem on top.
1517
$IDBStore: IDBStore,
16-
emscripten_idb_async_load__deps: ['$UTF8ToString', 'malloc', 'free'],
18+
emscripten_idb_async_load__deps: ['$UTF8ToString', '$callUserCallback', 'malloc', 'free'],
1719
emscripten_idb_async_load: function(db, id, arg, onload, onerror) {
18-
IDBStore.getFile(UTF8ToString(db), UTF8ToString(id), function(error, byteArray) {
19-
if (error) {
20-
if (onerror) {{{ makeDynCall('vi', 'onerror') }}}(arg);
21-
return;
22-
}
23-
var buffer = _malloc(byteArray.length);
24-
HEAPU8.set(byteArray, buffer);
25-
{{{ makeDynCall('viii', 'onload') }}}(arg, buffer, byteArray.length);
26-
_free(buffer);
20+
{{{ runtimeKeepalivePush() }}};
21+
IDBStore.getFile(UTF8ToString(db), UTF8ToString(id), (error, byteArray) => {
22+
{{{ runtimeKeepalivePop() }}}
23+
callUserCallback(() => {
24+
if (error) {
25+
if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(arg);
26+
return;
27+
}
28+
var buffer = _malloc(byteArray.length);
29+
HEAPU8.set(byteArray, buffer);
30+
{{{ makeDynCall('vppi', 'onload') }}}(arg, buffer, byteArray.length);
31+
_free(buffer);
32+
});
2733
});
2834
},
35+
emscripten_idb_async_store__deps: ['$UTF8ToString', 'free', '$callUserCallback'],
2936
emscripten_idb_async_store: function(db, id, ptr, num, arg, onstore, onerror) {
30-
// note that we copy the data here, as these are async operatins - changes to HEAPU8 meanwhile should not affect us!
31-
IDBStore.setFile(UTF8ToString(db), UTF8ToString(id), new Uint8Array(HEAPU8.subarray(ptr, ptr+num)), function(error) {
32-
if (error) {
33-
if (onerror) {{{ makeDynCall('vi', 'onerror') }}}(arg);
34-
return;
35-
}
36-
if (onstore) {{{ makeDynCall('vi', 'onstore') }}}(arg);
37+
// note that we copy the data here, as these are async operatins - changes
38+
// to HEAPU8 meanwhile should not affect us!
39+
{{{ runtimeKeepalivePush() }}};
40+
IDBStore.setFile(UTF8ToString(db), UTF8ToString(id), new Uint8Array(HEAPU8.subarray(ptr, ptr+num)), (error) => {
41+
{{{ runtimeKeepalivePop() }}}
42+
callUserCallback(() => {
43+
if (error) {
44+
if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(arg);
45+
return;
46+
}
47+
if (onstore) {{{ makeDynCall('vp', 'onstore') }}}(arg);
48+
});
3749
});
3850
},
51+
emscripten_idb_async_delete__deps: ['$UTF8ToString', '$callUserCallback'],
3952
emscripten_idb_async_delete: function(db, id, arg, ondelete, onerror) {
40-
IDBStore.deleteFile(UTF8ToString(db), UTF8ToString(id), function(error) {
41-
if (error) {
42-
if (onerror) {{{ makeDynCall('vi', 'onerror') }}}(arg);
43-
return;
44-
}
45-
if (ondelete) {{{ makeDynCall('vi', 'ondelete') }}}(arg);
53+
{{{ runtimeKeepalivePush() }}};
54+
IDBStore.deleteFile(UTF8ToString(db), UTF8ToString(id), (error) => {
55+
{{{ runtimeKeepalivePop() }}}
56+
callUserCallback(() => {
57+
if (error) {
58+
if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(arg);
59+
return;
60+
}
61+
if (ondelete) {{{ makeDynCall('vp', 'ondelete') }}}(arg);
62+
});
4663
});
4764
},
65+
emscripten_idb_async_exists__deps: ['$UTF8ToString', '$callUserCallback'],
4866
emscripten_idb_async_exists: function(db, id, arg, oncheck, onerror) {
49-
IDBStore.existsFile(UTF8ToString(db), UTF8ToString(id), function(error, exists) {
50-
if (error) {
51-
if (onerror) {{{ makeDynCall('vi', 'onerror') }}}(arg);
52-
return;
53-
}
54-
if (oncheck) {{{ makeDynCall('vii', 'oncheck') }}}(arg, exists);
67+
{{{ runtimeKeepalivePush() }}};
68+
IDBStore.existsFile(UTF8ToString(db), UTF8ToString(id), (error, exists) => {
69+
{{{ runtimeKeepalivePop() }}}
70+
callUserCallback(() => {
71+
if (error) {
72+
if (onerror) {{{ makeDynCall('vp', 'onerror') }}}(arg);
73+
return;
74+
}
75+
if (oncheck) {{{ makeDynCall('vpi', 'oncheck') }}}(arg, exists);
76+
});
5577
});
5678
},
5779

test/browser/test_idbstore.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,37 @@
2020
long expected;
2121
int result;
2222

23-
void doExit(void* userData) {
24-
emscripten_force_exit(0);
25-
}
26-
27-
void ok(void* arg)
28-
{
23+
void ok(void* arg) {
2924
assert(expected == (long)arg);
30-
emscripten_set_timeout(doExit, 0, 0);
3125
}
3226

33-
void onerror(void* arg)
34-
{
27+
void onerror(void* arg) {
3528
assert(expected == (long)arg);
3629
assert(false);
3730
}
3831

39-
void onload(void* arg, void* ptr, int num)
40-
{
32+
void onload(void* arg, void* ptr, int num) {
4133
assert(expected == (long)arg);
4234
printf("loaded %s\n", (char*)ptr);
4335
assert(num == strlen(SECRET)+1);
4436
assert(strcmp(ptr, SECRET) == 0);
45-
emscripten_set_timeout(doExit, 0, 0);
4637
}
4738

48-
void onbadload(void* arg, void* ptr, int num)
49-
{
39+
void onbadload(void* arg, void* ptr, int num) {
5040
printf("load failed, surprising\n");
5141
assert(false);
5242
}
5343

54-
void oncheck(void* arg, int exists)
55-
{
44+
void oncheck(void* arg, int exists) {
5645
assert(expected == (long)arg);
5746
printf("exists? %d\n", exists);
5847
assert(exists);
59-
emscripten_set_timeout(doExit, 0, 0);
6048
}
6149

62-
void onchecknope(void* arg, int exists)
63-
{
50+
void onchecknope(void* arg, int exists) {
6451
assert(expected == (long)arg);
6552
printf("exists (hopefully not)? %d\n", exists);
6653
assert(!exists);
67-
emscripten_set_timeout(doExit, 0, 0);
6854
}
6955

7056
int main() {
@@ -94,8 +80,5 @@ int main() {
9480
#else
9581
assert(0);
9682
#endif
97-
98-
emscripten_exit_with_live_runtime();
99-
__builtin_trap();
10083
}
10184

test/test_browser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,7 @@ def test_separate_metadata_later(self):
14981498
self.run_process([FILE_PACKAGER, 'more.data', '--preload', 'data.dat', '--separate-metadata', '--js-output=more.js'])
14991499
self.btest(Path('browser/separate_metadata_later.cpp'), '1', args=['-sFORCE_FILESYSTEM'])
15001500

1501+
@also_with_wasm64
15011502
def test_idbstore(self):
15021503
secret = str(time.time())
15031504
for stage in [0, 1, 2, 3, 0, 1, 2, 0, 0, 1, 4, 2, 5]:
@@ -1506,6 +1507,7 @@ def test_idbstore(self):
15061507
args=['-lidbstore.js', f'-DSTAGE={stage}', f'-DSECRET="{secret}"'],
15071508
output_basename=f'idbstore_{stage}')
15081509

1510+
@also_with_wasm64
15091511
def test_idbstore_sync(self):
15101512
secret = str(time.time())
15111513
self.btest(test_file('browser/test_idbstore_sync.c'), '6', args=['-lidbstore.js', f'-DSECRET="{secret}"', '-O3', '-g2', '-sASYNCIFY'])

0 commit comments

Comments
 (0)