From 5f18359a6fc8c5aad91e087cd27359f8beadca6c Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Sun, 7 Aug 2022 16:44:09 +0200 Subject: [PATCH] [mujs] history --- src/ecmascript/mujs.cpp | 2 + src/ecmascript/mujs/history.cpp | 144 ++++++++++++++++++++++++++++++++ src/ecmascript/mujs/history.h | 10 +++ src/ecmascript/mujs/meson.build | 2 +- 4 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/ecmascript/mujs/history.cpp create mode 100644 src/ecmascript/mujs/history.h diff --git a/src/ecmascript/mujs.cpp b/src/ecmascript/mujs.cpp index 72f084a2..1a663397 100644 --- a/src/ecmascript/mujs.cpp +++ b/src/ecmascript/mujs.cpp @@ -25,6 +25,7 @@ #include "document/view.h" #include "ecmascript/ecmascript.h" #include "ecmascript/mujs.h" +#include "ecmascript/mujs/history.h" #include "ecmascript/mujs/navigator.h" #include "ecmascript/mujs/screen.h" #include "ecmascript/mujs/unibar.h" @@ -82,6 +83,7 @@ mujs_get_interpreter(struct ecmascript_interpreter *interpreter) mjs_screen_init(interpreter, J); mjs_unibar_init(interpreter, J); mjs_navigator_init(interpreter, J); + mjs_history_init(interpreter, J); return J; #if 0 diff --git a/src/ecmascript/mujs/history.cpp b/src/ecmascript/mujs/history.cpp new file mode 100644 index 00000000..e5dc0021 --- /dev/null +++ b/src/ecmascript/mujs/history.cpp @@ -0,0 +1,144 @@ +/* The QuickJS history object implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "elinks.h" + +#include "bfu/dialog.h" +#include "cache/cache.h" +#include "cookies/cookies.h" +#include "dialogs/menu.h" +#include "dialogs/status.h" +#include "document/html/frames.h" +#include "document/document.h" +#include "document/forms.h" +#include "document/view.h" +#include "ecmascript/ecmascript.h" +#include "ecmascript/mujs.h" +#include "ecmascript/mujs/history.h" +#include "ecmascript/mujs/window.h" +#include "intl/libintl.h" +#include "main/select.h" +#include "osdep/newwin.h" +#include "osdep/sysname.h" +#include "protocol/http/http.h" +#include "protocol/uri.h" +#include "session/history.h" +#include "session/location.h" +#include "session/session.h" +#include "session/task.h" +#include "terminal/tab.h" +#include "terminal/terminal.h" +#include "util/conv.h" +#include "util/memory.h" +#include "util/string.h" +#include "viewer/text/draw.h" +#include "viewer/text/form.h" +#include "viewer/text/link.h" +#include "viewer/text/vs.h" + +static void +mjs_history_back(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_touserdata(J, 0, "history"); + + assert(interpreter); + struct document_view *doc_view = interpreter->vs->doc_view; + struct session *ses = doc_view->session; + + go_back(ses); + +/* history_back() must return 0 for onClick to cause displaying previous page + * and return non zero for to prevent + * "calculating" new link. Returned value 2 is changed to 0 in function + * spidermonkey_eval_boolback */ + js_pushnull(J); +} + +static void +mjs_history_forward(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_touserdata(J, 0, "history"); + + assert(interpreter); + struct document_view *doc_view = interpreter->vs->doc_view; + struct session *ses = doc_view->session; + + go_unback(ses); + + js_pushnull(J); +} + +static void +mjs_history_go(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_touserdata(J, 0, "history"); + + assert(interpreter); + struct document_view *doc_view = interpreter->vs->doc_view; + struct session *ses = doc_view->session; + struct location *loc; + + int index = js_tointeger(J, 1); + + for (loc = cur_loc(ses); + loc != (struct location *) &ses->history.history; + loc = index > 0 ? loc->next : loc->prev) { + if (!index) { + go_history(ses, loc); + break; + } + + index += index > 0 ? -1 : 1; + } + + js_pushnull(J); +} + +static void +mjs_history_toString(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + js_pushstring(J, "[history object]"); +} + +int +mjs_history_init(struct ecmascript_interpreter *interpreter, js_State *J) +{ + js_getglobal(J, "Object"); + js_getproperty(J, -1, "prototype"); + js_newuserdata(J, "history", interpreter, NULL); + + js_newcfunction(J, mjs_history_back, "history.prototype.back", 0); + js_defproperty(J, -2, "back", JS_DONTENUM); + + js_newcfunction(J, mjs_history_forward, "history.prototype.forward", 0); + js_defproperty(J, -2, "forward", JS_DONTENUM); + + js_newcfunction(J, mjs_history_go, "history.prototype.go", 1); + js_defproperty(J, -2, "go", JS_DONTENUM); + + js_newcfunction(J, mjs_history_toString, "history.prototype.toString", 0); + js_defproperty(J, -2, "toString", JS_DONTENUM); + + js_defglobal(J, "history", JS_DONTENUM); + + return 0; +} diff --git a/src/ecmascript/mujs/history.h b/src/ecmascript/mujs/history.h new file mode 100644 index 00000000..33f7fb3f --- /dev/null +++ b/src/ecmascript/mujs/history.h @@ -0,0 +1,10 @@ +#ifndef EL__ECMASCRIPT_MUJS_HISTORY_H +#define EL__ECMASCRIPT_MUJS_HISTORY_H + +#include + +struct ecmascript_interpreter; + +int mjs_history_init(struct ecmascript_interpreter *interpreter, js_State *J); + +#endif diff --git a/src/ecmascript/mujs/meson.build b/src/ecmascript/mujs/meson.build index 7001e190..14fc4ee1 100644 --- a/src/ecmascript/mujs/meson.build +++ b/src/ecmascript/mujs/meson.build @@ -1,3 +1,3 @@ #srcs += files('attr.cpp', 'attributes.cpp', 'collection.cpp', 'console.cpp', 'document.cpp', 'element.cpp', 'form.cpp', 'forms.cpp', 'heartbeat.cpp', 'history.cpp', 'implementation.cpp', #'input.cpp', 'localstorage.cpp', 'location.cpp', 'navigator.cpp', 'nodelist.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp') -srcs += files('navigator.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp') +srcs += files('history.cpp', 'navigator.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp')