From 0ff62b5ee891f08930567e6d2c42c403fb1447b7 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 14 Nov 2022 21:17:24 +0100 Subject: [PATCH] [ecmascript] Added ecmascript_string_item_list. This struct will contain info about current script element. --- src/document/document.cpp | 4 +-- src/document/document.h | 3 +- src/document/html/parser/general.c | 8 +++--- src/document/html/parser/stack.c | 4 +-- src/document/renderer.cpp | 26 ++++++++--------- src/ecmascript/ecmascript.cpp | 46 +++++++++++++++++++++++++++++- src/ecmascript/ecmascript.h | 13 +++++++-- 7 files changed, 79 insertions(+), 25 deletions(-) diff --git a/src/document/document.cpp b/src/document/document.cpp index aa86b449..df8aa57f 100644 --- a/src/document/document.cpp +++ b/src/document/document.cpp @@ -293,7 +293,7 @@ reset_document(struct document *document) free_uri_list(&document->css_imports); #endif #if defined(CONFIG_ECMASCRIPT_SMJS) || defined(CONFIG_QUICKJS) || defined(CONFIG_MUJS) - free_string_list(&document->onload_snippets); + free_ecmascript_string_list(&document->onload_snippets); free_uri_list(&document->ecmascript_imports); mem_free_set(&document->text, NULL); /// kill_timer(&document->timeout); @@ -363,7 +363,7 @@ done_document(struct document *document) free_uri_list(&document->css_imports); #endif #if defined(CONFIG_ECMASCRIPT_SMJS) || defined(CONFIG_QUICKJS) || defined(CONFIG_MUJS) - free_string_list(&document->onload_snippets); + free_ecmascript_string_list(&document->onload_snippets); free_uri_list(&document->ecmascript_imports); { diff --git a/src/document/document.h b/src/document/document.h index c8e72d53..b7994cdb 100644 --- a/src/document/document.h +++ b/src/document/document.h @@ -16,6 +16,7 @@ extern "C" { struct cache_entry; struct document_refresh; +struct ecmascript_string_list_item; struct ecmascript_timeout; struct el_form_control; struct frame_desc; @@ -211,7 +212,7 @@ struct document { * by an external reference - you must wait with processing other items * until it gets resolved and loaded. New items are guaranteed to * always appear at the list end. */ - LIST_OF(struct string_list_item) onload_snippets; + LIST_OF(struct ecmascript_string_list_item) onload_snippets; /** @todo FIXME: We should externally maybe using cache_entry store the * dependencies between the various entries so nothing gets removed * unneeded. */ diff --git a/src/document/html/parser/general.c b/src/document/html/parser/general.c index b2aaa954..26c1fc41 100644 --- a/src/document/html/parser/general.c +++ b/src/document/html/parser/general.c @@ -310,8 +310,8 @@ not_processed: /* Create URL reference onload snippet. */ insert_in_string(&import_url, 0, "^", 1); - add_to_string_list(&html_context->part->document->onload_snippets, - import_url, -1); + add_to_ecmascript_string_list(&html_context->part->document->onload_snippets, + import_url, -1, html_top->name - html_context->part->document->text); imported: /* Retreat. Do not permit nested scripts, tho'. */ @@ -390,8 +390,8 @@ imported: } if (html_context->part->document && *html != '^') { - add_to_string_list(&html_context->part->document->onload_snippets, - html, *end - html); + add_to_ecmascript_string_list(&html_context->part->document->onload_snippets, + html, *end - html, html_top->name - html_context->part->document->text); } #endif } diff --git a/src/document/html/parser/stack.c b/src/document/html/parser/stack.c index 1aa86848..bcbd773b 100644 --- a/src/document/html/parser/stack.c +++ b/src/document/html/parser/stack.c @@ -109,8 +109,8 @@ kill_html_stack_item(struct html_context *html_context, struct html_element *e) && html_context->part->document && onload && *onload && *onload != '^') { /* XXX: The following expression alone amounts two #includes. */ - add_to_string_list(&html_context->part->document->onload_snippets, - onload, -1); + add_to_ecmascript_string_list(&html_context->part->document->onload_snippets, + onload, -1, 0); } mem_free_if(onload); #endif diff --git a/src/document/renderer.cpp b/src/document/renderer.cpp index a0ac6ffc..1dbe9fa6 100644 --- a/src/document/renderer.cpp +++ b/src/document/renderer.cpp @@ -64,10 +64,10 @@ * But I want to take no risk by reworking that now. --pasky */ static void add_snippets(struct ecmascript_interpreter *interpreter, - LIST_OF(struct string_list_item) *doc_snippets, - LIST_OF(struct string_list_item) *queued_snippets) + LIST_OF(struct ecmascript_string_list_item) *doc_snippets, + LIST_OF(struct ecmascript_string_list_item) *queued_snippets) { - struct string_list_item *doc_current = (struct string_list_item *)doc_snippets->next; + struct ecmascript_string_list_item *doc_current = (struct ecmascript_string_list_item *)doc_snippets->next; #ifdef CONFIG_LEDS if (list_empty(*queued_snippets) && interpreter->vs->doc_view->session) @@ -86,10 +86,10 @@ add_snippets(struct ecmascript_interpreter *interpreter, * again. */ #ifdef CONFIG_DEBUG /* Hopefully. */ - struct string_list_item *iterator = queued_snippets->next; + struct ecmascript_string_list_item *iterator = queued_snippets->next; - while (iterator != (struct string_list_item *) queued_snippets) { - if (doc_current == (struct string_list_item *) doc_snippets) { + while (iterator != (struct ecmascript_string_list_item *) queued_snippets) { + if (doc_current == (struct ecmascript_string_list_item *) doc_snippets) { INTERNAL("add_snippets(): doc_snippets shorter than queued_snippets!"); return; } @@ -111,10 +111,10 @@ add_snippets(struct ecmascript_interpreter *interpreter, } assert(doc_current); - for (; doc_current != (struct string_list_item *) doc_snippets; + for (; doc_current != (struct ecmascript_string_list_item *) doc_snippets; doc_current = doc_current->next) { - add_to_string_list(queued_snippets, doc_current->string.source, - doc_current->string.length); + add_to_ecmascript_string_list(queued_snippets, doc_current->string.source, + doc_current->string.length, doc_current->element_offset); #if 0 DBG("Adding snippet\n%.*s\n #####", doc_current->string.length, @@ -125,12 +125,12 @@ add_snippets(struct ecmascript_interpreter *interpreter, static void process_snippets(struct ecmascript_interpreter *interpreter, - LIST_OF(struct string_list_item) *snippets, - struct string_list_item **current) + LIST_OF(struct ecmascript_string_list_item) *snippets, + struct ecmascript_string_list_item **current) { if (!*current) - *current = (struct string_list_item *)snippets->next; - for (; *current != (struct string_list_item *) snippets; + *current = (struct ecmascript_string_list_item *)snippets->next; + for (; *current != (struct ecmascript_string_list_item *) snippets; (*current) = (*current)->next) { struct string *string = &(*current)->string; char *uristring; diff --git a/src/ecmascript/ecmascript.cpp b/src/ecmascript/ecmascript.cpp index 0140121a..928ed687 100644 --- a/src/ecmascript/ecmascript.cpp +++ b/src/ecmascript/ecmascript.cpp @@ -109,6 +109,50 @@ char *local_storage_filename; int local_storage_ready; +struct string * +add_to_ecmascript_string_list(LIST_OF(struct ecmascript_string_list_item) *list, + const char *source, int length, int element_offset) +{ + struct ecmascript_string_list_item *item; + struct string *string; + + assertm(list && source, "[add_to_string_list]"); + if_assert_failed return NULL; + + item = (struct ecmascript_string_list_item *)mem_alloc(sizeof(*item)); + if (!item) return NULL; + + string = &item->string; + if (length < 0) length = strlen(source); + + if (!init_string(string) + || !add_bytes_to_string(string, source, length)) { + done_string(string); + mem_free(item); + return NULL; + } + + item->element_offset = element_offset; + + add_to_list_end(*list, item); + return string; +} + +void +free_ecmascript_string_list(LIST_OF(struct ecmascript_string_list_item) *list) +{ + assertm(list != NULL, "[free_string_list]"); + if_assert_failed return; + + while (!list_empty(*list)) { + struct ecmascript_string_list_item *item = (struct ecmascript_string_list_item *)list->next; + + del_from_list(item); + done_string(&item->string); + mem_free(item); + } +} + static int is_prefix(char *prefix, char *url, int dl) { @@ -276,7 +320,7 @@ ecmascript_put_interpreter(struct ecmascript_interpreter *interpreter) #else spidermonkey_put_interpreter(interpreter); #endif - free_string_list(&interpreter->onload_snippets); + free_ecmascript_string_list(&interpreter->onload_snippets); done_string(&interpreter->code); /* Is it superfluous? */ if (interpreter->vs->doc_view) { diff --git a/src/ecmascript/ecmascript.h b/src/ecmascript/ecmascript.h index c93973fa..dd2862ce 100644 --- a/src/ecmascript/ecmascript.h +++ b/src/ecmascript/ecmascript.h @@ -45,6 +45,11 @@ struct terminal; struct uri; struct view_state; +struct ecmascript_string_list_item { + LIST_HEAD(struct ecmascript_string_list_item); + struct string string; + int element_offset; +}; struct ecmascript_interpreter { struct view_state *vs; @@ -70,8 +75,8 @@ struct ecmascript_interpreter { * any new snippets in document.onload_snippets). Instead, as we * go through the list we maintain a pointer to the last processed * entry. */ - LIST_OF(struct string_list_item) onload_snippets; - struct string_list_item *current_onload_snippet; + LIST_OF(struct ecmascript_string_list_item) onload_snippets; + struct ecmascript_string_list_item *current_onload_snippet; /* ID of the {struct document} where those onload_snippets belong to. * It is kept at 0 until it is definitively hard-attached to a given @@ -190,6 +195,10 @@ void free_document(void *doc); void location_goto(struct document_view *doc_view, char *url); void location_goto_const(struct document_view *doc_view, const char *url); +struct string *add_to_ecmascript_string_list(LIST_OF(struct ecmascript_string_list_item) *list, const char *string, int length, int element_offset); + +void free_ecmascript_string_list(LIST_OF(struct ecmascript_string_list_item) *list); + extern char *console_error_filename; extern char *console_log_filename;