1
0
Fork 0

Merge branch 'MessageEvent'

This commit is contained in:
Witold Filipczyk 2022-12-25 21:13:27 +01:00
commit f4257b40fd
35 changed files with 1706 additions and 48 deletions

1
.gitignore vendored
View File

@ -18,6 +18,7 @@ elinks
*.swp
*.patch
*.o
*.obj
features.log
src/elinks.exe
test/server/__pycache__

View File

@ -13,7 +13,7 @@ RUN cd /etc/apk && echo https://dl-cdn.alpinelinux.org/alpine/edge/testing >> re
# install
# [*] install libraries
RUN cd /root && apk add \
RUN apk add \
brotli-dev \
brotli-static \
bzip2-dev \
@ -43,7 +43,7 @@ RUN cd /root && apk add \
xz-static
# [*] libxml++5
RUN cd /root && apk add mm-common libtool xz
RUN apk add mm-common libtool upx xz
# libxml++5 cd
RUN mkdir /root/tmp; cd /root/tmp; \

View File

@ -54,3 +54,7 @@ meson /root/tmp/builddir \
-Dzstd=true
meson compile -C /root/tmp/builddir
strip /root/tmp/builddir/src/elinks
upx /root/tmp/builddir/src/elinks

View File

@ -54,3 +54,7 @@ meson /root/tmp/builddir_js \
-Dzstd=true
meson compile -j $(($(nproc) - 1)) -C /root/tmp/builddir_js
strip /root/tmp/builddir_js/src/elinks
upx /root/tmp/builddir_js/src/elinks

View File

@ -9,7 +9,7 @@ FROM fedora:37
# prepare system
RUN dnf -y update && dnf -y install bash wget \
rsync vim screen git make automake meson unzip cmake patch
rsync vim screen git make automake meson unzip cmake patch upx
RUN cd /root; wget http://ftp.delorie.com/pub/djgpp/rpms/djcross-gcc-12.2.0/djcross-gcc-12.2.0-1ap.x86_64.rpm ; \
wget http://ftp.delorie.com/pub/djgpp/rpms/djcross-gcc-12.2.0/djcross-gcc-c++-12.2.0-1ap.x86_64.rpm ; \

View File

@ -59,5 +59,6 @@ meson /tmp/builddir --cross-file cross/linux-djgpp.txt \
meson compile -C /tmp/builddir
cd /tmp/builddir
ninja install
i586-pc-msdosdjgpp-strip /tmp/builddir/src/elinks.exe
upx /tmp/builddir/src/elinks.exe

View File

@ -59,3 +59,7 @@ meson /tmp/builddir2 --cross-file cross/linux-djgpp.txt \
-Dzstd=false \
meson compile -C /tmp/builddir2
i586-pc-msdosdjgpp-strip /tmp/builddir2/src/elinks.exe
upx /tmp/builddir2/src/elinks.exe

View File

@ -228,12 +228,20 @@ download_dialog_layouter(struct dialog_data *dlg_data)
mem_free(msg);
}
static enum dlg_refresh_code
refresh_file_download(struct dialog_data *dlg_data, void *data)
{
/* Always refresh (until we keep finished downloads) */
return are_there_downloads() ? REFRESH_DIALOG : REFRESH_STOP;
}
void
display_download(struct terminal *term, struct file_download *file_download,
struct session *ses)
{
/* [gettext_accelerator_context(display_download)] */
struct dialog *dlg;
struct dialog_data *ret;
if (!is_in_downloads_list(file_download))
return;
@ -282,9 +290,12 @@ display_download(struct terminal *term, struct file_download *file_download,
add_dlg_end(dlg, DOWNLOAD_WIDGETS_COUNT - !!file_download->external_handler);
#endif
do_dialog(term, dlg, getml(dlg, (void *) NULL));
}
ret = do_dialog(term, dlg, getml(dlg, (void *) NULL));
if (ret) {
refresh_dialog(ret, refresh_file_download, NULL);
}
}
/* The download manager */
@ -360,12 +371,6 @@ delete_file_download(struct listbox_item *item, int last)
register_bottom_half(do_abort_download, file_download);
}
static enum dlg_refresh_code
refresh_file_download(struct dialog_data *dlg_data, void *data)
{
/* Always refresh (until we keep finished downloads) */
return are_there_downloads() ? REFRESH_DIALOG : REFRESH_STOP;
}
/* TODO: Make it configurable */
#define DOWNLOAD_METER_WIDTH 15

View File

@ -373,19 +373,19 @@ check_for_rerender(struct ecmascript_interpreter *interpreter, const char* text)
struct cache_entry *cached = document->cached;
if (!strcmp(text, "eval")) {
if (interpreter->element_offset) {
if (interpreter->write_element_offset) {
if (interpreter->writecode.length) {
std::map<int, xmlpp::Element *> *mapa = (std::map<int, xmlpp::Element *> *)document->element_map;
if (mapa) {
auto element = (*mapa).find(interpreter->element_offset);
auto element = (*mapa).find(interpreter->write_element_offset);
if (element != (*mapa).end()) {
xmlpp::Element *el = element->second;
const xmlpp::Element *parent = el->get_parent();
if (!parent || !strcasecmp(parent->get_name().c_str(), "HEAD")) goto fromstart;
if (!parent) goto fromstart;
xmlpp::ustring text = "<root>";
text += interpreter->writecode.source;
@ -408,6 +408,7 @@ check_for_rerender(struct ecmascript_interpreter *interpreter, const char* text)
}
}
}
interpreter->write_element_offset = 0;
} else {
if (interpreter->writecode.length) {
fromstart:

View File

@ -110,6 +110,7 @@ struct ecmascript_interpreter {
#endif
bool changed;
int element_offset;
int write_element_offset;
};
struct ecmascript_timeout {

View File

@ -4,6 +4,6 @@ INCLUDES += $(MUJS_CFLAGS)
OBJS = attr.obj attributes.obj collection.obj console.obj document.obj element.obj form.obj \
forms.obj history.obj implementation.obj input.obj keyboard.obj location.obj \
localstorage.obj navigator.obj nodelist.obj screen.obj unibar.obj window.obj xhr.obj
localstorage.obj message.obj navigator.obj nodelist.obj screen.obj unibar.obj window.obj xhr.obj
include $(top_srcdir)/Makefile.lib

View File

@ -1340,6 +1340,39 @@ mjs_element_getAttributeNode(js_State *J)
mjs_push_attr(J, attr);
}
static void
mjs_element_getElementsByTagName(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
xmlpp::Element *el = static_cast<xmlpp::Element *>(mjs_getprivate(J, 0));
if (!el) {
js_pushundefined(J);
return;
}
const char *str = js_tostring(J, 1);
if (!str) {
js_pushnull(J);
return;
}
xmlpp::ustring id = str;
std::transform(id.begin(), id.end(), id.begin(), ::tolower);
xmlpp::ustring xpath = "//";
xpath += id;
xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet;
if (!elements) {
js_pushnull(J);
return;
}
*elements = el->find(xpath);
mjs_push_collection(J, elements);
}
static void
mjs_element_hasAttribute(js_State *J)
{
@ -1752,6 +1785,7 @@ mjs_push_element(js_State *J, void *node)
addmethod(J, "contains", mjs_element_contains, 1);
addmethod(J, "getAttribute", mjs_element_getAttribute, 1);
addmethod(J, "getAttributeNode", mjs_element_getAttributeNode, 1);
addmethod(J, "getElementsByTagName", mjs_element_getElementsByTagName, 1);
addmethod(J, "hasAttribute", mjs_element_hasAttribute, 1);
addmethod(J, "hasAttributes", mjs_element_hasAttributes, 0);
addmethod(J, "hasChildNodes", mjs_element_hasChildNodes, 0);

View File

@ -1,2 +1,2 @@
srcs += files('attr.cpp', 'attributes.cpp', 'collection.cpp', 'console.cpp', 'document.cpp', 'element.cpp', 'form.cpp', 'forms.cpp', 'history.cpp', 'implementation.cpp',
'input.cpp', 'keyboard.cpp', 'localstorage.cpp', 'location.cpp', 'navigator.cpp', 'nodelist.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp', 'xhr.cpp')
'input.cpp', 'keyboard.cpp', 'localstorage.cpp', 'location.cpp', 'message.cpp', 'navigator.cpp', 'nodelist.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp', 'xhr.cpp')

View File

@ -0,0 +1,177 @@
/* The MuJS MessageEvent object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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/mujs.h"
#include "ecmascript/mujs/message.h"
#include "ecmascript/timer.h"
#include "intl/libintl.h"
#include "main/select.h"
#include "main/timer.h"
#include "network/connection.h"
#include "osdep/newwin.h"
#include "osdep/sysname.h"
#include "protocol/http/http.h"
#include "protocol/uri.h"
#include "session/download.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 <iostream>
#include <list>
#include <map>
#include <utility>
#include <sstream>
#include <vector>
static void mjs_messageEvent_get_property_data(js_State *J);
static void mjs_messageEvent_get_property_lastEventId(js_State *J);
static void mjs_messageEvent_get_property_origin(js_State *J);
static void mjs_messageEvent_get_property_source(js_State *J);
struct message_event {
char *data;
char *lastEventId;
char *origin;
char *source;
};
static
void mjs_messageEvent_finalizer(js_State *J, void *val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = (struct message_event *)val;
if (event) {
mem_free_if(event->data);
mem_free_if(event->lastEventId);
mem_free_if(event->origin);
mem_free_if(event->source);
mem_free(event);
}
}
static int lastEventId;
void
mjs_push_messageEvent(js_State *J, char *data, char *origin, char *source)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = (struct message_event *)mem_calloc(1, sizeof(*event));
if (!event) {
js_pushnull(J);
return;
}
event->data = null_or_stracpy(data);
event->origin = null_or_stracpy(origin);
event->source = null_or_stracpy(source);
char id[32];
snprintf(id, "%d", 31, ++lastEventId);
event->lastEventId = stracpy(id);
js_newobject(J);
{
js_newuserdata(J, "event", event, mjs_messageEvent_finalizer);
addproperty(J, "data", mjs_messageEvent_get_property_data, NULL);
addproperty(J, "lastEventId", mjs_messageEvent_get_property_lastEventId, NULL);
addproperty(J, "origin", mjs_messageEvent_get_property_origin, NULL);
addproperty(J, "source", mjs_messageEvent_get_property_source, NULL);
}
}
static void
mjs_messageEvent_get_property_data(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = (struct message_event *)js_touserdata(J, 0, "event");
if (!event || !event->data) {
js_pushnull(J);
return;
}
js_pushstring(J, event->data);
}
static void
mjs_messageEvent_get_property_lastEventId(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = (struct message_event *)js_touserdata(J, 0, "event");
if (!event || !event->lastEventId) {
js_pushnull(J);
return;
}
js_pushstring(J, event->lastEventId);
}
static void
mjs_messageEvent_get_property_origin(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = (struct message_event *)js_touserdata(J, 0, "event");
if (!event || !event->origin) {
js_pushnull(J);
return;
}
js_pushstring(J, event->origin);
}
static void
mjs_messageEvent_get_property_source(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = (struct message_event *)js_touserdata(J, 0, "event");
if (!event || !event->source) {
js_pushnull(J);
return;
}
js_pushstring(J, event->source);
}

View File

@ -0,0 +1,8 @@
#ifndef EL__ECMASCRIPT_MUJS_MESSAGE_H
#define EL__ECMASCRIPT_MUJS_MESSAGE_H
#include <mujs.h>
void mjs_push_messageEvent(js_State *J, char *data, char *origin, char *source);
#endif

View File

@ -1,4 +1,4 @@
/* The Quickjs window object implementation. */
/* The MuJS window object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
@ -21,6 +21,7 @@
#include "document/view.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/mujs.h"
#include "ecmascript/mujs/message.h"
#include "ecmascript/mujs/window.h"
#include "ecmascript/timer.h"
#include "intl/libintl.h"
@ -44,6 +45,43 @@
#include "viewer/text/link.h"
#include "viewer/text/vs.h"
struct listener {
LIST_HEAD(struct listener);
char *typ;
char *fun;
};
struct el_window {
struct ecmascript_interpreter *interpreter;
char *thisval;
LIST_OF(struct listener) listeners;
char *onmessage;
};
struct el_message {
char *messageObject;
struct el_window *elwin;
};
static
void mjs_window_finalizer(js_State *J, void *val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct el_window *elwin = (struct el_window *)val;
if (elwin) {
struct listener *l;
foreach(l, elwin->listeners) {
mem_free_set(&l->typ, NULL);
}
free_list(elwin->listeners);
mem_free(elwin);
}
}
static void
mjs_window_get_property_closed(js_State *J)
{
@ -387,14 +425,211 @@ mjs_window_toString(js_State *J)
js_pushstring(J, "[window object]");
}
static void
mjs_window_addEventListener(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 el_window *elwin = (struct el_window *)js_touserdata(J, 0, "window");
if (!elwin) {
elwin = (struct el_window *)mem_calloc(1, sizeof(*elwin));
if (!elwin) {
js_pushnull(J);
return;
}
init_list(elwin->listeners);
elwin->interpreter = interpreter;
elwin->thisval = js_ref(J);
js_newuserdata(J, "window", elwin, mjs_window_finalizer);
}
const char *str = js_tostring(J, 1);
if (!str) {
js_pushnull(J);
return;
}
char *method = stracpy(str);
if (!method) {
js_pushnull(J);
return;
}
js_copy(J, 2);
const char *fun = js_ref(J);
struct listener *l;
foreach(l, elwin->listeners) {
if (strcmp(l->typ, method)) {
continue;
}
if (!strcmp(l->fun, fun)) {
mem_free(method);
js_pushundefined(J);
return;
}
}
struct listener *n = (struct listener *)mem_calloc(1, sizeof(*n));
if (n) {
n->typ = method;
n->fun = fun;
add_to_list_end(elwin->listeners, n);
}
js_pushundefined(J);
}
static void
mjs_window_removeEventListener(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 el_window *elwin = (struct el_window *)js_touserdata(J, 0, "window");
if (!elwin) {
js_pushnull(J);
return;
}
const char *str = js_tostring(J, 1);
if (!str) {
js_pushnull(J);
return;
}
char *method = stracpy(str);
if (!method) {
js_pushnull(J);
return;
}
js_copy(J, 2);
const char *fun = js_ref(J);
struct listener *l;
foreach(l, elwin->listeners) {
if (strcmp(l->typ, method)) {
continue;
}
if (!strcmp(l->fun, fun)) {
del_from_list(l);
mem_free_set(&l->typ, NULL);
if (l->fun) js_unref(J, l->fun);
mem_free(l);
mem_free(method);
js_pushundefined(J);
return;
}
}
mem_free(method);
js_pushundefined(J);
}
static void
onmessage_run(void *data)
{
struct el_message *mess = (struct el_message *)data;
if (mess) {
struct el_window *elwin = mess->elwin;
if (!elwin) {
mem_free(mess);
return;
}
struct ecmascript_interpreter *interpreter = elwin->interpreter;
js_State *J = (js_State *)interpreter->backend_data;
struct listener *l;
// TODO parameers for js_pcall
foreach(l, elwin->listeners) {
if (strcmp(l->typ, "message")) {
continue;
}
js_getregistry(J, l->fun); /* retrieve the js function from the registry */
js_getregistry(J, elwin->thisval);
js_pcall(J, 0);
js_pop(J, 1);
}
if (elwin->onmessage) {
js_getregistry(J, elwin->onmessage); /* retrieve the js function from the registry */
js_getregistry(J, mess->messageObject);
js_pcall(J, 0);
js_pop(J, 1);
}
check_for_rerender(interpreter, "window_message");
}
}
static void
mjs_window_postMessage(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 el_window *elwin = (struct el_window *)js_touserdata(J, 0, "window");
const char *str = js_tostring(J, 1);
if (!str) {
js_pushnull(J);
return;
}
char *data = stracpy(str);
const char *str2 = js_tostring(J, 2);
if (!str2) {
mem_free_if(data);
js_pushnull(J);
return;
}
char *targetOrigin = stracpy(str2);
char *source = stracpy("TODO");
mjs_push_messageEvent(J, data, targetOrigin, source);
mem_free_if(data);
mem_free_if(targetOrigin);
mem_free_if(source);
js_pop(J, 1);
char *val = js_ref(J);
struct el_message *mess = (struct el_message *)mem_calloc(1, sizeof(*mess));
if (!mess) {
js_pushnull(J);
return;
}
mess->messageObject = val;
mess->elwin = elwin;
register_bottom_half(onmessage_run, mess);
js_pushundefined(J);
}
int
mjs_window_init(js_State *J)
{
js_newobject(J);
{
addmethod(J, "addEventListener", mjs_window_addEventListener, 3);
addmethod(J, "alert", mjs_window_alert, 1);
addmethod(J, "clearTimeout", mjs_window_clearTimeout, 1);
addmethod(J, "open", mjs_window_open, 3);
addmethod(J, "postMessage", mjs_window_postMessage, 3);
addmethod(J, "removeEventListener", mjs_window_removeEventListener, 3);
addmethod(J, "setTimeout", mjs_window_setTimeout, 2);
addmethod(J, "toString", mjs_window_toString, 0);

View File

@ -3,6 +3,6 @@ include $(top_builddir)/Makefile.config
OBJS = attr.obj attributes.obj collection.obj console.obj document.obj element.obj form.obj \
forms.obj heartbeat.obj history.obj implementation.obj input.obj keyboard.obj location.obj \
localstorage.obj navigator.obj nodelist.obj screen.obj unibar.obj window.obj xhr.obj
localstorage.obj message.obj navigator.obj nodelist.obj screen.obj unibar.obj window.obj xhr.obj
include $(top_srcdir)/Makefile.lib

View File

@ -1396,6 +1396,45 @@ js_element_getAttributeNode(JSContext *ctx, JSValueConst this_val, int argc, JSV
return getAttr(ctx, attr);
}
static JSValue
js_element_getElementsByTagName(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
if (argc != 1) {
return JS_FALSE;
}
xmlpp::Element *el = static_cast<xmlpp::Element *>(js_getopaque(this_val, js_element_class_id));
if (!el) {
return JS_UNDEFINED;
}
const char *str;
size_t len;
str = JS_ToCStringLen(ctx, &len, argv[0]);
if (!str) {
return JS_EXCEPTION;
}
xmlpp::ustring id = str;
JS_FreeCString(ctx, str);
std::transform(id.begin(), id.end(), id.begin(), ::tolower);
xmlpp::ustring xpath = "//";
xpath += id;
xmlpp::Node::NodeSet *elements = new(std::nothrow) xmlpp::Node::NodeSet;
if (!elements) {
return JS_NULL;
}
*elements = el->find(xpath);
return getCollection(ctx, elements);
}
static JSValue
js_element_hasAttribute(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
@ -1883,6 +1922,7 @@ static const JSCFunctionListEntry js_element_proto_funcs[] = {
JS_CFUNC_DEF("contains", 1, js_element_contains),
JS_CFUNC_DEF("getAttribute", 1, js_element_getAttribute),
JS_CFUNC_DEF("getAttributeNode",1, js_element_getAttributeNode),
JS_CFUNC_DEF("getElementsByTagName", 1, js_element_getElementsByTagName),
JS_CFUNC_DEF("hasAttribute", 1, js_element_hasAttribute),
JS_CFUNC_DEF("hasAttributes", 0, js_element_hasAttributes),
JS_CFUNC_DEF("hasChildNodes", 0, js_element_hasChildNodes),

View File

@ -1,2 +1,2 @@
srcs += files('attr.cpp', 'attributes.cpp', 'collection.cpp', 'console.cpp', 'document.cpp', 'element.cpp', 'form.cpp', 'forms.cpp', 'heartbeat.cpp', 'history.cpp', 'implementation.cpp',
'input.cpp', 'keyboard.cpp', 'localstorage.cpp', 'location.cpp', 'navigator.cpp', 'nodelist.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp', 'xhr.cpp')
'input.cpp', 'keyboard.cpp', 'localstorage.cpp', 'location.cpp', 'message.cpp', 'navigator.cpp', 'nodelist.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp', 'xhr.cpp')

View File

@ -0,0 +1,240 @@
/* The QuickJS MessageEvent object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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.h"
#include "ecmascript/quickjs/heartbeat.h"
#include "ecmascript/quickjs/message.h"
#include "ecmascript/timer.h"
#include "intl/libintl.h"
#include "main/select.h"
#include "main/timer.h"
#include "network/connection.h"
#include "osdep/newwin.h"
#include "osdep/sysname.h"
#include "protocol/http/http.h"
#include "protocol/uri.h"
#include "session/download.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 <iostream>
#include <list>
#include <map>
#include <utility>
#include <sstream>
#include <vector>
#define countof(x) (sizeof(x) / sizeof((x)[0]))
static JSClassID js_messageEvent_class_id;
static JSValue js_messageEvent_get_property_data(JSContext *cx, JSValueConst this_val);
static JSValue js_messageEvent_get_property_lastEventId(JSContext *cx, JSValueConst this_val);
static JSValue js_messageEvent_get_property_origin(JSContext *cx, JSValueConst this_val);
static JSValue js_messageEvent_get_property_source(JSContext *cx, JSValueConst this_val);
struct message_event {
char *data;
char *lastEventId;
char *origin;
char *source;
};
static
void js_messageEvent_finalizer(JSRuntime *rt, JSValue val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = (struct message_event *)JS_GetOpaque(val, js_messageEvent_class_id);
if (event) {
mem_free_if(event->data);
mem_free_if(event->lastEventId);
mem_free_if(event->origin);
mem_free_if(event->source);
mem_free(event);
}
}
static JSClassDef js_messageEvent_class = {
"MessageEvent",
js_messageEvent_finalizer
};
static JSValue
js_messageEvent_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JSValue obj = JS_UNDEFINED;
JSValue proto;
struct message_event *event = (struct message_event *)mem_calloc(1, sizeof(*event));
if (!event) {
return JS_EXCEPTION;
}
/* 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_messageEvent_class_id);
JS_FreeValue(ctx, proto);
if (JS_IsException(obj)) {
goto fail;
}
JS_SetOpaque(obj, event);
RETURN_JS(obj);
fail:
JS_FreeValue(ctx, obj);
mem_free(event);
return JS_EXCEPTION;
}
static const JSCFunctionListEntry js_messageEvent_proto_funcs[] = {
JS_CGETSET_DEF("data", js_messageEvent_get_property_data, nullptr),
JS_CGETSET_DEF("lastEventId", js_messageEvent_get_property_lastEventId, nullptr),
JS_CGETSET_DEF("origin", js_messageEvent_get_property_origin, nullptr),
JS_CGETSET_DEF("source", js_messageEvent_get_property_source, nullptr)
};
static JSValue
js_messageEvent_get_property_data(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = static_cast<struct message_event *>(JS_GetOpaque(this_val, js_messageEvent_class_id));
if (!event || !event->data) {
return JS_NULL;
}
JSValue r = JS_NewString(ctx, event->data);
RETURN_JS(r);
}
static JSValue
js_messageEvent_get_property_lastEventId(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = static_cast<struct message_event *>(JS_GetOpaque(this_val, js_messageEvent_class_id));
if (!event || !event->lastEventId) {
return JS_NULL;
}
JSValue r = JS_NewString(ctx, event->lastEventId);
RETURN_JS(r);
}
static JSValue
js_messageEvent_get_property_origin(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = static_cast<struct message_event *>(JS_GetOpaque(this_val, js_messageEvent_class_id));
if (!event || !event->origin) {
return JS_NULL;
}
JSValue r = JS_NewString(ctx, event->origin);
RETURN_JS(r);
}
static JSValue
js_messageEvent_get_property_source(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct message_event *event = static_cast<struct message_event *>(JS_GetOpaque(this_val, js_messageEvent_class_id));
if (!event || !event->source) {
return JS_NULL;
}
JSValue r = JS_NewString(ctx, event->source);
RETURN_JS(r);
}
static int lastEventId;
JSValue
get_messageEvent(JSContext *ctx, char *data, char *origin, char *source)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
static int initialized;
/* create the element class */
if (!initialized) {
JS_NewClassID(&js_messageEvent_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_messageEvent_class_id, &js_messageEvent_class);
initialized = 1;
}
struct message_event *event = (struct message_event *)mem_calloc(1, sizeof(*event));
if (!event) {
return JS_NULL;
}
event->data = null_or_stracpy(data);
event->origin = null_or_stracpy(origin);
event->source = null_or_stracpy(source);
char id[32];
snprintf(id, "%d", 31, ++lastEventId);
event->lastEventId = stracpy(id);
JSValue event_obj = JS_NewObjectClass(ctx, js_messageEvent_class_id);
JS_SetPropertyFunctionList(ctx, event_obj, js_messageEvent_proto_funcs, countof(js_messageEvent_proto_funcs));
JS_SetClassProto(ctx, js_messageEvent_class_id, event_obj);
JS_SetOpaque(event_obj, event);
JSValue rr = JS_DupValue(ctx, event_obj);
RETURN_JS(rr);
}

View File

@ -0,0 +1,8 @@
#ifndef EL__ECMASCRIPT_QUICKJS_MESSAGE_H
#define EL__ECMASCRIPT_QUICKJS_MESSAGE_H
#include <quickjs/quickjs.h>
JSValue get_messageEvent(JSContext *ctx, char *data, char *origin, char *source);
#endif

View File

@ -21,6 +21,8 @@
#include "document/view.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/heartbeat.h"
#include "ecmascript/quickjs/message.h"
#include "ecmascript/quickjs/window.h"
#include "ecmascript/timer.h"
#include "intl/libintl.h"
@ -48,6 +50,43 @@
static JSClassID js_window_class_id;
struct listener {
LIST_HEAD(struct listener);
char *typ;
JSValue fun;
};
struct el_window {
struct ecmascript_interpreter *interpreter;
JSValue thisval;
LIST_OF(struct listener) listeners;
JSValue onmessage;
};
struct el_message {
JSValue messageObject;
struct el_window *elwin;
};
static void
js_window_finalize(JSRuntime *rt, JSValue val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct el_window *elwin = (struct el_window *)JS_GetOpaque(val, js_window_class_id);
if (elwin) {
struct listener *l;
foreach(l, elwin->listeners) {
mem_free_set(&l->typ, NULL);
}
free_list(elwin->listeners);
mem_free(elwin);
}
}
/* @window_funcs{"open"} */
JSValue
js_window_open(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
@ -422,6 +461,208 @@ js_window_toString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst
return JS_NewString(ctx, "[window object]");
}
static JSValue
js_window_addEventListener(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
struct el_window *elwin = (struct el_window *)(JS_GetOpaque(this_val, js_window_class_id));
if (!elwin) {
elwin = (struct el_window *)mem_calloc(1, sizeof(*elwin));
if (!elwin) {
return JS_EXCEPTION;
}
init_list(elwin->listeners);
elwin->interpreter = interpreter;
elwin->thisval = JS_DupValue(ctx, this_val);
JS_SetOpaque(this_val, elwin);
}
if (argc < 2) {
return JS_UNDEFINED;
}
const char *str;
size_t len;
str = JS_ToCStringLen(ctx, &len, argv[0]);
if (!str) {
return JS_EXCEPTION;
}
char *method = stracpy(str);
JS_FreeCString(ctx, str);
if (!method) {
return JS_EXCEPTION;
}
JSValue fun = argv[1];
struct listener *l;
foreach(l, elwin->listeners) {
if (strcmp(l->typ, method)) {
continue;
}
if (JS_VALUE_GET_PTR(l->fun) == JS_VALUE_GET_PTR(fun)) {
mem_free(method);
return JS_UNDEFINED;
}
}
struct listener *n = (struct listener *)mem_calloc(1, sizeof(*n));
if (n) {
n->typ = method;
n->fun = JS_DupValue(ctx, argv[1]);
add_to_list_end(elwin->listeners, n);
}
return JS_UNDEFINED;
}
static JSValue
js_window_removeEventListener(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
struct el_window *elwin = (struct el_window *)(JS_GetOpaque(this_val, js_window_class_id));
if (!elwin) {
return JS_NULL;
}
if (argc < 2) {
return JS_UNDEFINED;
}
const char *str;
size_t len;
str = JS_ToCStringLen(ctx, &len, argv[0]);
if (!str) {
return JS_EXCEPTION;
}
char *method = stracpy(str);
JS_FreeCString(ctx, str);
if (!method) {
return JS_EXCEPTION;
}
JSValue fun = argv[1];
struct listener *l;
foreach(l, elwin->listeners) {
if (strcmp(l->typ, method)) {
continue;
}
if (JS_VALUE_GET_PTR(l->fun) == JS_VALUE_GET_PTR(fun)) {
del_from_list(l);
mem_free_set(&l->typ, NULL);
mem_free(l);
mem_free(method);
return JS_UNDEFINED;
}
}
mem_free(method);
return JS_UNDEFINED;
}
static void
onmessage_run(void *data)
{
struct el_message *mess = (struct el_message *)data;
if (mess) {
struct el_window *elwin = mess->elwin;
if (!elwin) {
mem_free(mess);
return;
}
struct ecmascript_interpreter *interpreter = elwin->interpreter;
JSContext *ctx = (JSContext *)interpreter->backend_data;
interpreter->heartbeat = add_heartbeat(interpreter);
struct listener *l;
foreach(l, elwin->listeners) {
if (strcmp(l->typ, "message")) {
continue;
}
JSValue func = JS_DupValue(ctx, l->fun);
JSValue arg = JS_DupValue(ctx, mess->messageObject);
JSValue ret = JS_Call(ctx, func, elwin->thisval, 1, (JSValueConst *) &arg);
JS_FreeValue(ctx, ret);
JS_FreeValue(ctx, func);
JS_FreeValue(ctx, arg);
}
if (JS_IsFunction(ctx, elwin->onmessage)) {
JSValue func = JS_DupValue(ctx, elwin->onmessage);
JSValue arg = JS_DupValue(ctx, mess->messageObject);
JSValue ret = JS_Call(ctx, func, elwin->thisval, 1, (JSValueConst *) &arg);
JS_FreeValue(ctx, ret);
JS_FreeValue(ctx, func);
JS_FreeValue(ctx, arg);
}
done_heartbeat(interpreter->heartbeat);
check_for_rerender(interpreter, "window_message");
}
}
static JSValue
js_window_postMessage(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
struct el_window *elwin = (struct el_window *)(JS_GetOpaque(this_val, js_window_class_id));
if (argc < 2) {
return JS_UNDEFINED;
}
const char *str;
size_t len;
str = JS_ToCStringLen(ctx, &len, argv[0]);
if (!str) {
return JS_EXCEPTION;
}
char *data = stracpy(str);
JS_FreeCString(ctx, str);
str = JS_ToCStringLen(ctx, &len, argv[1]);
if (!str) {
mem_free_if(data);
return JS_EXCEPTION;
}
char *targetOrigin = stracpy(str);
JS_FreeCString(ctx, str);
char *source = stracpy("TODO");
JSValue val = get_messageEvent(ctx, data, targetOrigin, source);
mem_free_if(data);
mem_free_if(targetOrigin);
mem_free_if(source);
if (JS_IsNull(val) || !elwin) {
return JS_UNDEFINED;
}
struct el_message *mess = (struct el_message *)mem_calloc(1, sizeof(*mess));
if (!mess) {
return JS_EXCEPTION;
}
mess->messageObject = JS_DupValue(ctx, val);
mess->elwin = elwin;
register_bottom_half(onmessage_run, mess);
return JS_UNDEFINED;
}
static const JSCFunctionListEntry js_window_proto_funcs[] = {
JS_CGETSET_DEF("closed", js_window_get_property_closed, nullptr),
JS_CGETSET_DEF("parent", js_window_get_property_parent, nullptr),
@ -429,15 +670,19 @@ static const JSCFunctionListEntry js_window_proto_funcs[] = {
JS_CGETSET_DEF("status", js_window_get_property_status, js_window_set_property_status),
JS_CGETSET_DEF("top", js_window_get_property_top, nullptr),
JS_CGETSET_DEF("window", js_window_get_property_self, nullptr),
JS_CFUNC_DEF("addEventListener", js_window_addEventListener, 3),
JS_CFUNC_DEF("alert", 1, js_window_alert),
JS_CFUNC_DEF("clearTimeout", 1, js_window_clearTimeout),
JS_CFUNC_DEF("open", 3, js_window_open),
JS_CFUNC_DEF("postMessage", js_window_postMessage, 3),
JS_CFUNC_DEF("removeEventListener", js_window_removeEventListener, 3),
JS_CFUNC_DEF("setTimeout", 2, js_window_setTimeout),
JS_CFUNC_DEF("toString", 0, js_window_toString)
};
static JSClassDef js_window_class = {
"window",
js_window_finalize
};
int

View File

@ -3,6 +3,6 @@ include $(top_builddir)/Makefile.config
INCLUDES += $(SPIDERMONKEY_CFLAGS)
OBJS = attr.obj attributes.obj collection.obj console.obj document.obj element.obj form.obj forms.obj heartbeat.obj history.obj implementation.obj input.obj \
keyboard.obj location.obj localstorage.obj navigator.obj nodelist.obj screen.obj unibar.obj window.obj xhr.obj
keyboard.obj location.obj localstorage.obj message.obj navigator.obj nodelist.obj screen.obj unibar.obj window.obj xhr.obj
include $(top_srcdir)/Makefile.lib

View File

@ -1307,6 +1307,7 @@ document_write_do(JSContext *ctx, unsigned int argc, JS::Value *rval, int newlin
if (argc >= 1)
{
interpreter->write_element_offset = interpreter->element_offset;
for (unsigned int i = 0; i < argc; ++i)
{
char *str = jsval_to_string(ctx, args[i]);

View File

@ -2331,6 +2331,7 @@ static bool element_closest(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_contains(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_getAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_getElementsByTagName(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval);
static bool element_hasChildNodes(JSContext *ctx, unsigned int argc, JS::Value *rval);
@ -2354,6 +2355,7 @@ const spidermonkeyFunctionSpec element_funcs[] = {
{ "contains", element_contains, 1 },
{ "getAttribute", element_getAttribute, 1 },
{ "getAttributeNode", element_getAttributeNode, 1 },
{ "getElementsByTagName", element_getElementsByTagName, 1 },
{ "hasAttribute", element_hasAttribute, 1 },
{ "hasAttributes", element_hasAttributes, 0 },
{ "hasChildNodes", element_hasChildNodes, 0 },
@ -2880,6 +2882,50 @@ element_getAttributeNode(JSContext *ctx, unsigned int argc, JS::Value *rval)
return true;
}
static bool
element_getElementsByTagName(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
if (argc != 1) {
args.rval().setBoolean(false);
return true;
}
xmlpp::Element *el = JS::GetMaybePtrFromReservedSlot<xmlpp::Element>(hobj, 0);
struct string idstr;
if (!init_string(&idstr)) {
return false;
}
jshandle_value_to_char_string(&idstr, ctx, args[0]);
xmlpp::ustring id = idstr.source;
std::transform(id.begin(), id.end(), id.begin(), ::tolower);
xmlpp::ustring xpath = "//";
xpath += id;
done_string(&idstr);
xmlpp::Node::NodeSet *elements = new xmlpp::Node::NodeSet;
*elements = el->find(xpath);
JSObject *elem = getCollection(ctx, elements);
if (elem) {
args.rval().setObject(*elem);
} else {