From c6a3d04e21861f3e8454c5b8da81211afd79aea1 Mon Sep 17 00:00:00 2001 From: "nobody@earth.com" Date: Sun, 21 Feb 2021 22:33:29 +0100 Subject: [PATCH] SpiderMonkey Update v0.1.a - Console Log --- src/ecmascript/spidermonkey.c | 13 ++- src/ecmascript/spidermonkey/Makefile | 2 +- src/ecmascript/spidermonkey/console.c | 137 ++++++++++++++++++++++++++ src/ecmascript/spidermonkey/console.h | 11 +++ test/ecmascript/console_log.html | 13 +++ 5 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 src/ecmascript/spidermonkey/console.c create mode 100644 src/ecmascript/spidermonkey/console.h create mode 100644 test/ecmascript/console_log.html diff --git a/src/ecmascript/spidermonkey.c b/src/ecmascript/spidermonkey.c index f20c98a8c..d4f959312 100644 --- a/src/ecmascript/spidermonkey.c +++ b/src/ecmascript/spidermonkey.c @@ -25,6 +25,7 @@ #include "document/view.h" #include "ecmascript/ecmascript.h" #include "ecmascript/spidermonkey.h" +#include "ecmascript/spidermonkey/console.h" #include "ecmascript/spidermonkey/document.h" #include "ecmascript/spidermonkey/form.h" #include "ecmascript/spidermonkey/heartbeat.h" @@ -207,7 +208,7 @@ void * spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter) { JSContext *ctx; - JSObject *document_obj, *forms_obj, *history_obj, *location_obj, + JSObject *console_obj, *document_obj, *forms_obj, *history_obj, *location_obj, *statusbar_obj, *menubar_obj, *navigator_obj; static int initialized = 0; @@ -315,6 +316,16 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter) if (!navigator_obj) { goto release_and_fail; } + + console_obj = spidermonkey_InitClass(ctx, window_obj, NULL, + &console_class, NULL, 0, + console_props, + console_funcs, + NULL, NULL); + if (!console_obj) { + goto release_and_fail; + } + JS_SetCompartmentPrivate(js::GetContextCompartment(ctx), interpreter); return ctx; diff --git a/src/ecmascript/spidermonkey/Makefile b/src/ecmascript/spidermonkey/Makefile index 1d7129946..aefffcc99 100644 --- a/src/ecmascript/spidermonkey/Makefile +++ b/src/ecmascript/spidermonkey/Makefile @@ -2,6 +2,6 @@ top_builddir=../../.. include $(top_builddir)/Makefile.config INCLUDES += $(SPIDERMONKEY_CFLAGS) -OBJS = document.o form.o heartbeat.o location.o navigator.o unibar.o window.o +OBJS = console.o document.o form.o heartbeat.o location.o navigator.o unibar.o window.o include $(top_srcdir)/Makefile.lib diff --git a/src/ecmascript/spidermonkey/console.c b/src/ecmascript/spidermonkey/console.c new file mode 100644 index 000000000..69e664a67 --- /dev/null +++ b/src/ecmascript/spidermonkey/console.c @@ -0,0 +1,137 @@ +/* The SpiderMonkey console object implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#include "elinks.h" + +#include "ecmascript/spidermonkey/util.h" + +#include "bfu/dialog.h" +#include "cache/cache.h" +#include "config/home.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/spidermonkey/console.h" +#include "ecmascript/spidermonkey/form.h" +#include "ecmascript/spidermonkey/location.h" +#include "ecmascript/spidermonkey/document.h" +#include "ecmascript/spidermonkey/window.h" +#include "intl/gettext/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" + +#include +#include "document/renderer.h" +#include "document/refresh.h" +#include "terminal/screen.h" + +#define DEBUG 0 + +static bool console_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp); + +JSClassOps console_ops = { + JS_PropertyStub, nullptr, + console_get_property, JS_StrictPropertyStub, + nullptr, nullptr, nullptr +}; + +/* Each @console_class object must have a @window_class parent. */ +const JSClass console_class = { + "console", + JSCLASS_HAS_PRIVATE, + &console_ops +}; + +/* Tinyids of properties. Use negative values to distinguish these + * from array indexes (even though this object has no array elements). + * ECMAScript code should not use these directly as in document[-1]; + * future versions of ELinks may change the numbers. */ +enum console_prop { + JSP_LOCALSTORAGE_LOC = -1, + JSP_LOCALSTORAGE_REF = -2, + JSP_LOCALSTORAGE_TITLE = -3, + JSP_LOCALSTORAGE_URL = -4, +}; +/* "cookie" is special; it isn't a regular property but we channel it to the + * cookie-module. XXX: Would it work if "cookie" was defined in this array? */ +const JSPropertySpec console_props[] = { + { NULL } +}; + +/* @console_class.getProperty */ +static bool +console_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp) +{ + JSObject *parent_win; /* instance of @window_class */ + struct view_state *vs; + struct document_view *doc_view; + struct document *document; + struct session *ses; + + return true; +} + +static bool console_log(JSContext *ctx, unsigned int argc, JS::Value *vp); + +const spidermonkeyFunctionSpec console_funcs[] = { + { "log", console_log, 2 }, + { NULL } +}; + +static bool +console_log(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ + struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); + struct document_view *doc_view = interpreter->vs->doc_view; + struct session *ses = doc_view->session; + struct terminal *term = ses->tab->term; + struct string *ret = interpreter->ret; + struct document *document; + JS::CallArgs args = CallArgsFromVp(argc, vp); + + document = doc_view->document; + + if (argc != 1) { + args.rval().setBoolean(true); + return(true); + } + + unsigned char *key= JS_EncodeString(ctx, args[0].toString()); + char log_fname[8192]=""; + strcat(log_fname,elinks_home); + strcat(log_fname,"console.log"); + FILE *f = fopen(log_fname,"a"); + fprintf(f, "%s\n", key); + fclose(f); + args.rval().setBoolean(true); + return(true); + +} diff --git a/src/ecmascript/spidermonkey/console.h b/src/ecmascript/spidermonkey/console.h new file mode 100644 index 000000000..6742c215e --- /dev/null +++ b/src/ecmascript/spidermonkey/console.h @@ -0,0 +1,11 @@ + +#ifndef EL__ECMASCRIPT_SPIDERMONKEY_CONSOLE_H +#define EL__ECMASCRIPT_SPIDERMONKEY_CONSOLE_H + +#include "ecmascript/spidermonkey/util.h" + +extern const JSClass console_class; +extern const spidermonkeyFunctionSpec console_funcs[]; +extern const JSPropertySpec console_props[]; + +#endif diff --git a/test/ecmascript/console_log.html b/test/ecmascript/console_log.html new file mode 100644 index 000000000..19a9059ba --- /dev/null +++ b/test/ecmascript/console_log.html @@ -0,0 +1,13 @@ + + + +
+You'll find the console.log 
+at config dir (~/elinks)
+
+ +