From e697d57c78b72b04ef45f2d9183d917917a88d03 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Wed, 12 Apr 2023 16:08:08 +0200 Subject: [PATCH] [mujs] attr.c --- src/ecmascript/libdom/Makefile | 1 + src/ecmascript/libdom/meson.build | 4 + src/ecmascript/libdom/mujs/Makefile | 6 ++ src/ecmascript/libdom/mujs/attr.c | 116 +++++++++++++++++++++++++ src/ecmascript/libdom/mujs/mapa.cpp | 90 +++++++++++++++++++ src/ecmascript/libdom/mujs/mapa.h | 24 +++++ src/ecmascript/libdom/mujs/meson.build | 1 + src/ecmascript/libdom/quickjs/attr.c | 1 - src/ecmascript/mujs.h | 9 ++ src/ecmascript/mujs/attr.cpp | 3 + src/ecmascript/mujs/attr.h | 8 ++ 11 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 src/ecmascript/libdom/mujs/Makefile create mode 100644 src/ecmascript/libdom/mujs/attr.c create mode 100644 src/ecmascript/libdom/mujs/mapa.cpp create mode 100644 src/ecmascript/libdom/mujs/mapa.h create mode 100644 src/ecmascript/libdom/mujs/meson.build diff --git a/src/ecmascript/libdom/Makefile b/src/ecmascript/libdom/Makefile index a59fbafb..aa47de32 100644 --- a/src/ecmascript/libdom/Makefile +++ b/src/ecmascript/libdom/Makefile @@ -2,6 +2,7 @@ top_builddir=../../.. include $(top_builddir)/Makefile.config INCLUDES += $(LIBDOM_CFLAGS) +SUBDIRS-$(CONFIG_MUJS) += mujs SUBDIRS-$(CONFIG_QUICKJS) += quickjs OBJS = parse.o diff --git a/src/ecmascript/libdom/meson.build b/src/ecmascript/libdom/meson.build index 643d77ec..d0e5c6eb 100644 --- a/src/ecmascript/libdom/meson.build +++ b/src/ecmascript/libdom/meson.build @@ -1,3 +1,7 @@ +if conf_data.get('CONFIG_MUJS') + subdir('mujs') +endif + if conf_data.get('CONFIG_QUICKJS') subdir('quickjs') endif diff --git a/src/ecmascript/libdom/mujs/Makefile b/src/ecmascript/libdom/mujs/Makefile new file mode 100644 index 00000000..a773e822 --- /dev/null +++ b/src/ecmascript/libdom/mujs/Makefile @@ -0,0 +1,6 @@ +top_builddir=../../../.. +include $(top_builddir)/Makefile.config + +OBJS = attr.o mapa.obj + +include $(top_srcdir)/Makefile.lib diff --git a/src/ecmascript/libdom/mujs/attr.c b/src/ecmascript/libdom/mujs/attr.c new file mode 100644 index 00000000..da02325a --- /dev/null +++ b/src/ecmascript/libdom/mujs/attr.c @@ -0,0 +1,116 @@ +/* The MuJS attr object implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#ifdef CONFIG_LIBDOM +#include +#include +#endif + +#include "elinks.h" + +#include "ecmascript/ecmascript.h" +#include "ecmascript/libdom/mujs/mapa.h" +#include "ecmascript/mujs.h" +#include "ecmascript/mujs/attr.h" + +static void +mjs_attr_get_property_name(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + struct view_state *vs = interpreter->vs; + dom_exception exc; + + if (!vs) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + js_error(J, "!vs"); + return; + } + dom_attr *attr = (dom_attr *)(js_touserdata(J, 0, "attr")); + + if (!attr) { + js_pushnull(J); + return; + } + dom_string *name = NULL; + exc = dom_attr_get_name(attr, &name); + + if (exc != DOM_NO_ERR || name == NULL) { + js_pushnull(J); + return; + } + js_pushstring(J, dom_string_data(name)); + dom_string_unref(name); +} + +static void +mjs_attr_get_property_value(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + struct view_state *vs = interpreter->vs; + dom_exception exc; + + if (!vs) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + js_error(J, "!vs"); + return; + } + dom_attr *attr = (dom_attr *)(js_touserdata(J, 0, "attr")); + + if (!attr) { + js_pushnull(J); + return; + } + dom_string *value = NULL; + exc = dom_attr_get_value(attr, &value); + + if (exc != DOM_NO_ERR || value == NULL) { + js_pushnull(J); + return; + } + js_pushstring(J, dom_string_data(value)); + dom_string_unref(value); +} + +static void +mjs_attr_toString(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + js_pushstring(J, "[attr object]"); +} + +void *map_attrs; + +static +void mjs_attr_finalizer(js_State *J, void *node) +{ + attr_erase_from_map(map_attrs, node); +} + +void +mjs_push_attr(js_State *J, void *node) +{ + js_newobject(J); + { + js_newuserdata(J, "attr", node, mjs_attr_finalizer); + addmethod(J, "toString", mjs_attr_toString, 0); + addproperty(J, "name", mjs_attr_get_property_name, NULL); + addproperty(J, "value", mjs_attr_get_property_value, NULL); + } +} diff --git a/src/ecmascript/libdom/mujs/mapa.cpp b/src/ecmascript/libdom/mujs/mapa.cpp new file mode 100644 index 00000000..b748ded7 --- /dev/null +++ b/src/ecmascript/libdom/mujs/mapa.cpp @@ -0,0 +1,90 @@ +/* map temporary file */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include +#include +#include + +#include "ecmascript/libdom/mujs/mapa.h" +#include "ecmascript/mujs.h" +#include "ecmascript/mujs/xhr.h" +#include "util/memory.h" +#include "util/string.h" + +void +attr_save_in_map(void *m, void *node, void *value) +{ + std::map *mapa = static_cast *>(m); + (*mapa)[node] = value; +} + +void * +attr_create_new_attrs_map(void) +{ + std::map *mapa = new std::map; + + return (void *)mapa; +} + +struct classcomp { + bool operator() (const std::string& lhs, const std::string& rhs) const + { + return strcasecmp(lhs.c_str(), rhs.c_str()) < 0; + } +}; + +void +attr_clear_map(void *m) +{ + std::map *mapa = static_cast *>(m); + mapa->clear(); +} + +void +delete_map_str(void *m) +{ + std::map *mapa = static_cast *>(m); + + if (mapa) { + delete(mapa); + } +} + +void +attr_delete_map(void *m) +{ + std::map *mapa = static_cast *>(m); + + if (mapa) { + delete(mapa); + } +} + +void * +attr_find_in_map(void *m, void *node) +{ + std::map *mapa = static_cast *>(m); + + if (!mapa) { + return NULL; + } + auto value = (*mapa).find(node); + + if (value == (*mapa).end()) { + return NULL; + } + return value->second; +} + +void +attr_erase_from_map(void *m, void *node) +{ + std::map *mapa = static_cast *>(m); + mapa->erase(node); +} diff --git a/src/ecmascript/libdom/mujs/mapa.h b/src/ecmascript/libdom/mujs/mapa.h new file mode 100644 index 00000000..6a99cfba --- /dev/null +++ b/src/ecmascript/libdom/mujs/mapa.h @@ -0,0 +1,24 @@ +#ifndef EL__DOCUMENT_ECMASCRIPT_LIBDOM_MUJS_MAPA_H +#define EL__DOCUMENT_ECMASCRIPT_LIBDOM_MUJS_MAPA_H + +#ifdef __cplusplus +extern "C" { +#endif + +extern void *map_attrs; + +void attr_save_in_map(void *m, void *node, void *value); + +void *attr_create_new_attrs_map(void); + +void *attr_find_in_map(void *m, void *node); + +void attr_erase_from_map(void *m, void *node); + +void attr_clear_map(void *m); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/ecmascript/libdom/mujs/meson.build b/src/ecmascript/libdom/mujs/meson.build new file mode 100644 index 00000000..f07679aa --- /dev/null +++ b/src/ecmascript/libdom/mujs/meson.build @@ -0,0 +1 @@ +srcs += files('attr.c', 'mapa.cpp') diff --git a/src/ecmascript/libdom/quickjs/attr.c b/src/ecmascript/libdom/quickjs/attr.c index 62023ffd..9bff13e4 100644 --- a/src/ecmascript/libdom/quickjs/attr.c +++ b/src/ecmascript/libdom/quickjs/attr.c @@ -79,7 +79,6 @@ js_attr_get_property_value(JSContext *ctx, JSValueConst this_val) #endif return JS_EXCEPTION; } - dom_attr *attr = (dom_attr *)(JS_GetOpaque(this_val, js_attr_class_id)); if (!attr) { diff --git a/src/ecmascript/mujs.h b/src/ecmascript/mujs.h index b8cd924d..050f16b4 100644 --- a/src/ecmascript/mujs.h +++ b/src/ecmascript/mujs.h @@ -3,6 +3,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + #ifdef ECMASCRIPT_DEBUG #if 0 @@ -44,4 +48,9 @@ void addmethod(js_State *J, const char *name, js_CFunction fun, int n); void addproperty(js_State *J, const char *name, js_CFunction getfun, js_CFunction setfun); extern struct module mujs_module; + +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/ecmascript/mujs/attr.cpp b/src/ecmascript/mujs/attr.cpp index c03377b6..2458499d 100644 --- a/src/ecmascript/mujs/attr.cpp +++ b/src/ecmascript/mujs/attr.cpp @@ -52,6 +52,8 @@ #include #include +#ifndef CONFIG_LIBDOM + static void mjs_attr_get_property_name(js_State *J) { @@ -134,3 +136,4 @@ mjs_push_attr(js_State *J, void *node) addproperty(J, "value", mjs_attr_get_property_value, NULL); } } +#endif diff --git a/src/ecmascript/mujs/attr.h b/src/ecmascript/mujs/attr.h index ef74c8f7..99a5b7af 100644 --- a/src/ecmascript/mujs/attr.h +++ b/src/ecmascript/mujs/attr.h @@ -3,6 +3,14 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + void mjs_push_attr(js_State *J, void *node); +#ifdef __cplusplus +} +#endif + #endif