1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00

[quikcjs] libdom message.c

This commit is contained in:
Witold Filipczyk 2023-03-27 19:00:57 +02:00
parent 802f3292ed
commit bc4d673ba8
5 changed files with 185 additions and 2 deletions

View File

@ -1,6 +1,6 @@
top_builddir=../../../..
include $(top_builddir)/Makefile.config
OBJS = attr.o attributes.o collection.o console.o heartbeat.o history.o keyboard.o localstorage.o mapa.obj navigator.o nodelist.o screen.o unibar.o
OBJS = attr.o attributes.o collection.o console.o heartbeat.o history.o keyboard.o localstorage.o mapa.obj message.o navigator.o nodelist.o screen.o unibar.o
include $(top_srcdir)/Makefile.lib

View File

@ -1 +1 @@
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'heartbeat.c', 'history.c', 'keyboard.c', 'localstorage.c', 'mapa.cpp', 'navigator.c', 'nodelist.c', 'screen.c', 'unibar.c')
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'heartbeat.c', 'history.c', 'keyboard.c', 'localstorage.c', 'mapa.cpp', 'message.c', 'navigator.c', 'nodelist.c', 'screen.c', 'unibar.c')

View File

@ -0,0 +1,172 @@
/* 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 "ecmascript/ecmascript.h"
#include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/message.h"
#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
REF_JS(val);
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 const JSCFunctionListEntry js_messageEvent_proto_funcs[] = {
JS_CGETSET_DEF("data", js_messageEvent_get_property_data, NULL),
JS_CGETSET_DEF("lastEventId", js_messageEvent_get_property_lastEventId, NULL),
JS_CGETSET_DEF("origin", js_messageEvent_get_property_origin, NULL),
JS_CGETSET_DEF("source", js_messageEvent_get_property_source, NULL)
};
static JSValue
js_messageEvent_get_property_data(JSContext *ctx, JSValueConst this_val)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
struct message_event *event = (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
REF_JS(this_val);
struct message_event *event = (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
REF_JS(this_val);
struct message_event *event = (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
REF_JS(this_val);
struct message_event *event = (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, 31, "%d", ++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

@ -54,6 +54,8 @@
#include <sstream>
#include <vector>
#ifndef CONFIG_LIBDOM
#define countof(x) (sizeof(x) / sizeof((x)[0]))
static JSClassID js_messageEvent_class_id;
@ -253,3 +255,4 @@ get_messageEvent(JSContext *ctx, char *data, char *origin, char *source)
JSValue rr = JS_DupValue(ctx, event_obj);
RETURN_JS(rr);
}
#endif

View File

@ -3,6 +3,14 @@
#include <quickjs/quickjs.h>
#ifdef __cplusplus
extern "C" {
#endif
JSValue get_messageEvent(JSContext *ctx, char *data, char *origin, char *source);
#ifdef __cplusplus
}
#endif
#endif