From 2e26484ccad5af4c890b0782d6affa286af486ae Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 18 Oct 2021 19:34:11 +0200 Subject: [PATCH] [quickjs] screen --- src/ecmascript/quickjs.c | 2 + src/ecmascript/quickjs/meson.build | 2 +- src/ecmascript/quickjs/screen.c | 190 +++++++++++++++++++++++++++ src/ecmascript/quickjs/screen.h | 9 ++ src/ecmascript/spidermonkey/screen.c | 2 +- test/ecmascript/availHeight_2.html | 13 ++ test/ecmascript/availWidth_2.html | 11 ++ test/ecmascript/height_2.html | 10 ++ test/ecmascript/width_2.html | 11 ++ 9 files changed, 248 insertions(+), 2 deletions(-) create mode 100644 src/ecmascript/quickjs/screen.c create mode 100644 src/ecmascript/quickjs/screen.h create mode 100644 test/ecmascript/availHeight_2.html create mode 100644 test/ecmascript/availWidth_2.html create mode 100644 test/ecmascript/height_2.html create mode 100644 test/ecmascript/width_2.html diff --git a/src/ecmascript/quickjs.c b/src/ecmascript/quickjs.c index 1c6428a4..9a4c0881 100644 --- a/src/ecmascript/quickjs.c +++ b/src/ecmascript/quickjs.c @@ -25,6 +25,7 @@ #include "document/view.h" #include "ecmascript/ecmascript.h" #include "ecmascript/quickjs.h" +#include "ecmascript/quickjs/screen.h" #include "ecmascript/quickjs/window.h" #include "intl/libintl.h" #include "main/select.h" @@ -159,6 +160,7 @@ quickjs_get_interpreter(struct ecmascript_interpreter *interpreter) JS_SetPropertyStr(ctx, window_obj, "alert", JS_NewCFunction(ctx, js_window_alert, "alert", 1)); JS_SetPropertyStr(ctx, global_obj, "window", window_obj); + js_screen_init(ctx, global_obj); JS_FreeValue(ctx, global_obj); diff --git a/src/ecmascript/quickjs/meson.build b/src/ecmascript/quickjs/meson.build index a73839e3..ed6429ad 100644 --- a/src/ecmascript/quickjs/meson.build +++ b/src/ecmascript/quickjs/meson.build @@ -1 +1 @@ -srcs += files('window.c') +srcs += files('screen.c', 'window.c') diff --git a/src/ecmascript/quickjs/screen.c b/src/ecmascript/quickjs/screen.c new file mode 100644 index 00000000..35517ce3 --- /dev/null +++ b/src/ecmascript/quickjs/screen.c @@ -0,0 +1,190 @@ +/* The QuickJS screen 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/quickjs/screen.h" +#include "ecmascript/quickjs/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" + +#define countof(x) (sizeof(x) / sizeof((x)[0])) + +static JSClassID js_screen_class_id; + +static JSValue +js_screen_get_property_availHeight(JSContext *ctx, JSValueConst this_val, int magic) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + + if (!doc_view) { + return JS_UNDEFINED; + } + + return JS_NewInt32(ctx, doc_view->box.height * 16); +} + +static JSValue +js_screen_get_property_availWidth(JSContext *ctx, JSValueConst this_val, int magic) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + + if (!doc_view) { + return JS_UNDEFINED; + } + + return JS_NewInt32(ctx, doc_view->box.width * 8); +} + +static JSValue +js_screen_get_property_height(JSContext *ctx, JSValueConst this_val, int magic) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + + if (!doc_view) { + return JS_UNDEFINED; + } + + struct session *ses = doc_view->session; + + if (!ses) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return JS_UNDEFINED; + } + + return JS_NewInt32(ctx, ses->tab->term->height * 16); +} + +static JSValue +js_screen_get_property_width(JSContext *ctx, JSValueConst this_val, int magic) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); + struct view_state *vs = interpreter->vs; + struct document_view *doc_view = vs->doc_view; + + if (!doc_view) { + return JS_UNDEFINED; + } + + struct session *ses = doc_view->session; + + if (!ses) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return JS_UNDEFINED; + } + + return JS_NewInt32(ctx, ses->tab->term->width * 8); +} + +static const JSCFunctionListEntry js_screen_proto_funcs[] = { + JS_CGETSET_MAGIC_DEF("availHeight", js_screen_get_property_availHeight, nullptr, 0), + JS_CGETSET_MAGIC_DEF("availWidth", js_screen_get_property_availWidth, nullptr, 0), + JS_CGETSET_MAGIC_DEF("height", js_screen_get_property_height, nullptr, 0), + JS_CGETSET_MAGIC_DEF("width", js_screen_get_property_width, nullptr, 0), +}; + +static JSClassDef js_screen_class = { + "screen", +}; + +static JSValue +js_screen_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv) +{ + JSValue obj = JS_UNDEFINED; + JSValue proto; + /* using new_target to get the prototype is necessary when the + class is extended. */ + proto = JS_GetPropertyStr(ctx, new_target, "prototype"); + + if (JS_IsException(proto)) { + goto fail; + } + obj = JS_NewObjectProtoClass(ctx, proto, js_screen_class_id); + JS_FreeValue(ctx, proto); + + if (JS_IsException(obj)) { + goto fail; + } + return obj; + +fail: + JS_FreeValue(ctx, obj); + return JS_EXCEPTION; +} + +int +js_screen_init(JSContext *ctx, JSValue global_obj) +{ + JSValue screen_proto, screen_class; + + /* create the screen class */ + JS_NewClassID(&js_screen_class_id); + JS_NewClass(JS_GetRuntime(ctx), js_screen_class_id, &js_screen_class); + + screen_proto = JS_NewObject(ctx); + JS_SetPropertyFunctionList(ctx, screen_proto, js_screen_proto_funcs, countof(js_screen_proto_funcs)); + + screen_class = JS_NewCFunction2(ctx, js_screen_ctor, "screen", 2, JS_CFUNC_constructor, 0); + /* set proto.constructor and ctor.prototype */ + JS_SetConstructor(ctx, screen_class, screen_proto); + JS_SetClassProto(ctx, js_screen_class_id, screen_proto); + + JS_SetPropertyStr(ctx, global_obj, "screen", screen_proto); + return 0; +} diff --git a/src/ecmascript/quickjs/screen.h b/src/ecmascript/quickjs/screen.h new file mode 100644 index 00000000..0ef74221 --- /dev/null +++ b/src/ecmascript/quickjs/screen.h @@ -0,0 +1,9 @@ + +#ifndef EL__ECMASCRIPT_QUICKJS_SCREEN_H +#define EL__ECMASCRIPT_QUICKJS_SCREEN_H + +#include + +int js_screen_init(JSContext *ctx, JSValue global_obj); + +#endif diff --git a/src/ecmascript/spidermonkey/screen.c b/src/ecmascript/spidermonkey/screen.c index 999d58ae..5468dc41 100644 --- a/src/ecmascript/spidermonkey/screen.c +++ b/src/ecmascript/spidermonkey/screen.c @@ -1,4 +1,4 @@ -/* The SpiderMonkey location and history objects implementation. */ +/* The SpiderMonkey screen object implementation. */ #ifdef HAVE_CONFIG_H #include "config.h" diff --git a/test/ecmascript/availHeight_2.html b/test/ecmascript/availHeight_2.html new file mode 100644 index 00000000..afca2a61 --- /dev/null +++ b/test/ecmascript/availHeight_2.html @@ -0,0 +1,13 @@ + + + + +

+ + + + + diff --git a/test/ecmascript/availWidth_2.html b/test/ecmascript/availWidth_2.html new file mode 100644 index 00000000..53ca6234 --- /dev/null +++ b/test/ecmascript/availWidth_2.html @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/test/ecmascript/height_2.html b/test/ecmascript/height_2.html new file mode 100644 index 00000000..ccb83f35 --- /dev/null +++ b/test/ecmascript/height_2.html @@ -0,0 +1,10 @@ + + + + + + + diff --git a/test/ecmascript/width_2.html b/test/ecmascript/width_2.html new file mode 100644 index 00000000..d85a2ed1 --- /dev/null +++ b/test/ecmascript/width_2.html @@ -0,0 +1,11 @@ + + + + + + + +