From 2f4829947b97a88ab49672a4b3df6151b0963b34 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Thu, 10 Oct 2024 17:15:27 +0200 Subject: [PATCH] [quickjs] Check whether interpreter is existing in document event handler --- src/js/quickjs.c | 5 +++ src/js/quickjs.h | 1 + src/js/quickjs/document.c | 4 ++ src/js/quickjs/mapa.c | 78 +++++++++++++++++++++++++++++++++++++++ src/js/quickjs/mapa.h | 8 ++++ 5 files changed, 96 insertions(+) diff --git a/src/js/quickjs.c b/src/js/quickjs.c index ac0c8b565..34036249e 100644 --- a/src/js/quickjs.c +++ b/src/js/quickjs.c @@ -73,6 +73,7 @@ static JSRuntime *quickjs_runtime; +void *map_interp; /*** Global methods */ static void @@ -181,6 +182,7 @@ quickjs_init(struct module *module) #else quickjs_runtime = JS_NewRuntime(); #endif + map_interp = interp_new_map(); } static void @@ -212,6 +214,7 @@ quickjs_done(struct module *xxx) attr_delete_map(map_csses); attr_delete_map_rev(map_rev_csses); + interp_delete_map(map_interp); if (quickjs_runtime) { JS_FreeRuntime(quickjs_runtime); @@ -333,6 +336,7 @@ quickjs_get_interpreter(struct ecmascript_interpreter *interpreter) if (!ctx) { return NULL; } + interp_save_in_map(map_interp, interpreter); interpreter->backend_data = ctx; JS_SetContextOpaque(ctx, interpreter); @@ -392,6 +396,7 @@ fprintf(stderr, "Before JS_FreeContext: %s:%d\n", __FUNCTION__, __LINE__); #endif JS_FreeContext(ctx); interpreter->backend_data = NULL; + interp_erase_from_map(map_interp, interpreter); } static void diff --git a/src/js/quickjs.h b/src/js/quickjs.h index 11d587ed8..749af098a 100644 --- a/src/js/quickjs.h +++ b/src/js/quickjs.h @@ -56,6 +56,7 @@ int quickjs_eval_boolback(struct ecmascript_interpreter *interpreter, struct str void quickjs_call_function(struct ecmascript_interpreter *interpreter, JSValueConst fun, struct string *ret); +extern void *interps; extern struct module quickjs_module; #ifdef __cplusplus diff --git a/src/js/quickjs/document.c b/src/js/quickjs/document.c index b22d8be35..fe6cca658 100644 --- a/src/js/quickjs/document.c +++ b/src/js/quickjs/document.c @@ -2082,6 +2082,10 @@ document_event_handler(dom_event *event, void *pw) #endif struct js_document_private *doc_private = (struct js_document_private *)pw; struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)doc_private->interpreter; + + if (!interp_find_in_map(map_interp, interpreter)) { + return; + } JSContext *ctx = (JSContext *)interpreter->backend_data; if (!event) { diff --git a/src/js/quickjs/mapa.c b/src/js/quickjs/mapa.c index 13664b2ab..f5bf15b9e 100644 --- a/src/js/quickjs/mapa.c +++ b/src/js/quickjs/mapa.c @@ -156,6 +156,84 @@ attr_create_new_csses_map_rev(void) return (void *)init_hash8(); } +void * +interp_new_map(void) +{ + return (void *)init_hash8(); +} + +bool +interp_find_in_map(void *m, void *interpreter) +{ + struct hash *hash = (struct hash *)m; + + if (hash) { + char *key = memacpy((const char *)&interpreter, sizeof(interpreter)); + + if (key) { + struct hash_item *item = get_hash_item(hash, key, sizeof(interpreter)); + + mem_free(key); + + if (item) { + return true; + } + } + } + + return false; +} + +void +interp_save_in_map(void *m, void *interpreter) +{ + struct hash *hash = (struct hash *)m; + void *value = (void *)1; + + char *key = memacpy((const char *)&interpreter, sizeof(interpreter)); + + if (key) { + add_hash_item(hash, key, sizeof(value), value); + } +} + +void +interp_erase_from_map(void *m, void *interpreter) +{ + struct hash *hash = (struct hash *)m; + + if (hash) { + char *key = memacpy((const char *)&interpreter, sizeof(interpreter)); + + if (key) { + struct hash_item *item = get_hash_item(hash, key, sizeof(interpreter)); + + if (item) { + mem_free_set(&item->key, NULL); + //mem_free_set(&item->value, NULL); + del_hash_item(hash, item); + } + mem_free(key); + } + } +} + +void +interp_delete_map(void *m) +{ + struct hash *hash = (struct hash *)m; + + if (hash) { + struct hash_item *item; + int i; + + foreach_hash_item (item, *hash, i) { + mem_free_set(&item->key, NULL); + //mem_free_set(&item->value, NULL); + } + free_hash(&hash); + } +} #if 0 struct classcomp { diff --git a/src/js/quickjs/mapa.h b/src/js/quickjs/mapa.h index 8bc952a53..e8d3780d3 100644 --- a/src/js/quickjs/mapa.h +++ b/src/js/quickjs/mapa.h @@ -30,9 +30,17 @@ extern void *map_form_elements_rev; extern void *map_csses; extern void *map_rev_csses; +extern void *map_interp; + void attr_save_in_map(void *m, void *node, JSValueConst value); void attr_save_in_map_void(void *m, void *node, void *value); +void *interp_new_map(void); +bool interp_find_in_map(void *m, void *interpreter); +void interp_save_in_map(void *m, void *interpreter); +void interp_erase_from_map(void *m, void *interpreter); +void interp_delete_map(void *m); + void *attr_create_new_attrs_map(void); void *attr_create_new_attributes_map(void); void *attr_create_new_attributes_map_rev(void);