Skip to content

Commit 25f545f

Browse files
committed
[persistence] Fix of AV when storage was not closed explicitly.
1 parent d1ea93e commit 25f545f

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

quickjs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54779,7 +54779,7 @@ JS_BOOL js_set_persistent_rt(JSRuntime* rt, JSValue val, struct JSStorage* pst,
5477954779
js_free_rt(rt,po->persistent);
5478054780
po->persistent = NULL;
5478154781
}
54782-
return 1;
54782+
return 0;
5478354783
}
5478454784

5478554785
if (!po->persistent) {

storage/dybase/include/dybase.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef void * dybase_handle_t;
5858
typedef void * dybase_iterator_t;
5959
typedef unsigned dybase_oid_t;
6060

61-
typedef void * hastable_t;
61+
typedef void * hashtable_t;
6262

6363
typedef void (*dybase_error_handler_t)(int error_code, char const *msg);
6464

@@ -415,16 +415,16 @@ void DYBASE_DLL_ENTRY dybase_set_gc_threshold(dybase_storage_t storage,
415415
void DYBASE_DLL_ENTRY dybase_gc(dybase_storage_t storage);
416416

417417

418-
hastable_t DYBASE_DLL_ENTRY hashtable_create();
419-
void DYBASE_DLL_ENTRY hashtable_put(hastable_t ht, void *key, int keySize, void *value);
420-
void* DYBASE_DLL_ENTRY hashtable_get(hastable_t ht, void *key, int keySize);
421-
void* DYBASE_DLL_ENTRY hashtable_remove(hastable_t ht, void *key, int keySize);
422-
void DYBASE_DLL_ENTRY hashtable_clear(hastable_t ht);
418+
hashtable_t DYBASE_DLL_ENTRY hashtable_create();
419+
void DYBASE_DLL_ENTRY hashtable_put(hashtable_t ht, void *key, int keySize, void *value);
420+
void* DYBASE_DLL_ENTRY hashtable_get(hashtable_t ht, void *key, int keySize);
421+
void* DYBASE_DLL_ENTRY hashtable_remove(hashtable_t ht, void *key, int keySize);
422+
void DYBASE_DLL_ENTRY hashtable_clear(hashtable_t ht);
423423

424424
typedef int each_cb_t(void* key, unsigned int key_length, void* data, void* opaque);
425425

426-
void DYBASE_DLL_ENTRY hashtable_each(hastable_t ht, each_cb_t* pcb, void* opaque);
427-
void DYBASE_DLL_ENTRY hashtable_free(hastable_t ht);
426+
void DYBASE_DLL_ENTRY hashtable_each(hashtable_t ht, each_cb_t* pcb, void* opaque);
427+
void DYBASE_DLL_ENTRY hashtable_free(hashtable_t ht);
428428

429429

430430
#ifdef __cplusplus

storage/dybase/src/dybase.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,38 +264,38 @@ void dybase_set_gc_threshold(dybase_storage_t storage, long allocated_delta) {
264264
void dybase_gc(dybase_storage_t storage) { ((dbDatabase *)storage)->gc(); }
265265

266266

267-
hastable_t hashtable_create() {
267+
hashtable_t hashtable_create() {
268268
return new dbHashtable();
269269
}
270-
void hashtable_put(hastable_t ht, void *key, int keySize, void *value)
270+
void hashtable_put(hashtable_t ht, void *key, int keySize, void *value)
271271
{
272272
dbHashtable* pht = (dbHashtable*)ht;
273273
pht->put(key, keySize, value);
274274
}
275-
void* hashtable_get(hastable_t ht, void *key, int keySize)
275+
void* hashtable_get(hashtable_t ht, void *key, int keySize)
276276
{
277277
dbHashtable* pht = (dbHashtable*)ht;
278278
return pht->get(key, keySize);
279279
}
280-
void hashtable_free(hastable_t ht)
280+
void hashtable_free(hashtable_t ht)
281281
{
282282
dbHashtable* pht = (dbHashtable*)ht;
283283
delete pht;
284284
}
285285

286-
void* hashtable_remove(hastable_t ht, void *key, int keySize)
286+
void* hashtable_remove(hashtable_t ht, void *key, int keySize)
287287
{
288288
dbHashtable* pht = (dbHashtable*)ht;
289289
return pht->remove(key, keySize);
290290
}
291-
void hashtable_clear(hastable_t ht) {
291+
void hashtable_clear(hashtable_t ht) {
292292
dbHashtable* pht = (dbHashtable*)ht;
293293
pht->clear();
294294
}
295295

296296
typedef int each_cb_t(void* key, unsigned int key_length, void* data, void* opaque);
297297

298-
void hashtable_each(hastable_t ht, each_cb_t* pcb, void* opaque)
298+
void hashtable_each(hashtable_t ht, each_cb_t* pcb, void* opaque)
299299
{
300300
dbHashtable* pht = (dbHashtable*)ht;
301301
pht->each(pcb, opaque);

storage/quickjs-storage.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ JSValue JS_GetLocalValue(JSContext *ctx, JSAtom name);
4444
typedef struct JSStorage {
4545
dybase_storage_t hs;
4646
JSContext* ctx;
47-
hastable_t oid2obj;
47+
hashtable_t oid2obj;
4848
JSValue classname2proto;
4949
JSValue root;
5050
} JSStorage;
@@ -405,14 +405,16 @@ int db_fetch_array_data(JSContext *ctx, JSValue obj, JSStorage* pst, dybase_oid_
405405
JSValue db_fetch_object(JSContext *ctx, JSStorage* pst, dybase_oid_t oid)
406406
{
407407
JSValue rv = JS_NewObject(ctx);
408-
js_set_persistent(ctx, rv, pst, oid, JS_PERSISTENT_DORMANT);
408+
if(js_set_persistent(ctx, rv, pst, oid, JS_PERSISTENT_DORMANT))
409+
hashtable_put(pst->oid2obj, &oid, sizeof(oid), JS_VALUE_GET_PTR(rv));
409410
return rv;
410411
}
411412

412413
JSValue db_fetch_array(JSContext *ctx, JSStorage* pst, dybase_oid_t oid)
413414
{
414415
JSValue rv = JS_NewArray(ctx);
415-
js_set_persistent(ctx, rv, pst, oid, JS_PERSISTENT_DORMANT);
416+
if(js_set_persistent(ctx, rv, pst, oid, JS_PERSISTENT_DORMANT))
417+
hashtable_put(pst->oid2obj, &oid, sizeof(oid), JS_VALUE_GET_PTR(rv));
416418
return rv;
417419
}
418420

@@ -645,6 +647,7 @@ typedef struct commit_ctx {
645647
JSContext *ctx;
646648
JSStorage *pst;
647649
int forget;
650+
int count;
648651
} commit_ctx;
649652

650653
static int commit_value(void* key, unsigned int key_length, void* data, void* opaque) {
@@ -661,6 +664,7 @@ static int final_commit_value(void* key, unsigned int key_length, void* data, vo
661664
dybase_oid_t *poid = (dybase_oid_t *)key;
662665
db_store_entity(cc->ctx, cc->pst, *poid, obj);
663666
js_set_persistent(cc->ctx, obj, NULL, 0, JS_NOT_PERSISTENT);
667+
++cc->count;
664668
return 0;
665669
}
666670

@@ -670,20 +674,27 @@ static void commit_storage(JSContext *ctx, JSStorage* pst) {
670674
}
671675

672676
static void final_commit_storage(JSContext *ctx, JSStorage* pst) {
673-
commit_ctx cc = { ctx,pst,1 };
674-
hashtable_each(pst->oid2obj, &final_commit_value, &cc);
677+
commit_ctx cc = { ctx,pst,1, 0 };
678+
do {
679+
cc.count = 0;
680+
hashtable_t ht = pst->oid2obj;
681+
pst->oid2obj = hashtable_create();
682+
hashtable_each(ht, &final_commit_value, &cc);
683+
hashtable_free(ht);
684+
} while (cc.count);
675685
}
676686

677687
void free_storage(JSValue st)
678688
{
679689
JSStorage* ps = storage_of(st);
680690
if (!ps) return;
681691
JSContext *ctx = ps->ctx;
692+
js_set_persistent(ctx, ps->root, NULL, 0, JS_NOT_PERSISTENT);
693+
JS_FreeValue(ctx, ps->root);
694+
JS_FreeValue(ctx, ps->classname2proto);
682695
final_commit_storage(ctx, ps);
683696
dybase_commit(ps->hs);
684697
dybase_close(ps->hs);
685-
JS_FreeValue(ctx, ps->root);
686-
JS_FreeValue(ctx, ps->classname2proto);
687698
ps->hs = 0;
688699
hashtable_free(ps->oid2obj);
689700
JS_FreeContext(ctx);
@@ -757,7 +768,7 @@ static void js_storage_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_f
757768
JSStorage *pst = get_storage(val);
758769
if (pst) {
759770
JS_MarkValue(rt, pst->root, mark_func);
760-
//JS_MarkValue(rt, s->oid2obj, mark_func);
771+
//JS_MarkValue(rt, pst->oid2obj, mark_func);
761772
JS_MarkValue(rt, pst->classname2proto, mark_func);
762773
}
763774
}

0 commit comments

Comments
 (0)