2005-09-15 09:58:31 -04:00
|
|
|
#ifndef EL__ECMASCRIPT_ECMASCRIPT_H
|
|
|
|
#define EL__ECMASCRIPT_ECMASCRIPT_H
|
|
|
|
|
|
|
|
/* This is a trivial ECMAScript driver. All your base are belong to pasky. */
|
|
|
|
/* In the future you will get DOM, a complete ECMAScript interface and free
|
|
|
|
* plasm displays for everyone. */
|
|
|
|
|
2022-12-31 16:06:57 -05:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2020-11-23 14:11:47 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2021-10-17 12:17:48 -04:00
|
|
|
#ifdef CONFIG_ECMASCRIPT_SMJS
|
2020-11-22 10:42:23 -05:00
|
|
|
#include <jsapi.h>
|
2021-10-17 12:17:48 -04:00
|
|
|
#endif
|
|
|
|
|
2021-10-29 16:06:39 -04:00
|
|
|
#ifdef CONFIG_QUICKJS
|
|
|
|
#include <quickjs/quickjs.h>
|
|
|
|
#endif
|
|
|
|
|
2022-08-21 15:33:13 -04:00
|
|
|
#ifdef CONFIG_MUJS
|
|
|
|
#include <mujs.h>
|
|
|
|
#endif
|
|
|
|
|
2021-10-17 12:17:48 -04:00
|
|
|
#ifdef CONFIG_ECMASCRIPT
|
2020-11-22 10:42:23 -05:00
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "main/module.h"
|
2021-12-01 13:27:50 -05:00
|
|
|
#include "main/timer.h"
|
2023-01-01 03:04:20 -05:00
|
|
|
#include "util/string.h"
|
2005-09-15 09:58:31 -04:00
|
|
|
#include "util/time.h"
|
|
|
|
|
2021-08-19 12:56:50 -04:00
|
|
|
//#define ECMASCRIPT_DEBUG 1
|
|
|
|
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2022-10-13 09:53:54 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2023-03-15 13:23:28 -04:00
|
|
|
struct document;
|
2021-10-22 04:44:37 -04:00
|
|
|
struct document_view;
|
2008-07-18 12:16:34 -04:00
|
|
|
struct form_state;
|
2008-07-18 12:43:30 -04:00
|
|
|
struct form_view;
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string;
|
2006-01-10 13:17:29 -05:00
|
|
|
struct terminal;
|
2005-09-15 09:58:31 -04:00
|
|
|
struct uri;
|
|
|
|
struct view_state;
|
|
|
|
|
|
|
|
struct ecmascript_interpreter {
|
|
|
|
struct view_state *vs;
|
|
|
|
void *backend_data;
|
2007-06-21 14:34:36 -04:00
|
|
|
|
|
|
|
/* Nesting level of calls to backend functions. When this is
|
|
|
|
* nonzero, there are references to backend_data in the C
|
|
|
|
* stack, so it is not safe to free the data yet. */
|
|
|
|
int backend_nesting;
|
|
|
|
|
2006-01-27 06:07:45 -05:00
|
|
|
/* Used by document.write() */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *ret;
|
2006-10-23 16:45:55 -04:00
|
|
|
|
|
|
|
/* The code evaluated by setTimeout() */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string code;
|
2007-04-26 09:02:04 -04:00
|
|
|
|
2022-11-19 13:24:15 -05:00
|
|
|
/* document.write buffer */
|
2022-12-28 15:05:59 -05:00
|
|
|
LIST_OF(struct ecmascript_string_list_item) writecode;
|
|
|
|
struct ecmascript_string_list_item *current_writecode;
|
2022-11-19 13:24:15 -05:00
|
|
|
|
2009-07-18 19:41:01 -04:00
|
|
|
struct heartbeat *heartbeat;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* This is a cross-rerenderings accumulator of
|
|
|
|
* @document.onload_snippets (see its description for juicy details).
|
|
|
|
* They enter this list as they continue to appear there, and they
|
|
|
|
* never leave it (so that we can always find from where to look for
|
|
|
|
* any new snippets in document.onload_snippets). Instead, as we
|
|
|
|
* go through the list we maintain a pointer to the last processed
|
|
|
|
* entry. */
|
2022-11-14 15:17:24 -05:00
|
|
|
LIST_OF(struct ecmascript_string_list_item) onload_snippets;
|
|
|
|
struct ecmascript_string_list_item *current_onload_snippet;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
* final document. Then if we suddenly appear with this structure upon
|
|
|
|
* a document with a different ID, we reset the state and start with a
|
|
|
|
* fresh one (normally, that does not happen since reloading sets
|
|
|
|
* ecmascript_fragile, but it can happen i.e. when the urrent document
|
|
|
|
* is reloaded in another tab and then you just cause the current tab
|
|
|
|
* to redraw. */
|
2008-08-03 14:27:56 -04:00
|
|
|
unsigned int onload_snippets_cache_id;
|
2023-01-19 12:52:18 -05:00
|
|
|
#ifdef CONFIG_ECMASCRIPT_SMJS
|
2023-05-05 07:23:35 -04:00
|
|
|
JSAutoRealm *ar;
|
2023-01-19 12:52:18 -05:00
|
|
|
JS::Heap<JSObject*> *ac;
|
|
|
|
#endif
|
2021-10-29 16:06:39 -04:00
|
|
|
#ifdef CONFIG_QUICKJS
|
2023-01-21 09:38:53 -05:00
|
|
|
JSRuntime *rt;
|
2021-10-29 16:06:39 -04:00
|
|
|
JSValue document_obj;
|
|
|
|
JSValue location_obj;
|
|
|
|
#else
|
2021-05-19 13:05:03 -04:00
|
|
|
void *document_obj;
|
2021-08-03 12:47:34 -04:00
|
|
|
void *location_obj;
|
2021-10-29 16:06:39 -04:00
|
|
|
#endif
|
2021-11-12 15:53:31 -05:00
|
|
|
#ifdef CONFIG_QUICKJS
|
|
|
|
JSValueConst fun;
|
2022-08-20 14:52:26 -04:00
|
|
|
#endif
|
2022-06-16 11:33:06 -04:00
|
|
|
#ifdef CONFIG_ECMASCRIPT_SMJS
|
2020-11-22 10:42:23 -05:00
|
|
|
JS::RootedValue fun;
|
2022-06-16 11:33:06 -04:00
|
|
|
#endif
|
2022-08-20 14:52:26 -04:00
|
|
|
#ifdef CONFIG_MUJS
|
|
|
|
const char *fun;
|
2021-10-17 12:17:48 -04:00
|
|
|
#endif
|
2022-11-14 15:33:24 -05:00
|
|
|
int element_offset;
|
2023-09-25 04:52:19 -04:00
|
|
|
unsigned int changed:1;
|
|
|
|
unsigned int was_write:1;
|
2005-09-15 09:58:31 -04:00
|
|
|
};
|
|
|
|
|
2022-09-07 14:41:46 -04:00
|
|
|
struct ecmascript_timeout {
|
2023-06-19 12:43:53 -04:00
|
|
|
LIST_HEAD_EL(struct ecmascript_timeout);
|
2022-09-07 14:41:46 -04:00
|
|
|
struct string code;
|
|
|
|
#ifdef CONFIG_QUICKJS
|
2024-01-20 09:56:23 -05:00
|
|
|
JSContext *ctx;
|
2022-09-07 14:41:46 -04:00
|
|
|
JSValueConst fun;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_ECMASCRIPT_SMJS
|
2024-01-20 09:56:23 -05:00
|
|
|
JSContext *ctx;
|
2022-09-07 14:41:46 -04:00
|
|
|
JS::RootedValue fun;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_MUJS
|
|
|
|
js_State *ctx;
|
|
|
|
const char *fun;
|
|
|
|
#endif
|
|
|
|
struct ecmascript_interpreter *interpreter;
|
|
|
|
timer_id_T tid;
|
2024-01-19 15:36:28 -05:00
|
|
|
int timeout_next;
|
2022-09-07 14:41:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-10-22 04:44:37 -04:00
|
|
|
struct delayed_goto {
|
|
|
|
/* It might look more convenient to pass doc_view around but it could
|
|
|
|
* disappear during wild dances inside of frames or so. */
|
|
|
|
struct view_state *vs;
|
|
|
|
struct uri *uri;
|
|
|
|
};
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
/* Why is the interpreter bound to {struct view_state} instead of {struct
|
|
|
|
* document}? That's easy, because the script won't raid just inside of the
|
|
|
|
* document, but it will also want to generate pop-up boxes, adjust form
|
|
|
|
* contents (which is doc_view-specific) etc. Of course the cons are that we
|
|
|
|
* need to wait with any javascript code execution until we get bound to the
|
|
|
|
* view_state through document_view - that means we are going to re-render the
|
|
|
|
* document if it contains a <script> area full of document.write()s. And why
|
|
|
|
* not bound the interpreter to {struct document_view} then? Because it is
|
|
|
|
* reset for each rerendering, and it sucks to do all the magic to preserve the
|
|
|
|
* interpreter over the rerenderings (we tried). */
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
int ecmascript_check_url(char *url, char *frame);
|
2006-04-02 11:00:55 -04:00
|
|
|
void ecmascript_free_urls(struct module *module);
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
struct ecmascript_interpreter *ecmascript_get_interpreter(struct view_state*vs);
|
|
|
|
|
|
|
|
void ecmascript_reset_state(struct view_state *vs);
|
|
|
|
|
2022-11-14 15:33:24 -05:00
|
|
|
void ecmascript_eval(struct ecmascript_interpreter *interpreter, struct string *code, struct string *ret, int element_offset);
|
2021-01-02 10:20:27 -05:00
|
|
|
char *ecmascript_eval_stringback(struct ecmascript_interpreter *interpreter, struct string *code);
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2006-01-10 13:17:29 -05:00
|
|
|
void ecmascript_timeout_dialog(struct terminal *term, int max_exec_time);
|
|
|
|
|
2021-01-02 10:20:27 -05:00
|
|
|
void ecmascript_set_action(char **action, char *string);
|
2006-01-10 13:17:29 -05:00
|
|
|
|
2024-01-20 09:56:23 -05:00
|
|
|
struct ecmascript_timeout *ecmascript_set_timeout(void *ctx, char *code, int timeout, int timeout_next);
|
2006-10-23 16:45:55 -04:00
|
|
|
|
2021-10-17 12:17:48 -04:00
|
|
|
#ifdef CONFIG_ECMASCRIPT_SMJS
|
2024-01-20 09:56:23 -05:00
|
|
|
struct ecmascript_timeout *ecmascript_set_timeout2(void *ctx, JS::HandleValue f, int timeout, int timeout_next);
|
2021-10-17 12:17:48 -04:00
|
|
|
#endif
|
2020-11-22 10:42:23 -05:00
|
|
|
|
2021-10-31 15:48:10 -04:00
|
|
|
#ifdef CONFIG_QUICKJS
|
2024-01-20 09:56:23 -05:00
|
|
|
struct ecmascript_timeout *ecmascript_set_timeout2q(void *ctx, JSValue f, int timeout, int timeout_next);
|
2021-10-31 15:48:10 -04:00
|
|
|
#endif
|
|
|
|
|
2022-08-20 14:52:26 -04:00
|
|
|
#ifdef CONFIG_MUJS
|
2024-01-19 15:36:28 -05:00
|
|
|
struct ecmascript_timeout *ecmascript_set_timeout2m(js_State *J, const char *handle, int timeout, int timeout_next);
|
2022-08-20 14:52:26 -04:00
|
|
|
#endif
|
|
|
|
|
2020-12-07 16:36:24 -05:00
|
|
|
int get_ecmascript_enable(struct ecmascript_interpreter *interpreter);
|
|
|
|
|
2021-07-20 04:05:58 -04:00
|
|
|
void check_for_rerender(struct ecmascript_interpreter *interpreter, const char* text);
|
|
|
|
|
2021-09-20 12:40:01 -04:00
|
|
|
void toggle_ecmascript(struct session *ses);
|
2021-09-19 13:40:01 -04:00
|
|
|
|
2021-10-22 04:44:37 -04:00
|
|
|
void location_goto(struct document_view *doc_view, char *url);
|
2022-01-14 13:48:40 -05:00
|
|
|
void location_goto_const(struct document_view *doc_view, const char *url);
|
2021-10-17 12:17:48 -04:00
|
|
|
|
2021-09-07 11:31:45 -04:00
|
|
|
extern char *console_error_filename;
|
2021-02-22 09:55:12 -05:00
|
|
|
extern char *console_log_filename;
|
2021-02-22 17:27:19 -05:00
|
|
|
|
|
|
|
extern char *local_storage_filename;
|
|
|
|
extern int local_storage_ready;
|
|
|
|
|
2022-10-13 09:53:54 -04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
#endif
|
2020-11-23 14:11:47 -05:00
|
|
|
|
|
|
|
#endif
|