From fb90f2a832d3c9af97c0e1611f18c438e1fb8fa8 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Mon, 31 May 2021 14:27:40 +0200 Subject: [PATCH] [js] screen.height (term->height * 16) --- src/ecmascript/spidermonkey/screen.c | 46 +++++++++++++++++++++++++++- test/ecmascript/height.html | 19 ++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 test/ecmascript/height.html diff --git a/src/ecmascript/spidermonkey/screen.c b/src/ecmascript/spidermonkey/screen.c index 6c105850..c8e7b56d 100644 --- a/src/ecmascript/spidermonkey/screen.c +++ b/src/ecmascript/spidermonkey/screen.c @@ -59,11 +59,13 @@ JSClass screen_class = { static bool screen_get_property_availHeight(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool screen_get_property_availWidth(JSContext *ctx, unsigned int argc, JS::Value *vp); +static bool screen_get_property_height(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool screen_get_property_width(JSContext *ctx, unsigned int argc, JS::Value *vp); JSPropertySpec screen_props[] = { JS_PSG("availHeight", screen_get_property_availHeight, JSPROP_ENUMERATE), JS_PSG("availWidth", screen_get_property_availWidth, JSPROP_ENUMERATE), + JS_PSG("height", screen_get_property_height, JSPROP_ENUMERATE), JS_PSG("width", screen_get_property_width, JSPROP_ENUMERATE), JS_PS_END }; @@ -142,6 +144,49 @@ screen_get_property_availWidth(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } +static bool +screen_get_property_height(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ + JS::CallArgs args = CallArgsFromVp(argc, vp); + JS::RootedObject hobj(ctx, &args.thisv().toObject()); + + struct view_state *vs; + JSCompartment *comp = js::GetContextCompartment(ctx); + + if (!comp) { + return false; + } + + struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp); + + /* This can be called if @obj if not itself an instance of the + * appropriate class but has one in its prototype chain. Fail + * such calls. */ + if (!JS_InstanceOf(ctx, hobj, &screen_class, NULL)) + return false; + + vs = interpreter->vs; + if (!vs) { + return false; + } + + struct document_view *doc_view = vs->doc_view; + + if (!doc_view) { + return false; + } + + struct session *ses = doc_view->session; + + if (!ses) { + return false; + } + + args.rval().setInt32(ses->tab->term->height * 16); + + return true; +} + static bool screen_get_property_width(JSContext *ctx, unsigned int argc, JS::Value *vp) { @@ -184,4 +229,3 @@ screen_get_property_width(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } - diff --git a/test/ecmascript/height.html b/test/ecmascript/height.html new file mode 100644 index 00000000..f8fac52e --- /dev/null +++ b/test/ecmascript/height.html @@ -0,0 +1,19 @@ + + + + +

Click the button to display the height of your screen, in pixels.

+ + + +

+ + + + +