0
0
mirror of https://github.com/rkd77/elinks.git synced 2025-06-30 22:19:29 -04:00

ecmascript: Handle multiple setTimeout simultaneously.

This commit is contained in:
Witold Filipczyk 2007-04-26 20:49:56 +02:00 committed by Witold Filipczyk
parent 2a3be5260a
commit 479f63036a
4 changed files with 63 additions and 20 deletions

View File

@ -54,6 +54,7 @@ init_document(struct cache_entry *cached, struct document_options *options)
#ifdef CONFIG_ECMASCRIPT
init_list(document->onload_snippets);
init_list(document->timeouts);
#endif
object_nolock(document, "document");
@ -104,6 +105,20 @@ done_link_members(struct link *link)
mem_free_if(link->points);
}
#ifdef CONFIG_ECMASCRIPT
void
kill_timeouts(struct document *document)
{
struct timeout_data *td;
foreach (td, document->timeouts) {
kill_timer(&td->timer);
mem_free(td->code);
}
free_list(document->timeouts);
}
#endif
void
done_document(struct document *document)
{
@ -153,7 +168,7 @@ done_document(struct document *document)
#ifdef CONFIG_ECMASCRIPT
free_string_list(&document->onload_snippets);
free_uri_list(&document->ecmascript_imports);
kill_timer(&document->timeout);
kill_timeouts(document);
#endif
free_list(document->tags);
@ -175,7 +190,7 @@ release_document(struct document *document)
if (document->refresh) kill_document_refresh(document->refresh);
#ifdef CONFIG_ECMASCRIPT
kill_timer(&document->timeout);
kill_timeouts(document);
#endif
object_unlock(document);
move_to_top_of_list(format_cache, document);

View File

@ -156,7 +156,7 @@ struct document {
* unneeded. */
struct uri_list ecmascript_imports;
/* used by setTimeout */
timer_id_T timeout;
struct list_head timeouts;
#endif
#ifdef CONFIG_CSS
/* FIXME: We should externally maybe using cache_entry store the
@ -203,6 +203,15 @@ struct document {
unsigned int links_sorted:1; /* whether links are already sorted */
};
#ifdef CONFIG_ECMASCRIPT
struct timeout_data {
LIST_HEAD(struct timeout_data);
struct ecmascript_interpreter *interpreter;
unsigned char *code;
timer_id_T timer;
};
#endif
#define document_has_frames(document_) ((document_) && (document_)->frame_desc)
/* Initializes a document and its canvas. */
@ -233,6 +242,11 @@ int get_format_cache_refresh_count(void);
void shrink_format_cache(int);
#ifdef CONFIG_ECMASCRIPT
void kill_timeouts(struct document *document);
#endif
extern struct module document_module;
/* FIXME: support for entities and all Unicode characters.

View File

@ -131,7 +131,6 @@ ecmascript_get_interpreter(struct view_state *vs)
#else
spidermonkey_get_interpreter(interpreter);
#endif
init_string(&interpreter->code);
return interpreter;
}
@ -145,10 +144,9 @@ ecmascript_put_interpreter(struct ecmascript_interpreter *interpreter)
spidermonkey_put_interpreter(interpreter);
#endif
free_string_list(&interpreter->onload_snippets);
done_string(&interpreter->code);
/* Is it superfluous? */
if (interpreter->vs->doc_view)
kill_timer(&interpreter->vs->doc_view->document->timeout);
kill_timeouts(interpreter->vs->doc_view->document);
interpreter->vs->ecmascript = NULL;
mem_free(interpreter);
}
@ -309,26 +307,42 @@ ecmascript_set_action(unsigned char **action, unsigned char *string)
static void
ecmascript_timeout_handler(void *i)
{
struct ecmascript_interpreter *interpreter = i;
struct string code;
struct timeout_data *td = i;
struct ecmascript_interpreter *interpreter = td->interpreter;
assertm(interpreter->vs->doc_view, "setTimeout: vs with no document (e_f %d)", interpreter->vs->ecmascript_fragile);
interpreter->vs->doc_view->document->timeout = TIMER_ID_UNDEF;
/* The expired timer ID has now been erased. */
ecmascript_eval(interpreter, &interpreter->code, NULL);
del_from_list(td);
if (init_string(&code)) {
add_to_string(&code, td->code);
ecmascript_eval(interpreter, &code, NULL);
done_string(&code);
}
mem_free(td->code);
mem_free(td);
}
void
ecmascript_set_timeout(struct ecmascript_interpreter *interpreter, unsigned char *code, int timeout)
{
struct timeout_data *td;
assert(interpreter && interpreter->vs->doc_view->document);
if (!code) return;
done_string(&interpreter->code);
init_string(&interpreter->code);
add_to_string(&interpreter->code, code);
mem_free(code);
kill_timer(&interpreter->vs->doc_view->document->timeout);
install_timer(&interpreter->vs->doc_view->document->timeout, timeout, ecmascript_timeout_handler, interpreter);
td = mem_calloc(1, sizeof(*td));
if (!td) {
mem_free(code);
return;
}
td->interpreter = interpreter;
td->code = code;
install_timer(&td->timer, timeout, ecmascript_timeout_handler, td);
if (td->timer != TIMER_ID_UNDEF) {
add_to_list_end(interpreter->vs->doc_view->document->timeouts, td);
} else {
mem_free(code);
mem_free(td);
}
}
static struct module *ecmascript_modules[] = {

View File

@ -6,8 +6,11 @@
* plasm displays for everyone. */
#include "main/module.h"
#include "main/timer.h"
#include "util/lists.h"
#include "util/time.h"
struct document;
struct string;
struct terminal;
struct uri;
@ -21,9 +24,6 @@ struct ecmascript_interpreter {
/* Used by document.write() */
struct string *ret;
/* The code evaluated by setTimeout() */
struct string code;
time_t exec_start;
/* This is a cross-rerenderings accumulator of