1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[libdom,quickjs] attr

This commit is contained in:
Witold Filipczyk 2023-03-15 18:23:28 +01:00
parent 850559d216
commit 934cad6bf3
11 changed files with 265 additions and 1 deletions

View File

@ -40,6 +40,7 @@
extern "C" { extern "C" {
#endif #endif
struct document;
struct document_view; struct document_view;
struct form_state; struct form_state;
struct form_view; struct form_view;

View File

@ -2,6 +2,8 @@ top_builddir=../../..
include $(top_builddir)/Makefile.config include $(top_builddir)/Makefile.config
INCLUDES += $(LIBDOM_CFLAGS) INCLUDES += $(LIBDOM_CFLAGS)
SUBDIRS-$(CONFIG_QUICKJS) += quickjs
OBJS = parse.o OBJS = parse.o
include $(top_srcdir)/Makefile.lib include $(top_srcdir)/Makefile.lib

View File

@ -1,2 +1,6 @@
if conf_data.get('CONFIG_QUICKJS')
subdir('quickjs')
endif
srcs += files('parse.c') srcs += files('parse.c')

View File

@ -0,0 +1,6 @@
top_builddir=../../../..
include $(top_builddir)/Makefile.config
OBJS = attr.o mapa.obj
include $(top_srcdir)/Makefile.lib

View File

@ -0,0 +1,165 @@
/* The QuickJS attr object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#ifdef CONFIG_LIBDOM
#include <dom/dom.h>
#include <dom/bindings/hubbub/parser.h>
#endif
#include "elinks.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/libdom/quickjs/mapa.h"
#include "ecmascript/quickjs.h"
#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)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
struct view_state *vs = interpreter->vs;
dom_string *name = NULL;
dom_exception err;
JSValue r;
if (!vs) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return JS_EXCEPTION;
}
dom_attr *attr = (dom_attr *)(JS_GetOpaque(this_val, js_attr_class_id));
if (!attr) {
return JS_NULL;
}
err = dom_attr_get_name(attr, &name);
if (err != DOM_NO_ERR || name == NULL) {
return JS_NULL;
}
r = JS_NewStringLen(ctx, dom_string_data(name), dom_string_length(name));
dom_string_unref(name);
RETURN_JS(r);
}
static JSValue
js_attr_get_property_value(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
struct view_state *vs = interpreter->vs;
dom_string *value = NULL;
dom_exception err;
JSValue r;
if (!vs) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return JS_EXCEPTION;
}
dom_attr *attr = (dom_attr *)(JS_GetOpaque(this_val, js_attr_class_id));
if (!attr) {
return JS_NULL;
}
err = dom_attr_get_value(attr, &value);
if (err != DOM_NO_ERR || value == NULL) {
return JS_NULL;
}
r = JS_NewStringLen(ctx, dom_string_data(value), dom_string_length(value));
dom_string_unref(value);
RETURN_JS(r);
}
static JSValue
js_attr_toString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
return JS_NewString(ctx, "[attr object]");
}
static const JSCFunctionListEntry js_attr_proto_funcs[] = {
JS_CGETSET_DEF("name", js_attr_get_property_name, NULL),
JS_CGETSET_DEF("value", js_attr_get_property_value, NULL),
JS_CFUNC_DEF("toString", 0, js_attr_toString)
};
static void *map_attrs;
static
void js_attr_finalizer(JSRuntime *rt, JSValue val)
{
REF_JS(val);
void *node = JS_GetOpaque(val, js_attr_class_id);
attr_erase_from_map(map_attrs, node);
}
static JSClassDef js_attr_class = {
"attr",
js_attr_finalizer
};
JSValue
getAttr(JSContext *ctx, void *node)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JSValue second = JS_NULL;
static int initialized;
/* create the element class */
if (!initialized) {
JS_NewClassID(&js_attr_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_attr_class_id, &js_attr_class);
map_attrs = attr_create_new_attrs_map();
initialized = 1;
}
second = attr_find_in_map(map_attrs, node);
if (!JS_IsNull(second)) {
JSValue r = JS_DupValue(ctx, second);
RETURN_JS(r);
}
JSValue attr_obj = JS_NewObjectClass(ctx, js_attr_class_id);
JS_SetPropertyFunctionList(ctx, attr_obj, js_attr_proto_funcs, countof(js_attr_proto_funcs));
JS_SetClassProto(ctx, js_attr_class_id, attr_obj);
JS_SetOpaque(attr_obj, node);
attr_save_in_map(map_attrs, node, attr_obj);
JSValue rr = JS_DupValue(ctx, attr_obj);
RETURN_JS(rr);
}

View File

@ -0,0 +1,54 @@
/* map temporary file */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <cstddef>
#include <map>
#include "ecmascript/libdom/quickjs/mapa.h"
void
attr_save_in_map(void *m, void *node, JSValueConst value)
{
std::map<void *, JSValueConst> *mapa = static_cast<std::map<void *, JSValueConst> *>(m);
(*mapa)[node] = value;
}
void *
attr_create_new_attrs_map(void)
{
std::map<void *, JSValueConst> *mapa = new std::map<void *, JSValueConst>;
return (void *)mapa;
}
void
attr_clear_map(void *m)
{
std::map<void *, JSValueConst> *mapa = static_cast<std::map<void *, JSValueConst> *>(m);
mapa->clear();
}
JSValue
attr_find_in_map(void *m, void *node)
{
std::map<void *, JSValueConst> *mapa = static_cast<std::map<void *, JSValueConst> *>(m);
if (!mapa) {
return JS_NULL;
}
auto value = (*mapa).find(node);
if (value == (*mapa).end()) {
return JS_NULL;
}
return value->second;
}
void
attr_erase_from_map(void *m, void *node)
{
std::map<void *, JSValueConst> *mapa = static_cast<std::map<void *, JSValueConst> *>(m);
mapa->erase(node);
}

View File

@ -0,0 +1,20 @@
#ifndef EL__DOCUMENT_ECMASCRIPT_LIBDOM_QUICKJS_MAPA_H
#define EL__DOCUMENT_ECMASCRIPT_LIBDOM_QUICKJS_MAPA_H
#include <quickjs/quickjs.h>
#ifdef __cplusplus
extern "C" {
#endif
void attr_save_in_map(void *m, void *node, JSValueConst value);
void *attr_create_new_attrs_map(void);
void attr_clear_map(void *m);
JSValue attr_find_in_map(void *m, void *node);
void attr_erase_from_map(void *m, void *node);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1 @@
srcs += files('attr.c', 'mapa.cpp')

View File

@ -45,7 +45,7 @@ int quickjs_eval_boolback(struct ecmascript_interpreter *interpreter, struct str
void quickjs_call_function(struct ecmascript_interpreter *interpreter, JSValueConst fun, struct string *ret); void quickjs_call_function(struct ecmascript_interpreter *interpreter, JSValueConst fun, struct string *ret);
#ifndef JS_NAN_BOXING #if !defined(JS_NAN_BOXING) && defined(__cplusplus)
inline int operator<(JSValueConst a, JSValueConst b) inline int operator<(JSValueConst a, JSValueConst b)
{ {
return JS_VALUE_GET_PTR(a) < JS_VALUE_GET_PTR(b); return JS_VALUE_GET_PTR(a) < JS_VALUE_GET_PTR(b);

View File

@ -54,6 +54,8 @@
#define countof(x) (sizeof(x) / sizeof((x)[0])) #define countof(x) (sizeof(x) / sizeof((x)[0]))
#ifndef CONFIG_LIBDOM
static JSClassID js_attr_class_id; static JSClassID js_attr_class_id;
static JSValue static JSValue
@ -184,3 +186,4 @@ getAttr(JSContext *ctx, void *node)
RETURN_JS(rr); RETURN_JS(rr);
} }
#endif

View File

@ -3,6 +3,14 @@
#include <quickjs/quickjs.h> #include <quickjs/quickjs.h>
#ifdef __cplusplus
extern "C" {
#endif
JSValue getAttr(JSContext *ctx, void *node); JSValue getAttr(JSContext *ctx, void *node);
#ifdef __cplusplus
}
#endif
#endif #endif