From adbdf68bda1382d18ae60ed98f0eefa4807d1165 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Sat, 23 Oct 2021 18:33:10 +0200 Subject: [PATCH] [quickjs] attr.c --- src/ecmascript/quickjs/attr.c | 179 +++++++++++++++++++++++++++++ src/ecmascript/quickjs/attr.h | 8 ++ src/ecmascript/quickjs/meson.build | 2 +- 3 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 src/ecmascript/quickjs/attr.c create mode 100644 src/ecmascript/quickjs/attr.h diff --git a/src/ecmascript/quickjs/attr.c b/src/ecmascript/quickjs/attr.c new file mode 100644 index 000000000..75504d9c9 --- /dev/null +++ b/src/ecmascript/quickjs/attr.c @@ -0,0 +1,179 @@ +/* The QuickJS attr 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/attr.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" + +#include +#include +#include +#include +#include + +#include +#include +#include + +#define countof(x) (sizeof(x) / sizeof((x)[0])) + +static JSClassID js_attr_class_id; + +static JSValue +js_attr_get_property_name(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; + + if (!vs) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return JS_EXCEPTION; + } + + xmlpp::AttributeNode *attr = JS_GetOpaque(this_val, js_attr_class_id); + + if (!attr) { + return JS_NULL; + } + + xmlpp::ustring v = attr->get_name(); + + return JS_NewString(ctx, v.c_str()); +} + +static JSValue +js_attr_get_property_value(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; + + if (!vs) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return JS_EXCEPTION; + } + + xmlpp::AttributeNode *attr = JS_GetOpaque(this_val, js_attr_class_id); + + if (!attr) { + return JS_NULL; + } + + xmlpp::ustring v = attr->get_value(); + + return JS_NewString(ctx, v.c_str()); +} + +static const JSCFunctionListEntry js_attr_proto_funcs[] = { + JS_CGETSET_MAGIC_DEF("name", js_attr_get_property_name, nullptr, 0), + JS_CGETSET_MAGIC_DEF("value", js_attr_get_property_value, nullptr, 0), +}; + +static JSClassDef js_attr_class = { + "attr", +}; + +static JSValue +js_attr_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_attr_class_id); + JS_FreeValue(ctx, proto); + + if (JS_IsException(obj)) { + goto fail; + } + return obj; + +fail: + JS_FreeValue(ctx, obj); + return JS_EXCEPTION; +} + +int +js_attr_init(JSContext *ctx, JSValue global_obj) +{ + JSValue attr_proto, attr_class; + + /* create the attr class */ + JS_NewClassID(&js_attr_class_id); + JS_NewClass(JS_GetRuntime(ctx), js_attr_class_id, &js_attr_class); + + attr_proto = JS_NewObject(ctx); + JS_SetPropertyFunctionList(ctx, attr_proto, js_attr_proto_funcs, countof(js_attr_proto_funcs)); + + attr_class = JS_NewCFunction2(ctx, js_attr_ctor, "attr", 0, JS_CFUNC_constructor, 0); + /* set proto.constructor and ctor.prototype */ + JS_SetConstructor(ctx, attr_class, attr_proto); + JS_SetClassProto(ctx, js_attr_class_id, attr_proto); + + JS_SetPropertyStr(ctx, global_obj, "attr", attr_proto); + return 0; +} + +JSValue +getAttr(JSContext *ctx, void *node) +{ + JSValue attr_obj = JS_NewObject(ctx); + JS_SetPropertyFunctionList(ctx, attr_obj, js_attr_proto_funcs, countof(js_attr_proto_funcs)); +// attr_class = JS_NewCFunction2(ctx, js_attr_ctor, "attr", 0, JS_CFUNC_constructor, 0); +// JS_SetConstructor(ctx, attr_class, attr_obj); +// JS_SetClassProto(ctx, js_attr_class_id, attr_obj); + + JS_SetOpaque(attr_obj, node); + + return attr_obj; +} diff --git a/src/ecmascript/quickjs/attr.h b/src/ecmascript/quickjs/attr.h new file mode 100644 index 000000000..e35fdffbf --- /dev/null +++ b/src/ecmascript/quickjs/attr.h @@ -0,0 +1,8 @@ +#ifndef EL__ECMASCRIPT_QUICKJS_ATTR_H +#define EL__ECMASCRIPT_QUICKJS_ATTR_H + +#include + +JSValue getAttr(JSContext *ctx, void *node); + +#endif diff --git a/src/ecmascript/quickjs/meson.build b/src/ecmascript/quickjs/meson.build index b3cff8667..047d91d9f 100644 --- a/src/ecmascript/quickjs/meson.build +++ b/src/ecmascript/quickjs/meson.build @@ -1 +1 @@ -srcs += files('console.c', 'history.c', 'location.c', 'navigator.c', 'screen.c', 'unibar.c', 'window.c') +srcs += files('attr.c', 'console.c', 'history.c', 'location.c', 'navigator.c', 'screen.c', 'unibar.c', 'window.c')