1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-28 01:35:32 +00:00

[mujs] attr.c

This commit is contained in:
Witold Filipczyk 2023-04-12 16:08:08 +02:00
parent b17f051f59
commit e697d57c78
11 changed files with 262 additions and 1 deletions

View File

@ -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

View File

@ -1,3 +1,7 @@
if conf_data.get('CONFIG_MUJS')
subdir('mujs')
endif
if conf_data.get('CONFIG_QUICKJS')
subdir('quickjs')
endif

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,116 @@
/* The MuJS 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/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);
}
}

View File

@ -0,0 +1,90 @@
/* map temporary file */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <cstddef>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#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<void *, void *> *mapa = static_cast<std::map<void *, void *> *>(m);
(*mapa)[node] = value;
}
void *
attr_create_new_attrs_map(void)
{
std::map<void *, void *> *mapa = new std::map<void *, void *>;
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<void *, void *> *mapa = static_cast<std::map<void *, void *> *>(m);
mapa->clear();
}
void
delete_map_str(void *m)
{
std::map<std::string, std::string> *mapa = static_cast<std::map<std::string, std::string> *>(m);
if (mapa) {
delete(mapa);
}
}
void
attr_delete_map(void *m)
{
std::map<void *, void *> *mapa = static_cast<std::map<void *, void *> *>(m);
if (mapa) {
delete(mapa);
}
}
void *
attr_find_in_map(void *m, void *node)
{
std::map<void *, void *> *mapa = static_cast<std::map<void *, void *> *>(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<void *, void *> *mapa = static_cast<std::map<void *, void *> *>(m);
mapa->erase(node);
}

View File

@ -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

View File

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

View File

@ -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) {

View File

@ -3,6 +3,10 @@
#include <mujs.h>
#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

View File

@ -52,6 +52,8 @@
#include <algorithm>
#include <string>
#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

View File

@ -3,6 +3,14 @@
#include <mujs.h>
#ifdef __cplusplus
extern "C" {
#endif
void mjs_push_attr(js_State *J, void *node);
#ifdef __cplusplus
}
#endif
#endif